Class: OpenTelemetry::SDK::Resources::Resource
- Inherits:
-
Object
- Object
- OpenTelemetry::SDK::Resources::Resource
- Defined in:
- lib/opentelemetry/sdk/resources/resource.rb
Overview
Resource represents a resource, which captures identifying information about the entities for which telemetry (metrics or traces) is reported.
Class Method Summary collapse
-
.create(attributes = {}) ⇒ Resource
Returns a newly created Resource with the specified attributes.
-
.default ⇒ Resource
Returns the default Resource for the current process and SDK.
-
.process ⇒ Resource
Returns a Resource describing the current process.
-
.telemetry_sdk ⇒ Resource
Returns a Resource describing this telemetry SDK.
Instance Method Summary collapse
-
#attribute_enumerator ⇒ Enumerator
Returns an enumerator for attributes of this Resource.
-
#initialize(frozen_attributes) ⇒ Resource
constructor
private
The constructor is private and only for use internally by the class.
-
#merge(other) ⇒ Resource
Returns a new, merged Resource by merging the current Resource with the other Resource.
Constructor Details
#initialize(frozen_attributes) ⇒ Resource
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The constructor is private and only for use internally by the class. Users should use the create factory method to obtain a Resource instance.
93 94 95 |
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 93 def initialize(frozen_attributes) @attributes = frozen_attributes end |
Class Method Details
.create(attributes = {}) ⇒ Resource
Returns a newly created Resource with the specified attributes
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 22 def create(attributes = {}) frozen_attributes = attributes.each_with_object({}) do |(k, v), memo| raise ArgumentError, 'attribute keys must be strings' unless k.is_a?(String) raise ArgumentError, 'attribute values must be (array of) strings, integers, floats, or booleans' unless Internal.valid_value?(v) memo[-k] = v.freeze end.freeze new(frozen_attributes) end |
.default ⇒ Resource
Returns the default Resource for the current process and SDK.
36 37 38 |
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 36 def default @default ||= create(SemanticConventions::Resource::SERVICE_NAME => 'unknown_service').merge(process).merge(telemetry_sdk).merge(service_name_from_env) end |
.process ⇒ Resource
Returns a Resource describing the current process.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 65 def process resource_attributes = { SemanticConventions::Resource::PROCESS_PID => Process.pid, SemanticConventions::Resource::PROCESS_COMMAND => $PROGRAM_NAME, SemanticConventions::Resource::PROCESS_RUNTIME_NAME => RUBY_ENGINE, SemanticConventions::Resource::PROCESS_RUNTIME_VERSION => RUBY_VERSION, SemanticConventions::Resource::PROCESS_RUNTIME_DESCRIPTION => RUBY_DESCRIPTION } create(resource_attributes) end |
.telemetry_sdk ⇒ Resource
Returns a Resource describing this telemetry SDK.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 43 def telemetry_sdk resource_attributes = { SemanticConventions::Resource::TELEMETRY_SDK_NAME => 'opentelemetry', SemanticConventions::Resource::TELEMETRY_SDK_LANGUAGE => 'ruby', SemanticConventions::Resource::TELEMETRY_SDK_VERSION => OpenTelemetry::SDK::VERSION } resource_pairs = ENV.fetch('OTEL_RESOURCE_ATTRIBUTES', nil) return create(resource_attributes) unless resource_pairs.is_a?(String) resource_pairs.split(',').each do |pair| key, value = pair.split('=') resource_attributes[key] = value end resource_attributes.delete_if { |_key, value| value.nil? || value.empty? } create(resource_attributes) end |
Instance Method Details
#attribute_enumerator ⇒ Enumerator
Returns an enumerator for attributes of this Resource
100 101 102 |
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 100 def attribute_enumerator @attribute_enumerator ||= attributes.to_enum end |
#merge(other) ⇒ Resource
Returns a new, merged Resource by merging the current Resource with the other Resource. In case of a collision, the other Resource takes precedence
111 112 113 114 115 |
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 111 def merge(other) return self unless other.is_a?(Resource) self.class.send(:new, attributes.merge(other.attributes).freeze) end |