Commit 56f50aff authored by Sean McGivern's avatar Sean McGivern

Allow setting extra Sentry tags from environment

When GitLab reports an exception from Sentry, it adds tags of
'correlation_id', 'locale', and 'program'. This allows further tags to
be specified by the administrator in the GITLAB_SENTRY_EXTRA_TAGS
environment variable.

This variable must be a JSON-encoded hash of tags to send on every
Sentry event. If it fails to decode from JSON and convert to a hash, it
will simply be empty.
parent e880ed94
---
title: Allow setting extra tags for Sentry exceptions with GITLAB_SENTRY_EXTRA_TAGS
merge_request: 35965
author:
type: changed
......@@ -28,7 +28,7 @@ module Gitlab
config.processors << ::Gitlab::ErrorTracking::Processor::SidekiqProcessor
# Sanitize authentication headers
config.sanitize_http_headers = %w[Authorization Private-Token]
config.tags = { program: Gitlab.process_name }
config.tags = extra_tags_from_env.merge(program: Gitlab.process_name)
config.before_send = method(:before_send)
yield config if block_given?
......@@ -165,6 +165,15 @@ module Gitlab
}
end
# Static tags that are set on application start
def extra_tags_from_env
Gitlab::Json.parse(ENV.fetch('GITLAB_SENTRY_EXTRA_TAGS', '{}')).to_hash
rescue => e
Gitlab::AppLogger.debug("GITLAB_SENTRY_EXTRA_TAGS could not be parsed as JSON: #{e.class.name}: #{e.message}")
{}
end
# Debugging for https://gitlab.com/gitlab-org/gitlab-foss/issues/57727
def add_context_from_exception_type(event, hint)
if ActiveModel::MissingAttributeError === hint[:exception]
......
......@@ -18,6 +18,8 @@ RSpec.describe Gitlab::ErrorTracking do
]
end
let(:sentry_event) { Gitlab::Json.parse(Raven.client.transport.events.last[1]) }
before do
stub_sentry_settings
......@@ -29,6 +31,86 @@ RSpec.describe Gitlab::ErrorTracking do
end
end
describe '.configure' do
context 'default tags from GITLAB_SENTRY_EXTRA_TAGS' do
context 'when the value is a JSON hash' do
it 'includes those tags in all events' do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', { foo: 'bar', baz: 'quux' }.to_json)
described_class.configure do |config|
config.encoding = 'json'
end
described_class.track_exception(StandardError.new)
expect(sentry_event['tags'].except('correlation_id', 'locale', 'program'))
.to eq('foo' => 'bar', 'baz' => 'quux')
end
end
context 'when the value is not set' do
before do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', nil)
end
it 'does not log an error' do
expect(Gitlab::AppLogger).not_to receive(:debug)
described_class.configure do |config|
config.encoding = 'json'
end
end
it 'does not send any extra tags' do
described_class.configure do |config|
config.encoding = 'json'
end
described_class.track_exception(StandardError.new)
expect(sentry_event['tags'].keys).to contain_exactly('correlation_id', 'locale', 'program')
end
end
context 'when the value is not a JSON hash' do
using RSpec::Parameterized::TableSyntax
where(:env_var, :error) do
{ foo: 'bar', baz: 'quux' }.inspect | 'JSON::ParserError'
[].to_json | 'NoMethodError'
[%w[foo bar]].to_json | 'NoMethodError'
%w[foo bar].to_json | 'NoMethodError'
'"string"' | 'NoMethodError'
end
with_them do
before do
stub_env('GITLAB_SENTRY_EXTRA_TAGS', env_var)
end
it 'does not include any extra tags' do
described_class.configure do |config|
config.encoding = 'json'
end
described_class.track_exception(StandardError.new)
expect(sentry_event['tags'].except('correlation_id', 'locale', 'program'))
.to be_empty
end
it 'logs the error class' do
expect(Gitlab::AppLogger).to receive(:debug).with(a_string_matching(error))
described_class.configure do |config|
config.encoding = 'json'
end
end
end
end
end
end
describe '.with_context' do
it 'adds the expected tags' do
described_class.with_context {}
......@@ -202,8 +284,6 @@ RSpec.describe Gitlab::ErrorTracking do
described_class.track_exception(exception, extra)
sentry_event = Gitlab::Json.parse(Raven.client.transport.events.last[1])
expect(sentry_event.dig('extra', 'sidekiq', 'args')).to eq(['[FILTERED]', 1, 2])
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