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.

Signed-off-by: hamistao <pedro.ribeiro@canonical.com>
  • Loading branch information
hamistao committed Sep 11, 2024
1 parent bd8abf5 commit f79b323
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lxd/metrics/api_rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"sync/atomic"

"github.com/canonical/lxd/lxd/auth"
"github.com/canonical/lxd/lxd/request"
"github.com/canonical/lxd/shared/entity"
"github.com/canonical/lxd/shared/logger"
Expand Down Expand Up @@ -101,7 +102,10 @@ func TrackStartedRequest(r *http.Request) {
// The callback function is used to mark the request as completed for the API metrics.
func UseMetricsCallback(req *http.Request, result RequestResult) {
callback, err := request.GetCtxValue[func(RequestResult)](req.Context(), request.MetricsCallbackFunc)
if err != nil && (strings.HasPrefix(req.URL.Path, "/1.0") || req.URL.Path == "/") {

// Verify the auth method in the request context to determine if the request comes from the /dev/lxd socket.
authMethod, _ := auth.GetAuthenticationMethodFromCtx(req.Context())
if err != nil && strings.HasPrefix(req.URL.Path, "/1.0") && authMethod != auth.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": req.URL.Path, "method": req.Method, "remote": req.RemoteAddr})
} else if err == nil && callback != nil {
Expand Down

0 comments on commit f79b323

Please sign in to comment.