Module: BrainzLab::Instrumentation::GraphQLInstrumentation::Tracer

Defined in:
lib/brainzlab/instrumentation/graphql.rb

Overview

GraphQL Ruby 2.0+ Tracer module Add to your schema: trace_with BrainzLab::Instrumentation::GraphQLInstrumentation::Tracer

Instance Method Summary collapse

Instance Method Details

#execute_field(field:, query:, ast_node:, arguments:, object:) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/brainzlab/instrumentation/graphql.rb', line 221

def execute_field(field:, query:, ast_node:, arguments:, object:)
  started_at = Time.now.utc

  result = super

  duration_ms = ((Time.now.utc - started_at) * 1000).round(2)

  # Only track slow field resolutions (> 10ms) to avoid noise
  if duration_ms > 10
    spans = Thread.current[:brainzlab_pulse_spans]
    if spans
      spans << {
        span_id: SecureRandom.uuid,
        name: "GraphQL field #{field.owner.graphql_name}.#{field.graphql_name}",
        kind: 'graphql.field',
        started_at: started_at,
        ended_at: Time.now.utc,
        duration_ms: duration_ms,
        data: {
          field: field.graphql_name,
          parent_type: field.owner.graphql_name
        }
      }
    end
  end

  result
end

#execute_query(query:) ⇒ Object



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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/brainzlab/instrumentation/graphql.rb', line 165

def execute_query(query:)
  started_at = Time.now.utc
  operation_name = query.operation_name || 'anonymous'
  operation_type = query.selected_operation&.operation_type || 'query'

  result = super

  duration_ms = ((Time.now.utc - started_at) * 1000).round(2)
  has_errors = result.to_h['errors']&.any?

  # Add breadcrumb
  if BrainzLab.configuration.reflex_enabled
    BrainzLab::Reflex.add_breadcrumb(
      "GraphQL #{operation_type} #{operation_name}",
      category: 'graphql.execute',
      level: has_errors ? :error : :info,
      data: {
        operation_name: operation_name,
        operation_type: operation_type,
        duration_ms: duration_ms
      }
    )
  end

  # Record span
  spans = Thread.current[:brainzlab_pulse_spans]
  if spans
    spans << {
      span_id: SecureRandom.uuid,
      name: "GraphQL #{operation_type} #{operation_name}",
      kind: 'graphql',
      started_at: started_at,
      ended_at: Time.now.utc,
      duration_ms: duration_ms,
      data: {
        operation_name: operation_name,
        operation_type: operation_type
      },
      error: has_errors
    }
  end

  result
rescue StandardError => e
  # Record error
  if BrainzLab.configuration.reflex_enabled
    BrainzLab::Reflex.add_breadcrumb(
      "GraphQL #{operation_type} #{operation_name} failed",
      category: 'graphql.error',
      level: :error,
      data: { error: e.class.name }
    )
  end
  raise
end