Class: ActiveJob::Temporal::Observability::Datadog

Inherits:
Adapter
  • Object
show all
Defined in:
lib/activejob/temporal/observability/datadog.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Attributes inherited from Adapter

#name

Instance Method Summary collapse

Methods inherited from Adapter

#start!, #started?, #stop!, #validate!

Constructor Details

#initialize(service: "activejob-temporal", statsd: nil, statsd_host: "127.0.0.1", statsd_port: 8125) ⇒ Datadog

Returns a new instance of Datadog.



13
14
15
16
17
18
19
# File 'lib/activejob/temporal/observability/datadog.rb', line 13

def initialize(service: "activejob-temporal", statsd: nil, statsd_host: "127.0.0.1", statsd_port: 8125)
  super(:datadog)
  @service = service
  @statsd = statsd
  @statsd_host = statsd_host
  @statsd_port = statsd_port
end

Instance Attribute Details

#serviceObject

Returns the value of attribute service.



10
11
12
# File 'lib/activejob/temporal/observability/datadog.rb', line 10

def service
  @service
end

#statsd=(value) ⇒ Object

Sets the attribute statsd

Parameters:

  • value

    the value to set the attribute statsd to.



11
12
13
# File 'lib/activejob/temporal/observability/datadog.rb', line 11

def statsd=(value)
  @statsd = value
end

#statsd_hostObject

Returns the value of attribute statsd_host.



10
11
12
# File 'lib/activejob/temporal/observability/datadog.rb', line 10

def statsd_host
  @statsd_host
end

#statsd_portObject

Returns the value of attribute statsd_port.



10
11
12
# File 'lib/activejob/temporal/observability/datadog.rb', line 10

def statsd_port
  @statsd_port
end

Instance Method Details

#instrument(event_name, payload) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/activejob/temporal/observability/datadog.rb', line 41

def instrument(event_name, payload)
  return yield unless event_name == :perform

  started_at = monotonic_time
  trace(event_name, payload) do
    result = yield
    statsd.increment("activejob_temporal.jobs.completed", tags: tags(payload))
    result
  rescue StandardError => e
    statsd.increment("activejob_temporal.jobs.failed", tags: tags(payload, error: e.class.name))
    raise
  ensure
    statsd.histogram(
      "activejob_temporal.job_duration.seconds",
      monotonic_time - started_at,
      tags: tags(payload)
    )
  end
end

#record(event_name, payload) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/activejob/temporal/observability/datadog.rb', line 30

def record(event_name, payload)
  case event_name
  when :enqueue then record_enqueue(payload)
  when :payload_serialize then record_payload_size(payload)
  when :retry then record_retry(payload)
  when :worker_start then record_worker_started(payload)
  when :worker_stop then record_worker_stopped(payload)
  when :active_tasks then record_active_tasks(payload)
  end
end

#trace_context_for_enqueue(_payload) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/activejob/temporal/observability/datadog.rb', line 21

def trace_context_for_enqueue(_payload)
  trace = ::Datadog::Tracing.active_trace if defined?(::Datadog::Tracing)
  return {} unless trace.respond_to?(:to_digest)

  carrier = {}
  ::Datadog::Tracing::Contrib::HTTP.inject(trace.to_digest, carrier)
  carrier
end

#validate_dependencies!Object



61
62
63
64
65
66
# File 'lib/activejob/temporal/observability/datadog.rb', line 61

def validate_dependencies!
  require_dependency("datadog", "datadog", "Datadog")
  require_dependency("datadog", "datadog/statsd", "Datadog")
  require_dependency("datadog", "datadog/tracing/contrib/http", "Datadog")
  self
end