Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address machineType not being returned in GCP metadata #1435

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/elastic_apm/metadata/cloud_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 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,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",
Expand Down Expand Up @@ -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') }

Expand Down
Loading