Class: OpenTelemetry::Exporter::OTLP::Exporter

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

Overview

An OpenTelemetry trace exporter that sends spans over HTTP as Protobuf encoded OTLP ExportTraceServiceRequests.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/traces'), certificate_file: config_opt('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'), ssl_verify_mode: Exporter.ssl_verify_mode, headers: config_opt('OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}), compression: config_opt('OTEL_EXPORTER_OTLP_TRACES_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'), timeout: config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10), metrics_reporter: nil) ⇒ Exporter

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 46

def initialize(endpoint: config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/traces'), # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
               certificate_file: config_opt('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'),
               ssl_verify_mode: Exporter.ssl_verify_mode,
               headers: config_opt('OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}),
               compression: config_opt('OTEL_EXPORTER_OTLP_TRACES_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'),
               timeout: config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
               metrics_reporter: nil)
  raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" if invalid_url?(endpoint)
  raise ArgumentError, "unsupported compression key #{compression}" unless compression.nil? || %w[gzip none].include?(compression)

  @uri = if endpoint == ENV['OTEL_EXPORTER_OTLP_ENDPOINT']
           URI("#{endpoint}/v1/traces")
         else
           URI(endpoint)
         end

  @http = http_connection(@uri, ssl_verify_mode, certificate_file)

  @path = @uri.path
  @headers = case headers
             when String then parse_headers(headers)
             when Hash then headers
             else
               raise ArgumentError, ERROR_MESSAGE_INVALID_HEADERS
             end
  @timeout = timeout.to_f
  @compression = compression
  @metrics_reporter = metrics_reporter || OpenTelemetry::SDK::Trace::Export::MetricsReporter
  @shutdown = false
end

Class Method Details

.ssl_verify_modeObject



36
37
38
39
40
41
42
43
44
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 36

def self.ssl_verify_mode
  if ENV.key?('OTEL_RUBY_EXPORTER_OTLP_SSL_VERIFY_PEER')
    OpenSSL::SSL::VERIFY_PEER
  elsif ENV.key?('OTEL_RUBY_EXPORTER_OTLP_SSL_VERIFY_NONE')
    OpenSSL::SSL::VERIFY_NONE
  else
    OpenSSL::SSL::VERIFY_PEER
  end
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.

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.

Returns:

  • (Integer)

    the result of the export.



84
85
86
87
88
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 84

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

  send_bytes(encode(span_data), timeout: timeout)
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.

Parameters:

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.



95
96
97
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 95

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.

Parameters:

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.



104
105
106
107
108
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 104

def shutdown(timeout: nil)
  @shutdown = true
  @http.finish if @http.started?
  SUCCESS
end