Class: Datadog::DataStreams::Processor

Inherits:
Core::Worker show all
Includes:
Core::Workers::Polling
Defined in:
lib/datadog/data_streams/processor.rb

Overview

Processor for Data Streams Monitoring This class is responsible for collecting and reporting pathway stats Periodically (every interval, 10 seconds by default) flushes stats to the Datadog agent.

Constant Summary collapse

PROPAGATION_KEY =
"dd-pathway-ctx-base64"
DEFAULT_BUFFER_SIZE =

Default buffer size for lock-free event queue Set to handle high-throughput scenarios (e.g., 10k events/sec for 10s interval)

100_000

Constants included from Core::Workers::Polling

Core::Workers::Polling::DEFAULT_SHUTDOWN_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Core::Worker

#task

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::Workers::Polling

#enabled=, #enabled?, included, #stop

Constructor Details

#initialize(interval:, logger:, settings:, agent_settings:, agent_info:, buffer_size: DEFAULT_BUFFER_SIZE) ⇒ Processor

Initialize the Data Streams Monitoring processor

Parameters:

Raises:



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
# File 'lib/datadog/data_streams/processor.rb', line 54

def initialize(interval:, logger:, settings:, agent_settings:, agent_info:, buffer_size: DEFAULT_BUFFER_SIZE)
  raise UnsupportedError, "DDSketch is not supported" unless Datadog::Core::DDSketch.supported?

  @settings = settings
  @agent_settings = agent_settings
  @agent_info = agent_info
  @logger = logger

  # Fiber-local (Thread#[] is fiber-local), so a consume on one thread never rewrites the
  # pathway of produces on another.
  @pathway_context_key = :"datadog_data_streams_pathway_context_#{Processor.next_instance_id}"
  @bucket_size_ns = (interval * 1e9).to_i
  @buckets = {}
  @consumer_stats = []
  @stats_mutex = Mutex.new
  @event_buffer = Core::Buffer::CRuby.new(buffer_size)

  super()
  self.loop_base_interval = interval
  # Without this, a preload-then-fork deployment model (e.g. Puma cluster mode with
  # preload_app!, or Sidekiq Enterprise's sidekiqswarm with SIDEKIQ_PRELOAD_APP) leaves
  # every forked child with a dead flush thread: Ruby threads don't survive fork, and the
  # default FORK_POLICY_STOP means #perform would otherwise just mark the worker stopped
  # instead of restarting it. See #restart_flush_thread.
  self.fork_policy = Core::Workers::Async::Thread::FORK_POLICY_RESTART

  perform
end

Instance Attribute Details

#bucket_size_ns ⇒ Object (readonly)

Returns the value of attribute bucket_size_ns.



32
33
34
# File 'lib/datadog/data_streams/processor.rb', line 32

def bucket_size_ns
  @bucket_size_ns
end

#buckets ⇒ Object (readonly)

Returns the value of attribute buckets.



32
33
34
# File 'lib/datadog/data_streams/processor.rb', line 32

def buckets
  @buckets
end

Class Method Details

.next_instance_id ⇒ Object

Not object_id: the VM may reuse it, letting a new processor read a stale context.



40
41
42
# File 'lib/datadog/data_streams/processor.rb', line 40

def self.next_instance_id
  UNIQUE_INSTANCE_MUTEX.synchronize { UNIQUE_INSTANCE_GENERATOR.next }
end

Instance Method Details

#pathway_context ⇒ Object



94
95
96
# File 'lib/datadog/data_streams/processor.rb', line 94

def pathway_context
  Thread.current[@pathway_context_key]
end

#pathway_context=(context) ⇒ Object



98
99
100
# File 'lib/datadog/data_streams/processor.rb', line 98

def pathway_context=(context)
  Thread.current[@pathway_context_key] = context
end

#perform ⇒ Object

Called periodically by the worker to flush stats to the agent



185
186
187
188
189
# File 'lib/datadog/data_streams/processor.rb', line 185

def perform
  process_events
  flush_stats
  true
end

#restart_flush_thread ⇒ Object

Restarts the flush thread if a fork has been detected since this processor was created. Called by Components#after_fork. Safe to call even when no fork occurred: #perform (via Workers::Async::Thread) only restarts the worker when forked? is true. Deliberately not named after_fork: Workers::Async::Thread already defines a protected after_fork template method that #perform's internal restart path (+restart_after_fork+) calls on every restart -- overriding it here would recurse.



89
90
91
92
# File 'lib/datadog/data_streams/processor.rb', line 89

def restart_flush_thread
  discard_inherited_state_after_fork! if forked?
  perform
end

#set_consume_checkpoint(type:, source:, manual_checkpoint: true, tags: {}) {|key| ... } ⇒ String

Set a consume checkpoint

Parameters:

  • type (String) —

    The type of the checkpoint (e.g., 'kafka', 'kinesis', 'sns')

  • source (String) —

    The source (e.g., topic, exchange, stream name)

  • manual_checkpoint (Boolean) (defaults to: true) —

    Whether this checkpoint was manually set (default: true)

  • tags (Hash) (defaults to: {}) —

    Additional tags to include

Yields:

  • (key) —

    Block to extract context from carrier

Returns:

  • (String) —

    Base64 encoded pathway context



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/datadog/data_streams/processor.rb', line 167

def set_consume_checkpoint(type:, source:, manual_checkpoint: true, tags: {}, &block)
  if block
    pathway_ctx = yield(PROPAGATION_KEY)
    if pathway_ctx
      decoded_ctx = decode_pathway_b64(pathway_ctx)
      set_pathway_context(decoded_ctx)
    end
  end

  checkpoint_tags = ["type:#{type}", "topic:#{source}", "direction:in"]
  checkpoint_tags << "manual_checkpoint:true" if manual_checkpoint
  checkpoint_tags.concat(tags.map { |k, v| "#{k}:#{v}" }) unless tags.empty?

  span = Datadog::Tracing.active_span
  set_checkpoint(tags: checkpoint_tags, span: span)
end

#set_produce_checkpoint(type:, destination:, manual_checkpoint: true, tags: {}) {|key, value| ... } ⇒ String

Set a produce checkpoint

Parameters:

  • type (String) —

    The type of the checkpoint (e.g., 'kafka', 'kinesis', 'sns')

  • destination (String) —

    The destination (e.g., topic, exchange, stream name)

  • manual_checkpoint (Boolean) (defaults to: true) —

    Whether this checkpoint was manually set (default: true)

  • tags (Hash) (defaults to: {}) —

    Additional tags to include

Yields:

  • (key, value) —

    Block to inject context into carrier

Returns:

  • (String) —

    Base64 encoded pathway context



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/datadog/data_streams/processor.rb', line 147

def set_produce_checkpoint(type:, destination:, manual_checkpoint: true, tags: {}, &block)
  checkpoint_tags = ["type:#{type}", "topic:#{destination}", "direction:out"]
  checkpoint_tags << "manual_checkpoint:true" if manual_checkpoint
  checkpoint_tags.concat(tags.map { |k, v| "#{k}:#{v}" }) unless tags.empty?

  span = Datadog::Tracing.active_span
  pathway = set_checkpoint(tags: checkpoint_tags, span: span)

  yield(PROPAGATION_KEY, pathway) if pathway && block

  pathway
end

#track_kafka_consume(topic, partition, offset, now) ⇒ Boolean

Track Kafka message consumption for consumer lag monitoring

Parameters:

  • topic (String) —

    The Kafka topic name

  • partition (Integer) —

    The partition number

  • offset (Integer) —

    The offset of the consumed message

  • now (Time) —

    Timestamp

Returns:

  • (Boolean) —

    true if tracking succeeded



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/datadog/data_streams/processor.rb', line 127

def track_kafka_consume(topic, partition, offset, now)
  @event_buffer.push(
    {
      type: :kafka_consume,
      topic: topic,
      partition: partition,
      offset: offset,
      timestamp: now,
    },
  )
  true
end

#track_kafka_produce(topic, partition, offset, now) ⇒ Boolean

Track Kafka produce offset for lag monitoring

Parameters:

  • topic (String) —

    The Kafka topic name

  • partition (Integer) —

    The partition number

  • offset (Integer) —

    The offset of the produced message

  • now (Time) —

    Timestamp

Returns:

  • (Boolean) —

    true if tracking succeeded



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/datadog/data_streams/processor.rb', line 108

def track_kafka_produce(topic, partition, offset, now)
  @event_buffer.push(
    {
      type: :kafka_produce,
      topic: topic,
      partition: partition,
      offset: offset,
      timestamp_ns: (now.to_f * 1e9).to_i,
    },
  )
  true
end