Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup instrumentation library #264

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions sdk/lib/opentelemetry/sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
13 changes: 13 additions & 0 deletions sdk/lib/opentelemetry/sdk/instrumentation_library.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions sdk/lib/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -237,14 +237,15 @@ def to_span_data
@links,
@events,
@library_resource,
@instrumentation_library,
context.span_id,
context.trace_id,
context.trace_flags
)
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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions sdk/lib/opentelemetry/sdk/trace/span_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Trace
:links,
:events,
:library_resource,
:instrumentation_library,
:span_id,
:trace_id,
:trace_flags)
Expand Down
3 changes: 2 additions & 1 deletion sdk/lib/opentelemetry/sdk/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions sdk/test/opentelemetry/sdk/trace/span_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -290,37 +290,37 @@
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

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

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
Expand Down
8 changes: 7 additions & 1 deletion sdk/test/opentelemetry/sdk/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down