Module: GraphQL::Tracing::PerfettoTrace

Defined in:
lib/graphql/tracing/perfetto_trace.rb,
lib/graphql/tracing/perfetto_trace/trace_pb.rb

Overview

This produces a trace file for inspecting in the Perfetto Trace Viewer.

To get the file, call #write on the trace.

Use “trace modes” to configure this to run on command or on a sample of traffic.

Examples:

Writing trace output


result = MySchema.execute(...)
result.query.trace.write(file: "tmp/trace.dump")

Running this instrumenter when trace: true is present in the request


class MySchema < GraphQL::Schema
  # Only run this tracer when `context[:trace_mode]` is `:trace`
  trace_with GraphQL::Tracing::Perfetto, mode: :trace
end

# In graphql_controller.rb:

context[:trace_mode] = params[:trace] ? :trace : nil
result = MySchema.execute(query_str, context: context, variables: variables, ...)
if context[:trace_mode] == :trace
  result.trace.write(file: ...)
end

Constant Summary collapse

PROTOBUF_AVAILABLE =

TODOs: - Make debug annotations visible on both parts when dataloader is involved

begin
  require "google/protobuf"
  true
rescue LoadError
  false
end
DATALOADER_CATEGORY_IIDS =
[5]
FIELD_EXECUTE_CATEGORY_IIDS =
[6]
ACTIVE_SUPPORT_NOTIFICATIONS_CATEGORY_IIDS =
[7]
AUTHORIZED_CATEGORY_IIDS =
[8]
RESOLVE_TYPE_CATEGORY_IIDS =
[9]
DA_OBJECT_IID =
10
DA_RESULT_IID =
11
DA_ARGUMENTS_IID =
12
DA_FETCH_KEYS_IID =
13
DA_STR_VAL_NIL_IID =
14
REVERSE_DEBUG_NAME_LOOKUP =
{
  DA_OBJECT_IID => "object",
  DA_RESULT_IID => "result",
  DA_ARGUMENTS_IID => "arguments",
  DA_FETCH_KEYS_IID => "fetch keys",
}
DEBUG_INSPECT_CATEGORY_IIDS =
[15]
DA_DEBUG_INSPECT_CLASS_IID =
16
DEBUG_INSPECT_EVENT_NAME_IID =
17
DA_DEBUG_INSPECT_FOR_IID =
18
Trace =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.Trace").msgclass
TracePacket =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.TracePacket").msgclass
TrackEvent =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.TrackEvent").msgclass
DebugAnnotation =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.DebugAnnotation").msgclass
TrackDescriptor =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.TrackDescriptor").msgclass
CounterDescriptor =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.CounterDescriptor").msgclass
InternedData =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.InternedData").msgclass
InternedString =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.InternedString").msgclass
EventCategory =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.EventCategory").msgclass
EventName =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.EventName").msgclass
DebugAnnotationName =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("perfetto_trace.protos.DebugAnnotationName").msgclass

Instance Method Summary collapse

Instance Method Details

#begin_analyze_multiplex(m, analyzers) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/graphql/tracing/perfetto_trace.rb', line 274

def begin_analyze_multiplex(m, analyzers)
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_BEGIN,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
    name: "Analysis") {
      [
        payload_to_debug("analyzers_count", analyzers.size),
        payload_to_debug("analyzers", analyzers),
      ]
    }
  super
end

#begin_authorized(type, obj, ctx) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/graphql/tracing/perfetto_trace.rb', line 496

def begin_authorized(type, obj, ctx)
  packet = trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_BEGIN,
    track_uuid: fid,
    category_iids: AUTHORIZED_CATEGORY_IIDS,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
    name_iid: @auth_name_iids[type],
  )
  @packets << packet
  fiber_flow_stack << packet
  super
end

#begin_dataloader(dl) ⇒ Object



438
439
440
441
442
443
444
445
446
447
# File 'lib/graphql/tracing/perfetto_trace.rb', line 438

def begin_dataloader(dl)
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_COUNTER,
    track_uuid: @fibers_counter_id,
    counter_value: count_fibers(1),
  )
  @did = fid
  @packets << track_descriptor_packet(@main_fiber_id, @did, "Dataloader Fiber ##{@did}")
  super
end

#begin_dataloader_source(source) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/graphql/tracing/perfetto_trace.rb', line 458

def begin_dataloader_source(source)
  fds = @flow_ids[source]
  fds_copy = fds.dup
  fds.clear

  packet = trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_BEGIN,
    track_uuid: fid,
    name_iid: @source_name_iids[source.class],
    category_iids: DATALOADER_CATEGORY_IIDS,
    flow_ids: fds_copy,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations]) {
      [
        payload_to_debug(nil, source.pending.values, iid: DA_FETCH_KEYS_IID, intern_value: true),
        *(source.instance_variables - [:@pending, :@fetching, :@results, :@dataloader]).map { |iv|
          payload_to_debug(iv.to_s, source.instance_variable_get(iv), intern_value: true)
        }
      ]
    }
  @packets << packet
  fiber_flow_stack << packet
  super
end

#begin_execute_field(field, object, arguments, query) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/graphql/tracing/perfetto_trace.rb', line 237

def begin_execute_field(field, object, arguments, query)
  packet = trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_BEGIN,
    track_uuid: fid,
    name: query.context.current_path.join("."),
    category_iids: FIELD_EXECUTE_CATEGORY_IIDS,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
  )
  @packets << packet
  fiber_flow_stack << packet
  super
end

#begin_resolve_type(type, value, context) ⇒ Object



526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/graphql/tracing/perfetto_trace.rb', line 526

def begin_resolve_type(type, value, context)
  packet = trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_BEGIN,
    track_uuid: fid,
    category_iids: RESOLVE_TYPE_CATEGORY_IIDS,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
    name_iid: @resolve_type_name_iids[type],
  )
  @packets << packet
  fiber_flow_stack << packet
  super
end

#begin_validate(query, validate) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/graphql/tracing/perfetto_trace.rb', line 321

def begin_validate(query, validate)
  @begin_validate = trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_BEGIN,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
    name: "Validate") {
      [payload_to_debug("validate?", validate)]
    }

  @packets << @begin_validate
  super
end

#dataloader_fiber_exitObject



426
427
428
429
430
431
432
433
434
435
436
# File 'lib/graphql/tracing/perfetto_trace.rb', line 426

def dataloader_fiber_exit
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_INSTANT,
    track_uuid: fid,
    name: "Fiber Exit",
    category_iids: DATALOADER_CATEGORY_IIDS,
    extra_counter_track_uuids: @counts_fibers,
    extra_counter_values: [count_fibers(-1)],
  )
  super
end

#dataloader_fiber_resume(source) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/graphql/tracing/perfetto_trace.rb', line 407

def dataloader_fiber_resume(source)
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_INSTANT,
    track_uuid: fid,
    name: "Fiber Resume",
    category_iids: DATALOADER_CATEGORY_IIDS,
  )

  ls = fiber_flow_stack.pop
  @packets << packet = TracePacket.new(
    timestamp: ts,
    track_event: dup_with(ls.track_event, { type: TrackEvent::Type::TYPE_SLICE_BEGIN }),
    trusted_packet_sequence_id: @sequence_id,
  )
  fiber_flow_stack << packet

  super
end

#dataloader_fiber_yield(source) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/graphql/tracing/perfetto_trace.rb', line 385

def dataloader_fiber_yield(source)
  ls = fiber_flow_stack.last
  if (flow_id = ls.track_event.flow_ids.first)
    # got it
  else
    flow_id = ls.track_event.name.object_id
    ls.track_event = dup_with(ls.track_event, {flow_ids: [flow_id] }, delete_counters: true)
  end
  @flow_ids[source] << flow_id
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
  )
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_INSTANT,
    track_uuid: fid,
    name: "Fiber Yield",
    category_iids: DATALOADER_CATEGORY_IIDS,
  )
  super
end

#dataloader_spawn_execution_fiber(jobs) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/graphql/tracing/perfetto_trace.rb', line 359

def dataloader_spawn_execution_fiber(jobs)
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_INSTANT,
    track_uuid: fid,
    name: "Create Execution Fiber",
    category_iids: DATALOADER_CATEGORY_IIDS,
    extra_counter_track_uuids: @counts_fibers_and_objects,
    extra_counter_values: [count_fibers(1), count_allocations]
  )
  @packets << track_descriptor_packet(@did, fid, "Exec Fiber ##{fid}")
  super
end

#dataloader_spawn_source_fiber(pending_sources) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/graphql/tracing/perfetto_trace.rb', line 372

def dataloader_spawn_source_fiber(pending_sources)
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_INSTANT,
    track_uuid: fid,
    name: "Create Source Fiber",
    category_iids: DATALOADER_CATEGORY_IIDS,
    extra_counter_track_uuids: @counts_fibers_and_objects,
    extra_counter_values: [count_fibers(1), count_allocations]
  )
  @packets << track_descriptor_packet(@did, fid, "Source Fiber ##{fid}")
  super
end

#end_analyze_multiplex(m, analyzers) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/graphql/tracing/perfetto_trace.rb', line 289

def end_analyze_multiplex(m, analyzers)
  end_ts = ts
  @packets << trace_packet(
    timestamp: end_ts,
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
  )
  super
end

#end_authorized(type, obj, ctx, is_authorized) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/graphql/tracing/perfetto_trace.rb', line 510

def end_authorized(type, obj, ctx, is_authorized)
  end_ts = ts
  @packets << trace_packet(
    timestamp: end_ts,
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
  )
  beg_auth = fiber_flow_stack.pop
  if @create_debug_annotations
    beg_auth.track_event = dup_with(beg_auth.track_event, { debug_annotations: [payload_to_debug("authorized?", is_authorized)] })
  end
  super
end

#end_dataloader(dl) ⇒ Object



449
450
451
452
453
454
455
456
# File 'lib/graphql/tracing/perfetto_trace.rb', line 449

def end_dataloader(dl)
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_COUNTER,
    track_uuid: @fibers_counter_id,
    counter_value: count_fibers(-1),
  )
  super
end

#end_dataloader_source(source) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/graphql/tracing/perfetto_trace.rb', line 483

def end_dataloader_source(source)
  end_ts = ts
  @packets << trace_packet(
    timestamp: end_ts,
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
  )
  fiber_flow_stack.pop
  super
end

#end_execute_field(field, object, arguments, query, app_result) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/graphql/tracing/perfetto_trace.rb', line 251

def end_execute_field(field, object, arguments, query, app_result)
  end_ts = ts
  start_field = fiber_flow_stack.pop
  if @create_debug_annotations
    start_field.track_event = dup_with(start_field.track_event,{
      debug_annotations: [
          payload_to_debug(nil, object.object, iid: DA_OBJECT_IID, intern_value: true),
          payload_to_debug(nil, arguments, iid: DA_ARGUMENTS_IID),
          payload_to_debug(nil, app_result, iid: DA_RESULT_IID, intern_value: true)
        ]
      })
  end

  @packets << trace_packet(
    timestamp: end_ts,
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects_and_fields,
    extra_counter_values: [count_allocations, count_fields],
  )
  super
end

#end_resolve_type(type, value, context, resolved_type) ⇒ Object



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/graphql/tracing/perfetto_trace.rb', line 540

def end_resolve_type(type, value, context, resolved_type)
  end_ts = ts
  @packets << trace_packet(
    timestamp: end_ts,
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
  )
  rt_begin = fiber_flow_stack.pop
  if @create_debug_annotations
    rt_begin.track_event = dup_with(rt_begin.track_event, { debug_annotations: [payload_to_debug("resolved_type", resolved_type, intern_value: true)] })
  end
  super
end

#end_validate(query, validate, validation_errors) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/graphql/tracing/perfetto_trace.rb', line 335

def end_validate(query, validate, validation_errors)
  end_ts = ts
  @packets << trace_packet(
    timestamp: end_ts,
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
  )

  if @create_debug_annotations
    new_bv_track_event = dup_with(
      @begin_validate.track_event, {
        debug_annotations: [
          @begin_validate.track_event.debug_annotations.first,
          payload_to_debug("valid?", validation_errors.empty?)
        ]
      }
    )
    @begin_validate.track_event = new_bv_track_event
  end
  super
end

#execute_multiplex(multiplex:) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/graphql/tracing/perfetto_trace.rb', line 207

def execute_multiplex(multiplex:)
  if defined?(ActiveSupport::Notifications) && @active_support_notifications_pattern != false
    subscribe_to_active_support_notifications(@active_support_notifications_pattern)
  end
  @operation_name = multiplex.queries.map { |q| q.selected_operation_name || "anonymous" }.join(",")
  @begin_time = Time.now
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_BEGIN,
    track_uuid: fid,
    name: "Multiplex"
  ) { [ payload_to_debug("query_string", multiplex.queries.map(&:sanitized_query_string).join("\n\n")) ] }

  result = super

  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
  )

  result
ensure
  unsubscribe_from_active_support_notifications
  if @save_profile
    begin_ts = (@begin_time.to_f * 1000).round
    end_ts = (Time.now.to_f * 1000).round
    duration_ms = end_ts - begin_ts
    multiplex.schema.detailed_trace.save_trace(@operation_name, duration_ms, begin_ts, Trace.encode(Trace.new(packet: @packets)))
  end
end

#initialize(active_support_notifications_pattern: nil, save_profile: false, **_rest) ⇒ Object

Parameters:

  • active_support_notifications_pattern (String, RegExp, false) (defaults to: nil)

    A filter for ActiveSupport::Notifications, if it’s present. Or false to skip subscribing.



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/graphql/tracing/perfetto_trace.rb', line 76

def initialize(active_support_notifications_pattern: nil, save_profile: false, **_rest)
  super
  @active_support_notifications_pattern = active_support_notifications_pattern
  @save_profile = save_profile

  query = if @multiplex
    @multiplex.queries.first
  else
    @query # could still be nil in some initializations
  end

  @detailed_trace = query&.schema&.detailed_trace || DetailedTrace
  @create_debug_annotations = if (ctx = query&.context).nil? || (ctx_debug = ctx[:detailed_trace_debug]).nil?
    @detailed_trace.debug?
  else
    ctx_debug
  end

  Fiber[:graphql_flow_stack] = nil
  @sequence_id = object_id
  @pid = Process.pid
  @flow_ids = Hash.new { |h, source_inst| h[source_inst] = [] }.compare_by_identity
  @new_interned_event_names = {}
  @interned_event_name_iids = Hash.new { |h, k|
    new_id = 100 + h.size
    @new_interned_event_names[k] = new_id
    h[k] = new_id
  }

  @source_name_iids = Hash.new do |h, source_class|
    h[source_class] = @interned_event_name_iids[source_class.name]
  end.compare_by_identity

  @auth_name_iids = Hash.new do |h, graphql_type|
    h[graphql_type] = @interned_event_name_iids["Authorize: #{graphql_type.graphql_name}"]
  end.compare_by_identity

  @resolve_type_name_iids = Hash.new do |h, graphql_type|
    h[graphql_type] = @interned_event_name_iids["Resolve Type: #{graphql_type.graphql_name}"]
  end.compare_by_identity

  @new_interned_da_names = {}
  @interned_da_name_ids = Hash.new { |h, k|
    next_id = 100 + h.size
    @new_interned_da_names[k] = next_id
    h[k] = next_id
  }

  @new_interned_da_string_values = {}
  @interned_da_string_values = Hash.new do |h, k|
    new_id = 100 + h.size
    @new_interned_da_string_values[k] = new_id
    h[k] = new_id
  end

  @class_name_iids = Hash.new do |h, k|
    h[k] = @interned_da_string_values[k.name]
  end.compare_by_identity

  @starting_objects = GC.stat(:total_allocated_objects)
  @objects_counter_id = :objects_counter.object_id
  @fibers_counter_id = :fibers_counter.object_id
  @fields_counter_id = :fields_counter.object_id
  @counts_objects = [@objects_counter_id]
  @counts_objects_and_fields = [@objects_counter_id, @fields_counter_id]
  @counts_fibers = [@fibers_counter_id]
  @counts_fibers_and_objects = [@fibers_counter_id, @objects_counter_id]
  @begin_validate = nil
  @begin_time = nil
  @packets = []
  @packets << TracePacket.new(
    track_descriptor: TrackDescriptor.new(
      uuid: tid,
      name: "Main Thread",
      child_ordering: TrackDescriptor::ChildTracksOrdering::CHRONOLOGICAL,
    ),
    first_packet_on_sequence: true,
    previous_packet_dropped: true,
    trusted_packet_sequence_id: @sequence_id,
    sequence_flags: 3,
  )
  @packets << TracePacket.new(
    interned_data: InternedData.new(
      event_categories: [
        EventCategory.new(name: "Dataloader", iid: DATALOADER_CATEGORY_IIDS.first),
        EventCategory.new(name: "Field Execution", iid: FIELD_EXECUTE_CATEGORY_IIDS.first),
        EventCategory.new(name: "ActiveSupport::Notifications", iid: ACTIVE_SUPPORT_NOTIFICATIONS_CATEGORY_IIDS.first),
        EventCategory.new(name: "Authorized", iid: AUTHORIZED_CATEGORY_IIDS.first),
        EventCategory.new(name: "Resolve Type", iid: RESOLVE_TYPE_CATEGORY_IIDS.first),
        EventCategory.new(name: "Debug Inspect", iid: DEBUG_INSPECT_CATEGORY_IIDS.first),
      ],
      debug_annotation_names: [
        *REVERSE_DEBUG_NAME_LOOKUP.map { |(iid, name)| DebugAnnotationName.new(name: name, iid: iid) },
        DebugAnnotationName.new(name: "inspect instance of", iid: DA_DEBUG_INSPECT_CLASS_IID),
        DebugAnnotationName.new(name: "inspecting for", iid: DA_DEBUG_INSPECT_FOR_IID)
      ],
      debug_annotation_string_values: [
        InternedString.new(str: "(nil)", iid: DA_STR_VAL_NIL_IID),
      ],
      event_names: [
        EventName.new(name: "#{(@detailed_trace.is_a?(Class) ? @detailed_trace : @detailed_trace.class).name}#inspect_object", iid: DEBUG_INSPECT_EVENT_NAME_IID)
      ],
    ),
    trusted_packet_sequence_id: @sequence_id,
    sequence_flags: 2,
  )
  @main_fiber_id = fid
  @packets << track_descriptor_packet(tid, fid, "Main Fiber")
  @packets << track_descriptor_packet(tid, @objects_counter_id, "Allocated Objects", counter: {})
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_COUNTER,
    track_uuid: @objects_counter_id,
    counter_value: count_allocations,
  )
  @packets << track_descriptor_packet(tid, @fibers_counter_id, "Active Fibers", counter: {})
  @fibers_count = 0
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_COUNTER,
    track_uuid: @fibers_counter_id,
    counter_value: count_fibers(0),
  )

  @packets << track_descriptor_packet(tid, @fields_counter_id, "Resolved Fields", counter: {})
  @fields_count = -1
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_COUNTER,
    track_uuid: @fields_counter_id,
    counter_value: count_fields,
  )
end

#parse(query_string:) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/graphql/tracing/perfetto_trace.rb', line 301

def parse(query_string:)
  @packets << trace_packet(
    type: TrackEvent::Type::TYPE_SLICE_BEGIN,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
    name: "Parse"
  )
  result = super
  end_ts = ts
  @packets << trace_packet(
    timestamp: end_ts,
    type: TrackEvent::Type::TYPE_SLICE_END,
    track_uuid: fid,
    extra_counter_track_uuids: @counts_objects,
    extra_counter_values: [count_allocations],
  )
  result
end

#write(file:, debug_json: false) ⇒ nil, ...

Dump protobuf output in the specified file.

Parameters:

  • file (String)

    path to a file in a directory that already exists

  • debug_json (Boolean) (defaults to: false)

    True to print JSON instead of binary

Returns:

  • (nil, String, Hash)

    If file was given, nil. If file was nil, a Hash if debug_json: true, else binary data.



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/graphql/tracing/perfetto_trace.rb', line 560

def write(file:, debug_json: false)
  trace = Trace.new(
    packet: @packets,
  )
  data = if debug_json
    small_json = Trace.encode_json(trace)
    JSON.pretty_generate(JSON.parse(small_json))
  else
    Trace.encode(trace)
  end

  if file
    File.write(file, data, mode: 'wb')
    nil
  else
    data
  end
end