@@ -103,46 +103,54 @@ func removeProtocolPrefixForOTLP(endpoint string) string {
103103 return pieces [1 ]
104104}
105105
106- func makeMetricsExporterFactory (cfg * config.AgentConfig ) func () (metric.Exporter , error ) {
106+ func makeMetricsExporterFactory (cfg * config.AgentConfig ) func (opts ... ServiceOption ) (metric.Exporter , error ) {
107107 // We are only supporting logging and otlp metric exporters for now. We will add support for prometheus
108108 // metrics later
109109 switch cfg .Reporting .MetricReporterType {
110110 case config .MetricReporterType_METRIC_REPORTER_TYPE_LOGGING :
111111 // stdout exporter
112- return func () (metric.Exporter , error ) {
112+ // currently only ServiceOption is WithHeaders so noop-ing ServiceOption for stdout for now
113+ return func (_ ... ServiceOption ) (metric.Exporter , error ) {
113114 // TODO: Define if endpoint could be a filepath to write into a file.
114115 return stdoutmetric .New ()
115116 }
116117 default :
117- endpoint := cfg .GetReporting ().GetMetricEndpoint ().GetValue ()
118- if len (endpoint ) == 0 {
119- endpoint = cfg .GetReporting ().GetEndpoint ().GetValue ()
120- }
118+ return func (opts ... ServiceOption ) (metric.Exporter , error ) {
119+ endpoint := cfg .GetReporting ().GetMetricEndpoint ().GetValue ()
120+ if len (endpoint ) == 0 {
121+ endpoint = cfg .GetReporting ().GetEndpoint ().GetValue ()
122+ }
121123
122- opts := []otlpmetricgrpc.Option {
123- otlpmetricgrpc .WithEndpoint (removeProtocolPrefixForOTLP (endpoint )),
124- }
124+ serviceOpts := & ServiceOptions {
125+ headers : make (map [string ]string ),
126+ }
127+ for _ , opt := range opts {
128+ opt (serviceOpts )
129+ }
125130
126- if ! cfg .GetReporting ().GetSecure ().GetValue () {
127- opts = append (opts , otlpmetricgrpc .WithInsecure ())
128- }
131+ metricOpts := []otlpmetricgrpc.Option {
132+ otlpmetricgrpc .WithEndpoint (removeProtocolPrefixForOTLP (endpoint )),
133+ otlpmetricgrpc .WithHeaders (serviceOpts .headers ),
134+ }
129135
130- certFile := cfg .GetReporting ().GetCertFile ().GetValue ()
131- if len (certFile ) > 0 {
132- if tlsCredentials , err := credentials .NewClientTLSFromFile (certFile , "" ); err == nil {
133- opts = append (opts , otlpmetricgrpc .WithTLSCredentials (tlsCredentials ))
134- } else {
135- log .Printf ("error while creating tls credentials from cert path %s: %v" , certFile , err )
136+ if ! cfg .GetReporting ().GetSecure ().GetValue () {
137+ metricOpts = append (metricOpts , otlpmetricgrpc .WithInsecure ())
136138 }
137- }
138139
139- if cfg .Reporting .GetEnableGrpcLoadbalancing ().GetValue () {
140- resolver .SetDefaultScheme ("dns" )
141- opts = append (opts , otlpmetricgrpc .WithServiceConfig (`{"loadBalancingConfig": [ { "round_robin": {} } ]}` ))
142- }
140+ certFile := cfg .GetReporting ().GetCertFile ().GetValue ()
141+ if len (certFile ) > 0 {
142+ if tlsCredentials , err := credentials .NewClientTLSFromFile (certFile , "" ); err == nil {
143+ metricOpts = append (metricOpts , otlpmetricgrpc .WithTLSCredentials (tlsCredentials ))
144+ } else {
145+ log .Printf ("error while creating tls credentials from cert path %s: %v" , certFile , err )
146+ }
147+ }
143148
144- return func () (metric.Exporter , error ) {
145- return otlpmetricgrpc .New (context .Background (), opts ... )
149+ if cfg .Reporting .GetEnableGrpcLoadbalancing ().GetValue () {
150+ resolver .SetDefaultScheme ("dns" )
151+ metricOpts = append (metricOpts , otlpmetricgrpc .WithServiceConfig (`{"loadBalancingConfig": [ { "round_robin": {} } ]}` ))
152+ }
153+ return otlpmetricgrpc .New (context .Background (), metricOpts ... )
146154 }
147155 }
148156}
@@ -305,7 +313,7 @@ func InitWithSpanProcessorWrapper(cfg *config.AgentConfig, wrapper SpanProcessor
305313// and returns a shutdown function to flush data immediately on a termination signal.
306314// Also sets opentelemetry internal errorhandler to the provider zap errorhandler
307315func InitWithSpanProcessorWrapperAndZap (cfg * config.AgentConfig , wrapper SpanProcessorWrapper ,
308- versionInfoAttrs []attribute.KeyValue , logger * zap.Logger ) func () {
316+ versionInfoAttrs []attribute.KeyValue , logger * zap.Logger , opts ... ServiceOption ) func () {
309317 mu .Lock ()
310318 defer mu .Unlock ()
311319 if initialized {
@@ -340,7 +348,7 @@ func InitWithSpanProcessorWrapperAndZap(cfg *config.AgentConfig, wrapper SpanPro
340348 }
341349
342350 // Initialize metrics
343- metricsShutdownFn := initializeMetrics (cfg , versionInfoAttrs )
351+ metricsShutdownFn := initializeMetrics (cfg , versionInfoAttrs , opts ... )
344352
345353 exporterFactory = makeExporterFactory (cfg )
346354 configFactory = makeConfigFactory (cfg )
@@ -485,13 +493,13 @@ func RegisterServiceWithSpanProcessorWrapper(key string, resourceAttributes map[
485493 }), tp , nil
486494}
487495
488- func initializeMetrics (cfg * config.AgentConfig , versionInfoAttrs []attribute.KeyValue ) func () {
496+ func initializeMetrics (cfg * config.AgentConfig , versionInfoAttrs []attribute.KeyValue , opts ... ServiceOption ) func () {
489497 if shouldDisableMetrics (cfg ) {
490498 return func () {}
491499 }
492500
493501 metricsExporterFactory := makeMetricsExporterFactory (cfg )
494- metricsExporter , err := metricsExporterFactory ()
502+ metricsExporter , err := metricsExporterFactory (opts ... )
495503 if err != nil {
496504 log .Fatal (err )
497505 }
0 commit comments