# frozen_string_literal: true require 'active_support/all' module Labkit module Tracing autoload :Common, 'labkit/tracing/common' autoload :Factory, 'labkit/tracing/factory' autoload :GRPCInterceptor, 'labkit/tracing/grpc_interceptor' autoload :JaegerFactory, 'labkit/tracing/jaeger_factory' autoload :RackMiddleware, 'labkit/tracing/rack_middleware' module Rails autoload :ActionViewSubscriber, 'labkit/tracing/rails/action_view_subscriber' autoload :ActiveRecordSubscriber, 'labkit/tracing/rails/active_record_subscriber' autoload :RailsCommon, 'labkit/tracing/rails/rails_common' end module Sidekiq autoload :ClientMiddleware, 'labkit/tracing/sidekiq/client_middleware' autoload :ServerMiddleware, 'labkit/tracing/sidekiq/server_middleware' autoload :SidekiqCommon, 'labkit/tracing/sidekiq/sidekiq_common' end # Only enable tracing when the `GITLAB_TRACING` env var is configured. Note that we avoid using ApplicationSettings since # the same environment variable needs to be configured for Workhorse, Gitaly and any other components which # emit tracing. Since other components may start before Rails, and may not have access to ApplicationSettings, # an env var makes more sense. def self.enabled? connection_string.present? end def self.connection_string ENV['GITLAB_TRACING'] end def self.tracing_url_template ENV['GITLAB_TRACING_URL'] end def self.tracing_url_enabled? enabled? && tracing_url_template.present? end # This will provide a link into the distributed tracing for the current trace, # if it has been captured. def self.tracing_url(service_name) return unless tracing_url_enabled? correlation_id = Labkit::Correlation::CorrelationId.current_id.to_s # Avoid using `format` since it can throw TypeErrors # which we want to avoid on unsanitised env var input tracing_url_template.to_s.gsub('{{ correlation_id }}', correlation_id).gsub('{{ service }}', service_name) end end end