gitlab-org--gitlab-foss/lib/gitlab/tracing.rb
Andrew Newdigate f78cd68ddf Switch back to using regexps in tracing_url_template
This approach is able to cope with `%` characters in the URL template,
which is important since `%` is a valid URL character. Additionally
this approach is less likely to fail on an invalid string. This is
important since the distributed tracing infrastructure is designed to
degrade gracefully when not properly configured, and a small mistake
in the configuration of the URL template could have led to a production
outage.
2019-02-22 14:47:37 +02:00

37 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Gitlab
module Tracing
# 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
return unless tracing_url_enabled?
# Avoid using `format` since it can throw TypeErrors
# which we want to avoid on unsanitised env var input
tracing_url_template.to_s
.gsub(/\{\{\s*correlation_id\s*\}\}/, Gitlab::CorrelationId.current_id.to_s)
.gsub(/\{\{\s*service\s*\}\}/, Gitlab.process_name)
end
end
end