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

Extend Prometheus Labels to include tags (requires restart for NEW labels on the monitor to be visible) #4704

Merged
merged 4 commits into from
Aug 24, 2024

Conversation

spali
Copy link
Contributor

@spali spali commented Apr 22, 2024

⚠️⚠️⚠️ Since we do not accept all types of pull requests and do not want to waste your time. Please be sure that you have read pull request rules:
https://github.com/louislam/uptime-kuma/blob/master/CONTRIBUTING.md#can-i-create-a-pull-request-for-uptime-kuma

Tick the checkbox if you understand [x]:

  • I have read and understand the pull request rules.

Description

adds dynamic prometheus labels
fixes #680
replaces #898

Type of change

Please delete any options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Checklist

  • My code follows the style guidelines of this project
  • I ran ESLint and other linters for modified files
  • I have performed a self-review of my own code and tested it
  • I have commented my code, particularly in hard-to-understand areas (including JSDoc for methods)
  • My changes generates no new warnings
  • My code needed automated testing. I have added them (this is optional task)

Screenshots (if any)

Please do not use any external image service. Instead, just paste in or drag and drop the image here, and it will be uploaded automatically.

@spali
Copy link
Contributor Author

spali commented Apr 22, 2024

I didn't had time to retest after fixing the merge conflicts...
wrote and tested the code over a year ago...
I would appreciate if someone could test a bit my PR.

Edit(CommanderStorm):
If somebody is curious how to test this, we have this docker image:

docker run --rm -it -p 3000:3000 -p 3001:3001 --pull always -e 'UPTIME_KUMA_GH_REPO=spali:dynamic_prometheus_labels' louislam/uptime-kuma:pr-test2

server/server.js Outdated Show resolved Hide resolved
@CommanderStorm CommanderStorm added area:metrics related to monitoring metrics help wanted May need your help to test or answer labels Apr 22, 2024
@spali spali force-pushed the dynamic_prometheus_labels branch from ec822e6 to 78fddc7 Compare April 30, 2024 08:58
@spali
Copy link
Contributor Author

spali commented Apr 30, 2024

did some basic tests and works for me as expected. After saving, the tags are immediately visible on the /metrics endpoint.

@CommanderStorm
Copy link
Collaborator

CommanderStorm commented Apr 30, 2024

I also just did, but I cannot reproduce this working. I am missing some part.
Could you share your testing mechanism?

Mine is:

  • docker run --rm -it -p 3000:3000 -p 3001:3001 --pull always -e 'UPTIME_KUMA_GH_REPO=spali:dynamic_prometheus_labels' louislam/uptime-kuma:pr-test2
    setup mariadb, log in
  • create a monitor
  • request /metrics => see monitor without tag
  • edit monitor and add a tag
  • request /metrics => see monitor without tag

@CommanderStorm CommanderStorm added the question Further information is requested label Apr 30, 2024
@spali
Copy link
Contributor Author

spali commented Apr 30, 2024

Did the same, but didn't use docker. In the repo and my PR branch npm install and npm run start.
I noticed you used pr-test2... is master based on V2?

@CommanderStorm
Copy link
Collaborator

CommanderStorm commented Apr 30, 2024

Yes, master and all further feature development is v2.0 only.
Given that this PR adds a feature, this is the correct branch imo.
See #4500 for the status of said release and the 1.23.0-release notes:

🐻 It should be the last minor version of Uptime Kuma v1. I will focus on the development of v2. Stay tuned!
Update: The last minor version (1.23.x) means that there won't be any new features introduced in v1. However, bug fixes will still be provided. You can expect to see 1.23.1, 1.23.2 and so on.

@spali
Copy link
Contributor Author

spali commented Apr 30, 2024

Then I can't explain the difference between our both test setup's. 🤔

@spali
Copy link
Contributor Author

spali commented May 15, 2024

Could reproduce it after fresh config and no data.

It's a feature 😉 Needs a server restart for new tag's.
See explanation in #898 (comment) which I also forgot about after this long time 😉
Question is, if solution 3.5 is fine to start with or if we need to put more work into it from start.
As it's a new feature, existing users won't miss anything. And it could be optimized for the user experience later. I think some here wait for it and don't mind a restart after introducing new tags.
Never got an answer from @louislam or any other Collaborator here.

Edit:
I suggest to stay with this solution (3.5) as it is the most less invasive one. And later optimizing is always possible if someone finds the time.

@CommanderStorm
Copy link
Collaborator

3.5 sounds fine by me..

@spali
Copy link
Contributor Author

spali commented May 15, 2024

Tested it again with the premise of restarting to see the monitor tags:

  • create ./data directory with 777 permission
  •  docker run --rm -it -p 3000:3000 -p 3001:3001 --pull always -v ./data:/app/data -e 'UPTIME_KUMA_GH_REPO=spali:dynamic_prometheus_labels' louislam/uptime-kuma:pr-test2
    
  • setup sqlite db
  • login
  • disable auth
  • create monitor with tag test
  • kill container and restart with same docker command as above
  • check /metrics for tag to be visible
  • edit monitor and remove existing tag test and re-add tag test with new value
  • check /metrics for tag with new value to be visible immediately
  • edit monitor and remove tag test
  • check /metrics for tag to be removed immediately

Looks good to me and works as intended.

@spali spali changed the title dynamic prometheus labels dynamic prometheus labels (requires restart for **new** labels on the monitor to be visible) May 15, 2024
@spali
Copy link
Contributor Author

spali commented May 15, 2024

renamed PR in case title finds it's way into the changelogs, so the behaviour is noted there:

  • dynamic prometheus labels (requires restart for new labels on the monitor to be visible)

@spali spali force-pushed the dynamic_prometheus_labels branch from 78fddc7 to 6c21c04 Compare May 17, 2024 19:22
@CommanderStorm CommanderStorm changed the title dynamic prometheus labels (requires restart for **new** labels on the monitor to be visible) Extend Prometheus Labels to include tags (requires restart for NEW labels on the monitor to be visible) May 17, 2024
@CommanderStorm CommanderStorm added the pr:needs review this PR needs a review by maintainers or other community members label May 19, 2024
@xlr-8
Copy link

xlr-8 commented Aug 19, 2024

Hey! Thanks for the MR, that's exactly what I wished to have! 🙏

Is there any chance for this MR to get reviewed? I don't know anything about JS 😓 but I think this MR could be a great addition to the project, since that would help using it better with prometheus/grafana and associated dashboards.

Looking forward to it whenever someone have time!

Copy link
Collaborator

@CommanderStorm CommanderStorm left a comment

Choose a reason for hiding this comment

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

Thanks for the bugfix/new feature! 🎉

Note

This PR is part of the v2.0 merge window => see #4500 for the bugs that need to be addressed before we can ship this release ^^

All changes in this PR are small and uncontroversial ⇒ merging with junior maintainer approval

@CommanderStorm
Copy link
Collaborator

@xlr-8
FYI: See #4704 (comment) for steps how you can review changes on a functional level without needing to get into the code

@CommanderStorm CommanderStorm merged commit 643d28c into louislam:master Aug 24, 2024
10 checks passed
@CommanderStorm CommanderStorm added this to the 2.0.0 milestone Aug 24, 2024
@spali spali deleted the dynamic_prometheus_labels branch August 27, 2024 07:01
@xlr-8
Copy link

xlr-8 commented Aug 28, 2024

@xlr-8 FYI: See #4704 (comment) for steps how you can review changes on a functional level without needing to get into the code

Thanks for the heads up, you're right.

I've tried now but can't seem to make it work based on your insight and README.

I've just:

> git clone https://github.com/louislam/uptime-kuma/ && cd uptime-kuma
> mkdir data && chmod 777 data
>  docker run --rm -it -p 3000:3000 -p 3001:3001 --pull always -v ./data:/app/data -e 'UPTIME_KUMA_GH_REPO=spali:dynamic_prometheus_labels' louislam/uptime-kuma:pr-test2

Whenever going onto the http://0.0.0.0:3001/setup-database, I hit a blank page no matter the browser / private window or not. I don't see much on the network tab either; is there something I'm missing?

Perhaps I could help by providing a docker-compose, Makefile or something like this if you judge it relevant ?

And thank you for the quick review/merge 🙏

@CommanderStorm
Copy link
Collaborator

Skip the first two lines (the docker image already does that)
The web client is available on port 3000 => change that port in the browsers location bar and everything should work.

Thanks for testing this PR. Much appreciated 👍

PS: yea, I know. We should likely redirect users in dev mode when they connect to the API server on the most common route that is only used by the webclient

@@ -336,7 +336,7 @@ class Monitor extends BeanModel {
let previousBeat = null;
let retries = 0;

this.prometheus = new Prometheus(this);
this.prometheus = await Prometheus.createAndInitMetrics(this);
Copy link
Owner

Choose a reason for hiding this comment

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

It could possibly throws an exception:

Trace: Error: Invalid label name
    at async Proxy.start (C:\Users\louis\Dropbox\My Projects\uptime-kuma\server\model\monitor.js:348:27)
    at async startMonitors (C:\Users\louis\Dropbox\My Projects\uptime-kuma\server\server.js:1801:9)
    at process.unexpectedErrorHandler (C:\Users\louis\Dropbox\My Projects\uptime-kuma\server\server.js:1855:13)
    at process.emit (node:events:520:28)
    at emitUnhandledRejection (node:internal/process/promises:250:13)
    at throwUnhandledRejectionsMode (node:internal/process/promises:385:19)
    at processPromiseRejections (node:internal/process/promises:470:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)
If you keep encountering errors, please report to https://github.com/louislam/uptime-kuma/issues

@louislam
Copy link
Owner

Actually, I have no idea how to fix #5096, if it is not fixed before 2.0.0-beta.0 release, it will be reverted and postpone to a later version.

@CommanderStorm
Copy link
Collaborator

Since Prometheus does only allow ASCII label names (thus handling of the client library is correct) we need to trim these chars out of the labels and only add a label if one is left afterwards.

Alternatively, we can just limit ourselves to labels which match the regex [a-zA-Z_][a-zA-Z0-9_]*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metrics related to monitoring metrics help wanted May need your help to test or answer pr:needs review this PR needs a review by maintainers or other community members question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend existing Prometheus labels to include tags
4 participants