Skip to content

Allow using specific Prometheus registerers#62

Draft
a-random-lemurian wants to merge 1 commit into
zsais:mainfrom
a-random-lemurian:use-own-registerer
Draft

Allow using specific Prometheus registerers#62
a-random-lemurian wants to merge 1 commit into
zsais:mainfrom
a-random-lemurian:use-own-registerer

Conversation

@a-random-lemurian

Copy link
Copy Markdown

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 Prometheus middleware now accepts an optional prometheus.Registerer in 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 to prometheus.DefaultRegisterer.
  • Dynamic Metric Registration: The internal metric registration logic within the middleware has been updated to use the configured registerer field, 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

  1. 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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread middleware_test.go
Comment on lines +24 to +31
func TestPrometheusMiddlewareCustomRegistry(t *testing.T) {
reg := prometheus.NewRegistry()
r := gin.New()
p := NewWithConfig(Config{Registry: reg})
p.Use(r)

runRestOfMiddlewareTest(t, r)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

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:

  1. Change prometheusHandler to 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)
        }
    }
  2. Update SetMetricsPath and SetMetricsPathWithAuth to call p.prometheusHandler() instead of prometheusHandler().

With these changes in middleware.go, this test should pass.

Comment thread middleware.go
Comment on lines +205 to +208
p.registerer = prometheus.DefaultRegisterer
if cfg.Registry != nil {
p.registerer = cfg.Registry
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Suggested change
p.registerer = prometheus.DefaultRegisterer
if cfg.Registry != nil {
p.registerer = cfg.Registry
}
p.registerer = cfg.Registry
if p.registerer == nil {
p.registerer = prometheus.DefaultRegisterer
}

@a-random-lemurian
a-random-lemurian marked this pull request as draft July 9, 2025 01:12
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.

1 participant