Class: OpenTelemetry::Exporter::GoogleCloudTrace::SpanExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/exporter/google_cloud_trace/span_exporter.rb

Overview

This provides an implementation of span exporter for Google Cloud Trace It will convert the Opentelemetry span data into Clould Trace spans and publish them to the Cloud trace service.

Instance Method Summary collapse

Constructor Details

#initialize(project_id: nil, credentials: nil, scope: nil, timeout: nil, endpoint: nil) ⇒ OpenTelemetry::Exporter::GoogleCloudTrace::SpanExporter

Creates a new object for google cloud trace opentelemetry span exporter. It creates client for Google cloud trace service to be used for publihing span.

Examples:

require 'opentelemetry/sdk'
require 'opentelemetry/instrumentation/all'
require 'opentelemetry/exporter/google_cloud_trace'
OpenTelemetry::SDK.configure do |c|
  c.service_name = 'test_app'
  c.add_span_processor(
       OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
         OpenTelemetry::Exporter::GoogleCloudTrace::SpanExporter.new
       )
     )
  c.use_all() # enables all instrumentation!
end

Parameters:

  • project_id (String) (defaults to: nil)

    Project identifier for the Trace service you are connecting to. If not present, the default project for the credentials is used.

  • credentials (String, Hash, Google::Auth::Credentials) (defaults to: nil)

    The path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object.

  • scope (String, Array<String>) (defaults to: nil)

    The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access. See Using OAuth 2.0 to Access Google APIs.

    The default scope is:

    • https://www.googleapis.com/auth/trace.append
  • timeout (Numeric) (defaults to: nil)

    Default timeout to use in requests. Optional.

  • endpoint (String) (defaults to: nil)

    Override of the endpoint host name. Optional. If the param is nil, uses the default endpoint.

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/opentelemetry/exporter/google_cloud_trace/span_exporter.rb', line 75

def initialize project_id: nil,
               credentials: nil,
               scope: nil,
               timeout: nil,
               endpoint: nil

  @client = ::Google::Cloud::Trace::V2::TraceService::Client.new do |config|
    config.project_id = project_id if project_id
    config.credentials = credentials if credentials
    config.scope = scope if scope
    config.timeout = timeout if timeout
    config.endpoint = endpoint if endpoint
  end
  @project_id = (project_id || default_project_id || credentials&.project_id)
  @project_id = @project_id.to_s
  raise ArgumentError, "project_id is missing" if @project_id.empty?
  @shutdown = false
  @translator = Translator.new @project_id
end

Instance Method Details

#export(span_data, timeout: nil) ⇒ Integer

Called to export sampled SDK::Trace::SpanData structs.

Parameters:

  • span_data (Enumerable<OpenTelemetry::SDK::Trace::SpanData>)

    the list of recorded SDK::Trace::SpanData structs to be exported.

Returns:

  • (Integer)

    the result of the export.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/opentelemetry/exporter/google_cloud_trace/span_exporter.rb', line 104

def export span_data, timeout: nil
  return FAILURE if @shutdown

  begin
    batch_request = @translator.create_batch span_data
    @client.batch_write_spans batch_request
    SUCCESS
  rescue StandardError
    FAILURE
  end
end

#force_flush(timeout: nil) ⇒ Object

Called when SDK::Trace::TracerProvider#force_flush is called, if this exporter is registered to a SDK::Trace::TracerProvider object.



119
120
121
# File 'lib/opentelemetry/exporter/google_cloud_trace/span_exporter.rb', line 119

def force_flush timeout: nil
  SUCCESS
end

#shutdown(timeout: nil) ⇒ Object

Called when SDK::Trace::TracerProvider#shutdown is called, if this exporter is registered to a SDK::Trace::TracerProvider object.



126
127
128
129
# File 'lib/opentelemetry/exporter/google_cloud_trace/span_exporter.rb', line 126

def shutdown timeout: nil
  @shutdown = true
  SUCCESS
end