# frozen_string_literal: true require_relative "opentelemetry/version" require_relative "opentelemetry/configurator" require_relative "opentelemetry/resource/detectors/aspecto" require_relative "opentelemetry/resource/detectors/deployment" require "opentelemetry/sdk" require "opentelemetry/exporter/otlp" require "opentelemetry/instrumentation/all" require "opentelemetry-instrumentation-aws_sdk" module Aspecto # Aspecto OpenTelemetry Distro module OpenTelemetry module_function class Error < StandardError; end def configure # rubocop:disable Metrics/AbcSize, Metrics/MethodLength configurator = Configurator.new yield configurator if block_given? validate_configurator_options(configurator) ::OpenTelemetry::SDK.configure do |c| c.logger = Logger.new($stdout, level: configurator.log_level) c.resource = Aspecto::OpenTelemetry::Resource::Detectors::Aspecto.detect c.resource = Aspecto::OpenTelemetry::Resource::Detectors::Deployment.detect c.resource = configurator.config_override_resource # must be last c.use_all "OpenTelemetry::Instrumentation::ActionPack" => { enable_recognize_route: true }, "OpenTelemetry::Instrumentation::AwsSdk" => { suppress_internal_instrumentation: true } c.add_span_processor( ::OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( ::OpenTelemetry::Exporter::OTLP::Exporter.new(endpoint: "https://otelcol.aspecto.io/v1/trace", headers: { "Authorization" => configurator.aspecto_auth }) ) ) end ::OpenTelemetry.tracer_provider.sampler = ::OpenTelemetry::SDK::Trace::Samplers.parent_based(root: ::OpenTelemetry::SDK::Trace::Samplers.trace_id_ratio_based(configurator.sampling_ratio)) rescue StandardError => e warn "Failed to initialize Aspecto tracing." warn e end def validate_configurator_options(configurator) # aspecto_auth unless configurator.aspecto_auth.instance_of?(String) raise " Unable to retrieve Aspecto token. In order for the Aspecto service to work, it requires an auth token. Please provide it through ASPECTO_AUTH env param OR aspecto_auth configure option. You can get the token from: https://app.aspecto.io/app/integration/api-key For more details please check: https://docs.aspecto.io " end # sampling_ratio unless (configurator.sampling_ratio.is_a?(Numeric) || configurator.sampling_ratio.is_a?(String)) && Float(configurator.sampling_ratio).between?(0, 1) # rubocop:disable Style/GuardClause raise " sampling_ratio should be number in range [0.0, 1.0], received #{configurator.sampling_ratio} " end end end end