From 42ba8f38c49ae60f511f1477cc9825665f0627fa Mon Sep 17 00:00:00 2001 From: Robert Laurin Date: Wed, 20 May 2020 20:14:54 -0400 Subject: [PATCH] Add default Telemetry SDK resource create method 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. --- sdk/lib/opentelemetry/sdk/configurator.rb | 8 ++++++-- .../opentelemetry/sdk/resources/resource.rb | 10 +++++++++- .../opentelemetry/sdk/configurator_test.rb | 18 ++++++++++++++++++ .../sdk/resources/resource_test.rb | 9 +++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/sdk/lib/opentelemetry/sdk/configurator.rb b/sdk/lib/opentelemetry/sdk/configurator.rb index 925a21fd6b..96bb90acd0 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 ebaf32e376..0390383dad 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 26bf22b97a..c332720b9c 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 4896f02c1c..e1d27be11c 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')