Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix labels handling #4

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/prometheus_exporter/ext/instrumentation/base_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def collect_data(data)
@client.send_json(metric)
end

# @param datum [Hash]
# @param data [Hash]
# @return [Hash]
def build_metric(datum)
metric = datum.dup
def build_metric(data)
metric = data.dup
metric[:type] = type
metric[:metric_labels] = @metric_labels
metric[:labels] = (metric[:labels] || {}).merge(@metric_labels)
metric
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/prometheus_exporter/ext/server/stats_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def fill_observers(obj, labels)
def build_labels(obj)
labels = {}
labels.merge!(obj['labels']) if obj['labels']
labels.merge!(obj['metric_labels']) if obj['metric_labels']
labels.merge!(obj['custom_labels']) if obj['custom_labels']

labels
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
{
type: 'test',
foo: 123, bar: 456,
labels: { qwe: 'asd' },
metric_labels: {}
labels: { qwe: 'asd' }
},
{
type: 'test',
foo: 124, bar: 457,
labels: { qwe: 'zxc' },
metric_labels: {}
labels: { qwe: 'zxc' }
}
]
)
Expand Down
123 changes: 123 additions & 0 deletions spec/prometheus_exporter/ext/server/stats_collector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# frozen_string_literal: true

RSpec.describe PrometheusExporter::Ext::Server::StatsCollector do
subject do
collector.collect(stringified_metric)
end

let(:collector) { TestCollector.new }
let(:stringified_metric) { JSON.parse JSON.generate(metric) }
let(:metric) do
{
type: 'test',
labels: { qwe: 'asd' },
g_metric: 1.23,
gwt_metric: 4.56,
c_metric: 7.89
}
end
let(:expected_labels) { metric[:labels] }

it 'observes prometheus metrics' do
subject
expect(collector.metrics).to contain_exactly(
a_gauge_metric('test_g_metric').with(1.23, expected_labels),
a_gauge_with_time_metric('test_gwt_metric').with([4.56, ms_since_epoch], expected_labels),
a_counter_metric('test_c_metric').with(7.89, expected_labels)
)
end

context 'with empty custom_labels' do
let(:metric) do
super().merge custom_labels: {}
end

it 'observes prometheus metrics' do
subject
expect(collector.metrics).to contain_exactly(
a_gauge_metric('test_g_metric').with(1.23, expected_labels),
a_gauge_with_time_metric('test_gwt_metric').with([4.56, ms_since_epoch],
expected_labels
),
a_counter_metric('test_c_metric').with(7.89, expected_labels)
)
end
end

context 'with filled custom_labels' do
let(:metric) do
super().merge custom_labels: { host: 'example.com' }
end
let(:expected_labels) { metric[:labels].merge metric[:custom_labels] }

it 'observes prometheus metrics' do
subject
expect(collector.metrics).to contain_exactly(
a_gauge_metric('test_g_metric').with(1.23, expected_labels),
a_gauge_with_time_metric('test_gwt_metric').with([4.56, ms_since_epoch], expected_labels),
a_counter_metric('test_c_metric').with(7.89, expected_labels)
)
end
end

context 'when collector has previous metrics with same labels' do
let(:prev_stringified_metric) { JSON.parse JSON.generate(prev_metric) }
let(:prev_metric) do
{
type: 'test',
labels: { qwe: 'asd' },
g_metric: 10,
gwt_metric: 20,
c_metric: 30
}
end

before do
collector.collect(prev_stringified_metric)
end

it 'observes prometheus metrics' do
subject
expect(collector.metrics).to contain_exactly(
a_gauge_metric('test_g_metric').with(1.23, expected_labels),
a_gauge_with_time_metric('test_gwt_metric').with([4.56, ms_since_epoch],
expected_labels
),
a_counter_metric('test_c_metric').with(37.89, expected_labels)
)
end
end

context 'when collector has previous metrics with different labels' do
let(:prev_stringified_metric) { JSON.parse JSON.generate(prev_metric) }
let(:prev_metric) do
{
type: 'test',
labels: { qwe: 'asd2' },
g_metric: 10,
gwt_metric: 20,
c_metric: 30
}
end
let(:prev_expected_labels) { prev_metric[:labels] }

before do
collector.collect(prev_stringified_metric)
end

it 'observes prometheus metrics' do
subject
expect(collector.metrics).to contain_exactly(
a_gauge_metric('test_g_metric')
.with(10, prev_expected_labels)
.with(1.23, expected_labels),
a_gauge_with_time_metric('test_gwt_metric')
.with([20, ms_since_epoch], prev_expected_labels)
.with([4.56, ms_since_epoch], expected_labels),
a_counter_metric('test_c_metric')
.with(30, prev_expected_labels)
.with(7.89, expected_labels)
)
end
end
end
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# frozen_string_literal: true

require 'prometheus_exporter'
require 'prometheus_exporter/server'
require 'prometheus_exporter/ext'
require 'prometheus_exporter/ext/rspec'
require 'prometheus_exporter/ext/instrumentation/base_stats'
require 'prometheus_exporter/ext/server/stats_collector'

RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = nil

class TestInstrumentation < PrometheusExporter::Ext::Instrumentation::BaseStats
self.type = 'test'
Expand All @@ -15,6 +19,15 @@ def collect(data_list)
end
end

class TestCollector < PrometheusExporter::Server::TypeCollector
include PrometheusExporter::Ext::Server::StatsCollector
self.type = 'test'

register_metric :g_metric, :gauge, 'test gauge metric'
register_metric :gwt_metric, :gauge_with_time, 'test gauge with time metric'
register_metric :c_metric, :counter, 'test counter metric'
end

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
Expand Down