Allow using specific Prometheus registerers#62
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @a-random-lemurian, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request enhances the Prometheus middleware by introducing the capability to specify a custom Prometheus Registerer. This change provides greater flexibility for users who need to manage metrics with isolated registries, moving away from the default global Registerer for all metric registrations. It allows for more advanced and controlled Prometheus setups within applications.
Highlights
- Configurable Prometheus Registerer: The
Prometheusmiddleware now accepts an optionalprometheus.Registererin its configuration (Config.Registry), allowing users to provide their own registry instance instead of relying solely on the default global one. If no custom registry is provided, it defaults toprometheus.DefaultRegisterer. - Dynamic Metric Registration: The internal metric registration logic within the middleware has been updated to use the configured
registererfield, ensuring that all metrics are registered with the specified Prometheus registry. - Improved Test Coverage: A new test case (
TestPrometheusMiddlewareCustomRegistry) has been added to specifically validate the functionality of using a custom Prometheus registry, ensuring the new feature works as expected.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request adds support for custom Prometheus registerers, which is a great enhancement. The implementation correctly registers metrics with the provided registerer. However, I've identified a critical issue where the /metrics endpoint doesn't serve metrics from this custom registerer, making the feature incomplete. I've provided a detailed fix for this in my review. Additionally, there's a minor suggestion to improve code style for better readability.
| func TestPrometheusMiddlewareCustomRegistry(t *testing.T) { | ||
| reg := prometheus.NewRegistry() | ||
| r := gin.New() | ||
| p := NewWithConfig(Config{Registry: reg}) | ||
| p.Use(r) | ||
|
|
||
| runRestOfMiddlewareTest(t, r) | ||
| } |
There was a problem hiding this comment.
This new test for a custom registry will fail because the feature is incomplete. The /metrics endpoint is not configured to use the custom registry to serve metrics.
The metrics are correctly registered with the custom reg, but the prometheusHandler() function in middleware.go is hardcoded to use promhttp.Handler(), which serves metrics from the global prometheus.DefaultGatherer. As a result, the check for requests_total inside runRestOfMiddlewareTest will fail for this test case.
To fix this, prometheusHandler should be a method on *Prometheus that uses the configured registerer to serve metrics. The registerer will need to be type-asserted to a prometheus.Gatherer.
Here's a suggested implementation for middleware.go:
-
Change
prometheusHandlerto be a method on*Prometheus:func (p *Prometheus) prometheusHandler() gin.HandlerFunc { gatherer, ok := p.registerer.(prometheus.Gatherer) if !ok { log.Warn("provided prometheus.Registerer does not implement prometheus.Gatherer, using default gatherer for /metrics endpoint") h := promhttp.Handler() return func(c *gin.Context) { h.ServeHTTP(c.Writer, c.Request) } } h := promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{}) return func(c *gin.Context) { h.ServeHTTP(c.Writer, c.Request) } }
-
Update
SetMetricsPathandSetMetricsPathWithAuthto callp.prometheusHandler()instead ofprometheusHandler().
With these changes in middleware.go, this test should pass.
| p.registerer = prometheus.DefaultRegisterer | ||
| if cfg.Registry != nil { | ||
| p.registerer = cfg.Registry | ||
| } |
There was a problem hiding this comment.
This logic for setting the default registerer can be made more concise and idiomatic by first assigning the configured registry and then checking for nil to apply the default.
| p.registerer = prometheus.DefaultRegisterer | |
| if cfg.Registry != nil { | |
| p.registerer = cfg.Registry | |
| } | |
| p.registerer = cfg.Registry | |
| if p.registerer == nil { | |
| p.registerer = prometheus.DefaultRegisterer | |
| } |
No description provided.