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.
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
- 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
-
#begin_analyze_multiplex(m, analyzers) ⇒ Object
-
#begin_authorized(type, obj, ctx) ⇒ Object
-
#begin_dataloader(dl) ⇒ Object
-
#begin_dataloader_source(source) ⇒ Object
-
#begin_execute_field(field, object, arguments, query) ⇒ Object
-
#begin_execute_multiplex(m) ⇒ Object
-
#begin_parse(str) ⇒ Object
-
#begin_resolve_type(type, value, context) ⇒ Object
-
#begin_validate(query, validate) ⇒ Object
-
#dataloader_fiber_exit ⇒ Object
-
#dataloader_fiber_resume(source) ⇒ Object
-
#dataloader_fiber_yield(source) ⇒ Object
-
#dataloader_spawn_execution_fiber(jobs) ⇒ Object
-
#dataloader_spawn_source_fiber(pending_sources) ⇒ Object
-
#end_analyze_multiplex(m, analyzers) ⇒ Object
-
#end_authorized(type, obj, ctx, is_authorized) ⇒ Object
-
#end_dataloader(dl) ⇒ Object
-
#end_dataloader_source(source) ⇒ Object
-
#end_execute_field(field, object, arguments, query, app_result) ⇒ Object
-
#end_execute_multiplex(m) ⇒ Object
-
#end_parse(str) ⇒ Object
-
#end_resolve_type(type, value, context, resolved_type) ⇒ Object
-
#end_validate(query, validate, validation_errors) ⇒ Object
-
#initialize(active_support_notifications_pattern: nil, save_profile: false, **_rest) ⇒ Object
-
#write(file:, debug_json: false) ⇒ nil, ...
Dump protobuf output in the specified file.
Instance Method Details
#begin_analyze_multiplex(m, analyzers) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 239 def begin_analyze_multiplex(m, analyzers) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_BEGIN, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], name: "Analysis", debug_annotations: [ payload_to_debug("analyzers_count", analyzers.size), payload_to_debug("analyzers", analyzers), ] ) super end |
#begin_authorized(type, obj, ctx) ⇒ Object
452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 452 def (type, obj, ctx) packet = trace_packet( type: TrackEvent::Type::TYPE_SLICE_BEGIN, track_uuid: fid, category_iids: AUTHORIZED_CATEGORY_IIDS, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], name_iid: @auth_name_iids[type], ) @packets << packet fiber_flow_stack << packet super end |
#begin_dataloader(dl) ⇒ Object
397 398 399 400 401 402 403 404 405 406 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 397 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
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 417 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: [@objects_counter_id], extra_counter_values: [count_allocations], debug_annotations: [ 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
206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 206 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: [@objects_counter_id], extra_counter_values: [count_allocations], ) @packets << packet fiber_flow_stack << packet super end |
#begin_execute_multiplex(m) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 177 def begin_execute_multiplex(m) @operation_name = m.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", debug_annotations: [ payload_to_debug("query_string", m.queries.map(&:sanitized_query_string).join("\n\n")) ] ) super end |
#begin_parse(str) ⇒ Object
264 265 266 267 268 269 270 271 272 273 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 264 def begin_parse(str) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_BEGIN, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], name: "Parse" ) super end |
#begin_resolve_type(type, value, context) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 478 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: [@objects_counter_id], 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
285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 285 def begin_validate(query, validate) @packets << @begin_validate = trace_packet( type: TrackEvent::Type::TYPE_SLICE_BEGIN, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], name: "Validate", debug_annotations: [ payload_to_debug("validate?", validate), ] ) super end |
#dataloader_fiber_exit ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 385 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: [@fibers_counter_id], extra_counter_values: [count_fibers(-1)], ) super end |
#dataloader_fiber_resume(source) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 366 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
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 344 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
318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 318 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: [@fibers_counter_id, @objects_counter_id], 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
331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 331 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: [@fibers_counter_id, @objects_counter_id], 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
254 255 256 257 258 259 260 261 262 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 254 def end_analyze_multiplex(m, analyzers) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_END, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], ) super end |
#end_authorized(type, obj, ctx, is_authorized) ⇒ Object
466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 466 def (type, obj, ctx, ) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_END, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], ) beg_auth = fiber_flow_stack.pop beg_auth.track_event = dup_with(beg_auth.track_event, { debug_annotations: [payload_to_debug("authorized?", )] }) super end |
#end_dataloader(dl) ⇒ Object
408 409 410 411 412 413 414 415 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 408 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
441 442 443 444 445 446 447 448 449 450 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 441 def end_dataloader_source(source) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_END, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], ) fiber_flow_stack.pop super end |
#end_execute_field(field, object, arguments, query, app_result) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 220 def end_execute_field(field, object, arguments, query, app_result) start_field = fiber_flow_stack.pop 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) ] }) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_END, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id, @fields_counter_id], extra_counter_values: [count_allocations, count_fields], ) super end |
#end_execute_multiplex(m) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 191 def end_execute_multiplex(m) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_END, track_uuid: fid, ) 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 m.schema.detailed_trace.save_trace(@operation_name, duration_ms, begin_ts, Trace.encode(Trace.new(packet: @packets))) end super end |
#end_parse(str) ⇒ Object
275 276 277 278 279 280 281 282 283 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 275 def end_parse(str) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_END, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], ) super end |
#end_resolve_type(type, value, context, resolved_type) ⇒ Object
492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 492 def end_resolve_type(type, value, context, resolved_type) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_END, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], ) rt_begin = fiber_flow_stack.pop rt_begin.track_event = dup_with(rt_begin.track_event, { debug_annotations: [payload_to_debug("resolved_type", resolved_type, intern_value: true)] }) super end |
#end_validate(query, validate, validation_errors) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 299 def end_validate(query, validate, validation_errors) @packets << trace_packet( type: TrackEvent::Type::TYPE_SLICE_END, track_uuid: fid, extra_counter_track_uuids: [@objects_counter_id], extra_counter_values: [count_allocations], ) @begin_validate.track_event = dup_with( @begin_validate.track_event, { debug_annotations: [ @begin_validate.track_event.debug_annotations.first, payload_to_debug("valid?", validation_errors.empty?) ] } ) super end |
#initialize(active_support_notifications_pattern: nil, save_profile: false, **_rest) ⇒ Object
64 65 66 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 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 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 64 def initialize(active_support_notifications_pattern: nil, save_profile: false, **_rest) super @save_profile = save_profile 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 @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), ], debug_annotation_names: [ DebugAnnotationName.new(name: "object", iid: DA_OBJECT_IID), DebugAnnotationName.new(name: "arguments", iid: DA_ARGUMENTS_IID), DebugAnnotationName.new(name: "result", iid: DA_RESULT_IID), DebugAnnotationName.new(name: "fetch keys", iid: DA_FETCH_KEYS_IID), ], debug_annotation_string_values: [ InternedString.new(str: "(nil)", iid: DA_STR_VAL_NIL_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, ) if defined?(ActiveSupport::Notifications) && active_support_notifications_pattern != false subscribe_to_active_support_notifications(active_support_notifications_pattern) end end |
#write(file:, debug_json: false) ⇒ nil, ...
Dump protobuf output in the specified file.
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/graphql/tracing/perfetto_trace.rb', line 508 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 |