Class: Instana::Exporter::Otlp::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/instana/exporter/otlp/resource.rb

Overview

Resource represents a resource, which captures identifying information about the entities for which telemetry (metrics or traces) is reported. This follows OpenTelemetry semantic conventions for resource attributes

Constant Summary collapse

PROC_SELF_CGROUP =
'/proc/self/cgroup'
DOCKER_ENV_FILE =
'/.dockerenv'
PODMAN_CONTAINERENV =
'/run/.containerenv'
MACHINE_ID_PATHS =

Linux-only stable machine-id paths (systemd and D-Bus fallback). These files do not exist on macOS or Windows; host_id returns nil there.

%w[/etc/machine-id /var/lib/dbus/machine-id].freeze
CLOUD_RESOURCE_ID =

cloud.resource_id is not yet in the installed semconv gem version

'cloud.resource_id'

Instance Attribute Summary collapse

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 Instana::Exporter::Otlp::Resource instance.

Parameters:

  • frozen_attributes (Hash<String, String>)

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



372
373
374
# File 'lib/instana/exporter/otlp/resource.rb', line 372

def initialize(frozen_attributes)
  @attributes = frozen_attributes
end

Instance Attribute Details

#attributesHash (readonly)

Returns the attributes hash for this resource

Returns:

  • (Hash)

    The frozen attributes hash



399
400
401
# File 'lib/instana/exporter/otlp/resource.rb', line 399

def attributes
  @attributes
end

Class Method Details

.create(attributes = {}) ⇒ Resource

Returns a newly created Instana::Exporter::Otlp::Resource with the specified attributes

Parameters:

  • attributes (Hash{String => String, Numeric, Boolean}) (defaults to: {})

    Hash of key-value pairs to be used as attributes for this resource

Returns:



33
34
35
36
37
38
39
# File 'lib/instana/exporter/otlp/resource.rb', line 33

def create(attributes = {})
  frozen_attributes = attributes.each_with_object({}) do |(k, v), memo|
    memo[k.freeze] = v.freeze
  end.freeze

  new(frozen_attributes)
end

.defaultResource

Returns the default resource with standard attributes

Returns:



44
45
46
47
48
49
50
51
52
# File 'lib/instana/exporter/otlp/resource.rb', line 44

def default
  @default ||= create(OpenTelemetry::SemanticConventions::Resource::SERVICE_NAME => 'ruby-service')
               .merge(process)
               .merge(telemetry_sdk)
               .merge(service_name_from_env)
               .merge(optional_attributes)
               .merge(container_attributes)
               .merge(faas_attributes)
end

.instanceHash

Get the global resource instance (singleton pattern) This method provides backward compatibility with the previous API

Returns:

  • (Hash)

    Resource attributes as a hash



58
59
60
# File 'lib/instana/exporter/otlp/resource.rb', line 58

def instance
  @instance ||= default.attributes
end

.processResource

Returns process resource attributes

Returns:



83
84
85
86
87
88
89
90
91
92
# File 'lib/instana/exporter/otlp/resource.rb', line 83

def process
  create(
    OpenTelemetry::SemanticConventions::Resource::PROCESS_PID => Process.pid,
    OpenTelemetry::SemanticConventions::Resource::PROCESS_COMMAND => $PROGRAM_NAME,
    OpenTelemetry::SemanticConventions::Resource::PROCESS_EXECUTABLE_NAME => File.basename($PROGRAM_NAME),
    OpenTelemetry::SemanticConventions::Resource::PROCESS_RUNTIME_NAME => RUBY_ENGINE,
    OpenTelemetry::SemanticConventions::Resource::PROCESS_RUNTIME_VERSION => RUBY_VERSION,
    OpenTelemetry::SemanticConventions::Resource::PROCESS_RUNTIME_DESCRIPTION => RUBY_DESCRIPTION
  )
end

.reset!Object

Reset the resource instance (useful for testing) This method provides backward compatibility with the previous API



64
65
66
67
# File 'lib/instana/exporter/otlp/resource.rb', line 64

def reset!
  @instance = nil
  @default = nil
end

.telemetry_sdkResource

Returns telemetry SDK resource attributes

Returns:



72
73
74
75
76
77
78
# File 'lib/instana/exporter/otlp/resource.rb', line 72

def telemetry_sdk
  create(
    OpenTelemetry::SemanticConventions::Resource::TELEMETRY_SDK_NAME => 'instana',
    OpenTelemetry::SemanticConventions::Resource::TELEMETRY_SDK_LANGUAGE => 'ruby',
    OpenTelemetry::SemanticConventions::Resource::TELEMETRY_SDK_VERSION => ::Instana::VERSION
  )
end

Instance Method Details

#attribute_enumeratorEnumerator

Returns an enumerator for attributes of this Instana::Exporter::Otlp::Resource

Returns:

  • (Enumerator)


379
380
381
# File 'lib/instana/exporter/otlp/resource.rb', line 379

def attribute_enumerator
  @attribute_enumerator ||= attributes.to_enum
end

#merge(other) ⇒ Resource

Returns a new, merged Instana::Exporter::Otlp::Resource by merging the current Instana::Exporter::Otlp::Resource with the other Instana::Exporter::Otlp::Resource. In case of a collision, the other Instana::Exporter::Otlp::Resource takes precedence

Parameters:

  • other (Resource)

    The other resource to merge

Returns:

  • (Resource)

    A new resource formed by merging the current resource with other



390
391
392
393
394
# File 'lib/instana/exporter/otlp/resource.rb', line 390

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

  self.class.send(:new, attributes.merge(other.send(:attributes)).freeze)
end