Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Add option for exhaustive gitserver logging#61270

Merged
ggilmore merged 26 commits into
mainfrom
spr/main/92fb433c
Apr 23, 2024
Merged

Add option for exhaustive gitserver logging#61270
ggilmore merged 26 commits into
mainfrom
spr/main/92fb433c

Conversation

@ggilmore

@ggilmore ggilmore commented Mar 19, 2024

Copy link
Copy Markdown
Contributor

Closes https://github.com/sourcegraph/sourcegraph/issues/62025

This PR adds exhaustive gRPC logging for gitserver when the SRC_GITSERVER_EXHAUSTIVE_LOGGING_ENABLED environment variable is set to true.

Each RPC now logs the method name, duration, status code, and request contents. (I've taken care to omit certain sensitive fields / redact where appropriate - but I'd appreciate a close review here).

Test plan

  • Unit tests for specific wrapper around gRPC client streaming
  • Ran sourcegraph locally with env SRC_GITSERVER_EXHAUSTIVE_LOGGING_ENABLED=true sg start and the following diff applied
diff --git a/sg.config.yaml b/sg.config.yaml
index f54f9b52139..11c6896edca 100644
--- a/sg.config.yaml
+++ b/sg.config.yaml
@@ -189,6 +189,7 @@ commands:
     checkBinary: .bin/gitserver
     env:
       HOSTNAME: 127.0.0.1:3178
+      SRC_LOG_LEVEL: dbug
     watch:
       - lib
       - internal

I then saw the following logs:

[    gitserver-0] DEBUG gitserver.gRPCRequestLogger internal/server_grpc_logger.go:44 Handled ResolveRevision RPC {"server": "gitserver.v1.GitserverService", "method": "ResolveRevision", "status": "OK", "traceID": "76fa5d07df4703686a9f3e23fffeae0a", "duration": "50.029ms", "request": {"repoName": "github.com/torvalds/linux", "revSpec": "HEAD", "ensureRevision": false}}
[    gitserver-0] DEBUG gitserver.gRPCRequestLogger internal/server_grpc_logger.go:44 Handled ResolveRevision RPC {"server": "gitserver.v1.GitserverService", "method": "ResolveRevision", "status": "OK", "traceID": "76fa5d07df4703686a9f3e23fffeae0a", "duration": "57.67625ms", "request": {"repoName": "github.com/sourcegraph/sourcegraph", "revSpec": "HEAD", "ensureRevision": false}}
...
[    gitserver-0] DEBUG gitserver.gRPCRequestLogger internal/server_grpc_logger.go:44 Handled DiskInfo RPC {"server": "gitserver.v1.GitserverService", "method": "DiskInfo", "status": "OK", "traceID": "6aad03690ee6c6b541526e8fb1843ba0", "duration": "8.75µs", "request": "<empty>"}
[    gitserver-1] DEBUG gitserver.gRPCRequestLogger internal/server_grpc_logger.go:44 Handled DiskInfo RPC {"server": "gitserver.v1.GitserverService", "method": "DiskInfo", "status": "OK", "traceID": "6aad03690ee6c6b541526e8fb1843ba0", "duration": "25.625µs", "request": "<empty>"}

@cla-bot cla-bot Bot added the cla-signed label Mar 19, 2024
@github-actions github-actions Bot added team/product-platform team/source Tickets under the purview of Source - the one Source to graph it all labels Mar 19, 2024
@ggilmore ggilmore marked this pull request as draft March 19, 2024 16:28

@mmanela mmanela left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I have one question and comment

Comment thread cmd/gitserver/internal/server_grpc_logger.go
Comment thread cmd/gitserver/internal/server_grpc_logger.go Outdated
@pjlast pjlast changed the title wip add logging for gitserver requests Add option for exhaustive gitserver logging Mar 25, 2024
Comment thread cmd/gitserver/internal/server_grpc_logger.go Outdated
@pjlast pjlast marked this pull request as ready for review March 25, 2024 14:39
@pjlast pjlast requested a review from eseliger March 25, 2024 14:39
Comment thread cmd/gitserver/internal/server_grpc.go Outdated
Comment thread cmd/gitserver/internal/server_grpc_logger.go Outdated
Comment thread cmd/gitserver/internal/server_grpc_logger.go Outdated
Comment thread cmd/gitserver/internal/server_grpc_logger.go Outdated
Comment thread cmd/gitserver/internal/server_grpc_logger.go
@ggilmore ggilmore force-pushed the spr/main/92fb433c branch 2 times, most recently from 9adc726 to b6788a9 Compare April 15, 2024 19:30
@ggilmore ggilmore force-pushed the spr/main/92fb433c branch from a23f5c3 to 85a61a4 Compare April 18, 2024 07:46
@ggilmore ggilmore force-pushed the spr/main/92fb433c branch from 85a61a4 to 56c4813 Compare April 18, 2024 20:07
@ggilmore ggilmore force-pushed the spr/main/92fb433c branch from 2c33079 to 8604dd8 Compare April 18, 2024 22:17
@ggilmore ggilmore force-pushed the spr/main/92fb433c branch from 89adce0 to 86d0d41 Compare April 18, 2024 22:37
@ggilmore ggilmore requested review from eseliger and pjlast April 18, 2024 22:52

@eseliger eseliger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a useful tool for emergency cases where we have no further way to debug what's happening, nice 👍

I'm not sure how I feel yet about how verbose with a limited number of methods this already is (810loc for the logger alone, yet-another-location-to-not-forget-to-update-when-adding-an-endpoint).

If we could make this a bit more transparent I think that would be nice, and largely reusable for all our gRPC services. Using an interceptor I believe we can get duration, error, status code, and traceID generically. For the request fields, we might be able to use a similar approach I used in the transparent proxy where we could use reflection to get field names and use an annotation to mark things as PII.

But that's not a blocker for getting us a debugging tool earlier, so approved.

Left a few small suggestions inline.

Comment on lines -26 to -27
# visibility = ["//cmd/gitserver:__subpackages__"],
visibility = ["//visibility:public"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh weird! thx for the fix

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't intentional. Just the result of running sg bazel configure

Comment on lines -104 to -105
# Test requires localhost database
"requires-network",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have we actually gotten rid of all the DB stuff in here? That'd be real nice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't intentional. Just the result of running sg bazel configure

Comment thread cmd/gitserver/internal/server_grpc.go Outdated
"github.com/sourcegraph/sourcegraph/lib/pointers"
)

var envGitserverExhaustiveLoggingEnabled = env.MustGetBool("SRC_GITSERVER_EXHAUSTIVE_LOGGING_ENABLED", false, "enable exhaustive request logging in gitserver")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cmd/gitserver/internal/server_grpc.go Outdated
Comment thread cmd/gitserver/internal/grpc_server_wrappers_test.go Outdated
Comment thread cmd/gitserver/internal/server_grpc_logger.go
Comment thread cmd/gitserver/internal/server_grpc_logger.go Outdated
Comment thread cmd/gitserver/internal/server_grpc_logger.go
Comment thread cmd/gitserver/internal/server_grpc_logger.go Outdated
Comment on lines +781 to +795
start := time.Now()

defer func() {
elapsed := time.Since(start)

l.doLog(
proto.GitserverService_ResolveRevision_FullMethodName,
status.Code(err),
trace.Context(ctx).TraceID,
elapsed,

resolveRevisionRequestToLogFields(request)...,
)

}()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After having read through a lot of these now, they are all very similar and it's kind of repetitive, I wonder if we could make this a little less verbose bu doing something like

func (l *loggingGRPCServer) ResolveRevision(ctx context.Context, request *proto.ResolveRevisionRequest) (response *proto.ResolveRevisionResponse, err error) {
  collector := l.logCollect(ctx, proto.GitserverService_ResolveRevision_FullMethodName, &err)
  defer collector(resolveRevisionRequestToLogFields(request))
}

The duration tracking, status code from err, and traceID from ctx seem to always be the same.

@ggilmore ggilmore force-pushed the spr/main/92fb433c branch from 8ba9ca4 to 56f5502 Compare April 22, 2024 23:09
@ggilmore ggilmore force-pushed the spr/main/92fb433c branch from 3745840 to a52ab8c Compare April 22, 2024 23:14
@ggilmore ggilmore force-pushed the spr/main/92fb433c branch from edf47f9 to f34693b Compare April 22, 2024 23:29
@ggilmore ggilmore force-pushed the spr/main/92fb433c branch from df66498 to 16c1963 Compare April 23, 2024 00:16
@ggilmore ggilmore enabled auto-merge (squash) April 23, 2024 06:40
@ggilmore ggilmore merged commit 92e4931 into main Apr 23, 2024
@ggilmore ggilmore deleted the spr/main/92fb433c branch April 23, 2024 06:41
ggilmore referenced this pull request Apr 24, 2024
)

This PR builds on https://github.com/sourcegraph/sourcegraph/pull/61270, and adds the request logging funcionality to the repository grpc server in gitserver.

## Test plan

Local testing:

Ran `env SRC_GITSERVER_EXHAUSTIVE_LOGGING_ENABLED=true SRC_LOG_SCOPE_LEVEL=gitserver.gRPCRequestLogger=debug sg start` locally, and saw the following output from the logger in my terminal (note that specifically the repository service is mentioned):

```
[    gitserver-0] DEBUG gitserver.gRPCRequestLogger internal/server_grpc_logger.go:46 Handled FetchRepository RPC {"server": "gitserver.v1.GitserverRepositoryService", "method": "FetchRepository", "status": "OK", "traceID": "67f1aa837367fa7ea72b164e70a67419", "duration": "1.068438833s", "request": {"repoName": "github.com/sgtest/megarepo"}}
```
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla-signed team/product-platform team/source Tickets under the purview of Source - the one Source to graph it all

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Finish up gitserver request logging PR

4 participants