Class: SplunkTracing::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/splunktracing/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.



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

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 = SplunkTracing.micros(Time.now)
  @report_start_time = start_time

  @runtime = {
    guid: guid,
    device:  Socket.gethostname,
    start_micros: start_time,
    component_name: component_name,
    tracer_platform: "ruby",
    tracer_version: SplunkTracing::VERSION,
    tracer_platform_version: RUBY_VERSION,
    attrs: tags
  }.freeze

  reset_on_fork
end

Instance Attribute Details

#max_span_recordsObject

Returns the value of attribute max_span_records.



9
10
11
# File 'lib/splunktracing/reporter.rb', line 9

def max_span_records
  @max_span_records
end

#periodObject

Returns the value of attribute period.



10
11
12
# File 'lib/splunktracing/reporter.rb', line 10

def period
  @period
end

Instance Method Details

#add_span(span) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/splunktracing/reporter.rb', line 36

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



46
47
48
49
50
51
# File 'lib/splunktracing/reporter.rb', line 46

def clear
  reset_on_fork

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

#flushObject



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
89
90
# File 'lib/splunktracing/reporter.rb', line 53

def flush
  reset_on_fork

  return if @span_records.empty?

  now = SplunkTracing.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
    SplunkTracing.logger.error "SplunkTracing 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