Skip to content

Commit

Permalink
Exposed runtime metrics (#10)
Browse files Browse the repository at this point in the history
- Key changes:
  - Exposed runtime metrics.
  • Loading branch information
weisdd authored Mar 21, 2022
1 parent 995ca01 commit 7600a12
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.8.0

- Key changes:
- Exposed runtime metrics.

## 0.7.0

- Key changes:
Expand Down
3 changes: 2 additions & 1 deletion cmd/lfgw/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (a *ACL) PrepareLF(ns string) (metricsql.LabelFilter, error) {
return lf, nil
}

// loadACL loads ACL from a file
func (app *application) loadACL() (ACLMap, error) {
aclMap := make(ACLMap)

Expand Down Expand Up @@ -176,7 +177,7 @@ func (app *application) getLF(roles []string) (metricsql.LabelFilter, error) {
return app.ACLMap[role].LabelFilter, nil
}

// If a user has a fullaccess role, there's no need to check any other one
// If a user has a fullaccess role, there's no need to check any other one.
for _, role := range roles {
if app.ACLMap[role].Fullaccess {
return app.ACLMap[role].LabelFilter, nil
Expand Down
8 changes: 5 additions & 3 deletions cmd/lfgw/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (app *application) getRawAccessToken(r *http.Request) (string, error) {
return "", fmt.Errorf("no bearer token found")
}

// lshortfile implements Lshortfile equivalent for zerolog's CallerMarshalFunc
// lshortfile implements Lshortfile equivalent for zerolog's CallerMarshalFunc.
func (app *application) lshortfile(file string, line int) string {
// Copied from the standard library: https://cs.opensource.google/go/go/+/refs/tags/go1.17.8:src/log/log.go;drc=926994fd7cf65b2703552686965fb05569699897;l=134
short := file
Expand All @@ -61,7 +61,7 @@ func (app *application) lshortfile(file string, line int) string {
return file + ":" + strconv.Itoa(line)
}

// enrichLogContext adds a custom field and a value to zerolog context
// enrichLogContext adds a custom field and a value to zerolog context.
func (app *application) enrichLogContext(r *http.Request, field string, value string) {
if field != "" && value != "" {
log := zerolog.Ctx(r.Context())
Expand All @@ -71,7 +71,7 @@ func (app *application) enrichLogContext(r *http.Request, field string, value st
}
}

// enrichDebugLogContext adds a custom field and a value to zerolog context if logging level is set to Debug
// enrichDebugLogContext adds a custom field and a value to zerolog context if logging level is set to Debug.
func (app *application) enrichDebugLogContext(r *http.Request, field string, value string) {
if app.Debug {
if field != "" && value != "" {
Expand All @@ -83,10 +83,12 @@ func (app *application) enrichDebugLogContext(r *http.Request, field string, val
}
}

// isNotAPIRequest returns true if the requested path does not target API or federate endpoints.
func (app *application) isNotAPIRequest(path string) bool {
return !strings.Contains(path, "/api/") && !strings.Contains(path, "/federate")
}

// isUnsafePath returns true if the requested path targets a potentially dangerous endpoint (admin or remote write).
func (app *application) isUnsafePath(path string) bool {
// TODO: move to regexp?
return strings.Contains(path, "/admin/tsdb") || strings.Contains(path, "/api/v1/write")
Expand Down
2 changes: 1 addition & 1 deletion cmd/lfgw/lf.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (app *application) optimizeMetricExpr(expr metricsql.Expr) metricsql.Expr {
return newExpr
}

// equalExpr says whether two expressions are equal
// equalExpr says whether two expressions are equal.
func (app *application) equalExpr(expr1 metricsql.Expr, expr2 metricsql.Expr) bool {
return string(expr1.AppendString(nil)) == string(expr2.AppendString(nil))
}
Expand Down
1 change: 1 addition & 0 deletions cmd/lfgw/logwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type stdErrorLogWrapper struct {

// TODO: new?

// Write implements io.Writer interface to redirect standard logger entries to zerolog. Also, it cuts caller from a log entry and passes it to zerolog's caller.
func (s stdErrorLogWrapper) Write(p []byte) (n int, err error) {
caller, errorMsg, _ := strings.Cut(string(p), " ")
caller = strings.TrimRight(caller, ":")
Expand Down
14 changes: 11 additions & 3 deletions cmd/lfgw/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@ import (
"strings"
"time"

"github.com/VictoriaMetrics/metrics"
"github.com/VictoriaMetrics/metricsql"
"github.com/rs/zerolog/hlog"
)

// healthzMiddleware is a workaround to support healthz endpoint while forwarding everything else to an upstream.
func (app *application) healthzMiddleware(next http.Handler) http.Handler {
// nonProxiedEndpointsMiddleware is a workaround to support healthz and metrics endpoints while forwarding everything else to an upstream.
func (app *application) nonProxiedEndpointsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/healthz") {
w.WriteHeader(http.StatusOK)
return
}

if strings.HasPrefix(r.URL.Path, "/metrics") {
metrics.WritePrometheus(w, true)
return
}

next.ServeHTTP(w, r)
})
}

func (app *application) logHandler(next http.Handler) http.Handler {
// logMiddleware populates zerolog context with additional fields that are used in log entries generated by other middlewares. Also, it optionally generates access logs.
func (app *application) logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next = hlog.RequestIDHandler("req_id", "Request-Id")(next)

Expand Down
4 changes: 2 additions & 2 deletions cmd/lfgw/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
// routes returns a router with all paths.
func (app *application) routes() *mux.Router {
r := mux.NewRouter()
r.Use(app.healthzMiddleware)
r.Use(app.nonProxiedEndpointsMiddleware)
r.Use(hlog.NewHandler(*app.logger))
r.Use(app.logHandler)
r.Use(app.logMiddleware)
r.Use(app.prohibitedMethodsMiddleware)
r.Use(app.proxyHeadersMiddleware)
r.Use(app.oidcModeMiddleware)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gitlab.rshbdev.ru/rshbintech/integrations/ckpr/infra/images/lfgw
go 1.18

require (
github.com/VictoriaMetrics/metrics v1.18.1
github.com/VictoriaMetrics/metricsql v0.40.0
github.com/caarlos0/env/v6 v6.9.1
github.com/coreos/go-oidc/v3 v3.1.0
Expand All @@ -12,7 +13,6 @@ require (
)

require (
github.com/VictoriaMetrics/metrics v1.18.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/rs/xid v1.3.0 // indirect
github.com/valyala/fastrand v1.1.0 // indirect
Expand Down

0 comments on commit 7600a12

Please sign in to comment.