diff --git a/instrumentation/bunny/lib/opentelemetry/instrumentation/bunny/patch_helpers.rb b/instrumentation/bunny/lib/opentelemetry/instrumentation/bunny/patch_helpers.rb index 4cb67b884..043a17569 100644 --- a/instrumentation/bunny/lib/opentelemetry/instrumentation/bunny/patch_helpers.rb +++ b/instrumentation/bunny/lib/opentelemetry/instrumentation/bunny/patch_helpers.rb @@ -35,6 +35,7 @@ def self.destination_name(exchange, routing_key) def self.extract_context(properties) # use the receive span as parent context parent_context = OpenTelemetry.propagation.extract(properties[:tracer_receive_headers]) + return [parent_context, nil] if properties[:headers].nil? # link to the producer context producer_context = OpenTelemetry.propagation.extract(properties[:headers]) diff --git a/instrumentation/bunny/test/opentelemetry/instrumentation/bunny/patch_helpers_test.rb b/instrumentation/bunny/test/opentelemetry/instrumentation/bunny/patch_helpers_test.rb new file mode 100644 index 000000000..522f555a2 --- /dev/null +++ b/instrumentation/bunny/test/opentelemetry/instrumentation/bunny/patch_helpers_test.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative '../../../../lib/opentelemetry/instrumentation/bunny/patch_helpers' + +describe OpenTelemetry::Instrumentation::Bunny::PatchHelpers do + let(:properties) do + { + headers: { + 'traceparent' => '00-eab67ae26433f603121bd5674149d9e1-2007f3325d3cb6d6-01' + }, + tracer_receive_headers: { + 'traceparent' => '00-cd52775b3cb38931adf5fa880f890c25-cddb52a470027489-01' + } + } + end + + describe '.extract_context' do + it 'returns the parent context with links when headers from producer exists' do + parent_context, links = OpenTelemetry::Instrumentation::Bunny::PatchHelpers.extract_context(properties) + _(parent_context).must_be_instance_of(OpenTelemetry::Context) + _(links).must_be_instance_of(Array) + _(links.first).must_be_instance_of(OpenTelemetry::Trace::Link) + end + + it 'returns the parent context with no links when headers from producer not present' do + properties.delete(:headers) + parent_context, links = OpenTelemetry::Instrumentation::Bunny::PatchHelpers.extract_context(properties) + _(parent_context).must_be_instance_of(OpenTelemetry::Context) + _(links).must_be_nil + end + end +end diff --git a/instrumentation/bunny/test/test_helper.rb b/instrumentation/bunny/test/test_helper.rb index 835bba61b..0c2839200 100644 --- a/instrumentation/bunny/test/test_helper.rb +++ b/instrumentation/bunny/test/test_helper.rb @@ -19,4 +19,5 @@ c.error_handler = ->(exception:, message:) { raise(exception || message) } c.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'fatal').to_sym) c.add_span_processor SPAN_PROCESSOR + c.propagators = [OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator] end diff --git a/instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb b/instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb index 3f470685d..881aa10ec 100644 --- a/instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb +++ b/instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb @@ -75,10 +75,11 @@ def execute_field(field:, query:, ast_node:, arguments:, object:, &block) platform_key = _otel_execute_field_key(field: field) return super unless platform_key - attributes = {} - attributes['graphql.field.parent'] = field.owner&.graphql_name - attributes['graphql.field.name'] = field.graphql_name - attributes['graphql.lazy'] = false + attributes = { + 'graphql.field.parent' => field.owner&.graphql_name, + 'graphql.field.name' => field.graphql_name, + 'graphql.lazy' => false + } tracer.in_span(platform_key, attributes: attributes, &block) end @@ -87,10 +88,11 @@ def execute_field_lazy(field:, query:, ast_node:, arguments:, object:, &block) platform_key = _otel_execute_field_key(field: field) return super unless platform_key - attributes = {} - attributes['graphql.field.parent'] = field.owner&.graphql_name - attributes['graphql.field.name'] = field.graphql_name - attributes['graphql.lazy'] = true + attributes = { + 'graphql.field.parent' => field.owner&.graphql_name, + 'graphql.field.name' => field.graphql_name, + 'graphql.lazy' => true + } tracer.in_span(platform_key, attributes: attributes, &block) end @@ -99,9 +101,10 @@ def authorized(query:, type:, object:, &block) platform_key = @_otel_authorized_key_cache[type] return super unless platform_key - attributes = {} - attributes['graphql.type.name'] = type.graphql_name - attributes['graphql.lazy'] = false + attributes = { + 'graphql.type.name' => type.graphql_name, + 'graphql.lazy' => false + } tracer.in_span(platform_key, attributes: attributes, &block) end @@ -110,9 +113,10 @@ def authorized_lazy(query:, type:, object:, &block) platform_key = @_otel_authorized_key_cache[type] return super unless platform_key - attributes = {} - attributes['graphql.type.name'] = type.graphql_name - attributes['graphql.lazy'] = true + attributes = { + 'graphql.type.name' => type.graphql_name, + 'graphql.lazy' => true + } tracer.in_span(platform_key, attributes: attributes, &block) end @@ -120,9 +124,10 @@ def authorized_lazy(query:, type:, object:, &block) def resolve_type(query:, type:, object:, &block) platform_key = @_otel_resolve_type_key_cache[type] - attributes = {} - attributes['graphql.type.name'] = type.graphql_name - attributes['graphql.lazy'] = false + attributes = { + 'graphql.type.name' => type.graphql_name, + 'graphql.lazy' => false + } tracer.in_span(platform_key, attributes: attributes, &block) end @@ -130,9 +135,10 @@ def resolve_type(query:, type:, object:, &block) def resolve_type_lazy(query:, type:, object:, &block) platform_key = @_otel_resolve_type_key_cache[type] - attributes = {} - attributes['graphql.type.name'] = type.graphql_name - attributes['graphql.lazy'] = true + attributes = { + 'graphql.type.name' => type.graphql_name, + 'graphql.lazy' => true + } tracer.in_span(platform_key, attributes: attributes, &block) end