Class: LightStep::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/lightstep/reporter.rb

Overview

Reporter builds up reports of spans and flushes them to a transport

Constant Summary collapse

DEFAULT_PERIOD_SECONDS =
3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_span_records:, transport:, guid:, component_name:, tags: {}) ⇒ Reporter

Returns a new instance of Reporter.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lightstep/reporter.rb', line 10

def initialize(max_span_records:, transport:, guid:, component_name:, tags: {})
  @max_span_records = max_span_records
  @span_records = Concurrent::Array.new
  @dropped_spans = Concurrent::AtomicFixnum.new
  @transport = transport
  @period = DEFAULT_PERIOD_SECONDS

  start_time = LightStep.micros(Time.now)
  @report_start_time = start_time

  @runtime = {
    guid: guid,
    start_micros: start_time,
    group_name: component_name,
    attrs: [
      {Key: "lightstep.tracer_platform",         Value: "ruby"},
      {Key: "lightstep.tracer_version",          Value: LightStep::VERSION},
      {Key: "lightstep.tracer_platform_version", Value: RUBY_VERSION}
    ] + tags.map{|k,v| {Key: k.to_s, Value: v.to_s}}
  }.freeze

  reset_on_fork
end

Instance Attribute Details

#max_span_recordsObject

Returns the value of attribute max_span_records.



7
8
9
# File 'lib/lightstep/reporter.rb', line 7

def max_span_records
  @max_span_records
end

#periodObject

Returns the value of attribute period.



8
9
10
# File 'lib/lightstep/reporter.rb', line 8

def period
  @period
end

Instance Method Details

#add_span(span) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/lightstep/reporter.rb', line 34

def add_span(span)
  reset_on_fork

  @span_records.push(span.to_h)
  if @span_records.size > max_span_records
    @span_records.shift
    @dropped_spans.increment
  end
end

#clearObject



44
45
46
47
48
49
# File 'lib/lightstep/reporter.rb', line 44

def clear
  reset_on_fork

  span_records = @span_records.slice!(0, @span_records.length)
  @dropped_spans.increment(span_records.size)
end

#flushObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lightstep/reporter.rb', line 51

def flush
  reset_on_fork

  return if @span_records.empty?

  now = LightStep.micros(Time.now)

  span_records = @span_records.slice!(0, @span_records.length)
  dropped_spans = 0
  @dropped_spans.update do |old|
    dropped_spans = old
    0
  end

  report_request = {
    runtime: @runtime,
    oldest_micros: @report_start_time,
    youngest_micros: now,
    span_records: span_records,
    internal_metrics: {
      counts: [{
        name: 'spans.dropped',
        int64_value: dropped_spans
      }]
    }
  }

  @report_start_time = now

  begin
    @transport.report(report_request)
  rescue StandardError => e
    LightStep.logger.error "LightStep error reporting to collector: #{e.message}"
    # an error occurs, add the previous dropped_spans and count of spans
    # that would have been recorded
    @dropped_spans.increment(dropped_spans + span_records.length)
  end
end