Module: Datadog::Tracing::MeasuredBuffer

Included in:
CRubyTraceBuffer, ThreadSafeTraceBuffer
Defined in:
lib/datadog/tracing/buffer.rb

Overview

Health metrics for trace buffers.

Instance Method Summary collapse

Instance Method Details

#add!(trace) ⇒ Object



19
20
21
22
23
24
# File 'lib/datadog/tracing/buffer.rb', line 19

def add!(trace)
  super

  # Emit health metrics
  measure_accept(trace)
end

#add_all!(traces) ⇒ Object



26
27
28
29
30
31
# File 'lib/datadog/tracing/buffer.rb', line 26

def add_all!(traces)
  super

  # Emit health metrics
  traces.each { |trace| measure_accept(trace) }
end

#drain!Object

Stored traces are returned and the local buffer is reset.



44
45
46
47
48
# File 'lib/datadog/tracing/buffer.rb', line 44

def drain!
  traces = super
  measure_pop(traces)
  traces
end

#initialize(*_) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/datadog/tracing/buffer.rb', line 10

def initialize(*_)
  super

  @buffer_accepted = 0
  @buffer_accepted_lengths = 0
  @buffer_dropped = 0
  @buffer_spans = 0
end

#measure_accept(trace) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/datadog/tracing/buffer.rb', line 50

def measure_accept(trace)
  @buffer_accepted += 1
  @buffer_accepted_lengths += trace.length

  @buffer_spans += trace.length
rescue StandardError => e
  Datadog.logger.debug(
    "Failed to measure queue accept. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
  )
end

#measure_drop(trace) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/datadog/tracing/buffer.rb', line 61

def measure_drop(trace)
  @buffer_dropped += 1

  @buffer_spans -= trace.length
rescue StandardError => e
  Datadog.logger.debug(
    "Failed to measure queue drop. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
  )
end

#measure_pop(traces) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/datadog/tracing/buffer.rb', line 71

def measure_pop(traces)
  # Accepted, cumulative totals
  Datadog.health_metrics.queue_accepted(@buffer_accepted)
  Datadog.health_metrics.queue_accepted_lengths(@buffer_accepted_lengths)

  # Dropped, cumulative totals
  Datadog.health_metrics.queue_dropped(@buffer_dropped)
  # TODO: are we missing a +queue_dropped_lengths+ metric?

  # Queue gauges, current values
  Datadog.health_metrics.queue_max_length(@max_size)
  Datadog.health_metrics.queue_spans(@buffer_spans)
  Datadog.health_metrics.queue_length(traces.length)

  # Reset aggregated metrics
  @buffer_accepted = 0
  @buffer_accepted_lengths = 0
  @buffer_dropped = 0
  @buffer_spans = 0
rescue StandardError => e
  Datadog.logger.debug(
    "Failed to measure queue. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
  )
end

#replace!(trace) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/datadog/tracing/buffer.rb', line 33

def replace!(trace)
  discarded_trace = super

  # Emit health metrics
  measure_accept(trace)
  measure_drop(discarded_trace) if discarded_trace

  discarded_trace
end