metrics: reduce gc pressure (#5530)

This commit is contained in:
Denis Mishin 2025-03-18 13:48:49 -04:00 committed by GitHub
parent 562101ae03
commit 5ef16bcd28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 393 additions and 23 deletions

View file

@ -15,6 +15,7 @@ import (
"github.com/pomerium/pomerium/internal/middleware"
"github.com/pomerium/pomerium/internal/telemetry"
"github.com/pomerium/pomerium/internal/telemetry/metrics"
metrics_const "github.com/pomerium/pomerium/pkg/metrics"
)
const (
@ -96,12 +97,17 @@ func (mgr *MetricsManager) updateServer(ctx context.Context, cfg *Config) {
return
}
var labels map[string]string
if cfg.Options.IsRuntimeFlagSet(RuntimeFlagAddExtraMetricsLabels) {
labels = getCommonLabels(mgr.installationID)
}
mgr.endpoints = append(cfg.MetricsScrapeEndpoints,
MetricsScrapeEndpoint{
Name: "envoy",
URL: url.URL{Scheme: "http", Host: cfg.Options.MetricsAddr, Path: "/metrics/envoy"},
})
handler, err := metrics.PrometheusHandler(toInternalEndpoints(mgr.endpoints), mgr.installationID, defaultMetricsTimeout)
handler, err := metrics.PrometheusHandler(toInternalEndpoints(mgr.endpoints), defaultMetricsTimeout, labels)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("metrics: failed to create prometheus handler")
return
@ -128,3 +134,17 @@ func toInternalEndpoints(src []MetricsScrapeEndpoint) []metrics.ScrapeEndpoint {
}
return dst
}
func getCommonLabels(installationID string) map[string]string {
hostname, err := os.Hostname()
if err != nil {
hostname = "__none__"
}
m := map[string]string{
metrics_const.HostnameLabel: hostname,
}
if installationID != "" {
m[metrics_const.InstallationIDLabel] = installationID
}
return m
}