Skip to content

Commit 9108622

Browse files
committed
chore: add test for dataloader patch
1 parent 2433671 commit 9108622

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/instrumentation.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

0 commit comments

Comments
 (0)