(prometheusRegistry *prometheus.Registry, logger slog.Logger)
| 2527 | } |
| 2528 | |
| 2529 | func PrometheusMetricsHandler(prometheusRegistry *prometheus.Registry, logger slog.Logger) http.Handler { |
| 2530 | return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 2531 | w.Header().Set("Content-Type", "text/plain") |
| 2532 | |
| 2533 | // Based on: https://github.com/tailscale/tailscale/blob/280255acae604796a1113861f5a84e6fa2dc6121/ipn/localapi/localapi.go#L489 |
| 2534 | clientmetric.WritePrometheusExpositionFormat(w) |
| 2535 | |
| 2536 | metricFamilies, err := prometheusRegistry.Gather() |
| 2537 | if err != nil { |
| 2538 | logger.Error(context.Background(), "prometheus handler failed to gather metric families", slog.Error(err)) |
| 2539 | return |
| 2540 | } |
| 2541 | |
| 2542 | for _, metricFamily := range metricFamilies { |
| 2543 | _, err = expfmt.MetricFamilyToText(w, metricFamily) |
| 2544 | if err != nil { |
| 2545 | logger.Error(context.Background(), "expfmt.MetricFamilyToText failed", slog.Error(err)) |
| 2546 | return |
| 2547 | } |
| 2548 | } |
| 2549 | }) |
| 2550 | } |
| 2551 | |
| 2552 | // SSHKeySeed converts an owner userName, workspaceName and agentName to an int64 hash. |
| 2553 | // This uses the FNV-1a hash algorithm which provides decent distribution and collision |
no test coverage detected