lib/elastic_apm/metadata/cloud_info.rb in elastic-apm-4.0.0 vs lib/elastic_apm/metadata/cloud_info.rb in elastic-apm-4.1.0

- old
+ new

@@ -55,13 +55,13 @@ when "aws" fetch_aws when "gcp" fetch_gcp when "azure" - fetch_azure + fetch_azure || read_azure_app_services when "auto" - fetch_aws || fetch_gcp || fetch_azure + fetch_aws || fetch_gcp || fetch_azure || read_azure_app_services when "none" nil else error("Unknown setting for cloud_provider '#{config.cloud_provider}'") end @@ -74,11 +74,11 @@ def fetch_aws resp = @client.get(AWS_URI) return unless resp.status == 200 - return unless (metadata = JSON.parse(resp.body)) + return unless (metadata = JSON.parse(resp.body.to_s)) self.provider = "aws" self.account_id = metadata["accountId"] self.instance_id = metadata["instanceId"] self.availability_zone = metadata["availabilityZone"] @@ -90,11 +90,11 @@ def fetch_gcp resp = @client.headers("Metadata-Flavor" => "Google").get(GCP_URI) return unless resp.status == 200 - return unless (metadata = JSON.parse(resp.body)) + 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"] @@ -110,11 +110,11 @@ def fetch_azure resp = @client.headers("Metadata" => "true").get(AZURE_URI) return unless resp.status == 200 - return unless (metadata = JSON.parse(resp.body)) + return unless (metadata = JSON.parse(resp.body.to_s)) self.provider = 'azure' self.account_id = metadata["subscriptionId"] self.instance_id = metadata["vmId"] self.instance_name = metadata["name"] @@ -122,9 +122,36 @@ self.availability_zone = metadata["zone"] self.machine_type = metadata["vmSize"] self.region = metadata["location"] rescue HTTP::TimeoutError, HTTP::ConnectionError nil + end + + def read_azure_app_services + owner_name, instance_id, site_name, resource_group = + ENV.values_at( + 'WEBSITE_OWNER_NAME', + 'WEBSITE_INSTANCE_ID', + 'WEBSITE_SITE_NAME', + 'WEBSITE_RESOURCE_GROUP' + ) + + return unless owner_name && instance_id && site_name && resource_group + + self.provider = 'azure' + self.instance_id = instance_id + self.instance_name = site_name + self.project_name = resource_group + self.account_id, self.region = parse_azure_app_services_owner_name(owner_name) + end + + private + + def parse_azure_app_services_owner_name(owner_name) + id, rest = owner_name.split('+') + *_, region = rest.split('-') + region.gsub!(/webspace.*$/, '') + [id, region] end end end end