Class: Datadog::Statsd::Telemetry

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

Constant Summary collapse

MAX_TELEMETRY_MESSAGE_SIZE_WT_TAGS =

Rough estimation of maximum telemetry message size without tags

50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

bytes


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/datadog/statsd/telemetry.rb', line 18

def initialize(flush_interval, global_tags: [], transport_type: :udp)
  @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)
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

#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


57
58
59
60
# File 'lib/datadog/statsd/telemetry.rb', line 57

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

#flushObject


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/datadog/statsd/telemetry.rb', line 66

def flush
  [
    sprintf(pattern, 'metrics', @metrics),
    sprintf(pattern, 'events', @events),
    sprintf(pattern, 'service_checks', @service_checks),
    sprintf(pattern, 'bytes_sent', @bytes_sent),
    sprintf(pattern, 'bytes_dropped', @bytes_dropped),
    sprintf(pattern, 'packets_sent', @packets_sent),
    sprintf(pattern, 'packets_dropped', @packets_dropped),
  ]
end

#resetObject


37
38
39
40
41
42
43
44
45
46
# File 'lib/datadog/statsd/telemetry.rb', line 37

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


48
49
50
51
52
53
54
55
# File 'lib/datadog/statsd/telemetry.rb', line 48

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

#should_flush?Boolean

Returns:

  • (Boolean)

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

def should_flush?
  @next_flush_time < now_in_s
end

#would_fit_in?(max_buffer_payload_size) ⇒ Boolean

Returns:

  • (Boolean)

33
34
35
# File 'lib/datadog/statsd/telemetry.rb', line 33

def would_fit_in?(max_buffer_payload_size)
  MAX_TELEMETRY_MESSAGE_SIZE_WT_TAGS + serialized_tags.size < max_buffer_payload_size
end