diff --git a/x-pack/elastic-agent/pkg/core/monitoring/server/process.go b/x-pack/elastic-agent/pkg/core/monitoring/server/process.go index 753fe3602b59..87a6ae8493c0 100644 --- a/x-pack/elastic-agent/pkg/core/monitoring/server/process.go +++ b/x-pack/elastic-agent/pkg/core/monitoring/server/process.go @@ -18,6 +18,7 @@ import ( "github.com/gorilla/mux" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/monitoring/beats" @@ -51,7 +52,7 @@ var ( } ) -func processHandler() func(http.ResponseWriter, *http.Request) error { +func processHandler(statsHandler func(http.ResponseWriter, *http.Request) error) func(http.ResponseWriter, *http.Request) error { return func(w http.ResponseWriter, r *http.Request) error { w.Header().Set("Content-Type", "application/json; charset=utf-8") @@ -62,6 +63,11 @@ func processHandler() func(http.ResponseWriter, *http.Request) error { return errorfWithStatus(http.StatusNotFound, "productID not found") } + if id == paths.BinaryName { + // proxy stats for elastic agent process + return statsHandler(w, r) + } + metricsBytes, statusCode, metricsErr := processMetrics(r.Context(), id) if metricsErr != nil { return metricsErr diff --git a/x-pack/elastic-agent/pkg/core/monitoring/server/server.go b/x-pack/elastic-agent/pkg/core/monitoring/server/server.go index 25703d1524df..47c18d617a3d 100644 --- a/x-pack/elastic-agent/pkg/core/monitoring/server/server.go +++ b/x-pack/elastic-agent/pkg/core/monitoring/server/server.go @@ -44,11 +44,12 @@ func New( func exposeMetricsEndpoint(log *logger.Logger, config *common.Config, ns func(string) *monitoring.Namespace, routesFetchFn func() *sorted.Set, enableProcessStats bool) (*api.Server, error) { r := mux.NewRouter() - r.Handle("/stats", createHandler(statsHandler(ns("stats")))) + statsHandler := statsHandler(ns("stats")) + r.Handle("/stats", createHandler(statsHandler)) if enableProcessStats { r.HandleFunc("/processes", processesHandler(routesFetchFn)) - r.Handle("/processes/{processID}", createHandler(processHandler())) + r.Handle("/processes/{processID}", createHandler(processHandler(statsHandler))) } mux := http.NewServeMux()