Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcp/observability: filter logging from cloud ops endpoints calls #5765

Merged
merged 3 commits into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions gcp/observability/logging.go
Expand Up @@ -338,6 +338,11 @@ type binaryLogger struct {
}

func (bl *binaryLogger) GetMethodLogger(methodName string) iblog.MethodLogger {
// Prevent logging from logging, traces, and metrics API calls.
if strings.HasPrefix(methodName, "/google.logging.v2.LoggingServiceV2/") || strings.HasPrefix(methodName, "/google.monitoring.v3.MetricService/") ||
strings.HasPrefix(methodName, "/google.devtools.cloudtrace.v2.TraceService/") {
return nil
}
s, _, err := grpcutil.ParseMethod(methodName)
if err != nil {
logger.Infof("binarylogging: failed to parse %q: %v", methodName, err)
Expand Down
61 changes: 61 additions & 0 deletions gcp/observability/logging_test.go
Expand Up @@ -166,6 +166,7 @@ func (s) TestClientRPCEventsLogAll(t *testing.T) {
if _, err := ss.Client.UnaryCall(ctx, &grpc_testing.SimpleRequest{}); err != nil {
t.Fatalf("Unexpected error from UnaryCall: %v", err)
}

grpcLogEntriesWant := []*grpcLogEntry{
{
Type: eventTypeClientHeader,
Expand Down Expand Up @@ -1056,6 +1057,66 @@ func (s) TestTranslateMetadata(t *testing.T) {
}
}

// TestCloudLoggingAPICallsFiltered tests that the observability plugin does not
// emit logs for cloud logging API calls.
func (s) TestCloudLoggingAPICallsFiltered(t *testing.T) {
fle := &fakeLoggingExporter{
t: t,
}

defer func(ne func(ctx context.Context, config *config) (loggingExporter, error)) {
newLoggingExporter = ne
}(newLoggingExporter)

newLoggingExporter = func(ctx context.Context, config *config) (loggingExporter, error) {
return fle, nil
}
configLogAll := &config{
ProjectID: "fake",
CloudLogging: &cloudLogging{
ClientRPCEvents: []clientRPCEvents{
{
Methods: []string{"*"},
MaxMetadataBytes: 30,
MaxMessageBytes: 30,
},
},
},
}
cleanup, err := setupObservabilitySystemWithConfig(configLogAll)
if err != nil {
t.Fatalf("error setting up observability %v", err)
}
defer cleanup()

ss := &stubserver.StubServer{}
if err := ss.Start(nil); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

// Any of the three cloud logging API calls should not cause any logs to be
// emitted, even though the configuration specifies to log any rpc
// regardless of method.
req := &grpc_testing.SimpleRequest{}
resp := &grpc_testing.SimpleResponse{}

ss.CC.Invoke(ctx, "/google.logging.v2.LoggingServiceV2/some-method", req, resp)
ss.CC.Invoke(ctx, "/google.monitoring.v3.MetricService/some-method", req, resp)
ss.CC.Invoke(ctx, "/google.devtools.cloudtrace.v2.TraceService/some-method", req, resp)
// The exporter should have received no new log entries due to these three
// calls, as they should be filtered out.
fle.mu.Lock()
if len(fle.entries) != 0 {
fle.mu.Unlock()
Copy link
Member

Choose a reason for hiding this comment

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

Nit: use a defer instead to avoid the two unlocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

t.Fatalf("Unexpected length of entries %v, want 0", len(fle.entries))
}
fle.mu.Unlock()
}

func (s) TestMarshalJSON(t *testing.T) {
logEntry := &grpcLogEntry{
CallID: "300-300-300",
Expand Down