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

feat: add support for custom labels in Prometheus metrics #979

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

appleboy
Copy link
Contributor

@appleboy appleboy commented Jan 26, 2025

  • Add toPtr function to convert a bool to a bool pointer
  • Add contains function to check if a key exists in a slice
  • Add GetMetricLabels method to Config struct to return unique metric labels from enabled endpoints
  • Change file permission notation from 0644 to 0o644 in config_test.go
  • Add Labels field to Endpoint struct for key-value pairs
  • Initialize Prometheus metrics with dynamic labels from configuration
  • Modify PublishMetricsForEndpoint to include dynamic labels
  • Add test for GetMetricLabels method in config_test.go
  • Update watchdog to pass labels to monitoring and execution functions

Summary

The changes for config.yaml

endpoints:
  - name: front-end
    group: core
    url: "https://twin.sh/health"
    interval: 5m
    conditions:
      - "[STATUS] == 200"
      - "[BODY].status == UP"
      - "[RESPONSE_TIME] < 150"
+   labels:
+     environment: staging

This pull request introduces several enhancements to the Gatus project, including the addition of a Labels field to the Config and Endpoint structs to support key-value pairs for metrics. It also updates the Prometheus metrics initialization to include dynamic labels from the configuration, modifies the PublishMetricsForEndpoint function to handle and include these labels, and adds corresponding tests and updates to the codebase to ensure the functionality works as intended.

New metrics preview

image

Checklist

  • Tested and/or added tests to validate that the changes work as intended, if applicable.
  • Updated documentation in README.md, if applicable.

@TwiN
Copy link
Owner

TwiN commented Jan 29, 2025

Hey @appleboy, thanks again for your contributions to Gatus!

What use does the global labels have vs, for instance, just pushing the labels defined in the endpoint config?

In other words, if the environment label is defined in the endpoint, well, its only use is for metrics, so if the user defined it in the endpoint config, it's because they explicitly want it and therefore it should be published with the metrics, which would then make the global/top-level labels configuration unnecessary, no?

Let me know if the comment above doesn't make sense, I'm running on very little sleep today 😅

@appleboy
Copy link
Contributor Author

@TwiN

Thank you for your response. I was also thinking about the issue you mentioned yesterday. I will further modify it so that if the endpoint is configured, it will be automatically added to the metrics.

- Add `toBoolPtr` function to convert a bool to a bool pointer
- Add `contains` function to check if a key exists in a slice
- Add `GetMetricLabels` method to `Config` struct to return unique metric labels from enabled endpoints
- Change file permission notation from `0644` to `0o644` in `config_test.go`
- Add `Labels` field to `Endpoint` struct for key-value pairs
- Initialize Prometheus metrics with dynamic labels from configuration
- Modify `PublishMetricsForEndpoint` to include dynamic labels
- Add test for `GetMetricLabels` method in `config_test.go`
- Update `watchdog` to pass labels to monitoring and execution functions

Signed-off-by: appleboy <[email protected]>
@appleboy
Copy link
Contributor Author

@TwiN Updated. Please review it again. I also add config file unit testing.

- Rename `toBoolPtr` function to a generic `toPtr` function
- Update tests to use the new `toPtr` function instead of `toBoolPtr`

Signed-off-by: appleboy <[email protected]>
config/config.go Outdated Show resolved Hide resolved
- Move `toPtr` and `contains` utility functions to a new file `util.go`

Signed-off-by: appleboy <[email protected]>
@appleboy
Copy link
Contributor Author

appleboy commented Feb 2, 2025

@TwiN Any feedbacks?

watchdog/watchdog.go Outdated Show resolved Hide resolved
watchdog/watchdog.go Outdated Show resolved Hide resolved
watchdog/watchdog.go Show resolved Hide resolved
- Reorder parameters in `PublishMetricsForEndpoint` function
- Update test cases to match the new parameter order in `PublishMetricsForEndpoint`
- Reorder parameters in `monitor` function
- Adjust `monitor` function calls to match the new parameter order
- Reorder parameters in `execute` function call to `PublishMetricsForEndpoint`

Signed-off-by: Bo-Yi Wu <[email protected]>
@appleboy
Copy link
Contributor Author

appleboy commented Feb 6, 2025

@TwiN Done. See the changes 85c5e73

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