Class: HubStep::Tracer

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

Overview

Tracer wraps LightStep::Tracer. It provides a block-based API for creating and configuring spans and support for enabling and disabling tracing at runtime.

Defined Under Namespace

Classes: InertSpan

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport: default_transport, tags: {}) ⇒ Tracer

Create a Tracer.

tags - Hash of tags to assign to the tracer. These will be

associated with every span the tracer creates.

transport - instance of a LightStep::Transport::Base subclass



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hubstep/tracer.rb', line 17

def initialize(transport: default_transport, tags: {})
  name = HubStep..values_at("app", "role").join("-")

  default_tags = {
    "hostname" => HubStep.hostname,
  }

  @tracer = LightStep::Tracer.new(component_name: name,
                                  transport: transport,
                                  tags: default_tags.merge(tags))
  @spans = []
  self.enabled = false
end

Instance Attribute Details

#enabled=(value) ⇒ Object (writeonly)

Enable/disable the tracer at runtime

When disabled, all #span blocks will be passed InertSpans instead of real spans. Operations on InertSpan are no-ops.



39
40
41
# File 'lib/hubstep/tracer.rb', line 39

def enabled=(value)
  @enabled = value
end

Instance Method Details

#bottom_spanObject

Get the bottommost span in the stack

This is the span that has no children.

Returns a LightStep::Span or InertSpan.



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

def bottom_span
  span = @spans.last if enabled?
  span || InertSpan.instance
end

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  !!@enabled
end

#flushObject

Submit all buffered spans to the collector

This happens automatically so you probably don’t need to call this outside of tests.

Returns nothing.



113
114
115
# File 'lib/hubstep/tracer.rb', line 113

def flush
  @tracer.flush if enabled?
end

#record_exception(span, exception) ⇒ Object

Record an exception that happened during a span

span - Span or InertSpan instance exception - Exception instance

Returns nothing.



99
100
101
102
103
104
105
# File 'lib/hubstep/tracer.rb', line 99

def record_exception(span, exception)
  span.configure do
    span.set_tag("error", true)
    span.set_tag("error.class", exception.class.name)
    span.set_tag("error.message", exception.message)
  end
end

#span(operation_name, start_time: nil, tags: nil, finish: true) ⇒ Object

Record a span representing the execution of the given block

operation_name - short human-readable String identifying the work done by the span start_time - Time instance representing when the span began tags - Hash of String => String tags to add to the span finish - Boolean indicating whether to “finish” (i.e., record the

span's end time and submit it to the collector).
Defaults to true.

Yields a LightStep::Span or InertSpan to the block. Returns the block’s return value.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hubstep/tracer.rb', line 71

def span(operation_name, start_time: nil, tags: nil, finish: true)
  unless enabled?
    return yield InertSpan.instance
  end

  span = @tracer.start_span(operation_name,
                            child_of: @spans.last,
                            start_time: start_time,
                            tags: tags)
  @spans << span

  begin
    yield span
  ensure
    record_exception(span, $ERROR_INFO) if $ERROR_INFO

    remove(span)

    span.finish if finish && span.end_micros.nil?
  end
end

#with_enabled(value) ⇒ Object

Enable/disable the tracer within a block



42
43
44
45
46
47
48
# File 'lib/hubstep/tracer.rb', line 42

def with_enabled(value)
  original = enabled?
  self.enabled = value
  yield
ensure
  self.enabled = original
end