Skip to content

Commit 2976399

Browse files
dmathieumwear
andauthored
setup instrumentation library (#264)
Co-authored-by: Matthew Wear <[email protected]>
1 parent 5b4808f commit 2976399

File tree

9 files changed

+38
-13
lines changed

9 files changed

+38
-13
lines changed

exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter/span_encoder_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def create_span_data(attributes: nil, events: nil, links: nil, trace_id: OpenTel
7676
links,
7777
events,
7878
nil,
79+
nil,
7980
OpenTelemetry::Trace.generate_span_id,
8081
trace_id,
8182
trace_flags

exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@
7979

8080
def create_span_data(name: '', kind: nil, status: nil, parent_span_id: OpenTelemetry::Trace::INVALID_SPAN_ID, child_count: 0,
8181
total_recorded_attributes: 0, total_recorded_events: 0, total_recorded_links: 0, start_timestamp: Time.now,
82-
end_timestamp: Time.now, attributes: nil, links: nil, events: nil, library_resource: nil,
82+
end_timestamp: Time.now, attributes: nil, links: nil, events: nil, library_resource: nil, instrumentation_library: nil,
8383
span_id: OpenTelemetry::Trace.generate_span_id, trace_id: OpenTelemetry::Trace.generate_trace_id,
8484
trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT)
8585
OpenTelemetry::SDK::Trace::SpanData.new(name, kind, status, parent_span_id, child_count, total_recorded_attributes,
8686
total_recorded_events, total_recorded_links, start_timestamp, end_timestamp,
87-
attributes, links, events, library_resource, span_id, trace_id, trace_flags)
87+
attributes, links, events, library_resource, instrumentation_library, span_id, trace_id, trace_flags)
8888
end
8989
end

sdk/lib/opentelemetry/sdk.rb

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def configure
6363
require 'opentelemetry/sdk/configurator'
6464
require 'opentelemetry/sdk/correlation_context'
6565
require 'opentelemetry/sdk/internal'
66+
require 'opentelemetry/sdk/instrumentation_library'
6667
require 'opentelemetry/sdk/resources'
6768
require 'opentelemetry/sdk/trace'
6869
require 'opentelemetry/sdk/version'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright 2019 OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module SDK
9+
# InstrumentationLibrary is a struct containing library information for export.
10+
InstrumentationLibrary = Struct.new(:name,
11+
:version)
12+
end
13+
end

sdk/lib/opentelemetry/sdk/trace/span.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Trace
1818
class Span < OpenTelemetry::Trace::Span
1919
# The following readers are intended for the use of SpanProcessors and
2020
# should not be considered part of the public interface for instrumentation.
21-
attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :library_resource
21+
attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :library_resource, :instrumentation_library
2222

2323
# Return a frozen copy of the current attributes. This is intended for
2424
# use of SpanProcesses and should not be considered part of the public
@@ -237,14 +237,15 @@ def to_span_data
237237
@links,
238238
@events,
239239
@library_resource,
240+
@instrumentation_library,
240241
context.span_id,
241242
context.trace_id,
242243
context.trace_flags
243244
)
244245
end
245246

246247
# @api private
247-
def initialize(context, name, kind, parent_span_id, trace_config, span_processor, attributes, links, start_timestamp, library_resource) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
248+
def initialize(context, name, kind, parent_span_id, trace_config, span_processor, attributes, links, start_timestamp, library_resource, instrumentation_library) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
248249
super(span_context: context)
249250
@mutex = Mutex.new
250251
@name = name
@@ -253,6 +254,7 @@ def initialize(context, name, kind, parent_span_id, trace_config, span_processor
253254
@trace_config = trace_config
254255
@span_processor = span_processor
255256
@library_resource = library_resource
257+
@instrumentation_library = instrumentation_library
256258
@ended = false
257259
@status = nil
258260
@child_count = 0

sdk/lib/opentelemetry/sdk/trace/span_data.rb

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Trace
2424
:links,
2525
:events,
2626
:library_resource,
27+
:instrumentation_library,
2728
:span_id,
2829
:trace_id,
2930
:trace_flags)

sdk/lib/opentelemetry/sdk/trace/tracer.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def initialize(name, version)
2424
@name = name
2525
@version = version
2626
@resource = Resources::Resource.create('name' => name, 'version' => version)
27+
@instrumentation_library = InstrumentationLibrary.new(name, version)
2728
end
2829

2930
def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
@@ -55,7 +56,7 @@ def internal_create_span(result, name, kind, trace_id, span_id, parent_span_id,
5556
attributes = attributes&.merge(result.attributes) || result.attributes
5657
active_trace_config = OpenTelemetry.tracer_provider.active_trace_config
5758
active_span_processor = OpenTelemetry.tracer_provider.active_span_processor
58-
Span.new(context, name, kind, parent_span_id, active_trace_config, active_span_processor, attributes, links, start_timestamp || Time.now, @resource)
59+
Span.new(context, name, kind, parent_span_id, active_trace_config, active_span_processor, attributes, links, start_timestamp || Time.now, @resource, @instrumentation_library)
5960
else
6061
OpenTelemetry::Trace::Span.new(span_context: OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id))
6162
end

sdk/test/opentelemetry/sdk/trace/span_test.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
end
2828
let(:span) do
2929
Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config,
30-
span_processor, nil, nil, Time.now, nil)
30+
span_processor, nil, nil, Time.now, nil, nil)
3131
end
3232

3333
describe '#attributes' do
@@ -261,7 +261,7 @@
261261
it 'calls the span processor #on_finish callback' do
262262
mock_span_processor.expect(:on_start, nil) { |_| true }
263263
span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config,
264-
mock_span_processor, nil, nil, Time.now, nil)
264+
mock_span_processor, nil, nil, Time.now, nil, nil)
265265
mock_span_processor.expect(:on_finish, nil, [span])
266266
span.finish
267267
mock_span_processor.verify
@@ -290,37 +290,37 @@
290290
yielded_span = nil
291291
mock_span_processor.expect(:on_start, nil) { |s| yielded_span = s }
292292
span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config,
293-
mock_span_processor, nil, nil, Time.now, nil)
293+
mock_span_processor, nil, nil, Time.now, nil, nil)
294294
_(yielded_span).must_equal(span)
295295
mock_span_processor.verify
296296
end
297297

298298
it 'trims excess attributes' do
299299
attributes = { 'foo': 'bar', 'other': 'attr' }
300300
span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config,
301-
span_processor, attributes, nil, Time.now, nil)
301+
span_processor, attributes, nil, Time.now, nil, nil)
302302
_(span.to_span_data.total_recorded_attributes).must_equal(2)
303303
_(span.attributes.length).must_equal(1)
304304
end
305305

306306
it 'counts attributes' do
307307
attributes = { 'foo': 'bar', 'other': 'attr' }
308308
span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config,
309-
span_processor, attributes, nil, Time.now, nil)
309+
span_processor, attributes, nil, Time.now, nil, nil)
310310
_(span.to_span_data.total_recorded_attributes).must_equal(2)
311311
end
312312

313313
it 'counts links' do
314314
links = [OpenTelemetry::Trace::Link.new(context), OpenTelemetry::Trace::Link.new(context)]
315315
span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config,
316-
span_processor, nil, links, Time.now, nil)
316+
span_processor, nil, links, Time.now, nil, nil)
317317
_(span.to_span_data.total_recorded_links).must_equal(2)
318318
end
319319

320320
it 'trims excess links' do
321321
links = [OpenTelemetry::Trace::Link.new(context), OpenTelemetry::Trace::Link.new(context)]
322322
span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config,
323-
span_processor, nil, links, Time.now, nil)
323+
span_processor, nil, links, Time.now, nil, nil)
324324
_(span.links.size).must_equal(1)
325325
end
326326
end

sdk/test/opentelemetry/sdk/trace/tracer_test.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
let(:tracer_provider) { OpenTelemetry::SDK::Trace::TracerProvider.new }
1313
let(:tracer) do
1414
OpenTelemetry.tracer_provider = tracer_provider
15-
OpenTelemetry.tracer_provider.tracer
15+
OpenTelemetry.tracer_provider.tracer('component-tracer', '1.0.0')
1616
end
1717
let(:record_sampler) do
1818
->(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) { Result.new(decision: Decision::RECORD) } # rubocop:disable Lint/UnusedBlockArgument
@@ -220,6 +220,12 @@
220220
_(span.context.trace_id).must_equal(span_context.trace_id)
221221
end
222222

223+
it 'creates a span with the provided instrumentation library' do
224+
span = tracer.start_span('span', with_parent_context: context)
225+
_(span.instrumentation_library.name).must_equal('component-tracer')
226+
_(span.instrumentation_library.version).must_equal('1.0.0')
227+
end
228+
223229
it 'creates a span with all supplied parameters' do
224230
links = [OpenTelemetry::Trace::Link.new(context)]
225231
name = 'span'

0 commit comments

Comments
 (0)