From d901fa88ae47428576c36bf8ebc99d9978042f9e Mon Sep 17 00:00:00 2001 From: Andrew Newdigate Date: Mon, 10 Dec 2018 10:41:20 +0000 Subject: [PATCH] Log correlation_id field in structured logging output --- changelogs/unreleased/add-correlation_id.yml | 5 +++++ internal/middleware/metadatahandler/metadatahandler.go | 9 +++++++++ internal/server/server.go | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/add-correlation_id.yml diff --git a/changelogs/unreleased/add-correlation_id.yml b/changelogs/unreleased/add-correlation_id.yml new file mode 100644 index 00000000000..f88776ea9d3 --- /dev/null +++ b/changelogs/unreleased/add-correlation_id.yml @@ -0,0 +1,5 @@ +--- +title: Log correlation_id field in structured logging output +merge_request: 995 +author: +type: other diff --git a/internal/middleware/metadatahandler/metadatahandler.go b/internal/middleware/metadatahandler/metadatahandler.go index 06b67b94582..5091e6654b9 100644 --- a/internal/middleware/metadatahandler/metadatahandler.go +++ b/internal/middleware/metadatahandler/metadatahandler.go @@ -5,6 +5,7 @@ import ( "github.com/prometheus/client_golang/prometheus" gitalyauth "gitlab.com/gitlab-org/gitaly/auth" "gitlab.com/gitlab-org/gitaly/internal/helper" + "gitlab.com/gitlab-org/labkit/correlation" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/metadata" @@ -41,6 +42,8 @@ const ClientNameKey = "grpc.meta.client_name" // AuthVersionKey is the key used in ctx_tags to store the auth version const AuthVersionKey = "grpc.meta.auth_version" +const correlationIDKey = "correlation_id" + // Unknown client and feature. Matches the prometheus grpc unknown value const unknownValue = "unknown" @@ -88,6 +91,12 @@ func addMetadataTags(ctx context.Context) metadataTags { tags.Set(AuthVersionKey, authInfo.Version) } + // This is a stop-gap approach to logging correlation_ids + correlationID := correlation.ExtractFromContext(ctx) + if correlationID != "" { + tags.Set(correlationIDKey, correlationID) + } + return metaTags } diff --git a/internal/server/server.go b/internal/server/server.go index a29405c0d73..cf5630badc4 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -74,6 +74,7 @@ func createNewServer(rubyServer *rubyserver.Server, secure bool) *grpc.Server { opts := []grpc.ServerOption{ grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( grpc_ctxtags.StreamServerInterceptor(ctxTagOpts...), + grpccorrelation.StreamServerCorrelationInterceptor(), // Must be above the metadata handler metadatahandler.StreamInterceptor, grpc_prometheus.StreamServerInterceptor, grpc_logrus.StreamServerInterceptor(logrusEntry), @@ -81,13 +82,13 @@ func createNewServer(rubyServer *rubyserver.Server, secure bool) *grpc.Server { cancelhandler.Stream, // Should be below LogHandler lh.StreamInterceptor(), auth.StreamServerInterceptor(), - grpccorrelation.StreamServerCorrelationInterceptor(), // Panic handler should remain last so that application panics will be // converted to errors and logged panichandler.StreamPanicHandler, )), grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( grpc_ctxtags.UnaryServerInterceptor(ctxTagOpts...), + grpccorrelation.UnaryServerCorrelationInterceptor(), // Must be above the metadata handler metadatahandler.UnaryInterceptor, grpc_prometheus.UnaryServerInterceptor, grpc_logrus.UnaryServerInterceptor(logrusEntry), @@ -95,7 +96,6 @@ func createNewServer(rubyServer *rubyserver.Server, secure bool) *grpc.Server { cancelhandler.Unary, // Should be below LogHandler lh.UnaryInterceptor(), auth.UnaryServerInterceptor(), - grpccorrelation.UnaryServerCorrelationInterceptor(), // Panic handler should remain last so that application panics will be // converted to errors and logged panichandler.UnaryPanicHandler, -- GitLab