Class: OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter
- Inherits:
-
SDK::Metrics::Export::MetricReader
- Object
- SDK::Metrics::Export::MetricReader
- OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter
- 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
-
#metric_snapshots ⇒ Object
readonly
Returns the value of attribute metric_snapshots.
Class Method Summary collapse
Instance Method Summary collapse
- #as_otlp_aggregation_temporality(type) ⇒ Object
-
#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.
- #encode(metrics_data) ⇒ Object
-
#export(metrics, timeout: nil) ⇒ Object
metrics Array.
- #force_flush(timeout: nil) ⇒ Object
- #histogram_data_point(hdp) ⇒ Object
-
#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)) ⇒ MetricsExporter
constructor
A new instance of MetricsExporter.
- #number_data_point(ndp) ⇒ Object
-
#pull ⇒ Object
consolidate the metrics data into the form of MetricData.
-
#reset ⇒ Object
may not need this.
- #send_bytes(bytes, timeout:) ⇒ Object
- #shutdown(timeout: nil) ⇒ Object
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)) ⇒ MetricsExporter
Returns a new instance of MetricsExporter.
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 |
# 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)) 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() @uri = if endpoint == ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] 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_snapshots ⇒ Object (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_mode ⇒ Object
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
263 264 265 266 267 268 269 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 263 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_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
metrics [MetricData]
220 221 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 255 256 257 258 259 260 261 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 220 def as_otlp_metrics(metrics) case metrics.instrument_kind when :observable_gauge Opentelemetry::Proto::Metrics::V1::Metric.new( name: metrics.name, description: metrics.description, unit: metrics.unit, gauge: Opentelemetry::Proto::Metrics::V1::Gauge.new( aggregation_temporality: as_otlp_aggregation_temporality(metrics.aggregation_temporality), data_points: metrics.data_points.map do |ndp| number_data_point(ndp) end ) ) when :counter, :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 ) ) when :histogram 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| histogram_data_point(hdp) end ) ) end end |
#encode(metrics_data) ⇒ Object
186 187 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 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 186 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 |
#export(metrics, timeout: nil) ⇒ Object
metrics Array
86 87 88 89 90 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 86 def export(metrics, timeout: nil) @mutex.synchronize do send_bytes(encode(metrics), timeout: timeout) end end |
#force_flush(timeout: nil) ⇒ Object
301 302 303 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 301 def force_flush(timeout: nil) SUCCESS end |
#histogram_data_point(hdp) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 271 def 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: hdp.exemplars, min: hdp.min, max: hdp.max ) end |
#number_data_point(ndp) ⇒ Object
286 287 288 289 290 291 292 293 294 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 286 def number_data_point(ndp) Opentelemetry::Proto::Metrics::V1::NumberDataPoint.new( attributes: ndp.attributes.map { |k, v| as_otlp_key_value(k, v) }, as_int: ndp.value, start_time_unix_nano: ndp.start_time_unix_nano, time_unix_nano: ndp.time_unix_nano, exemplars: ndp.exemplars # exemplars not implemented yet from metrics sdk ) end |
#pull ⇒ Object
consolidate the metrics data into the form of MetricData
return MetricData
81 82 83 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 81 def pull export(collect) end |
#reset ⇒ Object
may not need this
297 298 299 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 297 def reset SUCCESS end |
#send_bytes(bytes, timeout:) ⇒ Object
92 93 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 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 92 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. 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::HTTPOK 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.}") 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
305 306 307 308 |
# File 'lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb', line 305 def shutdown(timeout: nil) @shutdown = true SUCCESS end |