Class: OpenTelemetry::SDK::Resources::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 OpenTelemetry::SDK::Resources::Resource instance.

Parameters:

  • frozen_attributes (Hash<String, String>)

    Frozen-hash of frozen-string key-value pairs to be used as attributes for this resource



61
62
63
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 61

def initialize(frozen_attributes)
  @attributes = frozen_attributes
end

Class Method Details

.create(attributes = {}) ⇒ Resource

Returns a newly created OpenTelemetry::SDK::Resources::Resource with the specified attributes

Parameters:

  • Hash{String (Hash{String => String, Numeric, Boolean} attributes Hash of key-value pairs to be used as attributes for this resource)

    => String, Numeric, Boolean} attributes Hash of key-value pairs to be used as attributes for this resource

Returns:

Raises:

  • (ArgumentError)

    If attribute keys and values are not strings



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 strings, integers, floats, or booleans' unless Internal.valid_value?(v)

    memo[-k] = v.freeze
  end.freeze

  new(frozen_attributes)
end

.telemetry_sdkObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 33

def telemetry_sdk
  resource_attributes = {
    Constants::TELEMETRY_SDK_RESOURCE[:name] => 'opentelemetry',
    Constants::TELEMETRY_SDK_RESOURCE[:language] => 'ruby',
    Constants::TELEMETRY_SDK_RESOURCE[:version] => OpenTelemetry::SDK::VERSION
  }

  resource_pairs = ENV['OTEL_RESOURCE_ATTRIBUTES']
  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_enumeratorEnumerator

Returns an enumerator for attributes of this OpenTelemetry::SDK::Resources::Resource

Returns:

  • (Enumerator)


68
69
70
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 68

def attribute_enumerator
  @attribute_enumerator ||= attributes.to_enum
end

#merge(other) ⇒ Resource

Returns a new, merged OpenTelemetry::SDK::Resources::Resource by merging the current OpenTelemetry::SDK::Resources::Resource with the other OpenTelemetry::SDK::Resources::Resource. In case of a collision, the current OpenTelemetry::SDK::Resources::Resource takes precedence

Parameters:

  • other (Resource)

    The other resource to merge

Returns:

  • (Resource)

    A new resource formed by merging the current resource with other



79
80
81
82
83
84
85
86
87
# File 'lib/opentelemetry/sdk/resources/resource.rb', line 79

def merge(other)
  return self unless other.is_a?(Resource)

  merged_attributes = attributes.merge(other.attributes) do |_, old_v, new_v|
    old_v.empty? ? new_v : old_v
  end

  self.class.send(:new, merged_attributes.freeze)
end