Commit 47d8b264 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'qmnguyen0711/add-queue-to-background-transaction' into 'master'

Add queue to background transaction metrics

See merge request gitlab-org/gitlab!59344
parents 605717c5 c0bb9d07
---
title: Add queue label to metrics dispatched by background transaction
merge_request: 59344
author:
type: changed
...@@ -34,8 +34,9 @@ module Gitlab ...@@ -34,8 +34,9 @@ module Gitlab
def labels def labels
@labels ||= { @labels ||= {
endpoint_id: current_context&.get_attribute(:caller_id), endpoint_id: endpoint_id,
feature_category: current_context&.get_attribute(:feature_category) feature_category: feature_category,
queue: queue
} }
end end
...@@ -44,6 +45,21 @@ module Gitlab ...@@ -44,6 +45,21 @@ module Gitlab
def current_context def current_context
Labkit::Context.current Labkit::Context.current
end 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 end
end end
...@@ -29,17 +29,60 @@ RSpec.describe Gitlab::Metrics::BackgroundTransaction do ...@@ -29,17 +29,60 @@ RSpec.describe Gitlab::Metrics::BackgroundTransaction do
end end
describe '#labels' do describe '#labels' do
it 'provides labels with endpoint_id and feature_category' do context 'when the worker queue is accessible' do
Gitlab::ApplicationContext.with_raw_context(feature_category: 'projects', caller_id: 'TestWorker') do before do
expect(transaction.labels).to eq({ endpoint_id: 'TestWorker', feature_category: 'projects' }) 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 end
end end
RSpec.shared_examples 'metric with labels' do |metric_method| 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 it 'measures with correct labels and value' do
value = 1 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 Gitlab::ApplicationContext.with_raw_context(feature_category: 'projects', caller_id: 'TestWorker') do
transaction.send(metric_method, :test_metric, value) 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