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

Implement TracerFactory #114

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions api/lib/opentelemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
module OpenTelemetry
extend self

attr_writer :tracer, :meter, :distributed_context_manager
attr_writer :tracer_factory, :meter, :distributed_context_manager

attr_accessor :logger
# @return [Object, Trace::Tracer] registered tracer or a default no-op
# implementation of the tracer
def tracer
@tracer ||= Trace::Tracer.new

# @return [Object, Trace::TracerFactory] registered tracer factory or a
# default no-op implementation of the tracer factory.
def tracer_factory
@tracer_factory ||= Trace::TracerFactory.new
end

# @return [Object, Metrics::Meter] registered meter or a default no-op
Expand Down
1 change: 1 addition & 0 deletions api/lib/opentelemetry/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ def self.generate_span_id
require 'opentelemetry/trace/sampling_hint'
require 'opentelemetry/trace/status'
require 'opentelemetry/trace/tracer'
require 'opentelemetry/trace/tracer_factory'
22 changes: 22 additions & 0 deletions api/lib/opentelemetry/trace/tracer_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

# Copyright 2019 OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module Trace
# No-op implementation of a tracer factory.
class TracerFactory
# Returns a {Tracer} instance.
#
# @param [optional String] name Instrumentation package name
# @param [optional String] version Instrumentation package version
#
# @return [Tracer]
def tracer(name = nil, version = nil)
@tracer ||= Tracer.new
end
end
end
end
19 changes: 19 additions & 0 deletions api/test/opentelemetry/trace/tracer_factory_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# Copyright 2019 OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

describe OpenTelemetry::Trace::TracerFactory do
let(:tracer_factory) { OpenTelemetry::Trace::TracerFactory.new }

describe '.tracer' do
it 'returns the same tracer for the same arguments' do
tracer1 = tracer_factory.tracer('component', 'semver:1.0')
tracer2 = tracer_factory.tracer('component', 'semver:1.0')
_(tracer1).must_equal(tracer2)
end
end
end
22 changes: 11 additions & 11 deletions api/test/opentelemetry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
require 'tempfile'

describe OpenTelemetry do
describe '.tracer' do
describe '.tracer_factory' do
after do
# Ensure we don't leak custom tracers to other tests
OpenTelemetry.tracer = nil
# Ensure we don't leak custom tracer factories and tracers to other tests
OpenTelemetry.tracer_factory = nil
end

it 'returns instance of Trace::Tracer by default' do
tracer = OpenTelemetry.tracer
tracer.must_be_instance_of(OpenTelemetry::Trace::Tracer)
it 'returns instance of Trace::TracerFactory by default' do
tracer_factory = OpenTelemetry.tracer_factory
_(tracer_factory).must_be_instance_of(OpenTelemetry::Trace::TracerFactory)
end

it 'returns the same instance when accessed multiple times' do
OpenTelemetry.tracer.must_equal(OpenTelemetry.tracer)
_(OpenTelemetry.tracer_factory).must_equal(OpenTelemetry.tracer_factory)
end

it 'returns user specified tracer' do
custom_tracer = 'a custom tracer'
OpenTelemetry.tracer = custom_tracer
OpenTelemetry.tracer.must_equal(custom_tracer)
it 'returns user specified tracer factory' do
custom_tracer_factory = 'a custom tracer factory'
OpenTelemetry.tracer_factory = custom_tracer_factory
_(OpenTelemetry.tracer_factory).must_equal(custom_tracer_factory)
end
end

Expand Down
1 change: 1 addition & 0 deletions sdk/lib/opentelemetry/sdk/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ module Trace
require 'opentelemetry/sdk/trace/span_data'
require 'opentelemetry/sdk/trace/span'
require 'opentelemetry/sdk/trace/tracer'
require 'opentelemetry/sdk/trace/tracer_factory'
11 changes: 10 additions & 1 deletion sdk/lib/opentelemetry/sdk/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ module Trace
class Tracer < OpenTelemetry::Trace::Tracer
attr_accessor :active_trace_config
attr_accessor :resource # TODO: should this be read-only? If not, how should exporters know when it is updated?
attr_reader :name
attr_reader :version

# @api private
#
# Returns a new {Tracer} instance.
#
# @param [String] name Instrumentation package name
# @param [String] version Instrumentation package version
#
# @return [Tracer]
def initialize
def initialize(name, version)
@name = name
@version = version
@active_span_processor = NoopSpanProcessor.instance
@active_trace_config = Config::TraceConfig::DEFAULT
@registered_span_processors = []
Expand Down
37 changes: 37 additions & 0 deletions sdk/lib/opentelemetry/sdk/trace/tracer_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# Copyright 2019 OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module SDK
module Trace
# No-op implementation of a tracer factory.
class TracerFactory
Key = Struct.new(:name, :version)
private_constant(:Key)

# Returns a new {TracerFactory} instance.
#
# @return [TracerFactory]
def initialize
@mutex = Mutex.new
@registry = {}
end

# Returns a {Tracer} instance.
#
# @param [optional String] name Instrumentation package name
# @param [optional String] version Instrumentation package version
#
# @return [Tracer]
def tracer(name = nil, version = nil)
name ||= ''
version ||= ''
@mutex.synchronize { @registry[Key.new(name, version)] ||= Tracer.new(name, version) }
end
end
end
end
end
37 changes: 37 additions & 0 deletions sdk/test/opentelemetry/sdk/trace/tracer_factory_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# Copyright 2019 OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

describe OpenTelemetry::SDK::Trace::TracerFactory do
let(:tracer_factory) { OpenTelemetry::SDK::Trace::TracerFactory.new }

describe '.tracer' do
it 'returns the same tracer for the same arguments' do
tracer1 = tracer_factory.tracer('component', 'semver:1.0')
tracer2 = tracer_factory.tracer('component', 'semver:1.0')
_(tracer1).must_equal(tracer2)
end

it 'returns a default name-less version-less tracer' do
tracer = tracer_factory.tracer
_(tracer.name).must_equal('')
_(tracer.version).must_equal('')
end

it 'returns different tracers for different names' do
tracer1 = tracer_factory.tracer('component1', 'semver:1.0')
tracer2 = tracer_factory.tracer('component2', 'semver:1.0')
_(tracer1).wont_equal(tracer2)
end

it 'returns different tracers for different versions' do
tracer1 = tracer_factory.tracer('component', 'semver:1.0')
tracer2 = tracer_factory.tracer('component', 'semver:2.0')
_(tracer1).wont_equal(tracer2)
end
end
end
18 changes: 15 additions & 3 deletions sdk/test/opentelemetry/sdk/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@
describe OpenTelemetry::SDK::Trace::Tracer do
Tracer = OpenTelemetry::SDK::Trace::Tracer

let(:tracer) { Tracer.new }
let(:tracer) { Tracer.new('', '') }
let(:record_sampler) do
->(trace_id:, span_id:, parent_context:, hint:, links:, name:, kind:, attributes:) { Result.new(decision: Decision::RECORD) } # rubocop:disable Lint/UnusedBlockArgument
end

describe '#initialize' do
it 'installs a Resource' do
Tracer.new.resource.wont_be_nil
_(Tracer.new(nil, nil).resource).wont_be_nil
end

it 'activates a default TraceConfig' do
Tracer.new.active_trace_config.must_equal(TraceConfig::DEFAULT)
_(Tracer.new(nil, nil).active_trace_config).must_equal(TraceConfig::DEFAULT)
end
end

describe '#name' do
it 'reflects the name passed in' do
_(Tracer.new('component', nil).name).must_equal('component')
end
end

describe '#version' do
it 'reflects the version passed in' do
_(Tracer.new(nil, 'semver:1.0').version).must_equal('semver:1.0')
end
end

Expand Down