Class: HubStep::Instrumenter

Inherits:
Object
  • Object
show all
Defined in:
lib/hubstep/instrumenter.rb

Overview

Wrapper around ActiveSupport::Notifications (or any object with the same API) that traces instrumented blocks. Behaves identically to the underlying object except that blocks passed to #instrument will be passed two arguments instead of one: the payload Hash (as usual) and the Span that will represent this block of code in the trace. The Span can be used to set tags etc.

require "hubstep/instrumenter"

instrumenter = HubStep::Instrumenter.new(tracer, ActiveSupport::Notifications)

def deflate(data)
  # This block will be recorded as a span by the tracer and will also
  # generate a notification as usual.
  instrumenter.instrument("deflate.zlib") do |payload, span|
    span.set_tag("bytesize", data.bytesize)
    Zlib::Deflate.deflate(data)
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(tracer, service) ⇒ Instrumenter

Creates an Instrumenter

tracer - HubStep::Tracer instance service - Object that provides ActiveSupport::Notifications’ API (i.e.,

you could just pass ActiveSupport::Notifications here, or wrap
it in some other object).


30
31
32
33
# File 'lib/hubstep/instrumenter.rb', line 30

def initialize(tracer, service)
  @tracer = tracer
  @service = service
end

Instance Method Details

#instrument(name, payload = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/hubstep/instrumenter.rb', line 39

def instrument(name, payload = {})
  @tracer.span(name) do |span|
    service.instrument(name, payload) do |inner_payload|
      yield inner_payload, span if block_given?
    end
  end
end

#publish(name, *args) ⇒ Object



35
36
37
# File 'lib/hubstep/instrumenter.rb', line 35

def publish(name, *args)
  service.publish(name, *args)
end

#subscribe(*args, &block) ⇒ Object



47
48
49
# File 'lib/hubstep/instrumenter.rb', line 47

def subscribe(*args, &block)
  service.subscribe(*args, &block)
end

#subscribed(callback, *args, &block) ⇒ Object



51
52
53
# File 'lib/hubstep/instrumenter.rb', line 51

def subscribed(callback, *args, &block)
  service.subscribed(callback, *args, &block)
end

#unsubscribe(args) ⇒ Object



55
56
57
# File 'lib/hubstep/instrumenter.rb', line 55

def unsubscribe(args)
  service.unsubscribe(args)
end