class Instana::Snapshot::GoogleCloudRunInstance

@since 1.199

Constants

ID

Public Class Methods

new(metadata_uri: 'http://metadata.google.internal') click to toggle source
# File lib/instana/snapshot/google_cloud_run_instance.rb, line 10
def initialize(metadata_uri: 'http://metadata.google.internal')
  @metadata_uri = URI(metadata_uri)
  @client = Backend::RequestClient.new(@metadata_uri.host, @metadata_uri.port, use_ssl: @metadata_uri.scheme == "https")
end

Public Instance Methods

data() click to toggle source
# File lib/instana/snapshot/google_cloud_run_instance.rb, line 19
def data
  {
    runtime: 'ruby',
    region: gcp_region,
    service: ENV['K_SERVICE'],
    configuration: ENV['K_CONFIGURATION'],
    revision: ENV['K_REVISION'],
    instanceId: entity_id,
    port: ENV['PORT'],
    numericProjectId: lookup('/computeMetadata/v1/project/numeric-project-id'),
    projectId: lookup('/computeMetadata/v1/project/project-id')
  }.reject { |_, v| v.nil? }
end
entity_id() click to toggle source
# File lib/instana/snapshot/google_cloud_run_instance.rb, line 15
def entity_id
  lookup('/computeMetadata/v1/instance/id')
end
host_name() click to toggle source
# File lib/instana/snapshot/google_cloud_run_instance.rb, line 49
def host_name
  "gcp:cloud-run:revision:#{ENV['K_REVISION']}"
end
snapshot() click to toggle source
# File lib/instana/snapshot/google_cloud_run_instance.rb, line 33
def snapshot
  {
    name: ID,
    entityId: entity_id,
    data: data
  }
end
source() click to toggle source
# File lib/instana/snapshot/google_cloud_run_instance.rb, line 41
def source
  {
    hl: true,
    cp: 'gcp',
    e: entity_id
  }
end

Private Instance Methods

gcp_region() click to toggle source
# File lib/instana/snapshot/google_cloud_run_instance.rb, line 55
def gcp_region
  lookup('/computeMetadata/v1/instance/zone').split('/').last
end
lookup(resource) click to toggle source
# File lib/instana/snapshot/google_cloud_run_instance.rb, line 59
def lookup(resource)
  path = @metadata_uri.path + resource
  response = @client.send_request('GET', path, nil, {'Metadata-Flavor' => 'Google'})

  raise "Unable to get `#{path}`. Got `#{response.code}` `#{response['location']}`." unless response.ok?

  response.body
end