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

Cherry-pick #25193 to 7.x: Proxy processes/elastic-agent to stats #25197

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion x-pack/elastic-agent/pkg/core/monitoring/server/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions x-pack/elastic-agent/pkg/core/monitoring/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down