diff --git a/pkg/app/api/service/pipedservice/service.proto b/pkg/app/api/service/pipedservice/service.proto index 3c4e5b9628..ef65b5fec9 100644 --- a/pkg/app/api/service/pipedservice/service.proto +++ b/pkg/app/api/service/pipedservice/service.proto @@ -165,7 +165,7 @@ message PingResponse { message ReportStatRequest { // Metrics byte sequence in OpenMetrics format. - bytes piped_stats = 1 [(validate.rules).bytes.min_len = 1]; + bytes piped_stats = 1; } message ReportStatResponse { diff --git a/pkg/app/piped/cmd/piped/piped.go b/pkg/app/piped/cmd/piped/piped.go index 7536d9d3ba..01ad207782 100644 --- a/pkg/app/piped/cmd/piped/piped.go +++ b/pkg/app/piped/cmd/piped/piped.go @@ -134,7 +134,7 @@ func (p *piped) run(ctx context.Context, t cli.Telemetry) (runErr error) { } // Register all metrics. - registerMetrics(cfg.PipedID) + registry := registerMetrics(cfg.PipedID) // Initialize notifier and add piped events. notifier, err := notifier.NewNotifier(cfg, t.Logger) @@ -202,7 +202,7 @@ func (p *piped) run(ctx context.Context, t cli.Telemetry) (runErr error) { admin.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok")) }) - admin.Handle("/metrics", t.PrometheusMetricsHandler()) + admin.Handle("/metrics", t.PrometheusMetricsHandlerFor(registry)) group.Go(func() error { return admin.Run(ctx) @@ -616,14 +616,20 @@ func (p *piped) insertLoginUserToPasswd(ctx context.Context) error { return nil } -func registerMetrics(pipedID string) { - r := prometheus.DefaultRegisterer - // TODO: Add piped version as label. +func registerMetrics(pipedID string) *prometheus.Registry { + r := prometheus.NewRegistry() wrapped := prometheus.WrapRegistererWith( - prometheus.Labels{"piped": pipedID}, + prometheus.Labels{ + "piped": pipedID, + "piped_version": version.Get().Version, + }, r, ) + wrapped.Register(prometheus.NewGoCollector()) + wrapped.Register(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) k8scloudprovidermetrics.Register(wrapped) k8slivestatestoremetrics.Register(wrapped) + + return r } diff --git a/pkg/cli/BUILD.bazel b/pkg/cli/BUILD.bazel index 6f801034e5..49240dac31 100644 --- a/pkg/cli/BUILD.bazel +++ b/pkg/cli/BUILD.bazel @@ -11,6 +11,7 @@ go_library( deps = [ "//pkg/log:go_default_library", "//pkg/version:go_default_library", + "@com_github_prometheus_client_golang//prometheus:go_default_library", "@com_github_prometheus_client_golang//prometheus/promhttp:go_default_library", "@com_github_spf13_cobra//:go_default_library", "@com_github_spf13_pflag//:go_default_library", diff --git a/pkg/cli/cmd.go b/pkg/cli/cmd.go index 8a99137ea3..50345e3cdb 100644 --- a/pkg/cli/cmd.go +++ b/pkg/cli/cmd.go @@ -23,6 +23,7 @@ import ( "syscall" "cloud.google.com/go/profiler" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spf13/cobra" "go.uber.org/zap" @@ -126,6 +127,16 @@ func (t Telemetry) PrometheusMetricsHandler() http.Handler { return empty } +func (t Telemetry) PrometheusMetricsHandlerFor(r *prometheus.Registry) http.Handler { + if t.Flags.Metrics { + return promhttp.HandlerFor(r, promhttp.HandlerOpts{}) + } + var empty http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("")) + } + return empty +} + func extractServiceName(cmd *cobra.Command) string { return strings.Replace(cmd.CommandPath(), " ", ".", -1) }