Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/output/opentelemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type Config struct {
// HTTPExporterURLPath sets the target URL path the OpenTelemetry Exporter
HTTPExporterURLPath null.String `json:"httpExporterURLPath" envconfig:"K6_OTEL_HTTP_EXPORTER_URL_PATH"`

// Username is the User for Basic Auth.
HTTPUsername null.String `json:"username" envconfig:"K6_OTEL_HTTP_EXPORTER_USERNAME"`

// Password is the Password for the Basic Auth.
HTTPPassword null.String `json:"password" envconfig:"K6_OTEL_HTTP_EXPORTER_PASSWORD"`

// GRPCExporterEndpoint sets the target endpoint the OpenTelemetry Exporter
// will connect to.
GRPCExporterEndpoint null.String `json:"grpcExporterEndpoint" envconfig:"K6_OTEL_GRPC_EXPORTER_ENDPOINT"`
Expand Down Expand Up @@ -187,6 +193,14 @@ func (cfg Config) Apply(v Config) Config {
cfg.Headers = v.Headers
}

if v.HTTPUsername.Valid {
cfg.HTTPUsername = v.HTTPUsername
}

if v.HTTPPassword.Valid {
cfg.HTTPPassword = v.HTTPPassword
}

return cfg
}

Expand Down
10 changes: 9 additions & 1 deletion internal/output/opentelemetry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func TestConfig(t *testing.T) {
"K6_OTEL_TLS_CLIENT_CERTIFICATE": "client_cert_path",
"K6_OTEL_TLS_CLIENT_KEY": "client_key_path",
"K6_OTEL_HEADERS": "key1=value1,key2=value2",
"K6_OTEL_HTTP_EXPORTER_USERNAME": "foo",
"K6_OTEL_HTTP_EXPORTER_PASSWORD": "bar",
},
expectedConfig: Config{
ServiceName: null.NewString("foo", true),
Expand All @@ -86,6 +88,8 @@ func TestConfig(t *testing.T) {
TLSClientCertificate: null.NewString("client_cert_path", true),
TLSClientKey: null.NewString("client_key_path", true),
Headers: null.NewString("key1=value1,key2=value2", true),
HTTPUsername: null.NewString("foo", true),
HTTPPassword: null.NewString("bar", true),
},
},

Expand Down Expand Up @@ -124,7 +128,9 @@ func TestConfig(t *testing.T) {
`"tlsCertificate":"cert_path",` +
`"tlsClientCertificate":"client_cert_path",` +
`"tlsClientKey":"client_key_path",` +
`"headers":"key1=value1,key2=value2"` +
`"headers":"key1=value1,key2=value2",` +
`"username":"foo",` +
`"password":"bar"` +
`}`,
),
expectedConfig: Config{
Expand All @@ -143,6 +149,8 @@ func TestConfig(t *testing.T) {
TLSClientCertificate: null.NewString("client_cert_path", true),
TLSClientKey: null.NewString("client_key_path", true),
Headers: null.NewString("key1=value1,key2=value2", true),
HTTPUsername: null.NewString("foo", true),
HTTPPassword: null.NewString("bar", true),
},
},

Expand Down
7 changes: 7 additions & 0 deletions internal/output/opentelemetry/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package opentelemetry
import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -38,6 +39,12 @@ func getExporter(cfg Config) (metric.Exporter, error) {
}
}

// if at least valid user was configured, use basic auth
if cfg.HTTPUsername.Valid {
auth := []byte(cfg.HTTPUsername.String + ":" + cfg.HTTPPassword.String)
headers["Authorization"] = fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(auth))
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this require password as well and error if only one present. I guess this might need to happen earlier in the confiugraiton?


switch cfg.ExporterProtocol.String {
case grpcExporterProtocol:
return buildGRPCExporter(ctx, cfg, tlsConfig, headers)
Expand Down
Loading