Skip to content

Optimise handling of integers and floats for counters, gauges, summaries#176

Merged
NelsonVides merged 1 commit intomasterfrom
integer_float_handling
Mar 17, 2025
Merged

Optimise handling of integers and floats for counters, gauges, summaries#176
NelsonVides merged 1 commit intomasterfrom
integer_float_handling

Conversation

@NelsonVides
Copy link
Copy Markdown
Member

@NelsonVides NelsonVides commented Mar 5, 2025

The point is that these tables keep separate positions in the tuple for
integers and for floats, as on the integer position we can use the
optimised ets:update_counter/3,4, and probabilistically integers are
the most common input.

The issue is that under some circumstances, integer values would be
appended to the float position, which, while correct, misses the
opportunity for the optimisation.

Note too that gauges could take the value undefined to effectively
block changes until explicitly set. This was implemented, but not
tested, and in some edge-cases (the integer/float optimisation) it would
be buggy.

Split from #173

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 5, 2025

Codecov Report

Attention: Patch coverage is 94.20290% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/metrics/prometheus_counter.erl 66.66% 2 Missing ⚠️
src/metrics/prometheus_gauge.erl 92.85% 2 Missing ⚠️
Files with missing lines Coverage Δ
src/metrics/prometheus_summary.erl 96.15% <100.00%> (+0.15%) ⬆️
test/eunit/metric/prometheus_gauge_tests.erl 97.76% <100.00%> (+0.22%) ⬆️
src/metrics/prometheus_counter.erl 92.30% <66.66%> (-2.02%) ⬇️
src/metrics/prometheus_gauge.erl 96.55% <92.85%> (+2.61%) ⬆️
🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The point is that these tables keep separate positions in the tuple for
integers and for floats, as on the integer position we can use the
optimised `ets:update_counter/3,4`, and probabilistically integers are
the most common input.

The issue is that under some circumstances, integer values would be
appended to the float position, which, while correct, misses the
opportunity for the optimisation.

Note too that gauges could take the value `undefined` to effectively
block changes until explicitly set. This was implemented, but not
tested, and in some edge-cases (the integer/float optimisation) it would
be buggy.
@NelsonVides NelsonVides force-pushed the integer_float_handling branch from df43cb9 to c739c85 Compare March 11, 2025 09:20

insert_metric(Registry, Name, LabelValues, Value, ConflictCB) ->
prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LabelValues),
insert_metric_int(Registry, Name, LabelValues, Value, ConflictCB) ->
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that great in compiling beam code in my brain but wouldn't it help to hint the compiler with a is_integer(Value) guard here or is it sufficient enough to have a separate function for it? 🤔

(Same all the other specific functions)

Copy link
Copy Markdown
Member Author

@NelsonVides NelsonVides Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this function is only called from another function that already did the is_integer(Value) guard, so I'm pretty sure the compiler already generated optimised code for it. Haven't verify it though 🤔

@NelsonVides
Copy link
Copy Markdown
Member Author

@prometheus-erl/prometheus-erl any reviewer here? Just planning a new release after this minor optimisation is merged :)

@onno-vos-dev
Copy link
Copy Markdown
Member

onno-vos-dev commented Mar 17, 2025

@prometheus-erl/prometheus-erl any reviewer here? Just planning a new release after this minor optimisation is merged :)

Sorry my bad! Never pressed approve after your last reply!

@NelsonVides NelsonVides merged commit 09b95f8 into master Mar 17, 2025
@NelsonVides NelsonVides deleted the integer_float_handling branch March 17, 2025 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants