Module: OpenTelemetry::Resource::Detector::GoogleCloudPlatform

Extended by:
GoogleCloudPlatform
Included in:
GoogleCloudPlatform
Defined in:
lib/opentelemetry/resource/detector/google_cloud_platform.rb,
lib/opentelemetry/resource/detector/google_cloud_platform/version.rb

Overview

GoogleCloudPlatform contains detect class method for determining gcp environment resource attributes

Constant Summary collapse

VERSION =
'0.1.1'

Instance Method Summary collapse

Instance Method Details

#detectObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/opentelemetry/resource/detector/google_cloud_platform.rb', line 16

def detect
  gcp_env = Google::Cloud::Env.new
  resource_attributes = {}

  if gcp_env.compute_engine?
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER] = 'gcp'
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_ACCOUNT_ID] = gcp_env.project_id
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION] = gcp_env.instance_attribute('cluster-location')
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_AVAILABILITY_ZONE] = gcp_env.instance_zone

    resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_ID] = gcp_env.('instance', 'id')
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::HOST_NAME] = ENV['HOSTNAME'] ||
                                                                                   gcp_env.('instance', 'hostname') ||
                                                                                   safe_gethostname
  end

  if gcp_env.kubernetes_engine?
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::K8S_CLUSTER_NAME] = gcp_env.instance_attribute('cluster-name')
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::K8S_NAMESPACE_NAME] = gcp_env.kubernetes_engine_namespace_id
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::K8S_POD_NAME] = ENV['HOSTNAME'] || safe_gethostname
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::K8S_NODE_NAME] = gcp_env.('instance', 'hostname')

    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CONTAINER_NAME] = ENV.fetch('CONTAINER_NAME', nil)
  end

  if gcp_env.knative?
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_PROVIDER] = 'gcp'
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_ACCOUNT_ID] = gcp_env.project_id
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::FAAS_NAME] = gcp_env.knative_service_id
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::FAAS_VERSION] = gcp_env.knative_service_revision
    zone = gcp_env.instance_zone
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_REGION] = get_region zone
    resource_attributes[OpenTelemetry::SemanticConventions::Resource::CLOUD_AVAILABILITY_ZONE] = zone
  end

  resource_attributes.delete_if { |_key, value| value.nil? || value.empty? }
  OpenTelemetry::SDK::Resources::Resource.create(resource_attributes)
end