@@ -24,6 +24,7 @@ import (
2424	"math/rand" 
2525	"net" 
2626	"net/http" 
27+ 	"os" 
2728	"path" 
2829	"strings" 
2930	"time" 
@@ -62,30 +63,33 @@ var ourTransport *http.Transport = &http.Transport{
6263}
6364
6465type  driver  struct  {
65- 	client  * http.Client 
66- 	cfg     config 
66+ 	metricsDriver  signalDriver 
67+ 	tracesDriver   signalDriver 
68+ 	cfg            config 
6769
6870	stopCh  chan  struct {}
6971}
7072
73+ type  signalDriver  struct  {
74+ 	cfg         signalConfig 
75+ 	generalCfg  config 
76+ 	client      * http.Client 
77+ 	stopCh      chan  struct {}
78+ }
79+ 
7180var  _  otlp.ProtocolDriver  =  (* driver )(nil )
7281
7382// NewDriver creates a new HTTP driver. 
7483func  NewDriver (opts  ... Option ) otlp.ProtocolDriver  {
75- 	cfg  :=  config {
76- 		endpoint :       fmt .Sprintf ("%s:%d" , otlp .DefaultCollectorHost , otlp .DefaultCollectorPort ),
77- 		compression :    NoCompression ,
78- 		tracesURLPath :  DefaultTracesPath ,
79- 		metricsURLPath : DefaultMetricsPath ,
80- 		maxAttempts :    DefaultMaxAttempts ,
81- 		backoff :        DefaultBackoff ,
82- 	}
84+ 	cfg  :=  newDefaultConfig ()
85+ 	applyEnvConfigs (& cfg , os .Getenv )
86+ 
8387	for  _ , opt  :=  range  opts  {
8488		opt .Apply (& cfg )
8589	}
8690	for  pathPtr , defaultPath  :=  range  map [* string ]string {
87- 		& cfg .tracesURLPath :  DefaultTracesPath ,
88- 		& cfg .metricsURLPath : DefaultMetricsPath ,
91+ 		& cfg .traces . urlPath :  DefaultTracesPath ,
92+ 		& cfg .metrics . urlPath : DefaultMetricsPath ,
8993	} {
9094		tmp  :=  strings .TrimSpace (* pathPtr )
9195		if  tmp  ==  ""  {
@@ -107,18 +111,43 @@ func NewDriver(opts ...Option) otlp.ProtocolDriver {
107111	if  cfg .backoff  <=  0  {
108112		cfg .backoff  =  DefaultBackoff 
109113	}
110- 	client  :=  & http.Client {
114+ 
115+ 	metricsClient  :=  & http.Client {
116+ 		Transport : ourTransport ,
117+ 		Timeout :   cfg .metrics .timeout ,
118+ 	}
119+ 	if  cfg .metrics .tlsCfg  !=  nil  {
120+ 		transport  :=  ourTransport .Clone ()
121+ 		transport .TLSClientConfig  =  cfg .metrics .tlsCfg 
122+ 		metricsClient .Transport  =  transport 
123+ 	}
124+ 
125+ 	tracesClient  :=  & http.Client {
111126		Transport : ourTransport ,
127+ 		Timeout :   cfg .traces .timeout ,
112128	}
113- 	if  cfg .tlsCfg  !=  nil  {
129+ 	if  cfg .traces . tlsCfg  !=  nil  {
114130		transport  :=  ourTransport .Clone ()
115- 		transport .TLSClientConfig  =  cfg .tlsCfg 
116- 		client .Transport  =  transport 
131+ 		transport .TLSClientConfig  =  cfg .traces . tlsCfg 
132+ 		tracesClient .Transport  =  transport 
117133	}
134+ 
135+ 	stopCh  :=  make (chan  struct {})
118136	return  & driver {
119- 		client : client ,
137+ 		tracesDriver : signalDriver {
138+ 			cfg :        cfg .traces ,
139+ 			generalCfg : cfg ,
140+ 			stopCh :     stopCh ,
141+ 			client :     tracesClient ,
142+ 		},
143+ 		metricsDriver : signalDriver {
144+ 			cfg :        cfg .metrics ,
145+ 			generalCfg : cfg ,
146+ 			stopCh :     stopCh ,
147+ 			client :     metricsClient ,
148+ 		},
120149		cfg :    cfg ,
121- 		stopCh : make ( chan   struct {}) ,
150+ 		stopCh : stopCh ,
122151	}
123152}
124153
@@ -150,7 +179,7 @@ func (d *driver) ExportMetrics(ctx context.Context, cps metricsdk.CheckpointSet,
150179	if  err  !=  nil  {
151180		return  err 
152181	}
153- 	return  d .send (ctx , rawRequest ,  d . cfg . metricsURLPath )
182+ 	return  d .metricsDriver . send (ctx , rawRequest )
154183}
155184
156185// ExportTraces implements otlp.ProtocolDriver. 
@@ -166,7 +195,7 @@ func (d *driver) ExportTraces(ctx context.Context, ss []*tracesdk.SpanSnapshot)
166195	if  err  !=  nil  {
167196		return  err 
168197	}
169- 	return  d .send (ctx , rawRequest ,  d . cfg . tracesURLPath )
198+ 	return  d .tracesDriver . send (ctx , rawRequest )
170199}
171200
172201func  (d  * driver ) marshal (msg  proto.Message ) ([]byte , error ) {
@@ -176,12 +205,12 @@ func (d *driver) marshal(msg proto.Message) ([]byte, error) {
176205	return  proto .Marshal (msg )
177206}
178207
179- func  (d  * driver ) send (ctx  context.Context , rawRequest  []byte ,  urlPath   string ) error  {
180- 	address  :=  fmt .Sprintf ("%s://%s%s" , d .getScheme (), d .cfg .endpoint , urlPath )
208+ func  (d  * signalDriver ) send (ctx  context.Context , rawRequest  []byte ) error  {
209+ 	address  :=  fmt .Sprintf ("%s://%s%s" , d .getScheme (), d .cfg .endpoint , d . cfg . urlPath )
181210	var  cancel  context.CancelFunc 
182211	ctx , cancel  =  d .contextWithStop (ctx )
183212	defer  cancel ()
184- 	for  i  :=  0 ; i  <  d .cfg .maxAttempts ; i ++  {
213+ 	for  i  :=  0 ; i  <  d .generalCfg .maxAttempts ; i ++  {
185214		response , err  :=  d .singleSend (ctx , rawRequest , address )
186215		if  err  !=  nil  {
187216			return  err 
@@ -198,7 +227,7 @@ func (d *driver) send(ctx context.Context, rawRequest []byte, urlPath string) er
198227			fallthrough 
199228		case  http .StatusServiceUnavailable :
200229			select  {
201- 			case  <- time .After (getWaitDuration (d .cfg .backoff , i )):
230+ 			case  <- time .After (getWaitDuration (d .generalCfg .backoff , i )):
202231				continue 
203232			case  <- ctx .Done ():
204233				return  ctx .Err ()
@@ -207,10 +236,10 @@ func (d *driver) send(ctx context.Context, rawRequest []byte, urlPath string) er
207236			return  fmt .Errorf ("failed with HTTP status %s" , response .Status )
208237		}
209238	}
210- 	return  fmt .Errorf ("failed to send data to %s after %d tries" , address , d .cfg .maxAttempts )
239+ 	return  fmt .Errorf ("failed to send data to %s after %d tries" , address , d .generalCfg .maxAttempts )
211240}
212241
213- func  (d  * driver ) getScheme () string  {
242+ func  (d  * signalDriver ) getScheme () string  {
214243	if  d .cfg .insecure  {
215244		return  "http" 
216245	}
@@ -237,7 +266,7 @@ func getWaitDuration(backoff time.Duration, i int) time.Duration {
237266	return  (time .Duration )(k )* backoff  +  (time .Duration )(jitter )
238267}
239268
240- func  (d  * driver ) contextWithStop (ctx  context.Context ) (context.Context , context.CancelFunc ) {
269+ func  (d  * signalDriver ) contextWithStop (ctx  context.Context ) (context.Context , context.CancelFunc ) {
241270	// Unify the parent context Done signal with the driver's stop 
242271	// channel. 
243272	ctx , cancel  :=  context .WithCancel (ctx )
@@ -253,7 +282,7 @@ func (d *driver) contextWithStop(ctx context.Context) (context.Context, context.
253282	return  ctx , cancel 
254283}
255284
256- func  (d  * driver ) singleSend (ctx  context.Context , rawRequest  []byte , address  string ) (* http.Response , error ) {
285+ func  (d  * signalDriver ) singleSend (ctx  context.Context , rawRequest  []byte , address  string ) (* http.Response , error ) {
257286	request , err  :=  http .NewRequestWithContext (ctx , http .MethodPost , address , nil )
258287	if  err  !=  nil  {
259288		return  nil , err 
@@ -271,14 +300,14 @@ func (d *driver) singleSend(ctx context.Context, rawRequest []byte, address stri
271300	return  d .client .Do (request )
272301}
273302
274- func  (d  * driver ) prepareBody (rawRequest  []byte ) (io.ReadCloser , int64 , http.Header ) {
303+ func  (d  * signalDriver ) prepareBody (rawRequest  []byte ) (io.ReadCloser , int64 , http.Header ) {
275304	var  bodyReader  io.ReadCloser 
276305	headers  :=  http.Header {}
277306	for  k , v  :=  range  d .cfg .headers  {
278307		headers .Set (k , v )
279308	}
280309	contentLength  :=  (int64 )(len (rawRequest ))
281- 	if  d .cfg .marshaler  ==  MarshalJSON  {
310+ 	if  d .generalCfg .marshaler  ==  MarshalJSON  {
282311		headers .Set ("Content-Type" , contentTypeJSON )
283312	} else  {
284313		headers .Set ("Content-Type" , contentTypeProto )
0 commit comments