Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 1114622

Browse files
authored
Add log request middleware for Querier (#700)
* Add log request middleware Signed-off-by: hi-rustin <rustin.liu@gmail.com> * Use connect-go middleware Signed-off-by: hi-rustin <rustin.liu@gmail.com> * Add tenantID,traceID and duration Signed-off-by: hi-rustin <rustin.liu@gmail.com> * Add level Signed-off-by: hi-rustin <rustin.liu@gmail.com> * Rename to anonymous Signed-off-by: hi-rustin <rustin.liu@gmail.com> --------- Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent c7d96b7 commit 1114622

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

pkg/api/api.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/grafana/phlare/pkg/querier"
4141
"github.com/grafana/phlare/pkg/scheduler"
4242
"github.com/grafana/phlare/pkg/scheduler/schedulerpb/schedulerpbconnect"
43+
"github.com/grafana/phlare/pkg/util"
4344
"github.com/grafana/phlare/pkg/util/gziphandler"
4445
"github.com/grafana/phlare/pkg/validation/exporter"
4546
)
@@ -56,6 +57,7 @@ type API struct {
5657
httpAuthMiddleware middleware.Interface
5758
grpcGatewayMux *grpcgw.ServeMux
5859
grpcAuthMiddleware connect.Option
60+
grpcLogMiddleware connect.Option
5961

6062
cfg Config
6163
logger log.Logger
@@ -71,6 +73,7 @@ func New(cfg Config, s *server.Server, grpcGatewayMux *grpcgw.ServeMux, logger l
7173
indexPage: NewIndexPageContent(),
7274
grpcGatewayMux: grpcGatewayMux,
7375
grpcAuthMiddleware: cfg.GrpcAuthMiddleware,
76+
grpcLogMiddleware: connect.WithInterceptors(util.NewLogInterceptor(logger)),
7477
}
7578

7679
// If no authentication middleware is present in the config, use the default authentication middleware.
@@ -217,7 +220,7 @@ func (a *API) RegisterRing(r http.Handler) {
217220
// RegisterQuerier registers the endpoints associated with the querier.
218221
func (a *API) RegisterQuerier(svc querierv1connect.QuerierServiceHandler) {
219222
handlers := querier.NewHTTPHandlers(svc)
220-
querierv1connect.RegisterQuerierServiceHandler(a.server.HTTP, svc, a.grpcAuthMiddleware)
223+
querierv1connect.RegisterQuerierServiceHandler(a.server.HTTP, svc, a.grpcAuthMiddleware, a.grpcLogMiddleware)
221224

222225
a.RegisterRoute("/pyroscope/render", http.HandlerFunc(handlers.Render), true, true, "GET")
223226
a.RegisterRoute("/pyroscope/render-diff", http.HandlerFunc(handlers.RenderDiff), true, true, "GET")

pkg/util/interceptor.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import (
55
"time"
66

77
"github.com/bufbuild/connect-go"
8+
"github.com/go-kit/log"
9+
"github.com/go-kit/log/level"
10+
"github.com/weaveworks/common/tracing"
11+
12+
"github.com/grafana/phlare/pkg/tenant"
813
)
914

1015
type timeoutInterceptor struct {
@@ -39,3 +44,37 @@ func (s timeoutInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFu
3944
return next(ctx, shc)
4045
})
4146
}
47+
48+
// LogRequest logs the request parameters.
49+
// It logs all kinds of requests.
50+
func NewLogInterceptor(logger log.Logger) connect.UnaryInterceptorFunc {
51+
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
52+
return connect.UnaryFunc(func(
53+
ctx context.Context,
54+
req connect.AnyRequest,
55+
) (connect.AnyResponse, error) {
56+
begin := time.Now()
57+
tenantID, err := tenant.ExtractTenantIDFromContext(ctx)
58+
if err != nil {
59+
tenantID = "anonymous"
60+
}
61+
traceID, ok := tracing.ExtractTraceID(ctx)
62+
if !ok {
63+
traceID = "unknown"
64+
}
65+
defer func() {
66+
level.Info(logger).Log(
67+
"msg", "request parameters",
68+
"route", req.Spec().Procedure,
69+
"tenant", tenantID,
70+
"traceID", traceID,
71+
"parameters", req.Any(),
72+
"duration", time.Since(begin),
73+
)
74+
}()
75+
76+
return next(ctx, req)
77+
})
78+
}
79+
return connect.UnaryInterceptorFunc(interceptor)
80+
}

0 commit comments

Comments
 (0)