MM-45536: Track GraphQL queries in Grafana (#20609)

https://mattermost.atlassian.net/browse/MM-45536

```release-note
NONE
```
This commit is contained in:
Agniva De Sarker
2022-07-08 14:45:05 +05:30
committed by GitHub
parent ad181532af
commit b0327c2953
3 changed files with 19 additions and 7 deletions

View File

@@ -101,6 +101,8 @@ func (api *API) graphQL(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
c.GraphQLOperationName = params.OperationName
// Populate the context with required info.
reqCtx := r.Context()
reqCtx = context.WithValue(reqCtx, webCtx, c)

View File

@@ -19,12 +19,15 @@ import (
)
type Context struct {
App app.AppIface
AppContext *request.Context
Logger *mlog.Logger
Params *Params
Err *model.AppError
siteURLHeader string
App app.AppIface
AppContext *request.Context
Logger *mlog.Logger
Params *Params
Err *model.AppError
// This is used to track the graphQL query that's being executed,
// so that we can monitor the timings in Grafana.
GraphQLOperationName string
siteURLHeader string
}
// LogAuditRec logs an audit record using default LevelAPI.

View File

@@ -382,7 +382,14 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != model.APIURLSuffix+"/websocket" {
elapsed := float64(time.Since(now)) / float64(time.Second)
c.App.Metrics().ObserveAPIEndpointDuration(h.HandlerName, r.Method, statusCode, elapsed)
var endpoint string
if strings.HasPrefix(r.URL.Path, model.APIURLSuffixV5) {
// It's a graphQL query, so use the operation name.
endpoint = c.GraphQLOperationName
} else {
endpoint = h.HandlerName
}
c.App.Metrics().ObserveAPIEndpointDuration(endpoint, r.Method, statusCode, elapsed)
}
}
}