Skip to content

Commit

Permalink
Add default Telemetry SDK resource create method
Browse files Browse the repository at this point in the history
The specification requires a Telemetry::SDK resource, this adds a
method for create it directly from the resource class.  This also
initializes it by default on the configurator.  The resource
setter on the configurator class will merge the default resource
with the one provided.
  • Loading branch information
robertlaurin committed May 25, 2020
1 parent 5fa5ac0 commit 42ba8f3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
8 changes: 6 additions & 2 deletions sdk/lib/opentelemetry/sdk/configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Configurator # rubocop:disable Metrics/ClassLength
private_constant :USE_MODE_UNSPECIFIED, :USE_MODE_ONE, :USE_MODE_ALL

attr_writer :logger, :http_extractors, :http_injectors, :text_extractors,
:text_injectors, :resource
:text_injectors

def initialize
@adapter_names = []
Expand All @@ -27,13 +27,17 @@ def initialize
@text_injectors = nil
@span_processors = []
@use_mode = USE_MODE_UNSPECIFIED
@resource = OpenTelemetry::SDK::Resources::Resource.create
@resource = Resources::Resource.telemetry_sdk
end

def logger
@logger ||= Logger.new(STDOUT)
end

def resource=(new_resource)
@resource = @resource.merge(new_resource)
end

# Install an instrumentation adapter with specificied optional +config+.
# Use can be called multiple times to install multiple instrumentation
# adapters. Only +use+ or +use_all+, but not both when installing
Expand Down
10 changes: 9 additions & 1 deletion sdk/lib/opentelemetry/sdk/resources/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Resources
# for which telemetry (metrics or traces) is reported.
class Resource
class << self
private :new # rubocop:disable Style/AccessModifierDeclarations
private :new

# Returns a newly created {Resource} with the specified labels
#
Expand All @@ -29,6 +29,14 @@ def create(labels = {})

new(frozen_labels)
end

def telemetry_sdk
create(
Constants::TELEMETRY_SDK_RESOURCE[:name] => 'opentelemetry',
Constants::TELEMETRY_SDK_RESOURCE[:language] => 'ruby',
Constants::TELEMETRY_SDK_RESOURCE[:version] => "semver:#{OpenTelemetry::SDK::VERSION}"
)
end
end

# @api private
Expand Down
18 changes: 18 additions & 0 deletions sdk/test/opentelemetry/sdk/configurator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
end
end

describe '#resource=' do
let(:configurator_resource) { configurator.instance_variable_get(:@resource) }
let(:configurator_resource_labels) { configurator_resource.label_enumerator.to_h }
let(:expected_resource_labels) do
{
'telemetry.sdk.name' => 'opentelemetry',
'telemetry.sdk.language' => 'ruby',
'telemetry.sdk.version' => "semver:#{OpenTelemetry::SDK::VERSION}",
'test_key' => 'test_value'
}
end

it 'merges the resource' do
configurator.resource = OpenTelemetry::SDK::Resources::Resource.create('test_key' => 'test_value')
_(configurator_resource_labels).must_equal(expected_resource_labels)
end
end

describe '#use' do
it 'can be called multiple times' do
configurator.use('TestAdapter', enabled: true)
Expand Down
9 changes: 9 additions & 0 deletions sdk/test/opentelemetry/sdk/resources/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
end
end

describe '.telemetry_sdk' do
it 'returns a resource for the telemetry sdk' do
resource_labels = Resource.telemetry_sdk.label_enumerator.to_h
_(resource_labels['telemetry.sdk.name']).must_equal('opentelemetry')
_(resource_labels['telemetry.sdk.language']).must_equal('ruby')
_(resource_labels['telemetry.sdk.version']).must_match(/semver:\b\d{1,3}\.\d{1,3}\.\d{1,3}/)
end
end

describe '#merge' do
it 'merges two resources into a third' do
res1 = Resource.create('k1' => 'v1', 'k2' => 'v2')
Expand Down

0 comments on commit 42ba8f3

Please sign in to comment.