lib/opentelemetry/exporter/otlp/exporter.rb in opentelemetry-exporter-otlp-0.7.0 vs lib/opentelemetry/exporter/otlp/exporter.rb in opentelemetry-exporter-otlp-0.8.0

- old
+ new

@@ -5,10 +5,11 @@ # SPDX-License-Identifier: Apache-2.0 require 'opentelemetry/sdk' require 'net/http' require 'csv' +require 'zlib' require 'opentelemetry/proto/common/v1/common_pb' require 'opentelemetry/proto/resource/v1/resource_pb' require 'opentelemetry/proto/trace/v1/trace_pb' require 'opentelemetry/proto/collector/trace/v1/trace_service_pb' @@ -34,11 +35,11 @@ certificate_file: config_opt('OTEL_EXPORTER_OTLP_SPAN_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'), headers: config_opt('OTEL_EXPORTER_OTLP_SPAN_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS'), # TODO: what format is expected here? compression: config_opt('OTEL_EXPORTER_OTLP_SPAN_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION'), timeout: config_opt('OTEL_EXPORTER_OTLP_SPAN_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10)) raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" if invalid_url?("http://#{endpoint}") - raise ArgumentError, "unsupported compression key #{compression}" unless compression.nil? + raise ArgumentError, "unsupported compression key #{compression}" unless compression.nil? || compression == 'gzip' raise ArgumentError, 'headers must be comma-separated k:v pairs or a Hash' unless valid_headers?(headers) uri = URI "http://#{endpoint}" @http = Net::HTTP.new(uri.host, uri.port) @http.use_ssl = insecure.to_s.downcase == 'false' @@ -51,11 +52,11 @@ @headers = case headers when String then CSV.parse(headers, col_sep: ':', row_sep: ',').to_h when Hash then headers end @timeout = timeout.to_f # TODO: use this as a default timeout when we implement timeouts in https://github.com/open-telemetry/opentelemetry-ruby/pull/341 - @tracer = OpenTelemetry.tracer_provider.tracer + @compression = compression @shutdown = false end # Called to export sampled {OpenTelemetry::SDK::Trace::SpanData} structs. @@ -109,15 +110,18 @@ def send_bytes(bytes) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity retry_count = 0 untraced do # rubocop:disable Metrics/BlockLength request = Net::HTTP::Post.new(@path) - request.body = bytes + request.body = if @compression == 'gzip' + request.add_field('Content-Encoding', 'gzip') + Zlib.gzip(bytes) + else + bytes + end request.add_field('Content-Type', 'application/x-protobuf') @headers&.each { |key, value| request.add_field(key, value) } - # TODO: enable gzip when https://github.com/open-telemetry/opentelemetry-collector/issues/1344 is fixed. - # request.add_field('Content-Encoding', 'gzip') @http.start unless @http.started? response = @http.request(request) case response @@ -153,11 +157,11 @@ def handle_redirect(location) # TODO: figure out destination and reinitialize @http and @path end def untraced - @tracer.with_span(OpenTelemetry::Trace::Span.new) { yield } + OpenTelemetry::Trace.with_span(OpenTelemetry::Trace::Span.new) { yield } end def backoff?(retry_after: nil, retry_count:, reason:) return false if retry_count > RETRY_COUNT @@ -231,21 +235,21 @@ ) end, dropped_events_count: span_data.total_recorded_events - span_data.events&.size.to_i, links: span_data.links&.map do |link| Opentelemetry::Proto::Trace::V1::Span::Link.new( - trace_id: link.context.trace_id, - span_id: link.context.span_id, - trace_state: link.context.tracestate, + trace_id: link.span_context.trace_id, + span_id: link.span_context.span_id, + trace_state: link.span_context.tracestate, attributes: link.attributes&.map { |k, v| as_otlp_key_value(k, v) } # TODO: track dropped_attributes_count in Span#trim_links ) end, dropped_links_count: span_data.total_recorded_links - span_data.links&.size.to_i, status: span_data.status&.yield_self do |status| # TODO: fix this based on spec update. Opentelemetry::Proto::Trace::V1::Status.new( - code: status.canonical_code == OpenTelemetry::Trace::Status::ERROR ? Opentelemetry::Proto::Trace::V1::Status::StatusCode::UnknownError : Opentelemetry::Proto::Trace::V1::Status::StatusCode::Ok, + code: status.code == OpenTelemetry::Trace::Status::ERROR ? Opentelemetry::Proto::Trace::V1::Status::StatusCode::UnknownError : Opentelemetry::Proto::Trace::V1::Status::StatusCode::Ok, message: status.description ) end ) end