Sha256: e1932402d1b38681f4794cb90e61a527005440363689e20b5fa55c6bd6a492f6

Contents?: true

Size: 1.74 KB

Versions: 44

Compression:

Stored size: 1.74 KB

Contents

# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2021

module Instana
  module Snapshot
    # @since 1.199
    class GoogleCloudRunInstance
      ID = 'com.instana.plugin.gcp.run.revision.instance'.freeze

      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

      def entity_id
        lookup('/computeMetadata/v1/instance/id')
      end

      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

      def snapshot
        {
          name: ID,
          entityId: entity_id,
          data: data
        }
      end

      def source
        {
          hl: true,
          cp: 'gcp',
          e: entity_id
        }
      end

      def host_name
        "gcp:cloud-run:revision:#{ENV['K_REVISION']}"
      end

      private

      def gcp_region
        lookup('/computeMetadata/v1/instance/zone').split('/').last
      end

      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
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
instana-1.217.1 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.217.0 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.216.0 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.215.1 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.215.0 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.214.4 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.214.3 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.214.2 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.214.1 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.214.0 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.213.3 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.213.2 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.213.1 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.213.0 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.212.0 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.211.0 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.210.1 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.210.0 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.209.8 lib/instana/snapshot/google_cloud_run_instance.rb
instana-1.209.7 lib/instana/snapshot/google_cloud_run_instance.rb