Commit c0bb9d07 authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen Committed by Bob Van Landuyt

Add queue to background transaction metrics

parent 66ce587c
---
title: Add queue label to metrics dispatched by background transaction
merge_request: 59344
author:
type: changed
......@@ -34,8 +34,9 @@ module Gitlab
def labels
@labels ||= {
endpoint_id: current_context&.get_attribute(:caller_id),
feature_category: current_context&.get_attribute(:feature_category)
endpoint_id: endpoint_id,
feature_category: feature_category,
queue: queue
}
end
......@@ -44,6 +45,21 @@ module Gitlab
def current_context
Labkit::Context.current
end
def feature_category
current_context&.get_attribute(:feature_category)
end
def endpoint_id
current_context&.get_attribute(:caller_id)
end
def queue
worker_class = endpoint_id.to_s.safe_constantize
return if worker_class.blank? || !worker_class.respond_to?(:queue)
worker_class.queue.to_s
end
end
end
end
......@@ -29,17 +29,60 @@ RSpec.describe Gitlab::Metrics::BackgroundTransaction do
end
describe '#labels' do
it 'provides labels with endpoint_id and feature_category' do
Gitlab::ApplicationContext.with_raw_context(feature_category: 'projects', caller_id: 'TestWorker') do
expect(transaction.labels).to eq({ endpoint_id: 'TestWorker', feature_category: 'projects' })
context 'when the worker queue is accessible' do
before do
test_worker_class = Class.new do
def self.queue
'test_worker'
end
end
stub_const('TestWorker', test_worker_class)
end
it 'provides labels with endpoint_id, feature_category and queue' do
Gitlab::ApplicationContext.with_raw_context(feature_category: 'projects', caller_id: 'TestWorker') do
expect(transaction.labels).to eq({ endpoint_id: 'TestWorker', feature_category: 'projects', queue: 'test_worker' })
end
end
end
context 'when the worker name does not exist' do
it 'provides labels with endpoint_id and feature_category' do
# 123TestWorker is an invalid constant
Gitlab::ApplicationContext.with_raw_context(feature_category: 'projects', caller_id: '123TestWorker') do
expect(transaction.labels).to eq({ endpoint_id: '123TestWorker', feature_category: 'projects', queue: nil })
end
end
end
context 'when the worker queue is not accessible' do
before do
stub_const('TestWorker', Class.new)
end
it 'provides labels with endpoint_id and feature_category' do
Gitlab::ApplicationContext.with_raw_context(feature_category: 'projects', caller_id: 'TestWorker') do
expect(transaction.labels).to eq({ endpoint_id: 'TestWorker', feature_category: 'projects', queue: nil })
end
end
end
end
RSpec.shared_examples 'metric with labels' do |metric_method|
before do
test_worker_class = Class.new do
def self.queue
'test_worker'
end
end
stub_const('TestWorker', test_worker_class)
end
it 'measures with correct labels and value' do
value = 1
expect(prometheus_metric).to receive(metric_method).with({ endpoint_id: 'TestWorker', feature_category: 'projects' }, value)
expect(prometheus_metric).to receive(metric_method).with({
endpoint_id: 'TestWorker', feature_category: 'projects', queue: 'test_worker'
}, value)
Gitlab::ApplicationContext.with_raw_context(feature_category: 'projects', caller_id: 'TestWorker') do
transaction.send(metric_method, :test_metric, value)
......
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