@@ -24,6 +24,7 @@ import (
2424"encoding/json"
2525"fmt"
2626"io"
27+ "strings"
2728"sync"
2829"testing"
2930
@@ -99,13 +100,14 @@ func setupObservabilitySystemWithConfig(cfg *config) (func(), error) {
99100ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
100101defer cancel ()
101102err = Start (ctx )
102- if err != nil {
103- return nil , fmt .Errorf ("error in Start: %v" , err )
104- }
105- return func () {
103+ cleanup := func () {
106104End ()
107105envconfig .ObservabilityConfig = oldObservabilityConfig
108- }, nil
106+ }
107+ if err != nil {
108+ return cleanup , fmt .Errorf ("error in Start: %v" , err )
109+ }
110+ return cleanup , nil
109111}
110112
111113// TestClientRPCEventsLogAll tests the observability system configured with a
@@ -777,18 +779,18 @@ func (s) TestPrecedenceOrderingInConfiguration(t *testing.T) {
777779CloudLogging : & cloudLogging {
778780ClientRPCEvents : []clientRPCEvents {
779781{
780- Methods : []string {"/ grpc.testing.TestService/UnaryCall" },
782+ Methods : []string {"grpc.testing.TestService/UnaryCall" },
781783MaxMetadataBytes : 30 ,
782784MaxMessageBytes : 30 ,
783785},
784786{
785- Methods : []string {"/ grpc.testing.TestService/EmptyCall" },
787+ Methods : []string {"grpc.testing.TestService/EmptyCall" },
786788Exclude : true ,
787789MaxMetadataBytes : 30 ,
788790MaxMessageBytes : 30 ,
789791},
790792{
791- Methods : []string {"/ grpc.testing.TestService/*" },
793+ Methods : []string {"grpc.testing.TestService/*" },
792794MaxMetadataBytes : 30 ,
793795MaxMessageBytes : 30 ,
794796},
@@ -1273,3 +1275,111 @@ func (s) TestMetadataTruncationAccountsKey(t *testing.T) {
12731275}
12741276fle .mu .Unlock ()
12751277}
1278+
1279+ // TestMethodInConfiguration tests different method names with an expectation on
1280+ // whether they should error or not.
1281+ func (s ) TestMethodInConfiguration (t * testing.T ) {
1282+ // To skip creating a stackdriver exporter.
1283+ fle := & fakeLoggingExporter {
1284+ t : t ,
1285+ }
1286+
1287+ defer func (ne func (ctx context.Context , config * config ) (loggingExporter , error )) {
1288+ newLoggingExporter = ne
1289+ }(newLoggingExporter )
1290+
1291+ newLoggingExporter = func (ctx context.Context , config * config ) (loggingExporter , error ) {
1292+ return fle , nil
1293+ }
1294+
1295+ tests := []struct {
1296+ name string
1297+ config * config
1298+ wantErr string
1299+ }{
1300+ {
1301+ name : "leading-slash" ,
1302+ config : & config {
1303+ ProjectID : "fake" ,
1304+ CloudLogging : & cloudLogging {
1305+ ClientRPCEvents : []clientRPCEvents {
1306+ {
1307+ Methods : []string {"/service/method" },
1308+ },
1309+ },
1310+ },
1311+ },
1312+ wantErr : "cannot have a leading slash" ,
1313+ },
1314+ {
1315+ name : "wildcard service/method" ,
1316+ config : & config {
1317+ ProjectID : "fake" ,
1318+ CloudLogging : & cloudLogging {
1319+ ClientRPCEvents : []clientRPCEvents {
1320+ {
1321+ Methods : []string {"*/method" },
1322+ },
1323+ },
1324+ },
1325+ },
1326+ wantErr : "cannot have service wildcard *" ,
1327+ },
1328+ {
1329+ name : "/ in service name" ,
1330+ config : & config {
1331+ ProjectID : "fake" ,
1332+ CloudLogging : & cloudLogging {
1333+ ClientRPCEvents : []clientRPCEvents {
1334+ {
1335+ Methods : []string {"ser/vice/method" },
1336+ },
1337+ },
1338+ },
1339+ },
1340+ wantErr : "only one /" ,
1341+ },
1342+ {
1343+ name : "empty method name" ,
1344+ config : & config {
1345+ ProjectID : "fake" ,
1346+ CloudLogging : & cloudLogging {
1347+ ClientRPCEvents : []clientRPCEvents {
1348+ {
1349+ Methods : []string {"service/" },
1350+ },
1351+ },
1352+ },
1353+ },
1354+ wantErr : "method name must be non empty" ,
1355+ },
1356+ {
1357+ name : "normal" ,
1358+ config : & config {
1359+ ProjectID : "fake" ,
1360+ CloudLogging : & cloudLogging {
1361+ ClientRPCEvents : []clientRPCEvents {
1362+ {
1363+ Methods : []string {"service/method" },
1364+ },
1365+ },
1366+ },
1367+ },
1368+ wantErr : "" ,
1369+ },
1370+ }
1371+ for _ , test := range tests {
1372+ t .Run (test .name , func (t * testing.T ) {
1373+ cleanup , gotErr := setupObservabilitySystemWithConfig (test .config )
1374+ if cleanup != nil {
1375+ defer cleanup ()
1376+ }
1377+ if gotErr != nil && ! strings .Contains (gotErr .Error (), test .wantErr ) {
1378+ t .Fatalf ("Start(%v) = %v, wantErr %v" , test .config , gotErr , test .wantErr )
1379+ }
1380+ if (gotErr != nil ) != (test .wantErr != "" ) {
1381+ t .Fatalf ("Start(%v) = %v, wantErr %v" , test .config , gotErr , test .wantErr )
1382+ }
1383+ })
1384+ }
1385+ }
0 commit comments