@@ -43,54 +43,94 @@ func (c *OtelCollector) TraceDefaultReporting() bool {
43
43
return ! c .tracesDisabledByDefault
44
44
}
45
45
46
- // Exporter creates an Open Telemetry exporter instance.
47
- func Exporter (ctx context.Context , cfg config.OTLPExporter ) (* OtelCollector , error ) {
48
- if cfg .Port == 0 {
49
- cfg .Port = 4317
50
- }
51
- if cfg .Host == "" {
52
- cfg .Host = "localhost"
53
- }
46
+ func httpExporterWithOptions (ctx context.Context , cfg config.OTLPExporter ,
47
+ options []interface {}) (* OtelCollector , error ) {
54
48
55
- var exporter sdktrace.SpanExporter
56
- var metricExporter sdkmetric.Exporter
57
- var err error
58
- if cfg .UseHTTP {
59
- headers := make (map [string ]string , len (cfg .HTTPHeaders ))
60
- for _ , kvh := range cfg .HTTPHeaders {
61
- // WARN: we do not clean up / canonicalize the header name
62
- headers [kvh .Key ] = kvh .Value
49
+ tOpts := make ([]otlptracehttp.Option , 0 , len (options )+ 1 )
50
+ mOpts := make ([]otlpmetrichttp.Option , 0 , len (options )+ 1 )
51
+ for _ , iopt := range options {
52
+ if to , ok := iopt .(otlptracehttp.Option ); ok {
53
+ tOpts = append (tOpts , to )
63
54
}
64
- exporter , err = otlptracehttp .New (ctx ,
65
- otlptracehttp .WithHeaders (headers ),
66
- otlptracehttp .WithEndpoint (fmt .Sprintf ("%s:%d" , cfg .Host , cfg .Port )),
67
- )
68
- if err != nil {
69
- return nil , errors .New ("cannot create http exporter" )
55
+ if mo , ok := iopt .(otlpmetrichttp.Option ); ok {
56
+ mOpts = append (mOpts , mo )
70
57
}
58
+ }
59
+
60
+ endpoint := fmt .Sprintf ("%s:%d" , cfg .Host , cfg .Port )
61
+ tOpts = append (tOpts , otlptracehttp .WithEndpoint (endpoint ))
62
+ exporter , err := otlptracehttp .New (ctx , tOpts ... )
63
+ if err != nil {
64
+ return nil , errors .New ("cannot create http trace exporter" )
65
+ }
66
+
67
+ mOpts = append (mOpts , otlpmetrichttp .WithEndpoint (endpoint ))
68
+ metricExporter , err := otlpmetrichttp .New (ctx , mOpts ... )
69
+ if err != nil {
70
+ return nil , errors .New ("cannot create http metric exporter" )
71
+ }
72
+
73
+ return & OtelCollector {
74
+ exporter : exporter ,
75
+ metricExporter : metricExporter ,
76
+ metricsDisabledByDefault : cfg .DisableMetrics ,
77
+ tracesDisabledByDefault : cfg .DisableTraces ,
78
+ }, nil
79
+ }
80
+
81
+ func grpcExporterWithOptions (ctx context.Context , cfg config.OTLPExporter ,
82
+ options []interface {}) (* OtelCollector , error ) {
71
83
72
- metricExporter , err = otlpmetrichttp . New ( ctx ,
73
- otlpmetrichttp . WithHeaders ( headers ),
74
- otlpmetrichttp . WithEndpoint ( fmt . Sprintf ( "%s:%d" , cfg . Host , cfg . Port )))
75
- if err != nil {
76
- return nil , errors . New ( "cannot create http metric exporter" )
84
+ tOpts := make ([]otlptracegrpc. Option , 0 , len ( options ) + 1 )
85
+ mOpts := make ([]otlpmetricgrpc. Option , 0 , len ( options ) + 1 )
86
+ for _ , iopt := range options {
87
+ if to , ok := iopt .(otlptracegrpc. Option ); ok {
88
+ tOpts = append ( tOpts , to )
77
89
}
78
- } else {
79
- // by default, grpc conections has TLS enabled
80
- exporter , err = otlptracegrpc .New (ctx ,
81
- otlptracegrpc .WithInsecure (),
82
- otlptracegrpc .WithEndpoint (fmt .Sprintf ("%s:%d" , cfg .Host , cfg .Port )))
83
- if err != nil {
84
- return nil , errors .New ("cannot create grpc exporter" )
90
+ if mo , ok := iopt .(otlpmetricgrpc.Option ); ok {
91
+ mOpts = append (mOpts , mo )
85
92
}
86
- metricExporter , err = otlpmetricgrpc .New (ctx ,
87
- otlpmetricgrpc .WithInsecure (),
88
- otlpmetricgrpc .WithEndpoint (fmt .Sprintf ("%s:%d" , cfg .Host , cfg .Port )))
89
93
}
94
+
95
+ endpoint := fmt .Sprintf ("%s:%d" , cfg .Host , cfg .Port )
96
+ tOpts = append (tOpts , otlptracegrpc .WithEndpoint (endpoint ))
97
+ exporter , err := otlptracegrpc .New (ctx , tOpts ... )
98
+ if err != nil {
99
+ return nil , errors .New ("cannot create grpc traces exporter" )
100
+ }
101
+ mOpts = append (mOpts , otlpmetricgrpc .WithEndpoint (endpoint ))
102
+ metricExporter , err := otlpmetricgrpc .New (ctx , mOpts ... )
103
+ if err != nil {
104
+ return nil , errors .New ("cannot create grpc metric exporter" )
105
+ }
106
+
90
107
return & OtelCollector {
91
108
exporter : exporter ,
92
109
metricExporter : metricExporter ,
93
110
metricsDisabledByDefault : cfg .DisableMetrics ,
94
111
tracesDisabledByDefault : cfg .DisableTraces ,
95
112
}, nil
96
113
}
114
+
115
+ func ExporterWithOptions (ctx context.Context , cfg config.OTLPExporter , options []interface {}) (* OtelCollector , error ) {
116
+ if cfg .Port == 0 {
117
+ cfg .Port = 4317
118
+ }
119
+ if cfg .Host == "" {
120
+ cfg .Host = "localhost"
121
+ }
122
+
123
+ if cfg .UseHTTP {
124
+ return httpExporterWithOptions (ctx , cfg , options )
125
+ }
126
+ return grpcExporterWithOptions (ctx , cfg , options )
127
+ }
128
+
129
+ // Exporter creates an Open Telemetry exporter instance.
130
+ func Exporter (ctx context.Context , cfg config.OTLPExporter ) (* OtelCollector , error ) {
131
+ options := make ([]interface {}, 0 , 2 )
132
+ // by default, grpc conections have TLS enabled:
133
+ options = append (options , otlptracegrpc .WithInsecure ())
134
+ options = append (options , otlpmetricgrpc .WithInsecure ())
135
+ return ExporterWithOptions (ctx , cfg , options )
136
+ }
0 commit comments