Skip to content

Add fake metric entry to populate prometheus #35

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

Merged
merged 3 commits into from
Mar 6, 2025

Conversation

mcollina
Copy link
Member

@mcollina mcollina commented Mar 1, 2025

Alternative implementation of platformatic/platformatic#3883.

Signed-off-by: Matteo Collina <[email protected]>
index.js Outdated
@@ -27,19 +27,45 @@ module.exports = fp(async function (fastify, opts) {
return false
}

let needEntrySummary = true
Copy link
Member

Choose a reason for hiding this comment

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

I would make this optional (disabled by default.)

index.js Outdated
const summary = new Summary({
name: 'http_request_summary_seconds',
help: 'request duration in seconds summary',
labelNames,
registers,
collect: function () {
Copy link
Member

Choose a reason for hiding this comment

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

This will save an "empty" metric every time the prom fetches metrics (every 1s in most cases). Are you sure you need to push a new metric every second and not just save it once after summary/histogram initialisation?

something like summary.observe(0)

index.js Outdated
const histogram = new Histogram({
name: 'http_request_duration_seconds',
help: 'request duration in seconds',
labelNames,
registers,
collect: function () {
Copy link
Member

Choose a reason for hiding this comment

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

index.js Outdated
if (needEntrySummary) {
const summaryTimer = this.startTimer()
summaryTimer({
method: 'GET',
Copy link
Member

Choose a reason for hiding this comment

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

Tiny problem with that solution is that it will work only if you aggregate values when you query the prom metrics. For example if I want to get metrics for all POST requests or all requests with status code 200 they will not have this default values as they stored with labels "GET/__empty_metrics/404".

Obviously we don't know all route labels, but maybe it will make sense to store such metrics for some method + status_code combinations.

Signed-off-by: Matteo Collina <[email protected]>
@mcollina
Copy link
Member Author

mcollina commented Mar 3, 2025

@ivan-tymoshenko PTAL

Copy link

@MzUgM MzUgM left a comment

Choose a reason for hiding this comment

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

Looks good to me

@MzUgM MzUgM requested a review from ivan-tymoshenko March 4, 2025 17:38
Copy link
Member

@ivan-tymoshenko ivan-tymoshenko left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit e0d4c62 into main Mar 6, 2025
6 checks passed
MzUgM added a commit that referenced this pull request Mar 21, 2025
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.

3 participants