Skip to content

Commit

Permalink
Address machineType not being returned in GCP metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
picandocodigo committed Jan 18, 2024
1 parent 0b4a9ac commit d246784
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/elastic_apm/metadata/cloud_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def fetch_gcp
self.project_id = metadata["project"]["projectId"]
self.availability_zone = zone
self.region = zone.split("-")[0..-2].join("-")
self.machine_type = metadata["instance"]["machineType"].split("/")[-1]
self.machine_type = metadata["instance"]["machineType"]&.split("/")&.[](-1)
rescue HTTP::TimeoutError, HTTP::ConnectionError
nil
end
Expand Down
34 changes: 34 additions & 0 deletions spec/elastic_apm/metadata/cloud_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ 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

AZURE_EXAMPLE = <<-JSON
{
"location": "westus2",
Expand Down Expand Up @@ -118,6 +129,29 @@ 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 'azure' do
let(:config) { Config.new(cloud_provider: 'azure') }

Expand Down

0 comments on commit d246784

Please sign in to comment.