Commit 5d639f3f authored by Markus Koller's avatar Markus Koller

Use webhook context when logging rate-limit

Include the hook metadata in the log, and remove the redundant call
to `Gitlab::AppLogger` since `auth.log` is visible in Kibana as well.
parent 772754bc
......@@ -91,12 +91,10 @@ class WebHookService
end
def async_execute
if rate_limited?(hook)
log_rate_limit(hook)
else
Gitlab::ApplicationContext.with_context(hook.application_context) do
WebHookWorker.perform_async(hook.id, data, hook_name)
end
Gitlab::ApplicationContext.with_context(hook.application_context) do
break log_rate_limit if rate_limited?
WebHookWorker.perform_async(hook.id, data, hook_name)
end
end
......@@ -177,7 +175,7 @@ class WebHookService
response.body.encode('UTF-8', invalid: :replace, undef: :replace, replace: '')
end
def rate_limited?(hook)
def rate_limited?
return false unless Feature.enabled?(:web_hooks_rate_limit, default_enabled: :yaml)
return false if rate_limit.nil?
......@@ -192,18 +190,13 @@ class WebHookService
@rate_limit ||= hook.rate_limit
end
def log_rate_limit(hook)
payload = {
def log_rate_limit
Gitlab::AuthLogger.error(
message: 'Webhook rate limit exceeded',
hook_id: hook.id,
hook_type: hook.type,
hook_name: hook_name
}
Gitlab::AuthLogger.error(payload)
# Also log into application log for now, so we can use this information
# to determine suitable limits for gitlab.com
Gitlab::AppLogger.error(payload)
hook_name: hook_name,
**Gitlab::ApplicationContext.current
)
end
end
......@@ -375,15 +375,18 @@ RSpec.describe WebHookService do
it 'does not queue a worker and logs an error' do
expect(WebHookWorker).not_to receive(:perform_async)
payload = {
message: 'Webhook rate limit exceeded',
hook_id: project_hook.id,
hook_type: 'ProjectHook',
hook_name: 'push_hooks'
}
expect(Gitlab::AuthLogger).to receive(:error).with(payload)
expect(Gitlab::AppLogger).to receive(:error).with(payload)
expect(Gitlab::AuthLogger).to receive(:error).with(
include(
message: 'Webhook rate limit exceeded',
hook_id: project_hook.id,
hook_type: 'ProjectHook',
hook_name: 'push_hooks',
"correlation_id" => kind_of(String),
"meta.project" => project.full_path,
"meta.related_class" => 'ProjectHook',
"meta.root_namespace" => project.root_namespace.full_path
)
)
service_instance.async_execute
end
......@@ -403,7 +406,6 @@ RSpec.describe WebHookService do
it 'stops queueing workers and logs errors' do
expect(Gitlab::AuthLogger).to receive(:error).twice
expect(Gitlab::AppLogger).to receive(:error).twice
2.times { service_instance.async_execute }
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