Skip to content

Commit 72812fe

Browse files
authored
gcp/observability: filter logging from cloud ops endpoints calls (#5765)
1 parent 0ae33e6 commit 72812fe

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

gcp/observability/logging.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ type binaryLogger struct {
338338
}
339339

340340
func (bl *binaryLogger) GetMethodLogger(methodName string) iblog.MethodLogger {
341+
// Prevent logging from logging, traces, and metrics API calls.
342+
if strings.HasPrefix(methodName, "/google.logging.v2.LoggingServiceV2/") || strings.HasPrefix(methodName, "/google.monitoring.v3.MetricService/") ||
343+
strings.HasPrefix(methodName, "/google.devtools.cloudtrace.v2.TraceService/") {
344+
return nil
345+
}
341346
s, _, err := grpcutil.ParseMethod(methodName)
342347
if err != nil {
343348
logger.Infof("binarylogging: failed to parse %q: %v", methodName, err)

gcp/observability/logging_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func (s) TestClientRPCEventsLogAll(t *testing.T) {
166166
if _, err := ss.Client.UnaryCall(ctx, &grpc_testing.SimpleRequest{}); err != nil {
167167
t.Fatalf("Unexpected error from UnaryCall: %v", err)
168168
}
169+
169170
grpcLogEntriesWant := []*grpcLogEntry{
170171
{
171172
Type: eventTypeClientHeader,
@@ -1056,6 +1057,65 @@ func (s) TestTranslateMetadata(t *testing.T) {
10561057
}
10571058
}
10581059

1060+
// TestCloudLoggingAPICallsFiltered tests that the observability plugin does not
1061+
// emit logs for cloud logging API calls.
1062+
func (s) TestCloudLoggingAPICallsFiltered(t *testing.T) {
1063+
fle := &fakeLoggingExporter{
1064+
t: t,
1065+
}
1066+
1067+
defer func(ne func(ctx context.Context, config *config) (loggingExporter, error)) {
1068+
newLoggingExporter = ne
1069+
}(newLoggingExporter)
1070+
1071+
newLoggingExporter = func(ctx context.Context, config *config) (loggingExporter, error) {
1072+
return fle, nil
1073+
}
1074+
configLogAll := &config{
1075+
ProjectID: "fake",
1076+
CloudLogging: &cloudLogging{
1077+
ClientRPCEvents: []clientRPCEvents{
1078+
{
1079+
Methods: []string{"*"},
1080+
MaxMetadataBytes: 30,
1081+
MaxMessageBytes: 30,
1082+
},
1083+
},
1084+
},
1085+
}
1086+
cleanup, err := setupObservabilitySystemWithConfig(configLogAll)
1087+
if err != nil {
1088+
t.Fatalf("error setting up observability %v", err)
1089+
}
1090+
defer cleanup()
1091+
1092+
ss := &stubserver.StubServer{}
1093+
if err := ss.Start(nil); err != nil {
1094+
t.Fatalf("Error starting endpoint server: %v", err)
1095+
}
1096+
defer ss.Stop()
1097+
1098+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1099+
defer cancel()
1100+
1101+
// Any of the three cloud logging API calls should not cause any logs to be
1102+
// emitted, even though the configuration specifies to log any rpc
1103+
// regardless of method.
1104+
req := &grpc_testing.SimpleRequest{}
1105+
resp := &grpc_testing.SimpleResponse{}
1106+
1107+
ss.CC.Invoke(ctx, "/google.logging.v2.LoggingServiceV2/some-method", req, resp)
1108+
ss.CC.Invoke(ctx, "/google.monitoring.v3.MetricService/some-method", req, resp)
1109+
ss.CC.Invoke(ctx, "/google.devtools.cloudtrace.v2.TraceService/some-method", req, resp)
1110+
// The exporter should have received no new log entries due to these three
1111+
// calls, as they should be filtered out.
1112+
fle.mu.Lock()
1113+
defer fle.mu.Unlock()
1114+
if len(fle.entries) != 0 {
1115+
t.Fatalf("Unexpected length of entries %v, want 0", len(fle.entries))
1116+
}
1117+
}
1118+
10591119
func (s) TestMarshalJSON(t *testing.T) {
10601120
logEntry := &grpcLogEntry{
10611121
CallID: "300-300-300",

0 commit comments

Comments
 (0)