Module: Aspecto::OpenTelemetry

Defined in:
lib/aspecto/opentelemetry.rb,
lib/aspecto/opentelemetry/version.rb,
lib/aspecto/opentelemetry/configurator.rb,
lib/aspecto/opentelemetry/sampler/operator.rb,
lib/aspecto/opentelemetry/sampler/condition.rb,
lib/aspecto/opentelemetry/propagator/aspecto.rb,
lib/aspecto/opentelemetry/config/remote_config.rb,
lib/aspecto/opentelemetry/sampler/rules_sampler.rb,
lib/aspecto/opentelemetry/sampler/sampling_rule.rb,
lib/aspecto/opentelemetry/resource/detectors/aspecto.rb,
lib/aspecto/opentelemetry/sampler/message_process_sampler.rb

Overview

Aspecto OpenTelemetry Distro

Defined Under Namespace

Modules: Config, Propagator, Resource, Sampler Classes: Configurator, Error

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.configureObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aspecto/opentelemetry.rb', line 22

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 = ::OpenTelemetry::Resource::Detector::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,
                inject_messaging_context: true,
                extract_messaging_context: true
              }

    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
      # 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

  # Propagation
  ::OpenTelemetry.propagation = ::Aspecto::OpenTelemetry::Propagator::Aspecto.from_configurator configurator

  # Sampling
  if configurator.require_config_for_traces
    ::OpenTelemetry.logger.info "[Aspecto] Require config for traces. Applying ALWAYS_OFF sampler"
    ::OpenTelemetry.tracer_provider.sampler = ::OpenTelemetry::SDK::Trace::Samplers::ALWAYS_OFF
  end

  fallback_sampler = ::OpenTelemetry::SDK::Trace::Samplers.trace_id_ratio_based(configurator.sampling_ratio)
  # 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 }
  @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

.shutdownObject



73
74
75
76
77
# File 'lib/aspecto/opentelemetry.rb', line 73

def shutdown
  ::OpenTelemetry.logger.info("Aspecto is shuting down. tracing is now stopped")
  ::OpenTelemetry.tracer_provider.shutdown
  @remote_config_service&.shutdown
end

.validate_configurator_options(configurator) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/aspecto/opentelemetry.rb', line 79

def validate_configurator_options(configurator)
  # aspecto_auth
  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
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