lib/aspecto/opentelemetry.rb in aspecto-opentelemetry-0.1.0 vs lib/aspecto/opentelemetry.rb in aspecto-opentelemetry-0.1.1

- old
+ new

@@ -2,10 +2,13 @@ require_relative "opentelemetry/version" require_relative "opentelemetry/configurator" require_relative "opentelemetry/resource/detectors/aspecto" require_relative "opentelemetry/resource/detectors/deployment" +require_relative "opentelemetry/remote_config" +require_relative "opentelemetry/sampler/rules_sampler" + require "opentelemetry/sdk" require "opentelemetry/exporter/otlp" require "opentelemetry/instrumentation/all" require "opentelemetry-instrumentation-aws_sdk" @@ -35,12 +38,22 @@ ::OpenTelemetry::Exporter::OTLP::Exporter.new(endpoint: "https://otelcol.aspecto.io/v1/trace", headers: { "Authorization" => configurator.aspecto_auth }) ) ) + + at_exit do + ::OpenTelemetry.tracer_provider.shutdown + end end - ::OpenTelemetry.tracer_provider.sampler = ::OpenTelemetry::SDK::Trace::Samplers.parent_based(root: ::OpenTelemetry::SDK::Trace::Samplers.trace_id_ratio_based(configurator.sampling_ratio)) + fallback_sampler = ::OpenTelemetry::SDK::Trace::Samplers.trace_id_ratio_based(configurator.sampling_ratio) + remote_config = fetch_config configurator.aspecto_auth + # TODO: how to properly extract the data from resource? + _, service_name = ::OpenTelemetry.tracer_provider.resource.attribute_enumerator.detect { |elem| elem[0] == ::OpenTelemetry::SemanticConventions::Resource::SERVICE_NAME } + _, env = ::OpenTelemetry.tracer_provider.resource.attribute_enumerator.detect { |elem| elem[0] == ::OpenTelemetry::SemanticConventions::Resource::DEPLOYMENT_ENVIRONMENT } + rules_sampler = Sampler::RulesSampler.new remote_config["samplingRules"], fallback_sampler, service_name, env + ::OpenTelemetry.tracer_provider.sampler = ::OpenTelemetry::SDK::Trace::Samplers.parent_based(root: rules_sampler) rescue StandardError => e warn "Failed to initialize Aspecto tracing." warn e end