Commit cbec8861 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'allow-extra-sentry-tags-from-environment' into 'master'

Allow setting extra Sentry tags from environment

See merge request gitlab-org/gitlab!35965
parents f42a3f1b 56f50aff
---
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 ...@@ -28,7 +28,7 @@ module Gitlab
config.processors << ::Gitlab::ErrorTracking::Processor::SidekiqProcessor config.processors << ::Gitlab::ErrorTracking::Processor::SidekiqProcessor
# Sanitize authentication headers # Sanitize authentication headers
config.sanitize_http_headers = %w[Authorization Private-Token] 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) config.before_send = method(:before_send)
yield config if block_given? yield config if block_given?
...@@ -165,6 +165,15 @@ module Gitlab ...@@ -165,6 +165,15 @@ module Gitlab
} }
end 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 # Debugging for https://gitlab.com/gitlab-org/gitlab-foss/issues/57727
def add_context_from_exception_type(event, hint) def add_context_from_exception_type(event, hint)
if ActiveModel::MissingAttributeError === hint[:exception] if ActiveModel::MissingAttributeError === hint[:exception]
......
...@@ -18,6 +18,8 @@ RSpec.describe Gitlab::ErrorTracking do ...@@ -18,6 +18,8 @@ RSpec.describe Gitlab::ErrorTracking do
] ]
end end
let(:sentry_event) { Gitlab::Json.parse(Raven.client.transport.events.last[1]) }
before do before do
stub_sentry_settings stub_sentry_settings
...@@ -29,6 +31,86 @@ RSpec.describe Gitlab::ErrorTracking do ...@@ -29,6 +31,86 @@ RSpec.describe Gitlab::ErrorTracking do
end end
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 describe '.with_context' do
it 'adds the expected tags' do it 'adds the expected tags' do
described_class.with_context {} described_class.with_context {}
...@@ -202,8 +284,6 @@ RSpec.describe Gitlab::ErrorTracking do ...@@ -202,8 +284,6 @@ RSpec.describe Gitlab::ErrorTracking do
described_class.track_exception(exception, extra) 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]) expect(sentry_event.dig('extra', 'sidekiq', 'args')).to eq(['[FILTERED]', 1, 2])
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