Skip to content

Commit

Permalink
lxd/request: Fix request not counted warning
Browse files Browse the repository at this point in the history
This makes it so that we don't expect the metrics callback to be
present on devlxd endpoints and neither on the `/` endpoint.
Also this avoids using `GetAuthenticationMethodFromCtx` to avoid
creating a import loop

Signed-off-by: hamistao <pedro.ribeiro@canonical.com>
  • Loading branch information
hamistao committed Sep 10, 2024
1 parent 9b6d519 commit f8cbbef
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lxd/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ func CountStartedRequest(r *http.Request) {
// The callback function is used to mark the request as completed for the API metrics.
func MetricsCallback(request *http.Request, result metrics.RequestResult) {
callback, err := GetCtxValue[func(metrics.RequestResult)](request.Context(), MetricsCallbackFunc)
if err != nil && (strings.HasPrefix(request.URL.Path, "/1.0") || request.URL.Path == "/") {

// Verify the auth method in the request context to determine if the request comes from the /dev/lxd socket.
authMethod, _ := GetCtxValue[string](request.Context(), CtxProtocol)
if err != nil && strings.HasPrefix(request.URL.Path, "/1.0") && authMethod != api.AuthenticationMethodDevLxd {
// Log a warning if endpoint is part of the main API, and therefore should be counted fot the API metrics.
logger.Warn("Request will not be counted for the API metrics", logger.Ctx{"url": request.URL.Path, "method": request.Method, "remote": request.RemoteAddr})
} else if err == nil && callback != nil {
Expand Down

0 comments on commit f8cbbef

Please sign in to comment.