lib/aspecto/opentelemetry.rb in aspecto-opentelemetry-0.1.1 vs lib/aspecto/opentelemetry.rb in aspecto-opentelemetry-0.1.2
- old
+ new
@@ -2,12 +2,11 @@
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_relative "opentelemetry/config/remote_config"
require "opentelemetry/sdk"
require "opentelemetry/exporter/otlp"
require "opentelemetry/instrumentation/all"
require "opentelemetry-instrumentation-aws_sdk"
@@ -31,37 +30,42 @@
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
- })
- )
- )
+ otlp_exporter = ::OpenTelemetry::Exporter::OTLP::Exporter.new(endpoint: configurator.otel_exporter_otlp_traces_endpoint, headers: {
+ "Authorization" => configurator.aspecto_auth
+ })
+ span_processor = ::OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new otlp_exporter
+ c.add_span_processor span_processor
at_exit do
- ::OpenTelemetry.tracer_provider.shutdown
+ # at_exit might be call when service terminates
+ # but can also be the initiator or the application like with sinatra:
+ # https://github.com/sinatra/sinatra/blob/cd503e6c590cd48c2c9bb7869522494bfc62cb14/lib/sinatra/main.rb#L25
+ span_processor.force_flush timeout: 2
end
end
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)
+ @remote_config_service = Config::RemoteConfig.new configurator.aspecto_auth, service_name, env, fallback_sampler
rescue StandardError => e
warn "Failed to initialize Aspecto tracing."
warn e
end
+ def shutdown
+ ::OpenTelemetry.logger.info("Aspecto is shuting down. tracing is now stopped")
+ ::OpenTelemetry.tracer_provider.shutdown
+ @remote_config_service&.shutdown
+ end
+
def validate_configurator_options(configurator)
# aspecto_auth
- unless configurator.aspecto_auth.instance_of?(String)
+ unless configurator.aspecto_auth.instance_of?(String) && !configurator.aspecto_auth.empty?
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