@@ -23,6 +23,7 @@ import (
23
23
sfxpb "github.com/signalfx/com_signalfx_metrics_protobuf/model"
24
24
"github.com/stretchr/testify/assert"
25
25
"github.com/stretchr/testify/require"
26
+ "go.opentelemetry.io/collector/client"
26
27
"go.opentelemetry.io/collector/component/componenttest"
27
28
"go.opentelemetry.io/collector/config/confighttp"
28
29
"go.opentelemetry.io/collector/config/configopaque"
@@ -567,6 +568,138 @@ func TestConsumeMetricsWithAccessTokenPassthrough(t *testing.T) {
567
568
}
568
569
}
569
570
571
+ func TestConsumeMetricsAccessTokenPassthroughPriorityToContext (t * testing.T ) {
572
+ fromHeaders := "AccessTokenFromClientHeaders"
573
+ fromLabels := []string {"AccessTokenFromLabel0" , "AccessTokenFromLabel1" }
574
+ fromContext := "AccessTokenFromContext"
575
+
576
+ validMetricsWithToken := func (includeToken bool , token string , histogram bool ) pmetric.Metrics {
577
+ out := pmetric .NewMetrics ()
578
+ rm := out .ResourceMetrics ().AppendEmpty ()
579
+
580
+ if includeToken {
581
+ rm .Resource ().Attributes ().PutStr ("com.splunk.signalfx.access_token" , token )
582
+ }
583
+
584
+ ilm := rm .ScopeMetrics ().AppendEmpty ()
585
+ m := ilm .Metrics ().AppendEmpty ()
586
+
587
+ if histogram {
588
+ buildHistogram (m , "test_histogram" , pcommon .Timestamp (100000000 ), 1 )
589
+ } else {
590
+ m .SetName ("test_gauge" )
591
+
592
+ dp := m .SetEmptyGauge ().DataPoints ().AppendEmpty ()
593
+ dp .Attributes ().PutStr ("k0" , "v0" )
594
+ dp .Attributes ().PutStr ("k1" , "v1" )
595
+ dp .SetDoubleValue (123 )
596
+ }
597
+
598
+ return out
599
+ }
600
+
601
+ tests := []struct {
602
+ name string
603
+ accessTokenPassthrough bool
604
+ metrics pmetric.Metrics
605
+ additionalHeaders map [string ]string
606
+ pushedTokens []string
607
+ sendOTLPHistograms bool
608
+ inContext bool
609
+ }{
610
+ {
611
+ name : "passthrough access token and included in md" ,
612
+ accessTokenPassthrough : true ,
613
+ inContext : true ,
614
+ metrics : validMetricsWithToken (true , fromLabels [0 ], false ),
615
+ pushedTokens : []string {fromContext },
616
+ },
617
+ {
618
+ name : "passthrough access token and not included in md" ,
619
+ accessTokenPassthrough : true ,
620
+ inContext : true ,
621
+ metrics : validMetricsWithToken (false , fromLabels [0 ], false ),
622
+ pushedTokens : []string {fromContext },
623
+ sendOTLPHistograms : false ,
624
+ },
625
+ {
626
+ name : "passthrough access token and included in md" ,
627
+ accessTokenPassthrough : true ,
628
+ inContext : false ,
629
+ metrics : validMetricsWithToken (true , fromLabels [0 ], false ),
630
+ pushedTokens : []string {fromLabels [0 ]},
631
+ },
632
+ {
633
+ name : "passthrough access token and not included in md" ,
634
+ accessTokenPassthrough : true ,
635
+ inContext : false ,
636
+ metrics : validMetricsWithToken (false , fromLabels [0 ], false ),
637
+ pushedTokens : []string {fromHeaders },
638
+ sendOTLPHistograms : false ,
639
+ },
640
+ }
641
+ for _ , tt := range tests {
642
+ receivedTokens := struct {
643
+ sync.Mutex
644
+ tokens []string
645
+ }{}
646
+ receivedTokens .tokens = []string {}
647
+ t .Run (tt .name , func (t * testing.T ) {
648
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
649
+ assert .Equal (t , tt .name , r .Header .Get ("test_header_" ))
650
+ receivedTokens .Lock ()
651
+
652
+ token := r .Header .Get ("x-sf-token" )
653
+ receivedTokens .tokens = append (receivedTokens .tokens , token )
654
+
655
+ receivedTokens .Unlock ()
656
+ w .WriteHeader (http .StatusAccepted )
657
+ }))
658
+ defer server .Close ()
659
+
660
+ factory := NewFactory ()
661
+ cfg := factory .CreateDefaultConfig ().(* Config )
662
+ cfg .IngestURL = server .URL
663
+ cfg .APIURL = server .URL
664
+ cfg .ClientConfig .Headers = make (map [string ]configopaque.String )
665
+ for k , v := range tt .additionalHeaders {
666
+ cfg .ClientConfig .Headers [k ] = configopaque .String (v )
667
+ }
668
+ cfg .ClientConfig .Headers ["test_header_" ] = configopaque .String (tt .name )
669
+ cfg .AccessToken = configopaque .String (fromHeaders )
670
+ cfg .AccessTokenPassthrough = tt .accessTokenPassthrough
671
+ cfg .SendOTLPHistograms = tt .sendOTLPHistograms
672
+ sfxExp , err := NewFactory ().CreateMetrics (context .Background (), exportertest .NewNopSettings (), cfg )
673
+ require .NoError (t , err )
674
+ ctx := context .Background ()
675
+ if tt .inContext {
676
+ ctx = client .NewContext (
677
+ ctx ,
678
+ client.Info {Metadata : client .NewMetadata (
679
+ map [string ][]string {splunk .SFxAccessTokenHeader : {fromContext }},
680
+ )},
681
+ )
682
+ }
683
+ require .NoError (t , sfxExp .Start (ctx , componenttest .NewNopHost ()))
684
+ defer func () {
685
+ require .NoError (t , sfxExp .Shutdown (context .Background ()))
686
+ }()
687
+
688
+ err = sfxExp .ConsumeMetrics (ctx , tt .metrics )
689
+
690
+ assert .NoError (t , err )
691
+ require .Eventually (t , func () bool {
692
+ receivedTokens .Lock ()
693
+ defer receivedTokens .Unlock ()
694
+ return len (tt .pushedTokens ) == len (receivedTokens .tokens )
695
+ }, 1 * time .Second , 10 * time .Millisecond )
696
+ sort .Strings (tt .pushedTokens )
697
+ sort .Strings (receivedTokens .tokens )
698
+ assert .Equal (t , tt .pushedTokens , receivedTokens .tokens )
699
+ })
700
+ }
701
+ }
702
+
570
703
func TestNewEventExporter (t * testing.T ) {
571
704
got , err := newEventExporter (nil , exportertest .NewNopSettings ())
572
705
assert .EqualError (t , err , "nil config" )
@@ -812,6 +945,102 @@ func TestConsumeLogsDataWithAccessTokenPassthrough(t *testing.T) {
812
945
}
813
946
}
814
947
948
+ func TestConsumeLogsAccessTokenPassthrough (t * testing.T ) {
949
+ fromHeaders := "AccessTokenFromClientHeaders"
950
+ fromLabels := "AccessTokenFromLabel"
951
+ fromContext := "AccessTokenFromContext"
952
+
953
+ newLogData := func (includeToken bool ) plog.Logs {
954
+ out := makeSampleResourceLogs ()
955
+ makeSampleResourceLogs ().ResourceLogs ().At (0 ).CopyTo (out .ResourceLogs ().AppendEmpty ())
956
+
957
+ if includeToken {
958
+ out .ResourceLogs ().At (0 ).Resource ().Attributes ().PutStr ("com.splunk.signalfx.access_token" , fromLabels )
959
+ out .ResourceLogs ().At (1 ).Resource ().Attributes ().PutStr ("com.splunk.signalfx.access_token" , fromLabels )
960
+ }
961
+ return out
962
+ }
963
+
964
+ tests := []struct {
965
+ name string
966
+ accessTokenPassthrough bool
967
+ includedInLogData bool
968
+ inContext bool
969
+ expectedToken string
970
+ }{
971
+ {
972
+ name : "passthrough access token and not included in request context" ,
973
+ inContext : true ,
974
+ accessTokenPassthrough : true ,
975
+ includedInLogData : true ,
976
+ expectedToken : fromContext ,
977
+ },
978
+ {
979
+ name : "passthrough access token and included in logs" ,
980
+ inContext : false ,
981
+ accessTokenPassthrough : true ,
982
+ includedInLogData : true ,
983
+ expectedToken : fromLabels ,
984
+ },
985
+ {
986
+ name : "passthrough access token and not included in logs" ,
987
+ inContext : false ,
988
+ accessTokenPassthrough : false ,
989
+ includedInLogData : false ,
990
+ expectedToken : fromHeaders ,
991
+ },
992
+ }
993
+ for _ , tt := range tests {
994
+ t .Run (tt .name , func (t * testing.T ) {
995
+ receivedTokens := struct {
996
+ sync.Mutex
997
+ tokens []string
998
+ }{}
999
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1000
+ assert .Equal (t , tt .name , r .Header .Get ("test_header_" ))
1001
+ receivedTokens .Lock ()
1002
+ receivedTokens .tokens = append (receivedTokens .tokens , r .Header .Get ("x-sf-token" ))
1003
+ receivedTokens .Unlock ()
1004
+ w .WriteHeader (http .StatusAccepted )
1005
+ }))
1006
+ defer server .Close ()
1007
+
1008
+ factory := NewFactory ()
1009
+ cfg := factory .CreateDefaultConfig ().(* Config )
1010
+ cfg .IngestURL = server .URL
1011
+ cfg .APIURL = server .URL
1012
+ cfg .Headers = make (map [string ]configopaque.String )
1013
+ cfg .Headers ["test_header_" ] = configopaque .String (tt .name )
1014
+ cfg .AccessToken = configopaque .String (fromHeaders )
1015
+ cfg .AccessTokenPassthrough = tt .accessTokenPassthrough
1016
+ sfxExp , err := NewFactory ().CreateLogs (context .Background (), exportertest .NewNopSettings (), cfg )
1017
+ require .NoError (t , err )
1018
+ require .NoError (t , sfxExp .Start (context .Background (), componenttest .NewNopHost ()))
1019
+ defer func () {
1020
+ require .NoError (t , sfxExp .Shutdown (context .Background ()))
1021
+ }()
1022
+
1023
+ ctx := context .Background ()
1024
+ if tt .inContext {
1025
+ ctx = client .NewContext (
1026
+ ctx ,
1027
+ client.Info {Metadata : client .NewMetadata (
1028
+ map [string ][]string {splunk .SFxAccessTokenHeader : {"AccessTokenFromContext" }},
1029
+ )},
1030
+ )
1031
+ }
1032
+ assert .NoError (t , sfxExp .ConsumeLogs (ctx , newLogData (tt .includedInLogData )))
1033
+
1034
+ require .Eventually (t , func () bool {
1035
+ receivedTokens .Lock ()
1036
+ defer receivedTokens .Unlock ()
1037
+ return len (receivedTokens .tokens ) == 1
1038
+ }, 1 * time .Second , 10 * time .Millisecond )
1039
+ assert .Equal (t , tt .expectedToken , receivedTokens .tokens [0 ])
1040
+ })
1041
+ }
1042
+ }
1043
+
815
1044
func generateLargeDPBatch () pmetric.Metrics {
816
1045
md := pmetric .NewMetrics ()
817
1046
md .ResourceMetrics ().EnsureCapacity (6500 )
0 commit comments