Class: Datadog::Statsd::Telemetry

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/statsd/telemetry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(disabled, flush_interval, global_tags: [], transport_type: :udp) ⇒ Telemetry

Returns a new instance of Telemetry.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/datadog/statsd/telemetry.rb', line 16

def initialize(disabled, flush_interval, global_tags: [], transport_type: :udp)
  @disabled = disabled
  @flush_interval = flush_interval
  @global_tags = global_tags
  @transport_type = transport_type
  reset

  # TODO: Karim: I don't know why but telemetry tags are serialized
  # before global tags so by refactoring this, I am keeping the same behavior
  @serialized_tags = Serialization::TagSerializer.new(
    client: 'ruby',
    client_version: VERSION,
    client_transport: transport_type,
  ).format(global_tags)

  # estimate_max_size is an estimation or the maximum size of the
  # telemetry payload. Since we don't want our packet to go over
  # 'max_buffer_bytes', we have to adjust with the size of the telemetry
  # (and any tags used). The telemetry payload size will change depending
  # on the actual value of metrics: metrics received, packet dropped,
  # etc. This is why we add a 63bytes margin: 9 bytes for each of the 7
  # telemetry metrics.
  @estimate_max_size = disabled ? 0 : flush.length + 9 * 7
end

Instance Attribute Details

#bytes_droppedObject (readonly)

Returns the value of attribute bytes_dropped.



11
12
13
# File 'lib/datadog/statsd/telemetry.rb', line 11

def bytes_dropped
  @bytes_dropped
end

#bytes_sentObject (readonly)

Returns the value of attribute bytes_sent.



10
11
12
# File 'lib/datadog/statsd/telemetry.rb', line 10

def bytes_sent
  @bytes_sent
end

#estimate_max_sizeObject (readonly)

Returns the value of attribute estimate_max_size.



14
15
16
# File 'lib/datadog/statsd/telemetry.rb', line 14

def estimate_max_size
  @estimate_max_size
end

#eventsObject (readonly)

Returns the value of attribute events.



8
9
10
# File 'lib/datadog/statsd/telemetry.rb', line 8

def events
  @events
end

#metricsObject (readonly)

Returns the value of attribute metrics.



7
8
9
# File 'lib/datadog/statsd/telemetry.rb', line 7

def metrics
  @metrics
end

#packets_droppedObject (readonly)

Returns the value of attribute packets_dropped.



13
14
15
# File 'lib/datadog/statsd/telemetry.rb', line 13

def packets_dropped
  @packets_dropped
end

#packets_sentObject (readonly)

Returns the value of attribute packets_sent.



12
13
14
# File 'lib/datadog/statsd/telemetry.rb', line 12

def packets_sent
  @packets_sent
end

#service_checksObject (readonly)

Returns the value of attribute service_checks.



9
10
11
# File 'lib/datadog/statsd/telemetry.rb', line 9

def service_checks
  @service_checks
end

Instance Method Details

#dropped(bytes: 0, packets: 0) ⇒ Object



61
62
63
64
# File 'lib/datadog/statsd/telemetry.rb', line 61

def dropped(bytes: 0, packets: 0)
  @bytes_dropped += bytes
  @packets_dropped += packets
end

#flushObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/datadog/statsd/telemetry.rb', line 70

def flush
  return '' if @disabled

  # using shorthand syntax to reduce the garbage collection
  %Q(
datadog.dogstatsd.client.metrics:#{@metrics}|#{COUNTER_TYPE}|##{serialized_tags}
datadog.dogstatsd.client.events:#{@events}|#{COUNTER_TYPE}|##{serialized_tags}
datadog.dogstatsd.client.service_checks:#{@service_checks}|#{COUNTER_TYPE}|##{serialized_tags}
datadog.dogstatsd.client.bytes_sent:#{@bytes_sent}|#{COUNTER_TYPE}|##{serialized_tags}
datadog.dogstatsd.client.bytes_dropped:#{@bytes_dropped}|#{COUNTER_TYPE}|##{serialized_tags}
datadog.dogstatsd.client.packets_sent:#{@packets_sent}|#{COUNTER_TYPE}|##{serialized_tags}
datadog.dogstatsd.client.packets_dropped:#{@packets_dropped}|#{COUNTER_TYPE}|##{serialized_tags})
end

#flush?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/datadog/statsd/telemetry.rb', line 66

def flush?
  @next_flush_time < now_in_s
end

#resetObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/datadog/statsd/telemetry.rb', line 41

def reset
  @metrics = 0
  @events = 0
  @service_checks = 0
  @bytes_sent = 0
  @bytes_dropped = 0
  @packets_sent = 0
  @packets_dropped = 0
  @next_flush_time = now_in_s + @flush_interval
end

#sent(metrics: 0, events: 0, service_checks: 0, bytes: 0, packets: 0) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/datadog/statsd/telemetry.rb', line 52

def sent(metrics: 0, events: 0, service_checks: 0, bytes: 0, packets: 0)
  @metrics += metrics
  @events += events
  @service_checks += service_checks

  @bytes_sent += bytes
  @packets_sent += packets
end