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

Add HandlerWithAllowlist which can control exposed metrics #1327

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions prometheus/promhttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
Expand All @@ -45,6 +46,8 @@
"github.com/prometheus/common/expfmt"

"github.com/prometheus/client_golang/prometheus"

dto "github.com/prometheus/client_model/go"
)

const (
Expand Down Expand Up @@ -90,6 +93,13 @@
return HandlerForTransactional(prometheus.ToTransactionalGatherer(reg), opts)
}

// HandlerWithAllowlist remove metrics whose name can't match with metricNameMatcher
func HandlerWithAllowlist(metricNameMatcher *regexp.Regexp) http.Handler {
Copy link
Member

Choose a reason for hiding this comment

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

This is nice, but we might want to discuss exact API. e.g. do we want only metric names or also labels? What would be flexible enough?

We think it might be useful to tackle that with OpenMetrics changes and have same API for per scrape or per handler options 🤔

return InstrumentMetricHandler(
prometheus.DefaultRegisterer, HandlerFor(&FilteredGatherer{gather: prometheus.DefaultGatherer, metricNameMatcher: metricNameMatcher}, HandlerOpts{}),
)
}

// HandlerForTransactional is like HandlerFor, but it uses transactional gather, which
// can safely change in-place returned *dto.MetricFamily before call to `Gather` and after
// call to `done` of that `Gather`.
Expand Down Expand Up @@ -406,3 +416,24 @@
http.StatusInternalServerError,
)
}

type FilteredGatherer struct {
gather prometheus.Gatherer
metricNameMatcher *regexp.Regexp
}

func (f *FilteredGatherer) Gather() ([]*dto.MetricFamily, error) {
results := []*dto.MetricFamily{}
metrics, err := f.gather.Gather()
if err != nil {
return nil, err
}
for _, m := range metrics {

Check failure on line 432 in prometheus/promhttp/http.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
if f.metricNameMatcher.MatchString(*m.Name) {
results = append(results, m)
}

Check failure on line 436 in prometheus/promhttp/http.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
}
return results, nil
}
Loading