Module: Datadog::Tracing::Contrib::Grape::Endpoint

Defined in:
lib/datadog/tracing/contrib/grape/endpoint.rb

Overview

Endpoint module includes a list of subscribers to create traces when a Grape endpoint is hit

Constant Summary collapse

KEY_RUN =
'datadog_grape_endpoint_run'.freeze
KEY_RENDER =
'datadog_grape_endpoint_render'.freeze

Class Method Summary collapse

Class Method Details

.endpoint_render(name, start, finish, id, payload) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/datadog/tracing/contrib/grape/endpoint.rb', line 131

def endpoint_render(name, start, finish, id, payload)
  return unless Thread.current[KEY_RENDER]

  Thread.current[KEY_RENDER] = false

  return unless enabled?

  span = Tracing.active_span
  return unless span

  # catch thrown exceptions
  begin
    # Measure service stats
    Contrib::Analytics.set_measured(span)

    span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object])
  ensure
    span.start(start)
    span.finish(finish)
  end
rescue StandardError => e
  Datadog.logger.error(e.message)
end

.endpoint_run(name, start, finish, id, payload) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/datadog/tracing/contrib/grape/endpoint.rb', line 67

def endpoint_run(name, start, finish, id, payload)
  return unless Thread.current[KEY_RUN]

  Thread.current[KEY_RUN] = false

  return unless enabled?

  trace = Tracing.active_trace
  span = Tracing.active_span
  return unless trace && span

  begin
    # collect endpoint details
    endpoint = payload.fetch(:endpoint)
    api_view = api_view(endpoint.options[:for])
    request_method = endpoint.options.fetch(:method).first
    path = endpoint_expand_path(endpoint)

    trace.resource = span.resource

    # Set analytics sample rate
    Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?

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

    # catch thrown exceptions

    span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object])

    # override the current span with this notification values
    span.set_tag(Ext::TAG_ROUTE_ENDPOINT, api_view) unless api_view.nil?
    span.set_tag(Ext::TAG_ROUTE_PATH, path)
    span.set_tag(Ext::TAG_ROUTE_METHOD, request_method)

    span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_METHOD, request_method)
    span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_URL, path)
  ensure
    span.start(start)
    span.finish(finish)
  end
rescue StandardError => e
  Datadog.logger.error(e.message)
end

.endpoint_run_filters(name, start, finish, id, payload) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/datadog/tracing/contrib/grape/endpoint.rb', line 155

def endpoint_run_filters(name, start, finish, id, payload)
  return unless enabled?

  # safe-guard to prevent submitting empty filters
  zero_length = (finish - start).zero?
  filters = payload[:filters]
  type = payload[:type]
  return if (!filters || filters.empty?) || !type || zero_length

  span = Tracing.trace(
    Ext::SPAN_ENDPOINT_RUN_FILTERS,
    service: service_name,
    span_type: Tracing::Metadata::Ext::HTTP::TYPE_INBOUND,
    start_time: start
  )

  begin
    span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
    span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_ENDPOINT_RUN_FILTERS)

    # Set analytics sample rate
    Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?

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

    # catch thrown exceptions
    span.set_error(payload[:exception_object]) if exception_is_error?(payload[:exception_object])

    span.set_tag(Ext::TAG_FILTER_TYPE, type.to_s)
  ensure
    span.start(start)
    span.finish(finish)
  end
rescue StandardError => e
  Datadog.logger.error(e.message)
end

.endpoint_start_process(_name, _start, _finish, _id, payload) ⇒ Object



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
# File 'lib/datadog/tracing/contrib/grape/endpoint.rb', line 36

def endpoint_start_process(_name, _start, _finish, _id, payload)
  return if Thread.current[KEY_RUN]
  return unless enabled?

  # collect endpoint details
  endpoint = payload.fetch(:endpoint)
  api_view = api_view(endpoint.options[:for])
  request_method = endpoint.options.fetch(:method).first
  path = endpoint_expand_path(endpoint)
  resource = "#{api_view} #{request_method} #{path}"

  # Store the beginning of a trace
  span = Tracing.trace(
    Ext::SPAN_ENDPOINT_RUN,
    service: service_name,
    span_type: Tracing::Metadata::Ext::HTTP::TYPE_INBOUND,
    resource: resource
  )
  trace = Tracing.active_trace

  # Set the trace resource
  trace.resource = span.resource

  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_ENDPOINT_RUN)

  Thread.current[KEY_RUN] = true
rescue StandardError => e
  Datadog.logger.error(e.message)
end

.endpoint_start_renderObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/datadog/tracing/contrib/grape/endpoint.rb', line 112

def endpoint_start_render(*)
  return if Thread.current[KEY_RENDER]
  return unless enabled?

  # Store the beginning of a trace
  span = Tracing.trace(
    Ext::SPAN_ENDPOINT_RENDER,
    service: service_name,
    span_type: Tracing::Metadata::Ext::HTTP::TYPE_TEMPLATE
  )

  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_ENDPOINT_RENDER)

  Thread.current[KEY_RENDER] = true
rescue StandardError => e
  Datadog.logger.error(e.message)
end

.subscribeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/datadog/tracing/contrib/grape/endpoint.rb', line 17

def subscribe
  # subscribe when a Grape endpoint is hit
  ::ActiveSupport::Notifications.subscribe('endpoint_run.grape.start_process') do |*args|
    endpoint_start_process(*args)
  end
  ::ActiveSupport::Notifications.subscribe('endpoint_run.grape') do |*args|
    endpoint_run(*args)
  end
  ::ActiveSupport::Notifications.subscribe('endpoint_render.grape.start_render') do |*args|
    endpoint_start_render(*args)
  end
  ::ActiveSupport::Notifications.subscribe('endpoint_render.grape') do |*args|
    endpoint_render(*args)
  end
  ::ActiveSupport::Notifications.subscribe('endpoint_run_filters.grape') do |*args|
    endpoint_run_filters(*args)
  end
end