-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V1 metrics monitoring for V2 (#1487)
V1 metrics monitoring for V2 (#1487)
- Loading branch information
1 parent
bfc490a
commit ec83c2c
Showing
23 changed files
with
2,271 additions
and
96 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package monitoring | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
const errTypeUnexpected = "UNEXPECTED" | ||
|
||
type apiError interface { | ||
Status() int | ||
} | ||
|
||
func createHandler(fn func(w http.ResponseWriter, r *http.Request) error) *apiHandler { | ||
return &apiHandler{ | ||
innerFn: fn, | ||
} | ||
} | ||
|
||
type apiHandler struct { | ||
innerFn func(w http.ResponseWriter, r *http.Request) error | ||
} | ||
|
||
// ServeHTTP sets status code based on err returned | ||
func (h *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
err := h.innerFn(w, r) | ||
if err != nil { | ||
switch e := err.(type) { // nolint:errorlint // Will need refactor. | ||
case apiError: | ||
w.WriteHeader(e.Status()) | ||
default: | ||
w.WriteHeader(http.StatusInternalServerError) | ||
|
||
} | ||
|
||
writeResponse(w, unexpectedErrorWithReason(err.Error())) | ||
} | ||
} | ||
|
||
func writeResponse(w http.ResponseWriter, c interface{}) { | ||
bytes, err := json.Marshal(c) | ||
if err != nil { | ||
// json marshal failed | ||
fmt.Fprintf(w, "Not valid json: %v", err) | ||
return | ||
} | ||
|
||
fmt.Fprint(w, string(bytes)) | ||
|
||
} | ||
|
||
type errResponse struct { | ||
// Type is a type of error | ||
Type string `json:"type"` | ||
|
||
// Reason is a detailed error message | ||
Reason string `json:"reason"` | ||
} | ||
|
||
func unexpectedErrorWithReason(reason string, args ...interface{}) errResponse { | ||
return errResponse{ | ||
Type: errTypeUnexpected, | ||
Reason: fmt.Sprintf(reason, args...), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package monitoring | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
"go.elastic.co/apm" | ||
"go.elastic.co/apm/module/apmgorilla" | ||
|
||
"github.com/elastic/elastic-agent-libs/api" | ||
"github.com/elastic/elastic-agent-libs/config" | ||
"github.com/elastic/elastic-agent-libs/monitoring" | ||
"github.com/elastic/elastic-agent/pkg/core/logger" | ||
) | ||
|
||
// New creates a new server exposing metrics and process information. | ||
func NewServer( | ||
log *logger.Logger, | ||
endpointConfig api.Config, | ||
ns func(string) *monitoring.Namespace, | ||
tracer *apm.Tracer, | ||
) (*api.Server, error) { | ||
if err := createAgentMonitoringDrop(endpointConfig.Host); err != nil { | ||
// log but ignore | ||
log.Errorf("failed to create monitoring drop: %v", err) | ||
} | ||
|
||
cfg, err := config.NewConfigFrom(endpointConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return exposeMetricsEndpoint(log, cfg, ns, tracer) | ||
} | ||
|
||
func exposeMetricsEndpoint( | ||
log *logger.Logger, | ||
config *config.C, | ||
ns func(string) *monitoring.Namespace, | ||
tracer *apm.Tracer, | ||
) (*api.Server, error) { | ||
r := mux.NewRouter() | ||
if tracer != nil { | ||
r.Use(apmgorilla.Middleware(apmgorilla.WithTracer(tracer))) | ||
} | ||
statsHandler := statsHandler(ns("stats")) | ||
r.Handle("/stats", createHandler(statsHandler)) | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle("/", r) | ||
|
||
return api.New(log, mux, config) | ||
} | ||
|
||
func createAgentMonitoringDrop(drop string) error { | ||
if drop == "" || runtime.GOOS == "windows" { | ||
return nil | ||
} | ||
|
||
path := strings.TrimPrefix(drop, "unix://") | ||
if strings.HasSuffix(path, ".sock") { | ||
path = filepath.Dir(path) | ||
} | ||
|
||
_, err := os.Stat(path) | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
return err | ||
} | ||
|
||
// create | ||
if err := os.MkdirAll(path, 0775); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return os.Chown(path, os.Geteuid(), os.Getegid()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package monitoring | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/elastic/elastic-agent-libs/monitoring" | ||
) | ||
|
||
func statsHandler(ns *monitoring.Namespace) 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") | ||
|
||
data := monitoring.CollectStructSnapshot( | ||
ns.GetRegistry(), | ||
monitoring.Full, | ||
false, | ||
) | ||
|
||
bytes, err := json.Marshal(data) | ||
var content string | ||
if err != nil { | ||
content = fmt.Sprintf("Not valid json: %v", err) | ||
} else { | ||
content = string(bytes) | ||
} | ||
fmt.Fprint(w, content) | ||
|
||
return nil | ||
} | ||
} |
Oops, something went wrong.