From 08a0ca09162c367384cac0995af99db3cb28235d Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Wed, 7 Oct 2020 17:38:34 -0700 Subject: [PATCH] chore!: remove duplicate hostname resource attribute --- .../resource/detectors/google_cloud_platform.rb | 13 +++++++------ .../detectors/google_cloud_platform_test.rb | 1 - sdk/lib/opentelemetry/sdk/resources/constants.rb | 4 ---- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/resource_detectors/lib/opentelemetry/resource/detectors/google_cloud_platform.rb b/resource_detectors/lib/opentelemetry/resource/detectors/google_cloud_platform.rb index 95ab24eac..dbcef2cf1 100644 --- a/resource_detectors/lib/opentelemetry/resource/detectors/google_cloud_platform.rb +++ b/resource_detectors/lib/opentelemetry/resource/detectors/google_cloud_platform.rb @@ -13,7 +13,7 @@ module Detectors module GoogleCloudPlatform extend self - def detect # rubocop:disable Metrics/AbcSize + def detect # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength gcp_env = Google::Cloud::Env.new resource_attributes = {} resource_constants = OpenTelemetry::SDK::Resources::Constants @@ -24,15 +24,16 @@ def detect # rubocop:disable Metrics/AbcSize resource_attributes[resource_constants::CLOUD_RESOURCE[:region]] = gcp_env.instance_attribute('cluster-location') resource_attributes[resource_constants::CLOUD_RESOURCE[:zone]] = gcp_env.instance_zone - resource_attributes[resource_constants::HOST_RESOURCE[:hostname]] = hostname resource_attributes[resource_constants::HOST_RESOURCE[:id]] = gcp_env.lookup_metadata('instance', 'id') - resource_attributes[resource_constants::HOST_RESOURCE[:name]] = gcp_env.lookup_metadata('instance', 'hostname') + resource_attributes[resource_constants::HOST_RESOURCE[:name]] = ENV['HOSTNAME'] || + gcp_env.lookup_metadata('instance', 'hostname') || + safe_gethostname end if gcp_env.kubernetes_engine? resource_attributes[resource_constants::K8S_RESOURCE[:cluster_name]] = gcp_env.instance_attribute('cluster-name') resource_attributes[resource_constants::K8S_RESOURCE[:namespace_name]] = gcp_env.kubernetes_engine_namespace_id - resource_attributes[resource_constants::K8S_RESOURCE[:pod_name]] = hostname + resource_attributes[resource_constants::K8S_RESOURCE[:pod_name]] = ENV['HOSTNAME'] || safe_gethostname resource_attributes[resource_constants::CONTAINER_RESOURCE[:name]] = ENV['CONTAINER_NAME'] end @@ -43,8 +44,8 @@ def detect # rubocop:disable Metrics/AbcSize private - def hostname - ENV['HOSTNAME'] || Socket.gethostname + def safe_gethostname + Socket.gethostname rescue StandardError '' end diff --git a/resource_detectors/test/opentelemetry/detectors/google_cloud_platform_test.rb b/resource_detectors/test/opentelemetry/detectors/google_cloud_platform_test.rb index f2cede721..0e026f914 100644 --- a/resource_detectors/test/opentelemetry/detectors/google_cloud_platform_test.rb +++ b/resource_detectors/test/opentelemetry/detectors/google_cloud_platform_test.rb @@ -51,7 +51,6 @@ 'cloud.account.id' => 'opentelemetry', 'cloud.region' => 'us-central1', 'cloud.zone' => 'us-central1-a', - 'host.hostname' => 'opentelemetry-test', 'host.id' => 'opentelemetry-test', 'host.name' => 'opentelemetry-test', 'k8s.cluster.name' => 'opentelemetry-cluster', diff --git a/sdk/lib/opentelemetry/sdk/resources/constants.rb b/sdk/lib/opentelemetry/sdk/resources/constants.rb index 6f7637292..6182f53b4 100644 --- a/sdk/lib/opentelemetry/sdk/resources/constants.rb +++ b/sdk/lib/opentelemetry/sdk/resources/constants.rb @@ -79,10 +79,6 @@ module Constants # Attributes defining a computing instance (e.g. host). HOST_RESOURCE = { - # Hostname of the host. It contains what the hostname command returns on the - # host machine. - hostname: 'host.hostname', - # Unique host id. For Cloud this must be the instance_id assigned by the # cloud provider id: 'host.id',