File tree Expand file tree Collapse file tree 2 files changed +54
-1
lines changed
lib/opentelemetry/instrumentation/graphql
test/instrumentation/graphql/patches Expand file tree Collapse file tree 2 files changed +54
-1
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,10 @@ def supports_new_tracer?
4343 Gem ::Requirement . new ( '>= 2.0.19' ) . satisfied_by? ( gem_version )
4444 end
4545
46+ def dataloader_has_spawn_fiber?
47+ Gem ::Requirement . new ( '>= 2.1.8' ) . satisfied_by? ( gem_version )
48+ end
49+
4650 ## Supported configuration keys for the install config hash:
4751 #
4852 # The enable_platform_field key expects a boolean value,
@@ -100,7 +104,7 @@ def install_new_tracer(config = {})
100104 end
101105
102106 def patch
103- return if gem_version < Gem :: Version . new ( '2.1.8' )
107+ return unless dataloader_has_spawn_fiber?
104108
105109 require_relative 'patches/dataloader'
106110 ::GraphQL ::Dataloader . prepend ( Patches ::Dataloader )
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ # Copyright The OpenTelemetry Authors
4+ #
5+ # SPDX-License-Identifier: Apache-2.0
6+
7+ require 'test_helper'
8+
9+ require_relative '../../../../lib/opentelemetry/instrumentation/graphql'
10+ require_relative '../../../../lib/opentelemetry/instrumentation/graphql/patches/dataloader'
11+
12+ describe OpenTelemetry ::Instrumentation ::GraphQL ::Patches ::Dataloader do
13+ let ( :instrumentation ) { OpenTelemetry ::Instrumentation ::GraphQL ::Instrumentation . instance }
14+ let ( :tracer ) { OpenTelemetry . tracer_provider . tracer ( 'test' ) }
15+ let ( :exporter ) { EXPORTER }
16+ let ( :spans ) { exporter . finished_spans }
17+
18+ before do
19+ instrumentation . instance_variable_set ( :@installed , false )
20+ instrumentation . install ( { } )
21+ end
22+
23+ if OpenTelemetry ::Instrumentation ::GraphQL ::Instrumentation . instance . dataloader_has_spawn_fiber?
24+ describe '#spawn_fiber' do
25+ it 'set context in the child fiber' do
26+ tracer . in_span ( 'parent' ) do
27+ fiber = GraphQL ::Dataloader . new . spawn_fiber do
28+ tracer . in_span ( 'child1' ) do
29+ # empty block
30+ end
31+ Fiber . yield
32+ tracer . in_span ( 'child2' ) do
33+ # empty block
34+ end
35+ end
36+ fiber . resume
37+ fiber . resume
38+ end
39+
40+ parent_span = spans . find { |s | s . name == 'parent' }
41+ child1_span = spans . find { |s | s . name == 'child1' }
42+ child2_span = spans . find { |s | s . name == 'child2' }
43+
44+ _ ( parent_span . span_id ) . must_equal ( child1_span . parent_span_id )
45+ _ ( parent_span . span_id ) . must_equal ( child2_span . parent_span_id )
46+ end
47+ end
48+ end
49+ end
You can’t perform that action at this time.
0 commit comments