> There's also a 3rd thing I didn't started working on: allow the code to
> set probes dynamically (instead of statistically adding them by
> modifying puppet code). I don't know if ruby versatility can allow this,
> but I'd love to be able to instance_eval { ... add probes around a
> method ... } to a given live puppet instance/class fully dynamically
> (that would be completely crazy).
>

This is in fact fairly easy to do, though you're right that it's completely
crazy.  :)

Suppose we have an array x and we want to do something with it's length
(negate it here, but presumably we'd be doing something more useful like
recording it or something):

irb(main)> x = [1,2,3]
=> [1,2,3]
irb(main)> unless x.respond_to? :brice_wrapped_length
irb(main)>   class <<x
irb(main)>     alias brice_wrapped_length length
irb(main)>     def length
irb(main)>       -brice_wrapped_length
irb(main)>     end
irb(main)>   end
irb(main)> end
=> nil
irb(main)> x.length
=> -3
irb(main)> x << :foo
=> [1, 2, 3, :foo]
irb(main)> x.length
=> -4

Note that this only affects the one object; other arrays aren't touched:

irb(main)> y = [:a,:b]
[:a,:b]
irb(main)> y.length
2

The unless part is important to prevent instrumenting the same object twice,
which will cause it to hang (and eventually stack overflow).  To remove the
instrumentation, just reverse everything (if instead of unless, alias the
other way, and then remove brice_wrapped_length).

For production use (which I'm describing, not advocating) you'd want to take
care that exceptions were properly handled, etc.  I almost always wind up
using ensure clauses when I do this--or rather, I would use them if I did do
this, which I'm not actually admitting to.

-- M
-----------------------------------------------------------
When in trouble or in doubt, run in circles,
scream and shout. -- 1920's parody of the
maritime general prudential rule
------------------------------------------------------------

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to