@@ -2,9 +2,11 @@ package opentelemetry
22
33import (
44 "encoding/json"
5+ "io"
56 "testing"
67 "time"
78
9+ "github.com/sirupsen/logrus"
810 "github.com/stretchr/testify/require"
911 "go.k6.io/k6/internal/build"
1012 "go.k6.io/k6/lib/types"
@@ -37,6 +39,37 @@ func TestConfig(t *testing.T) {
3739 },
3840 },
3941
42+ "OTLP exporter env vars overwrite defaults" : {
43+ env : map [string ]string {
44+ "OTEL_SERVICE_NAME" : "k6-test" ,
45+ "OTEL_EXPORTER_OTLP_PROTOCOL" : httpExporterProtocol ,
46+ "OTEL_METRIC_EXPORT_INTERVAL" : "1m" ,
47+ "OTEL_EXPORTER_OTLP_HEADERS" : "k1=v1,k2=v2" ,
48+ "OTEL_EXPORTER_OTLP_CERTIFICATE" : "fake-certificate" ,
49+ "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE" : "fake-client-certificate" ,
50+ "OTEL_EXPORTER_OTLP_CLIENT_KEY" : "fake-client-key" ,
51+ "OTEL_EXPORTER_OTLP_INSECURE" : "true" ,
52+ "OTEL_EXPORTER_OTLP_ENDPOINT" : "localhost:4317" ,
53+ },
54+ expectedConfig : Config {
55+ ServiceName : null .StringFrom ("k6-test" ),
56+ ServiceVersion : null .NewString (build .Version , false ),
57+ ExporterProtocol : null .StringFrom (httpExporterProtocol ),
58+ HTTPExporterInsecure : null .BoolFrom (true ),
59+ HTTPExporterEndpoint : null .StringFrom ("localhost:4317" ),
60+ HTTPExporterURLPath : null .NewString ("/v1/metrics" , false ),
61+ GRPCExporterInsecure : null .BoolFrom (true ),
62+ GRPCExporterEndpoint : null .StringFrom ("localhost:4317" ),
63+ ExportInterval : types .NullDurationFrom (1 * time .Minute ),
64+ FlushInterval : types .NewNullDuration (1 * time .Second , false ),
65+ SingleCounterForRate : null .NewBool (true , false ),
66+ Headers : null .StringFrom ("k1=v1,k2=v2" ),
67+ TLSCertificate : null .StringFrom ("fake-certificate" ),
68+ TLSClientCertificate : null .StringFrom ("fake-client-certificate" ),
69+ TLSClientKey : null .StringFrom ("fake-client-key" ),
70+ },
71+ },
72+
4073 "environment success merge" : {
4174 env : map [string ]string {"K6_OTEL_GRPC_EXPORTER_ENDPOINT" : "else" , "K6_OTEL_EXPORT_INTERVAL" : "4ms" },
4275 expectedConfig : Config {
@@ -95,22 +128,45 @@ func TestConfig(t *testing.T) {
95128 },
96129 },
97130
98- "OTEL environment variables " : {
131+ "OTLP exporter env vars overwritten by k6 env vars " : {
99132 env : map [string ]string {
100- "OTEL_SERVICE_NAME" : "otel-service" ,
133+ "OTEL_SERVICE_NAME" : "k6-test" ,
134+ "K6_OTEL_SERVICE_NAME" : "foo" ,
135+ "OTEL_EXPORTER_OTLP_PROTOCOL" : httpExporterProtocol ,
136+ "K6_OTEL_EXPORTER_PROTOCOL" : grpcExporterProtocol ,
137+ "OTEL_METRIC_EXPORT_INTERVAL" : "1m" ,
138+ "K6_OTEL_EXPORT_INTERVAL" : "4ms" ,
139+ "OTEL_EXPORTER_OTLP_HEADERS" : "k1=v1,k2=v2" ,
140+ "K6_OTEL_HEADERS" : "key1=value1,key2=value2" ,
141+ "OTEL_EXPORTER_OTLP_CERTIFICATE" : "fake-certificate" ,
142+ "K6_OTEL_TLS_CERTIFICATE" : "cert_path" ,
143+ "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE" : "fake-client-certificate" ,
144+ "K6_OTEL_TLS_CLIENT_CERTIFICATE" : "client_cert_path" ,
145+ "OTEL_EXPORTER_OTLP_CLIENT_KEY" : "fake-client-key" ,
146+ "K6_OTEL_TLS_CLIENT_KEY" : "client_key_path" ,
147+ "OTEL_EXPORTER_OTLP_INSECURE" : "true" ,
148+ "K6_OTEL_HTTP_EXPORTER_INSECURE" : "false" ,
149+ "K6_OTEL_GRPC_EXPORTER_INSECURE" : "false" ,
150+ "OTEL_EXPORTER_OTLP_ENDPOINT" : "localhost:4317" ,
151+ "K6_OTEL_HTTP_EXPORTER_ENDPOINT" : "localhost:4318" ,
152+ "K6_OTEL_GRPC_EXPORTER_ENDPOINT" : "localhost:4318" ,
101153 },
102154 expectedConfig : Config {
103- ServiceName : null .NewString ( "otel-service" , true ),
155+ ServiceName : null .StringFrom ( "foo" ),
104156 ServiceVersion : null .NewString (build .Version , false ),
105- ExporterProtocol : null .NewString (grpcExporterProtocol , false ),
106- HTTPExporterInsecure : null .NewBool ( false , false ),
107- HTTPExporterEndpoint : null .NewString ("localhost:4318" , false ),
157+ ExporterProtocol : null .StringFrom (grpcExporterProtocol ),
158+ HTTPExporterInsecure : null .BoolFrom ( false ),
159+ HTTPExporterEndpoint : null .StringFrom ("localhost:4318" ),
108160 HTTPExporterURLPath : null .NewString ("/v1/metrics" , false ),
109- GRPCExporterInsecure : null .NewBool ( false , false ),
110- GRPCExporterEndpoint : null .NewString ("localhost:4317" , false ),
111- ExportInterval : types .NewNullDuration ( 10 * time .Second , false ),
161+ GRPCExporterInsecure : null .BoolFrom ( false ),
162+ GRPCExporterEndpoint : null .StringFrom ("localhost:4318" ),
163+ ExportInterval : types .NullDurationFrom ( 4 * time .Millisecond ),
112164 FlushInterval : types .NewNullDuration (1 * time .Second , false ),
113165 SingleCounterForRate : null .NewBool (true , false ),
166+ Headers : null .StringFrom ("key1=value1,key2=value2" ),
167+ TLSCertificate : null .StringFrom ("cert_path" ),
168+ TLSClientCertificate : null .StringFrom ("client_cert_path" ),
169+ TLSClientKey : null .StringFrom ("client_key_path" ),
114170 },
115171 },
116172
@@ -157,6 +213,52 @@ func TestConfig(t *testing.T) {
157213 },
158214 },
159215
216+ "OTLP exporter env vars overwritten by JSON config" : {
217+ jsonRaw : json .RawMessage (
218+ `{` +
219+ `"serviceName":"foo",` +
220+ `"exporterProtocol":"grpc",` +
221+ `"exportInterval":"15ms",` +
222+ `"httpExporterInsecure":false,` +
223+ `"httpExporterEndpoint":"localhost:4318",` +
224+ `"grpcExporterInsecure":false,` +
225+ `"grpcExporterEndpoint":"localhost:4318",` +
226+ `"tlsCertificate":"cert_path",` +
227+ `"tlsClientCertificate":"client_cert_path",` +
228+ `"tlsClientKey":"client_key_path",` +
229+ `"headers":"key1=value1,key2=value2"` +
230+ `}` ,
231+ ),
232+ env : map [string ]string {
233+ "OTEL_SERVICE_NAME" : "k6-test" ,
234+ "OTEL_EXPORTER_OTLP_PROTOCOL" : httpExporterProtocol ,
235+ "OTEL_METRIC_EXPORT_INTERVAL" : "1m" ,
236+ "OTEL_EXPORTER_OTLP_HEADERS" : "k1=v1,k2=v2" ,
237+ "OTEL_EXPORTER_OTLP_CERTIFICATE" : "fake-certificate" ,
238+ "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE" : "fake-client-certificate" ,
239+ "OTEL_EXPORTER_OTLP_CLIENT_KEY" : "fake-client-key" ,
240+ "OTEL_EXPORTER_OTLP_INSECURE" : "true" ,
241+ "OTEL_EXPORTER_OTLP_ENDPOINT" : "localhost:4317" ,
242+ },
243+ expectedConfig : Config {
244+ ServiceName : null .StringFrom ("foo" ),
245+ ServiceVersion : null .NewString (build .Version , false ),
246+ ExporterProtocol : null .StringFrom (grpcExporterProtocol ),
247+ HTTPExporterInsecure : null .BoolFrom (false ),
248+ HTTPExporterEndpoint : null .StringFrom ("localhost:4318" ),
249+ HTTPExporterURLPath : null .NewString ("/v1/metrics" , false ),
250+ GRPCExporterInsecure : null .BoolFrom (false ),
251+ GRPCExporterEndpoint : null .StringFrom ("localhost:4318" ),
252+ ExportInterval : types .NullDurationFrom (15 * time .Millisecond ),
253+ FlushInterval : types .NewNullDuration (1 * time .Second , false ),
254+ SingleCounterForRate : null .NewBool (true , false ),
255+ Headers : null .StringFrom ("key1=value1,key2=value2" ),
256+ TLSCertificate : null .StringFrom ("cert_path" ),
257+ TLSClientCertificate : null .StringFrom ("client_cert_path" ),
258+ TLSClientKey : null .StringFrom ("client_key_path" ),
259+ },
260+ },
261+
160262 "JSON success merge" : {
161263 jsonRaw : json .RawMessage (`{"exporterType":"http","httpExporterEndpoint":"localhost:5566","httpExporterURLPath":"/lorem/ipsum","exportInterval":"15ms"}` ),
162264 expectedConfig : Config {
@@ -213,7 +315,11 @@ func TestConfig(t *testing.T) {
213315 for name , testCase := range testCases {
214316 t .Run (name , func (t * testing.T ) {
215317 t .Parallel ()
216- config , err := GetConsolidatedConfig (testCase .jsonRaw , testCase .env )
318+
319+ logger := logrus .New ()
320+ logger .SetOutput (io .Discard )
321+
322+ config , err := GetConsolidatedConfig (testCase .jsonRaw , testCase .env , logger )
217323 if testCase .err != "" {
218324 require .Error (t , err )
219325 require .Contains (t , err .Error (), testCase .err )
0 commit comments