Class: Tobox::Plugins::Datadog::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/tobox/plugins/datadog.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ EventHandler



11
12
13
14
# File 'lib/tobox/plugins/datadog.rb', line 11

def initialize(config)
  @config = config
  @db_table = @config[:table]
end

Instance Method Details

#on_error(event, error) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/tobox/plugins/datadog.rb', line 76

def on_error(event, error)
  span = event[:__tobox_event_span]

  return unless span

  span.set_error(error)
  span.finish
end

#on_finish(event) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/tobox/plugins/datadog.rb', line 68

def on_finish(event)
  span = event[:__tobox_event_span]

  return unless span

  span.finish
end

#on_start(event) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tobox/plugins/datadog.rb', line 16

def on_start(event)
  datadog_config = ::Datadog.configuration.tracing[:tobox]
  service = datadog_config[:service_name]
  error_handler = datadog_config[:error_handler]

  analytics_enabled = datadog_config[:analytics_enabled]
  analytics_sample_rate = datadog_config[:analytics_sample_rate]
  distributed_tracing = datadog_config[:distributed_tracing]

  resource = event[:type]

  if ( = event[:metadata])
    previous_span = ["datadog-parent-id"]

    if distributed_tracing && previous_span
      trace_digest = ::Datadog::Tracing::TraceDigest.new(
        span_id: previous_span,
        trace_id: event[:metadata]["datadog-trace-id"],
        trace_sampling_priority: event[:metadata]["datadog-sampling-priority"],
        trace_origin: event[:metadata]["datadog-origin"]
      )
      ::Datadog::Tracing.continue_trace!(trace_digest)
    end
  end

  span = ::Datadog::Tracing.trace(
    "tobox.event",
    service: service,
    span_type: ::Datadog::Tracing::::Ext::AppTypes::TYPE_WORKER,
    on_error: error_handler
  )
  span.resource = resource

  span.set_tag(::Datadog::Tracing::::Ext::TAG_COMPONENT, "tobox")
  span.set_tag(::Datadog::Tracing::::Ext::TAG_OPERATION, "event")

  if ::Datadog::Tracing::Contrib::Analytics.enabled?(analytics_enabled)
    ::Datadog::Tracing::Contrib::Analytics.set_sample_rate(span, analytics_sample_rate)
  end

  # Measure service stats
  ::Datadog::Tracing::Contrib::Analytics.set_measured(span)

  span.set_tag("tobox.event.id", event[:id])
  span.set_tag("tobox.event.type", event[:type])
  span.set_tag("tobox.event.retry", event[:attempts])
  span.set_tag("tobox.event.table", @db_table)
  span.set_tag("tobox.event.delay", (Time.now.utc - event[:created_at]).to_f)

  event[:__tobox_event_span] = span
end