Skip to content

Commit 5c864f9

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

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
Fiber.yield
29+
tracer.in_span('child') do
30+
# empty block
31+
end
32+
end
33+
fiber.resume
34+
fiber.resume
35+
end
36+
37+
parent_span = spans.find { |s| s.name == 'parent' }
38+
child_span = spans.find { |s| s.name == 'child' }
39+
40+
_(parent_span.span_id).must_equal(child_span.parent_span_id)
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)