From e603c3bc27d3b4f2214988b04f9ebf736a667aa4 Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Thu, 18 Jan 2024 13:15:55 +0000 Subject: [PATCH] Address machineType not being returned in GCP metadata Related: https://github.com/elastic/apm/issues/847 --- lib/elastic_apm/metadata/cloud_info.rb | 13 ++-- spec/elastic_apm/metadata/cloud_info_spec.rb | 62 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/lib/elastic_apm/metadata/cloud_info.rb b/lib/elastic_apm/metadata/cloud_info.rb index cde6b8f5e..a697e109a 100644 --- a/lib/elastic_apm/metadata/cloud_info.rb +++ b/lib/elastic_apm/metadata/cloud_info.rb @@ -94,15 +94,18 @@ def fetch_gcp return unless resp.status == 200 return unless (metadata = JSON.parse(resp.body.to_s)) - zone = metadata["instance"]["zone"]&.split("/")&.at(-1) - self.provider = "gcp" - self.instance_id = metadata["instance"]["id"].to_s - self.instance_name = metadata["instance"]["name"] self.project_id = metadata["project"]["projectId"] + + return unless metadata['instance'] + + zone = metadata["instance"]["zone"]&.split("/")&.at(-1) self.availability_zone = zone self.region = zone.split("-")[0..-2].join("-") - self.machine_type = metadata["instance"]["machineType"].split("/")[-1] + + self.instance_id = metadata["instance"]["id"].to_s + self.instance_name = metadata["instance"]["name"] + self.machine_type = metadata["instance"]["machineType"]&.split("/")&.at(-1) rescue HTTP::TimeoutError, HTTP::ConnectionError nil end diff --git a/spec/elastic_apm/metadata/cloud_info_spec.rb b/spec/elastic_apm/metadata/cloud_info_spec.rb index 35f4567e8..6c1c16b47 100644 --- a/spec/elastic_apm/metadata/cloud_info_spec.rb +++ b/spec/elastic_apm/metadata/cloud_info_spec.rb @@ -54,6 +54,22 @@ module CloudExamples } JSON + GCP_NO_MACHINE_EXAMPLE = <<-JSON + { + "instance": { + "id": 4306570268266786072, + "name": "basepi-test", + "zone": "projects/513326162531/zones/us-west3-a" + }, + "project": {"numericProjectId": 513326162531, "projectId": "elastic-apm"} + } + JSON + + GCP_NO_INSTANCE_EXAMPLE = <<-JSON + { "project": {"numericProjectId": 513326162531, "projectId": "elastic-apm"} } + JSON + + AZURE_EXAMPLE = <<-JSON { "location": "westus2", @@ -118,6 +134,52 @@ module CloudExamples end end + context 'gcp no machine_type' do + let (:config) { Config.new(cloud_provider: 'gcp') } + before do + @gcp_mock = + WebMock.stub_request(:get, Metadata::CloudInfo::GCP_URI) + .to_return(body: CloudExamples::GCP_NO_MACHINE_EXAMPLE) + end + + it 'fetches metadata from gcp' do + subject.fetch! + + expect(subject.provider).to eq('gcp') + expect(subject.instance_id).to eq("4306570268266786072") + expect(subject.instance_name).to eq("basepi-test") + expect(subject.project_id).to eq('elastic-apm') + expect(subject.availability_zone).to eq('us-west3-a') + expect(subject.region).to eq('us-west3') + expect(subject.machine_type).to be nil + + expect(@gcp_mock).to have_been_requested + end + end + + context 'gcp no instance' do + let (:config) { Config.new(cloud_provider: 'gcp') } + before do + @gcp_mock = + WebMock.stub_request(:get, Metadata::CloudInfo::GCP_URI) + .to_return(body: CloudExamples::GCP_NO_INSTANCE_EXAMPLE) + end + + it 'fetches metadata from gcp' do + subject.fetch! + + expect(subject.provider).to eq('gcp') + expect(subject.project_id).to eq('elastic-apm') + expect(subject.instance_id).to be nil + expect(subject.instance_name).to be nil + expect(subject.availability_zone).to be nil + expect(subject.region).to be nil + expect(subject.machine_type).to be nil + + expect(@gcp_mock).to have_been_requested + end + end + context 'azure' do let(:config) { Config.new(cloud_provider: 'azure') }