lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb in opentelemetry-exporter-otlp-metrics-0.1.0 vs lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb in opentelemetry-exporter-otlp-metrics-0.2.0

- old
+ new

@@ -24,11 +24,11 @@ module OpenTelemetry module Exporter module OTLP module Metrics # An OpenTelemetry metrics exporter that sends metrics over HTTP as Protobuf encoded OTLP ExportMetricsServiceRequest. - class MetricsExporter < ::OpenTelemetry::SDK::Metrics::Export::MetricReader # rubocop:disable Metrics/ClassLength + class MetricsExporter < ::OpenTelemetry::SDK::Metrics::Export::MetricReader include Util attr_reader :metric_snapshots SUCCESS = OpenTelemetry::SDK::Metrics::Export::SUCCESS @@ -45,10 +45,12 @@ end end 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) @@ -61,11 +63,11 @@ URI.join(endpoint, 'v1/metrics') else URI(endpoint) end - @http = http_connection(@uri, ssl_verify_mode, certificate_file) + @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 @@ -85,11 +87,11 @@ @mutex.synchronize do send_bytes(encode(metrics), timeout: timeout) end end - def send_bytes(bytes, timeout:) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity + def send_bytes(bytes, timeout:) return FAILURE if bytes.nil? request = Net::HTTP::Post.new(@path) if @compression == 'gzip' @@ -113,11 +115,11 @@ @http.open_timeout = remaining_timeout @http.read_timeout = remaining_timeout @http.write_timeout = remaining_timeout @http.start unless @http.started? - response = measure_request_duration { @http.request(request) } + response = @http.request(request) case response when Net::HTTPOK response.body # Read and discard body SUCCESS when Net::HTTPServiceUnavailable, Net::HTTPTooManyRequests @@ -179,11 +181,11 @@ @http.open_timeout = @timeout @http.read_timeout = @timeout @http.write_timeout = @timeout end - def encode(metrics_data) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity + 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| @@ -213,10 +215,10 @@ # 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] - def as_otlp_metrics(metrics) # rubocop:disable Metrics/MethodLength + def as_otlp_metrics(metrics) case metrics.instrument_kind when :observable_gauge Opentelemetry::Proto::Metrics::V1::Metric.new( name: metrics.name, description: metrics.description,