Skip to content

Commit

Permalink
chore!: remove duplicate hostname resource attribute (#433)
Browse files Browse the repository at this point in the history
Co-authored-by: Francis Bogsanyi <[email protected]>
  • Loading branch information
mwear and fbogsany authored Oct 9, 2020
1 parent f1e3a72 commit c8e0e57
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 0 additions & 4 deletions sdk/lib/opentelemetry/sdk/resources/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit c8e0e57

Please sign in to comment.