From 4a1da589d06b59853438e8c0801e396bbcaa392d Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Fri, 15 May 2020 11:17:56 +0200 Subject: [PATCH] setup instrumentation library --- .../exporters/jaeger/exporter/span_encoder_test.rb | 1 + .../exporters/jaeger/exporter_test.rb | 4 ++-- sdk/lib/opentelemetry/sdk.rb | 1 + .../opentelemetry/sdk/instrumentation_library.rb | 13 +++++++++++++ sdk/lib/opentelemetry/sdk/trace/span.rb | 6 ++++-- sdk/lib/opentelemetry/sdk/trace/span_data.rb | 1 + sdk/lib/opentelemetry/sdk/trace/tracer.rb | 3 ++- sdk/test/opentelemetry/sdk/trace/span_test.rb | 14 +++++++------- sdk/test/opentelemetry/sdk/trace/tracer_test.rb | 8 +++++++- 9 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 sdk/lib/opentelemetry/sdk/instrumentation_library.rb diff --git a/exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter/span_encoder_test.rb b/exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter/span_encoder_test.rb index 3be6d3740d..ca0f62c460 100644 --- a/exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter/span_encoder_test.rb +++ b/exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter/span_encoder_test.rb @@ -76,6 +76,7 @@ def create_span_data(attributes: nil, events: nil, links: nil, trace_id: OpenTel links, events, nil, + nil, OpenTelemetry::Trace.generate_span_id, trace_id, trace_flags diff --git a/exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter_test.rb b/exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter_test.rb index e00a57628e..1fb632fdd1 100644 --- a/exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter_test.rb +++ b/exporters/jaeger/test/opentelemetry/exporters/jaeger/exporter_test.rb @@ -79,11 +79,11 @@ def create_span_data(name: '', kind: nil, status: nil, parent_span_id: OpenTelemetry::Trace::INVALID_SPAN_ID, child_count: 0, total_recorded_attributes: 0, total_recorded_events: 0, total_recorded_links: 0, start_timestamp: Time.now, - end_timestamp: Time.now, attributes: nil, links: nil, events: nil, library_resource: nil, + end_timestamp: Time.now, attributes: nil, links: nil, events: nil, library_resource: nil, instrumentation_library: nil, span_id: OpenTelemetry::Trace.generate_span_id, trace_id: OpenTelemetry::Trace.generate_trace_id, trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT) OpenTelemetry::SDK::Trace::SpanData.new(name, kind, status, parent_span_id, child_count, total_recorded_attributes, total_recorded_events, total_recorded_links, start_timestamp, end_timestamp, - attributes, links, events, library_resource, span_id, trace_id, trace_flags) + attributes, links, events, library_resource, instrumentation_library, span_id, trace_id, trace_flags) end end diff --git a/sdk/lib/opentelemetry/sdk.rb b/sdk/lib/opentelemetry/sdk.rb index 3f6757377a..3fc3fe6ca7 100644 --- a/sdk/lib/opentelemetry/sdk.rb +++ b/sdk/lib/opentelemetry/sdk.rb @@ -63,6 +63,7 @@ def configure require 'opentelemetry/sdk/configurator' require 'opentelemetry/sdk/correlation_context' require 'opentelemetry/sdk/internal' +require 'opentelemetry/sdk/instrumentation_library' require 'opentelemetry/sdk/resources' require 'opentelemetry/sdk/trace' require 'opentelemetry/sdk/version' diff --git a/sdk/lib/opentelemetry/sdk/instrumentation_library.rb b/sdk/lib/opentelemetry/sdk/instrumentation_library.rb new file mode 100644 index 0000000000..7244b0230f --- /dev/null +++ b/sdk/lib/opentelemetry/sdk/instrumentation_library.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# Copyright 2019 OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module SDK + # InstrumentationLibrary is a struct containing library information for export. + InstrumentationLibrary = Struct.new(:name, + :version) + end +end diff --git a/sdk/lib/opentelemetry/sdk/trace/span.rb b/sdk/lib/opentelemetry/sdk/trace/span.rb index d0a2d02523..2cc6c9534e 100644 --- a/sdk/lib/opentelemetry/sdk/trace/span.rb +++ b/sdk/lib/opentelemetry/sdk/trace/span.rb @@ -18,7 +18,7 @@ module Trace class Span < OpenTelemetry::Trace::Span # The following readers are intended for the use of SpanProcessors and # should not be considered part of the public interface for instrumentation. - attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :library_resource + attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :library_resource, :instrumentation_library # Return a frozen copy of the current attributes. This is intended for # use of SpanProcesses and should not be considered part of the public @@ -237,6 +237,7 @@ def to_span_data @links, @events, @library_resource, + @instrumentation_library, context.span_id, context.trace_id, context.trace_flags @@ -244,7 +245,7 @@ def to_span_data end # @api private - def initialize(context, name, kind, parent_span_id, trace_config, span_processor, attributes, links, start_timestamp, library_resource) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + 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 super(span_context: context) @mutex = Mutex.new @name = name @@ -253,6 +254,7 @@ def initialize(context, name, kind, parent_span_id, trace_config, span_processor @trace_config = trace_config @span_processor = span_processor @library_resource = library_resource + @instrumentation_library = instrumentation_library @ended = false @status = nil @child_count = 0 diff --git a/sdk/lib/opentelemetry/sdk/trace/span_data.rb b/sdk/lib/opentelemetry/sdk/trace/span_data.rb index 4d50fcc5b5..317e93ce4a 100644 --- a/sdk/lib/opentelemetry/sdk/trace/span_data.rb +++ b/sdk/lib/opentelemetry/sdk/trace/span_data.rb @@ -24,6 +24,7 @@ module Trace :links, :events, :library_resource, + :instrumentation_library, :span_id, :trace_id, :trace_flags) diff --git a/sdk/lib/opentelemetry/sdk/trace/tracer.rb b/sdk/lib/opentelemetry/sdk/trace/tracer.rb index 2ec2e34688..0dea96738b 100644 --- a/sdk/lib/opentelemetry/sdk/trace/tracer.rb +++ b/sdk/lib/opentelemetry/sdk/trace/tracer.rb @@ -24,6 +24,7 @@ def initialize(name, version) @name = name @version = version @resource = Resources::Resource.create('name' => name, 'version' => version) + @instrumentation_library = InstrumentationLibrary.new(name, version) end 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, attributes = attributes&.merge(result.attributes) || result.attributes active_trace_config = OpenTelemetry.tracer_provider.active_trace_config active_span_processor = OpenTelemetry.tracer_provider.active_span_processor - Span.new(context, name, kind, parent_span_id, active_trace_config, active_span_processor, attributes, links, start_timestamp || Time.now, @resource) + Span.new(context, name, kind, parent_span_id, active_trace_config, active_span_processor, attributes, links, start_timestamp || Time.now, @resource, @instrumentation_library) else OpenTelemetry::Trace::Span.new(span_context: OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id)) end diff --git a/sdk/test/opentelemetry/sdk/trace/span_test.rb b/sdk/test/opentelemetry/sdk/trace/span_test.rb index c12fbdfff5..8e8321b971 100644 --- a/sdk/test/opentelemetry/sdk/trace/span_test.rb +++ b/sdk/test/opentelemetry/sdk/trace/span_test.rb @@ -27,7 +27,7 @@ end let(:span) do Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config, - span_processor, nil, nil, Time.now, nil) + span_processor, nil, nil, Time.now, nil, nil) end describe '#attributes' do @@ -261,7 +261,7 @@ it 'calls the span processor #on_finish callback' do mock_span_processor.expect(:on_start, nil) { |_| true } span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config, - mock_span_processor, nil, nil, Time.now, nil) + mock_span_processor, nil, nil, Time.now, nil, nil) mock_span_processor.expect(:on_finish, nil, [span]) span.finish mock_span_processor.verify @@ -290,7 +290,7 @@ yielded_span = nil mock_span_processor.expect(:on_start, nil) { |s| yielded_span = s } span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config, - mock_span_processor, nil, nil, Time.now, nil) + mock_span_processor, nil, nil, Time.now, nil, nil) _(yielded_span).must_equal(span) mock_span_processor.verify end @@ -298,7 +298,7 @@ it 'trims excess attributes' do attributes = { 'foo': 'bar', 'other': 'attr' } span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config, - span_processor, attributes, nil, Time.now, nil) + span_processor, attributes, nil, Time.now, nil, nil) _(span.to_span_data.total_recorded_attributes).must_equal(2) _(span.attributes.length).must_equal(1) end @@ -306,21 +306,21 @@ it 'counts attributes' do attributes = { 'foo': 'bar', 'other': 'attr' } span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config, - span_processor, attributes, nil, Time.now, nil) + span_processor, attributes, nil, Time.now, nil, nil) _(span.to_span_data.total_recorded_attributes).must_equal(2) end it 'counts links' do links = [OpenTelemetry::Trace::Link.new(context), OpenTelemetry::Trace::Link.new(context)] span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config, - span_processor, nil, links, Time.now, nil) + span_processor, nil, links, Time.now, nil, nil) _(span.to_span_data.total_recorded_links).must_equal(2) end it 'trims excess links' do links = [OpenTelemetry::Trace::Link.new(context), OpenTelemetry::Trace::Link.new(context)] span = Span.new(context, 'name', SpanKind::INTERNAL, nil, trace_config, - span_processor, nil, links, Time.now, nil) + span_processor, nil, links, Time.now, nil, nil) _(span.links.size).must_equal(1) end end diff --git a/sdk/test/opentelemetry/sdk/trace/tracer_test.rb b/sdk/test/opentelemetry/sdk/trace/tracer_test.rb index a9fc55d502..43102418a3 100644 --- a/sdk/test/opentelemetry/sdk/trace/tracer_test.rb +++ b/sdk/test/opentelemetry/sdk/trace/tracer_test.rb @@ -12,7 +12,7 @@ let(:tracer_provider) { OpenTelemetry::SDK::Trace::TracerProvider.new } let(:tracer) do OpenTelemetry.tracer_provider = tracer_provider - OpenTelemetry.tracer_provider.tracer + OpenTelemetry.tracer_provider.tracer('component-tracer', '1.0.0') end let(:record_sampler) do ->(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) { Result.new(decision: Decision::RECORD) } # rubocop:disable Lint/UnusedBlockArgument @@ -220,6 +220,12 @@ _(span.context.trace_id).must_equal(span_context.trace_id) end + it 'creates a span with the provided instrumentation library' do + span = tracer.start_span('span', with_parent_context: context) + _(span.instrumentation_library.name).must_equal('component-tracer') + _(span.instrumentation_library.version).must_equal('1.0.0') + end + it 'creates a span with all supplied parameters' do links = [OpenTelemetry::Trace::Link.new(context)] name = 'span'