@@ -38,6 +38,7 @@ import (
3838 "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/ptracetest"
3939 "github.com/stretchr/testify/assert"
4040 "github.com/stretchr/testify/require"
41+ "go.opentelemetry.io/collector/client"
4142 "go.opentelemetry.io/collector/component"
4243 "go.opentelemetry.io/collector/component/componenttest"
4344 "go.opentelemetry.io/collector/config/confighttp"
@@ -519,7 +520,65 @@ func TestTransactionsAndSpans(t *testing.T) {
519520 }
520521}
521522
522- func sendInput (t * testing.T , inputJsonFileName string , expectedYamlFileName string , testEndpoint string ) {
523+ func TestMetadataPropagation (t * testing.T ) {
524+ table := map [string ]struct {
525+ includeMetadata bool
526+ expectedMetadata client.Metadata
527+ }{
528+ "when include_metadata is disabled only mappinmapping-mode is propagated" : {
529+ expectedMetadata : client .NewMetadata (map [string ][]string {
530+ "x-elastic-mapping-mode" : {"ecs" },
531+ }),
532+ },
533+ "when include_metadata is enabled all request metadata is propagated" : {
534+ includeMetadata : true ,
535+ expectedMetadata : client .NewMetadata (map [string ][]string {
536+ "content-type" : {"application/x-ndjson" },
537+ "x-elastic-mapping-mode" : {"ecs" },
538+ }),
539+ },
540+ }
541+ for tname , tcase := range table {
542+ t .Run (tname , func (t * testing.T ) {
543+ factory := NewFactory ()
544+ testEndpoint := testutil .GetAvailableLocalAddress (t )
545+ cfg := & Config {
546+ ServerConfig : confighttp.ServerConfig {
547+ Endpoint : testEndpoint ,
548+ IncludeMetadata : tcase .includeMetadata ,
549+ },
550+ }
551+
552+ set := receivertest .NewNopSettings (metadata .Type )
553+ nextTrace := new (consumertest.TracesSink )
554+ receiver , _ := factory .CreateTraces (context .Background (), set , cfg , nextTrace )
555+
556+ if err := receiver .Start (context .Background (), componenttest .NewNopHost ()); err != nil {
557+ t .Errorf ("Starting receiver failed: %v" , err )
558+ }
559+ defer func () {
560+ if err := receiver .Shutdown (context .Background ()); err != nil {
561+ t .Errorf ("Shutdown failed: %v" , err )
562+ }
563+ }()
564+
565+ sendInput (t , "transactions_spans.ndjson" , testEndpoint )
566+
567+ ctxs := nextTrace .Contexts ()
568+ require .GreaterOrEqual (t , len (ctxs ), 1 )
569+ md := client .FromContext (ctxs [0 ]).Metadata
570+ if tcase .includeMetadata {
571+ for k := range tcase .expectedMetadata .Keys () {
572+ require .Equal (t , tcase .expectedMetadata .Get (k ), md .Get (k ))
573+ }
574+ } else {
575+ require .Equal (t , tcase .expectedMetadata , md )
576+ }
577+ })
578+ }
579+ }
580+
581+ func sendInput (t * testing.T , inputJsonFileName string , testEndpoint string ) {
523582 data , err := os .ReadFile (filepath .Join (testData , inputJsonFileName ))
524583 if err != nil {
525584 t .Fatalf ("failed to read file: %v" , err )
@@ -542,7 +601,7 @@ func runComparisonForTraces(t *testing.T, inputJsonFileName string, expectedYaml
542601) {
543602 nextTrace .Reset ()
544603
545- sendInput (t , inputJsonFileName , expectedYamlFileName , testEndpoint )
604+ sendInput (t , inputJsonFileName , testEndpoint )
546605 actualTraces := nextTrace .AllTraces ()[0 ]
547606 expectedFile := filepath .Join (testData , expectedYamlFileName )
548607 // Use this line to generate the expected yaml file:
@@ -558,7 +617,7 @@ func runComparisonForErrors(t *testing.T, inputJsonFileName string, expectedYaml
558617) {
559618 nextLog .Reset ()
560619
561- sendInput (t , inputJsonFileName , expectedYamlFileName , testEndpoint )
620+ sendInput (t , inputJsonFileName , testEndpoint )
562621 actualLogs := nextLog .AllLogs ()[0 ]
563622 expectedFile := filepath .Join (testData , expectedYamlFileName )
564623 // Use this line to generate the expected yaml file:
@@ -573,7 +632,7 @@ func runComparisonForMetrics(t *testing.T, inputJsonFileName string, expectedYam
573632) {
574633
575634 nextMetric .Reset ()
576- sendInput (t , inputJsonFileName , expectedYamlFileName , testEndpoint )
635+ sendInput (t , inputJsonFileName , testEndpoint )
577636 actualMetrics := nextMetric .AllMetrics ()[0 ]
578637 expectedFile := filepath .Join (testData , expectedYamlFileName )
579638 // Use this line to generate the expected yaml file:
0 commit comments