@@ -34,8 +34,8 @@ import (
34
34
"go.opencensus.io/trace"
35
35
"google.golang.org/grpc"
36
36
"google.golang.org/grpc/credentials/insecure"
37
- configpb "google.golang.org/grpc/gcp/observability/internal/config"
38
37
grpclogrecordpb "google.golang.org/grpc/gcp/observability/internal/logging"
38
+ "google.golang.org/grpc/internal"
39
39
iblog "google.golang.org/grpc/internal/binarylog"
40
40
"google.golang.org/grpc/internal/grpctest"
41
41
"google.golang.org/grpc/internal/leakcheck"
@@ -129,7 +129,6 @@ func (fle *fakeLoggingExporter) Close() error {
129
129
type test struct {
130
130
t * testing.T
131
131
fle * fakeLoggingExporter
132
- fe * fakeOpenCensusExporter
133
132
134
133
testServer testgrpc.TestServiceServer // nil means none
135
134
// srv and srvAddr are set once startServer is called.
@@ -197,7 +196,7 @@ func (te *test) clientConn() *grpc.ClientConn {
197
196
return te .cc
198
197
}
199
198
200
- func (te * test ) enablePluginWithConfig (config * configpb. ObservabilityConfig ) {
199
+ func (te * test ) enablePluginWithConfig (config * config ) {
201
200
// Injects the fake exporter for testing purposes
202
201
te .fle = & fakeLoggingExporter {t : te .t }
203
202
defaultLogger = newBinaryLogger (nil )
@@ -208,10 +207,10 @@ func (te *test) enablePluginWithConfig(config *configpb.ObservabilityConfig) {
208
207
}
209
208
210
209
func (te * test ) enablePluginWithCaptureAll () {
211
- te .enablePluginWithConfig (& configpb. ObservabilityConfig {
210
+ te .enablePluginWithConfig (& config {
212
211
EnableCloudLogging : true ,
213
- DestinationProjectId : "fake" ,
214
- LogFilters : []* configpb. ObservabilityConfig_LogFilter {
212
+ DestinationProjectID : "fake" ,
213
+ LogFilters : []logFilter {
215
214
{
216
215
Pattern : "*" ,
217
216
HeaderBytes : infinitySizeBytes ,
@@ -223,14 +222,13 @@ func (te *test) enablePluginWithCaptureAll() {
223
222
224
223
func (te * test ) enableOpenCensus () {
225
224
defaultMetricsReportingInterval = time .Millisecond * 100
226
- config := & configpb. ObservabilityConfig {
225
+ config := & config {
227
226
EnableCloudLogging : true ,
228
227
EnableCloudTrace : true ,
229
228
EnableCloudMonitoring : true ,
230
229
GlobalTraceSamplingRate : 1.0 ,
231
230
}
232
- te .fe = & fakeOpenCensusExporter {SeenViews : make (map [string ]string ), t : te .t }
233
- startOpenCensus (config , te .fe )
231
+ startOpenCensus (config )
234
232
}
235
233
236
234
func checkEventCommon (t * testing.T , seen * grpclogrecordpb.GrpcLogRecord ) {
@@ -522,7 +520,7 @@ func (s) TestLoggingForErrorCall(t *testing.T) {
522
520
func (s ) TestEmptyConfig (t * testing.T ) {
523
521
te := newTest (t )
524
522
defer te .tearDown ()
525
- te .enablePluginWithConfig (& configpb. ObservabilityConfig {})
523
+ te .enablePluginWithConfig (& config {})
526
524
te .startServer (& testServer {})
527
525
tc := testgrpc .NewTestServiceClient (te .clientConn ())
528
526
@@ -554,10 +552,10 @@ func (s) TestOverrideConfig(t *testing.T) {
554
552
// most specific one. The third filter allows message payload logging, and
555
553
// others disabling the message payload logging. We should observe this
556
554
// behavior latter.
557
- te .enablePluginWithConfig (& configpb. ObservabilityConfig {
555
+ te .enablePluginWithConfig (& config {
558
556
EnableCloudLogging : true ,
559
- DestinationProjectId : "fake" ,
560
- LogFilters : []* configpb. ObservabilityConfig_LogFilter {
557
+ DestinationProjectID : "fake" ,
558
+ LogFilters : []logFilter {
561
559
{
562
560
Pattern : "wont/match" ,
563
561
MessageBytes : 0 ,
@@ -621,10 +619,10 @@ func (s) TestNoMatch(t *testing.T) {
621
619
// Setting 3 filters, expected to use the second filter. The second filter
622
620
// allows message payload logging, and others disabling the message payload
623
621
// logging. We should observe this behavior latter.
624
- te .enablePluginWithConfig (& configpb. ObservabilityConfig {
622
+ te .enablePluginWithConfig (& config {
625
623
EnableCloudLogging : true ,
626
- DestinationProjectId : "fake" ,
627
- LogFilters : []* configpb. ObservabilityConfig_LogFilter {
624
+ DestinationProjectID : "fake" ,
625
+ LogFilters : []logFilter {
628
626
{
629
627
Pattern : "wont/match" ,
630
628
MessageBytes : 0 ,
@@ -661,10 +659,10 @@ func (s) TestNoMatch(t *testing.T) {
661
659
}
662
660
663
661
func (s ) TestRefuseStartWithInvalidPatterns (t * testing.T ) {
664
- config := & configpb. ObservabilityConfig {
662
+ config := & config {
665
663
EnableCloudLogging : true ,
666
- DestinationProjectId : "fake" ,
667
- LogFilters : []* configpb. ObservabilityConfig_LogFilter {
664
+ DestinationProjectID : "fake" ,
665
+ LogFilters : []logFilter {
668
666
{
669
667
Pattern : ":-)" ,
670
668
},
@@ -673,7 +671,7 @@ func (s) TestRefuseStartWithInvalidPatterns(t *testing.T) {
673
671
},
674
672
},
675
673
}
676
- configJSON , err := protojson .Marshal (config )
674
+ configJSON , err := json .Marshal (config )
677
675
if err != nil {
678
676
t .Fatalf ("failed to convert config to JSON: %v" , err )
679
677
}
@@ -689,7 +687,7 @@ func (s) TestRefuseStartWithInvalidPatterns(t *testing.T) {
689
687
// place in the temporary portion of the file system dependent on system. It
690
688
// also sets the environment variable GRPC_CONFIG_OBSERVABILITY_JSON to point to
691
689
// this created config.
692
- func createTmpConfigInFileSystem (rawJSON string ) (* os. File , error ) {
690
+ func createTmpConfigInFileSystem (rawJSON string ) (func () , error ) {
693
691
configJSONFile , err := ioutil .TempFile (os .TempDir (), "configJSON-" )
694
692
if err != nil {
695
693
return nil , fmt .Errorf ("cannot create file %v: %v" , configJSONFile .Name (), err )
@@ -699,19 +697,23 @@ func createTmpConfigInFileSystem(rawJSON string) (*os.File, error) {
699
697
return nil , fmt .Errorf ("cannot write marshalled JSON: %v" , err )
700
698
}
701
699
os .Setenv (envObservabilityConfigJSON , configJSONFile .Name ())
702
- return configJSONFile , nil
700
+ return func () {
701
+ configJSONFile .Close ()
702
+ os .Setenv (envObservabilityConfigJSON , "" )
703
+ }, nil
703
704
}
704
705
705
706
// TestJSONEnvVarSet tests a valid observability configuration specified by the
706
707
// GRPC_CONFIG_OBSERVABILITY_JSON environment variable, whose value represents a
707
708
// file path pointing to a JSON encoded config.
708
709
func (s ) TestJSONEnvVarSet (t * testing.T ) {
709
710
configJSON := `{
710
- "destinationProjectId ": "fake",
711
- "logFilters ":[{"pattern":"*","headerBytes ":1073741824,"messageBytes ":1073741824}]
711
+ "destination_project_id ": "fake",
712
+ "log_filters ":[{"pattern":"*","header_bytes ":1073741824,"message_bytes ":1073741824}]
712
713
}`
713
- configJSONFile , err := createTmpConfigInFileSystem (configJSON )
714
- defer configJSONFile .Close ()
714
+ cleanup , err := createTmpConfigInFileSystem (configJSON )
715
+ defer cleanup ()
716
+
715
717
if err != nil {
716
718
t .Fatalf ("failed to create config in file system: %v" , err )
717
719
}
@@ -730,27 +732,27 @@ func (s) TestJSONEnvVarSet(t *testing.T) {
730
732
// variable is set and valid.
731
733
func (s ) TestBothConfigEnvVarsSet (t * testing.T ) {
732
734
configJSON := `{
733
- "destinationProjectId ":"fake",
734
- "logFilters ":[{"pattern":":-)"}, {"pattern ":"*"}]
735
+ "destination_project_id ":"fake",
736
+ "log_filters ":[{"pattern":":-)"}, {"pattern_string ":"*"}]
735
737
}`
736
- configJSONFile , err := createTmpConfigInFileSystem (configJSON )
737
- defer configJSONFile . Close ()
738
+ cleanup , err := createTmpConfigInFileSystem (configJSON )
739
+ defer cleanup ()
738
740
if err != nil {
739
741
t .Fatalf ("failed to create config in file system: %v" , err )
740
742
}
741
743
// This configuration should be ignored, as precedence 2.
742
- validConfig := & configpb. ObservabilityConfig {
744
+ validConfig := & config {
743
745
EnableCloudLogging : true ,
744
- DestinationProjectId : "fake" ,
745
- LogFilters : []* configpb. ObservabilityConfig_LogFilter {
746
+ DestinationProjectID : "fake" ,
747
+ LogFilters : []logFilter {
746
748
{
747
749
Pattern : "*" ,
748
750
HeaderBytes : infinitySizeBytes ,
749
751
MessageBytes : infinitySizeBytes ,
750
752
},
751
753
},
752
754
}
753
- validConfigJSON , err := protojson .Marshal (validConfig )
755
+ validConfigJSON , err := json .Marshal (validConfig )
754
756
if err != nil {
755
757
t .Fatalf ("failed to convert config to JSON: %v" , err )
756
758
}
@@ -766,6 +768,7 @@ func (s) TestBothConfigEnvVarsSet(t *testing.T) {
766
768
// a file (or valid configuration).
767
769
func (s ) TestErrInFileSystemEnvVar (t * testing.T ) {
768
770
os .Setenv (envObservabilityConfigJSON , "/this-file/does-not-exist" )
771
+ defer os .Setenv (envObservabilityConfigJSON , "" )
769
772
if err := Start (context .Background ()); err == nil {
770
773
t .Fatalf ("Invalid file system path not triggering error" )
771
774
}
@@ -783,6 +786,16 @@ func (s) TestNoEnvSet(t *testing.T) {
783
786
func (s ) TestOpenCensusIntegration (t * testing.T ) {
784
787
te := newTest (t )
785
788
defer te .tearDown ()
789
+ fe := & fakeOpenCensusExporter {SeenViews : make (map [string ]string ), t : te .t }
790
+
791
+ defer func (ne func (config * config ) (tracingMetricsExporter , error )) {
792
+ newExporter = ne
793
+ }(newExporter )
794
+
795
+ newExporter = func (config * config ) (tracingMetricsExporter , error ) {
796
+ return fe , nil
797
+ }
798
+
786
799
te .enableOpenCensus ()
787
800
te .startServer (& testServer {})
788
801
tc := testgrpc .NewTestServiceClient (te .clientConn ())
@@ -807,17 +820,17 @@ func (s) TestOpenCensusIntegration(t *testing.T) {
807
820
defer cancel ()
808
821
for ctx .Err () == nil {
809
822
errs = nil
810
- te . fe .mu .RLock ()
811
- if value := te . fe .SeenViews ["grpc.io/client/completed_rpcs" ]; value != TypeOpenCensusViewCount {
823
+ fe .mu .RLock ()
824
+ if value := fe .SeenViews ["grpc.io/client/completed_rpcs" ]; value != TypeOpenCensusViewCount {
812
825
errs = append (errs , fmt .Errorf ("unexpected type for grpc.io/client/completed_rpcs: %s != %s" , value , TypeOpenCensusViewCount ))
813
826
}
814
- if value := te . fe .SeenViews ["grpc.io/server/completed_rpcs" ]; value != TypeOpenCensusViewCount {
827
+ if value := fe .SeenViews ["grpc.io/server/completed_rpcs" ]; value != TypeOpenCensusViewCount {
815
828
errs = append (errs , fmt .Errorf ("unexpected type for grpc.io/server/completed_rpcs: %s != %s" , value , TypeOpenCensusViewCount ))
816
829
}
817
- if te . fe .SeenSpans <= 0 {
818
- errs = append (errs , fmt .Errorf ("unexpected number of seen spans: %v <= 0" , te . fe .SeenSpans ))
830
+ if fe .SeenSpans <= 0 {
831
+ errs = append (errs , fmt .Errorf ("unexpected number of seen spans: %v <= 0" , fe .SeenSpans ))
819
832
}
820
- te . fe .mu .RUnlock ()
833
+ fe .mu .RUnlock ()
821
834
if len (errs ) == 0 {
822
835
break
823
836
}
@@ -827,3 +840,52 @@ func (s) TestOpenCensusIntegration(t *testing.T) {
827
840
t .Fatalf ("Invalid OpenCensus export data: %v" , errs )
828
841
}
829
842
}
843
+
844
+ // TestCustomTagsTracingMetrics verifies that the custom tags defined in our
845
+ // observability configuration and set to two hardcoded values are passed to the
846
+ // function to create an exporter.
847
+ func (s ) TestCustomTagsTracingMetrics (t * testing.T ) {
848
+ defer func (ne func (config * config ) (tracingMetricsExporter , error )) {
849
+ newExporter = ne
850
+ }(newExporter )
851
+ fe := & fakeOpenCensusExporter {SeenViews : make (map [string ]string ), t : t }
852
+ newExporter = func (config * config ) (tracingMetricsExporter , error ) {
853
+ ct := config .CustomTags
854
+ if len (ct ) < 1 {
855
+ t .Fatalf ("less than 2 custom tags sent in" )
856
+ }
857
+ if val , ok := ct ["customtag1" ]; ! ok || val != "wow" {
858
+ t .Fatalf ("incorrect custom tag: got %v, want %v" , val , "wow" )
859
+ }
860
+ if val , ok := ct ["customtag2" ]; ! ok || val != "nice" {
861
+ t .Fatalf ("incorrect custom tag: got %v, want %v" , val , "nice" )
862
+ }
863
+ return fe , nil
864
+ }
865
+
866
+ // This configuration present in file system and it's defined custom tags should make it
867
+ // to the created exporter.
868
+ configJSON := `{
869
+ "destination_project_id": "fake",
870
+ "enable_cloud_trace": true,
871
+ "enable_cloud_monitoring": true,
872
+ "global_trace_sampling_rate": 1.0,
873
+ "custom_tags":{"customtag1":"wow","customtag2":"nice"}
874
+ }`
875
+ cleanup , err := createTmpConfigInFileSystem (configJSON )
876
+ defer cleanup ()
877
+
878
+ // To clear globally registered tracing and metrics exporters.
879
+ defer func () {
880
+ internal .ClearExtraDialOptions ()
881
+ internal .ClearExtraServerOptions ()
882
+ }()
883
+
884
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
885
+ defer cancel ()
886
+ err = Start (ctx )
887
+ defer End ()
888
+ if err != nil {
889
+ t .Fatalf ("Start() failed with err: %v" , err )
890
+ }
891
+ }
0 commit comments