Class: OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter

Inherits:
SDK::Metrics::Export::MetricReader
  • Object
show all
Includes:
Util
Defined in:
lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb

Overview

An OpenTelemetry metrics exporter that sends metrics over HTTP as Protobuf encoded OTLP ExportMetricsServiceRequest.

Constant Summary

Constants included from Util

Util::DEFAULT_USER_AGENT, Util::ERROR_MESSAGE_INVALID_HEADERS, Util::KEEP_ALIVE_TIMEOUT, Util::RETRY_COUNT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#around_request, #as_otlp_any_value, #as_otlp_key_value, #backoff?, #handle_redirect, #http_connection, #log_status, #parse_headers, #prepare_headers

Constructor Details

#initialize(endpoint: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/metrics'), certificate_file: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'), client_certificate_file: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE'), client_key_file: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY', 'OTEL_EXPORTER_OTLP_CLIENT_KEY'), ssl_verify_mode: MetricsExporter.ssl_verify_mode, headers: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}), compression: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'), timeout: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10), aggregation_cardinality_limit: nil) ⇒ MetricsExporter

Returns a new instance of MetricsExporter.

Raises:

  • (ArgumentError)


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
76
77
78
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 48

def initialize(endpoint: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/metrics'),
               certificate_file: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'),
               client_certificate_file: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE'),
               client_key_file: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY', 'OTEL_EXPORTER_OTLP_CLIENT_KEY'),
               ssl_verify_mode: MetricsExporter.ssl_verify_mode,
               headers: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}),
               compression: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'),
               timeout: OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_METRICS_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
               aggregation_cardinality_limit: nil)
  raise ArgumentError, "invalid url for OTLP::MetricsExporter #{endpoint}" unless OpenTelemetry::Common::Utilities.valid_url?(endpoint)
  raise ArgumentError, "unsupported compression key #{compression}" unless compression.nil? || %w[gzip none].include?(compression)

  # create the MetricStore object
  super(aggregation_cardinality_limit: aggregation_cardinality_limit)

  @uri = if endpoint == ENV['OTEL_EXPORTER_OTLP_ENDPOINT']
           endpoint += '/' unless endpoint.end_with?('/')
           URI.join(endpoint, 'v1/metrics')
         else
           URI(endpoint)
         end

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

  @path = @uri.path
  @headers = prepare_headers(headers)
  @timeout = timeout.to_f
  @compression = compression
  @mutex = Mutex.new
  @shutdown = false
end

Instance Attribute Details

#metric_snapshotsObject (readonly)

Returns the value of attribute metric_snapshots.



32
33
34
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 32

def metric_snapshots
  @metric_snapshots
end

Class Method Details

.ssl_verify_modeObject



38
39
40
41
42
43
44
45
46
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 38

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

#as_otlp_aggregation_temporality(type) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 256

def as_otlp_aggregation_temporality(type)
  case type
  when :delta then Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA
  when :cumulative then Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE
  else Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_UNSPECIFIED
  end
end

#as_otlp_exemplar(exemplar) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 355

def as_otlp_exemplar(exemplar)
  args = {
    time_unix_nano: exemplar.time_unix_nano,
    span_id: exemplar.span_id,
    trace_id: exemplar.trace_id
  }

  # Add filtered_attributes if present
  args[:filtered_attributes] = exemplar.filtered_attributes.map { |k, v| as_otlp_key_value(k, v) } if exemplar.filtered_attributes

  # Set value based on type
  if exemplar.value.is_a?(Float)
    args[:as_double] = exemplar.value
  else
    args[:as_int] = exemplar.value
  end

  Opentelemetry::Proto::Metrics::V1::Exemplar.new(**args)
end

#as_otlp_exemplars(exemplars) ⇒ Object



351
352
353
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 351

def as_otlp_exemplars(exemplars)
  exemplars&.map { |ex| as_otlp_exemplar(ex) } || []
end

#as_otlp_metrics(metrics) ⇒ Object

metrics_pb has following type of data: :gauge, :sum, :histogram, :exponential_histogram, :summary current metric sdk only implements instrument: :counter -> :sum, :histogram -> :histogram, :gauge -> :gauge

metrics [MetricData]



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 222

def as_otlp_metrics(metrics)
  case metrics.instrument_kind
  when :observable_gauge, :gauge
    Opentelemetry::Proto::Metrics::V1::Metric.new(
      name: metrics.name,
      description: metrics.description,
      unit: metrics.unit,
      gauge: Opentelemetry::Proto::Metrics::V1::Gauge.new(
        data_points: metrics.data_points.map do |ndp|
          number_data_point(ndp)
        end
      )
    )

  when :counter, :up_down_counter, :observable_counter, :observable_up_down_counter
    Opentelemetry::Proto::Metrics::V1::Metric.new(
      name: metrics.name,
      description: metrics.description,
      unit: metrics.unit,
      sum: Opentelemetry::Proto::Metrics::V1::Sum.new(
        aggregation_temporality: as_otlp_aggregation_temporality(metrics.aggregation_temporality),
        data_points: metrics.data_points.map do |ndp|
          number_data_point(ndp)
        end,
        is_monotonic: metrics.is_monotonic
      )
    )

  when :histogram
    histogram_data_point(metrics)

  end
end

#encode(metrics_data) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 188

def encode(metrics_data)
  Opentelemetry::Proto::Collector::Metrics::V1::ExportMetricsServiceRequest.encode(
    Opentelemetry::Proto::Collector::Metrics::V1::ExportMetricsServiceRequest.new(
      resource_metrics: metrics_data
                        .group_by(&:resource)
                        .map do |resource, scope_metrics|
                          Opentelemetry::Proto::Metrics::V1::ResourceMetrics.new(
                            resource: Opentelemetry::Proto::Resource::V1::Resource.new(
                              attributes: resource.attribute_enumerator.map { |key, value| as_otlp_key_value(key, value) }
                            ),
                            scope_metrics: scope_metrics
                                           .group_by(&:instrumentation_scope)
                                           .map do |instrumentation_scope, metrics|
                                             Opentelemetry::Proto::Metrics::V1::ScopeMetrics.new(
                                               scope: Opentelemetry::Proto::Common::V1::InstrumentationScope.new(
                                                 name: instrumentation_scope.name,
                                                 version: instrumentation_scope.version
                                               ),
                                               metrics: metrics.map { |sd| as_otlp_metrics(sd) }
                                             )
                                           end
                          )
                        end
    )
  )
rescue StandardError => e
  OpenTelemetry.handle_error(exception: e, message: 'unexpected error in OTLP::MetricsExporter#encode')
  nil
end

#explicit_histogram_data_point(hdp) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 294

def explicit_histogram_data_point(hdp)
  Opentelemetry::Proto::Metrics::V1::HistogramDataPoint.new(
    attributes: hdp.attributes.map { |k, v| as_otlp_key_value(k, v) },
    start_time_unix_nano: hdp.start_time_unix_nano,
    time_unix_nano: hdp.time_unix_nano,
    count: hdp.count,
    sum: hdp.sum,
    bucket_counts: hdp.bucket_counts,
    explicit_bounds: hdp.explicit_bounds,
    exemplars: as_otlp_exemplars(hdp.exemplars),
    min: hdp.min,
    max: hdp.max
  )
end

#exponential_histogram_data_point(ehdp) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 309

def exponential_histogram_data_point(ehdp)
  Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint.new(
    attributes: ehdp.attributes.map { |k, v| as_otlp_key_value(k, v) },
    start_time_unix_nano: ehdp.start_time_unix_nano,
    time_unix_nano: ehdp.time_unix_nano,
    count: ehdp.count,
    sum: ehdp.sum,
    scale: ehdp.scale,
    zero_count: ehdp.zero_count,
    positive: Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint::Buckets.new(
      offset: ehdp.positive.offset,
      bucket_counts: ehdp.positive.counts
    ),
    negative: Opentelemetry::Proto::Metrics::V1::ExponentialHistogramDataPoint::Buckets.new(
      offset: ehdp.negative.offset,
      bucket_counts: ehdp.negative.counts
    ),
    flags: ehdp.flags,
    exemplars: as_otlp_exemplars(ehdp.exemplars),
    min: ehdp.min,
    max: ehdp.max,
    zero_threshold: ehdp.zero_threshold
  )
end

#export(metrics, timeout: nil) ⇒ Object

metrics Array[MetricData]



88
89
90
91
92
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 88

def export(metrics, timeout: nil)
  @mutex.synchronize do
    send_bytes(encode(metrics), timeout: timeout)
  end
end

#force_flush(timeout: nil) ⇒ Object



380
381
382
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 380

def force_flush(timeout: nil)
  SUCCESS
end

#histogram_data_point(metrics) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 264

def histogram_data_point(metrics)
  return if metrics.data_points.empty?

  if metrics.data_points.first.instance_of?(OpenTelemetry::SDK::Metrics::Aggregation::ExponentialHistogramDataPoint)
    Opentelemetry::Proto::Metrics::V1::Metric.new(
      name: metrics.name,
      description: metrics.description,
      unit: metrics.unit,
      exponential_histogram: Opentelemetry::Proto::Metrics::V1::ExponentialHistogram.new(
        aggregation_temporality: as_otlp_aggregation_temporality(metrics.aggregation_temporality),
        data_points: metrics.data_points.map do |ehdp|
          exponential_histogram_data_point(ehdp)
        end
      )
    )
  elsif metrics.data_points.first.instance_of?(OpenTelemetry::SDK::Metrics::Aggregation::HistogramDataPoint)
    Opentelemetry::Proto::Metrics::V1::Metric.new(
      name: metrics.name,
      description: metrics.description,
      unit: metrics.unit,
      histogram: Opentelemetry::Proto::Metrics::V1::Histogram.new(
        aggregation_temporality: as_otlp_aggregation_temporality(metrics.aggregation_temporality),
        data_points: metrics.data_points.map do |hdp|
          explicit_histogram_data_point(hdp)
        end
      )
    )
  end
end

#number_data_point(ndp) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 334

def number_data_point(ndp)
  args = {
    attributes: ndp.attributes.map { |k, v| as_otlp_key_value(k, v) },
    start_time_unix_nano: ndp.start_time_unix_nano,
    time_unix_nano: ndp.time_unix_nano,
    exemplars: as_otlp_exemplars(ndp.exemplars)
  }

  if ndp.value.is_a?(Float)
    args[:as_double] = ndp.value
  else
    args[:as_int] = ndp.value
  end

  Opentelemetry::Proto::Metrics::V1::NumberDataPoint.new(**args)
end

#pullObject

consolidate the metrics data into the form of MetricData

return MetricData



83
84
85
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 83

def pull
  export(collect)
end

#resetObject

may not need this



376
377
378
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 376

def reset
  SUCCESS
end

#send_bytes(bytes, timeout:) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 94

def send_bytes(bytes, timeout:)
  return FAILURE if bytes.nil?

  request = Net::HTTP::Post.new(@path)

  if @compression == 'gzip'
    request.add_field('Content-Encoding', 'gzip')
    body = Zlib.gzip(bytes)
  else
    body = bytes
  end

  request.body = body
  request.add_field('Content-Type', 'application/x-protobuf')
  @headers.each { |key, value| request.add_field(key, value) }

  retry_count = 0
  timeout ||= @timeout
  start_time = OpenTelemetry::Common::Utilities.timeout_timestamp

  around_request do
    remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
    return FAILURE if remaining_timeout.zero?

    @http.open_timeout = remaining_timeout
    @http.read_timeout = remaining_timeout
    @http.write_timeout = remaining_timeout
    @http.start unless @http.started?
    response = @http.request(request)
    case response
    when Net::HTTPSuccess
      response.body # Read and discard body
      SUCCESS
    when Net::HTTPServiceUnavailable, Net::HTTPTooManyRequests
      response.body # Read and discard body
      redo if backoff?(retry_after: response['Retry-After'], retry_count: retry_count += 1, reason: response.code)
      OpenTelemetry.logger.warn('Net::HTTPServiceUnavailable/Net::HTTPTooManyRequests in MetricsExporter#send_bytes')
      FAILURE
    when Net::HTTPRequestTimeOut, Net::HTTPGatewayTimeOut, Net::HTTPBadGateway
      response.body # Read and discard body
      redo if backoff?(retry_count: retry_count += 1, reason: response.code)
      OpenTelemetry.logger.warn('Net::HTTPRequestTimeOut/Net::HTTPGatewayTimeOut/Net::HTTPBadGateway in MetricsExporter#send_bytes')
      FAILURE
    when Net::HTTPNotFound
      OpenTelemetry.handle_error(message: "OTLP metrics_exporter received http.code=404 for uri: '#{@path}'")
      FAILURE
    when Net::HTTPBadRequest, Net::HTTPClientError, Net::HTTPServerError
      log_status(response.body)
      OpenTelemetry.logger.warn('Net::HTTPBadRequest/Net::HTTPClientError/Net::HTTPServerError in MetricsExporter#send_bytes')
      FAILURE
    when Net::HTTPRedirection
      @http.finish
      handle_redirect(response['location'])
      redo if backoff?(retry_after: 0, retry_count: retry_count += 1, reason: response.code)
    else
      @http.finish
      OpenTelemetry.logger.warn("Unexpected error in OTLP::MetricsExporter#send_bytes - #{response.message}")
      FAILURE
    end
  rescue Net::OpenTimeout, Net::ReadTimeout
    retry if backoff?(retry_count: retry_count += 1, reason: 'timeout')
    OpenTelemetry.logger.warn('Net::OpenTimeout/Net::ReadTimeout in MetricsExporter#send_bytes')
    return FAILURE
  rescue OpenSSL::SSL::SSLError
    retry if backoff?(retry_count: retry_count += 1, reason: 'openssl_error')
    OpenTelemetry.logger.warn('OpenSSL::SSL::SSLError in MetricsExporter#send_bytes')
    return FAILURE
  rescue SocketError
    retry if backoff?(retry_count: retry_count += 1, reason: 'socket_error')
    OpenTelemetry.logger.warn('SocketError in MetricsExporter#send_bytes')
    return FAILURE
  rescue SystemCallError => e
    retry if backoff?(retry_count: retry_count += 1, reason: e.class.name)
    OpenTelemetry.logger.warn('SystemCallError in MetricsExporter#send_bytes')
    return FAILURE
  rescue EOFError
    retry if backoff?(retry_count: retry_count += 1, reason: 'eof_error')
    OpenTelemetry.logger.warn('EOFError in MetricsExporter#send_bytes')
    return FAILURE
  rescue Zlib::DataError
    retry if backoff?(retry_count: retry_count += 1, reason: 'zlib_error')
    OpenTelemetry.logger.warn('Zlib::DataError in MetricsExporter#send_bytes')
    return FAILURE
  rescue StandardError => e
    OpenTelemetry.handle_error(exception: e, message: 'unexpected error in OTLP::MetricsExporter#send_bytes')
    return FAILURE
  end
ensure
  # Reset timeouts to defaults for the next call.
  @http.open_timeout = @timeout
  @http.read_timeout = @timeout
  @http.write_timeout = @timeout
end

#shutdown(timeout: nil) ⇒ Object



384
385
386
387
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 384

def shutdown(timeout: nil)
  @shutdown = true
  SUCCESS
end