diff --git a/sdk/lib/opentelemetry/sdk/configurator.rb b/sdk/lib/opentelemetry/sdk/configurator.rb index 925a21fd6..96bb90acd 100644 --- a/sdk/lib/opentelemetry/sdk/configurator.rb +++ b/sdk/lib/opentelemetry/sdk/configurator.rb @@ -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 = [] @@ -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 diff --git a/sdk/lib/opentelemetry/sdk/resources/resource.rb b/sdk/lib/opentelemetry/sdk/resources/resource.rb index ebaf32e37..0390383da 100644 --- a/sdk/lib/opentelemetry/sdk/resources/resource.rb +++ b/sdk/lib/opentelemetry/sdk/resources/resource.rb @@ -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 # @@ -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 diff --git a/sdk/test/opentelemetry/sdk/configurator_test.rb b/sdk/test/opentelemetry/sdk/configurator_test.rb index 26bf22b97..c332720b9 100644 --- a/sdk/test/opentelemetry/sdk/configurator_test.rb +++ b/sdk/test/opentelemetry/sdk/configurator_test.rb @@ -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) diff --git a/sdk/test/opentelemetry/sdk/resources/resource_test.rb b/sdk/test/opentelemetry/sdk/resources/resource_test.rb index 4896f02c1..e1d27be11 100644 --- a/sdk/test/opentelemetry/sdk/resources/resource_test.rb +++ b/sdk/test/opentelemetry/sdk/resources/resource_test.rb @@ -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')