Commit 8ca6ff4e authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'bvl-update-labkit-tracing' into 'master'

Update tracing initialization for labkit

See merge request gitlab-org/gitlab!25921
parents 8076bb38 761293f9
...@@ -301,7 +301,7 @@ gem 'sentry-raven', '~> 2.9' ...@@ -301,7 +301,7 @@ gem 'sentry-raven', '~> 2.9'
gem 'premailer-rails', '~> 1.10.3' gem 'premailer-rails', '~> 1.10.3'
# LabKit: Tracing and Correlation # LabKit: Tracing and Correlation
gem 'gitlab-labkit', '0.9.1' gem 'gitlab-labkit', '0.10.0'
# I18n # I18n
gem 'ruby_parser', '~> 3.8', require: false gem 'ruby_parser', '~> 3.8', require: false
......
...@@ -380,7 +380,7 @@ GEM ...@@ -380,7 +380,7 @@ GEM
github-markup (1.7.0) github-markup (1.7.0)
gitlab-chronic (0.10.5) gitlab-chronic (0.10.5)
numerizer (~> 0.2) numerizer (~> 0.2)
gitlab-labkit (0.9.1) gitlab-labkit (0.10.0)
actionpack (>= 5.0.0, < 6.1.0) actionpack (>= 5.0.0, < 6.1.0)
activesupport (>= 5.0.0, < 6.1.0) activesupport (>= 5.0.0, < 6.1.0)
grpc (~> 1.19) grpc (~> 1.19)
...@@ -1233,7 +1233,7 @@ DEPENDENCIES ...@@ -1233,7 +1233,7 @@ DEPENDENCIES
gitaly (~> 1.86.0) gitaly (~> 1.86.0)
github-markup (~> 1.7.0) github-markup (~> 1.7.0)
gitlab-chronic (~> 0.10.5) gitlab-chronic (~> 0.10.5)
gitlab-labkit (= 0.9.1) gitlab-labkit (= 0.10.0)
gitlab-license (~> 1.0) gitlab-license (~> 1.0)
gitlab-markup (~> 1.7.0) gitlab-markup (~> 1.7.0)
gitlab-net-dns (~> 0.9.1) gitlab-net-dns (~> 0.9.1)
......
...@@ -5,22 +5,6 @@ if Labkit::Tracing.enabled? ...@@ -5,22 +5,6 @@ if Labkit::Tracing.enabled?
config.middleware.insert_after Labkit::Middleware::Rack, ::Labkit::Tracing::RackMiddleware config.middleware.insert_after Labkit::Middleware::Rack, ::Labkit::Tracing::RackMiddleware
end end
# Instrument the Sidekiq client
Sidekiq.configure_client do |config|
config.client_middleware do |chain|
chain.add Labkit::Tracing::Sidekiq::ClientMiddleware
end
end
# Instrument Sidekiq server calls when running Sidekiq server
if Gitlab::Runtime.sidekiq?
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add Labkit::Tracing::Sidekiq::ServerMiddleware
end
end
end
# Instrument Redis # Instrument Redis
Labkit::Tracing::Redis.instrument Labkit::Tracing::Redis.instrument
......
# 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*\}\}/, Labkit::Correlation::CorrelationId.current_id.to_s)
.gsub(/\{\{\s*service\s*\}\}/, Gitlab.process_name)
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rspec-parameterized'
describe Gitlab::Tracing do
using RSpec::Parameterized::TableSyntax
describe '.enabled?' do
where(:connection_string, :enabled_state) do
nil | false
"" | false
"opentracing://jaeger" | true
end
with_them do
it 'returns the correct state for .enabled?' do
expect(described_class).to receive(:connection_string).and_return(connection_string)
expect(described_class.enabled?).to eq(enabled_state)
end
end
end
describe '.tracing_url_enabled?' do
where(:enabled?, :tracing_url_template, :tracing_url_enabled_state) do
false | nil | false
false | "" | false
false | "http://localhost" | false
true | nil | false
true | "" | false
true | "http://localhost" | true
end
with_them do
it 'returns the correct state for .tracing_url_enabled?' do
expect(described_class).to receive(:enabled?).and_return(enabled?)
allow(described_class).to receive(:tracing_url_template).and_return(tracing_url_template)
expect(described_class.tracing_url_enabled?).to eq(tracing_url_enabled_state)
end
end
end
describe '.tracing_url' do
where(:tracing_url_enabled?, :tracing_url_template, :correlation_id, :process_name, :tracing_url) do
false | "https://localhost" | "123" | "web" | nil
true | "https://localhost" | "123" | "web" | "https://localhost"
true | "https://localhost?service={{ service }}" | "123" | "web" | "https://localhost?service=web"
true | "https://localhost?c={{ correlation_id }}" | "123" | "web" | "https://localhost?c=123"
true | "https://localhost?c={{ correlation_id }}&s={{ service }}" | "123" | "web" | "https://localhost?c=123&s=web"
true | "https://localhost?c={{ correlation_id }}" | nil | "web" | "https://localhost?c="
true | "https://localhost?c={{ correlation_id }}&s=%22{{ service }}%22" | "123" | "web" | "https://localhost?c=123&s=%22web%22"
true | "https://localhost?c={{correlation_id}}&s={{service}}" | "123" | "web" | "https://localhost?c=123&s=web"
true | "https://localhost?c={{correlation_id }}&s={{ service}}" | "123" | "web" | "https://localhost?c=123&s=web"
end
with_them do
it 'returns the correct state for .tracing_url' do
expect(described_class).to receive(:tracing_url_enabled?).and_return(tracing_url_enabled?)
allow(described_class).to receive(:tracing_url_template).and_return(tracing_url_template)
allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return(correlation_id)
allow(Gitlab).to receive(:process_name).and_return(process_name)
expect(described_class.tracing_url).to eq(tracing_url)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment