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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The next release will require at least [Go 1.23].
- Support for the `OTEL_HTTP_CLIENT_COMPATIBILITY_MODE=http/dup` environment variable in `instrumentation/github.com/emicklei/go-restful/otelrestful` to emit attributes for both the v1.20.0 and v1.26.0 semantic conventions. (#6710)
- Added metrics support, and emit all stable metrics from the [Semantic Conventions](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-metrics.md) in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#6747)
- Support [Go 1.24]. (#6765)
- Add support for configuring `HeadersList` field for OTLP exporters in `go.opentelemetry.io/contrib/config`. (#6657)

### Changed

Expand Down
35 changes: 25 additions & 10 deletions config/v0.3.0/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"gopkg.in/yaml.v3"

"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/log"
nooplog "go.opentelemetry.io/otel/log/noop"
"go.opentelemetry.io/otel/metric"
Expand Down Expand Up @@ -152,16 +153,6 @@ func ParseYAML(file []byte) (*OpenTelemetryConfiguration, error) {
return &cfg, nil
}

func toStringMap(pairs []NameStringValuePair) map[string]string {
output := make(map[string]string)
for _, v := range pairs {
if v.Value != nil && len(v.Name) > 0 {
output[v.Name] = *v.Value
}
}
return output
}

// createTLSConfig creates a tls.Config from certificate files.
func createTLSConfig(caCertFile *string, clientCertFile *string, clientKeyFile *string) (*tls.Config, error) {
tlsConfig := &tls.Config{}
Expand All @@ -188,3 +179,27 @@ func createTLSConfig(caCertFile *string, clientCertFile *string, clientKeyFile *
}
return tlsConfig, nil
}

// createHeadersConfig combines the two header config fields. Headers take precedence over headersList.
func createHeadersConfig(headers []NameStringValuePair, headersList *string) (map[string]string, error) {
result := make(map[string]string)
if headersList != nil {
// Parsing follows https://github.com/open-telemetry/opentelemetry-configuration/blob/568e5080816d40d75792eb754fc96bde09654159/schema/type_descriptions.yaml#L584.
headerslist, err := baggage.Parse(*headersList)
Comment thread
pellared marked this conversation as resolved.
if err != nil {
return nil, fmt.Errorf("invalid headers list: %w", err)
}
for _, kv := range headerslist.Members() {
result[kv.Key()] = kv.Value()
}
}
// Headers take precedence over HeadersList, so this has to be after HeadersList is processed
if len(headers) > 0 {
for _, kv := range headers {
if kv.Value != nil {
result[kv.Name] = *kv.Value
}
}
}
return result, nil
}
95 changes: 91 additions & 4 deletions config/v0.3.0/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,97 @@ func TestCreateTLSConfig(t *testing.T) {
}
}

func TestToStringMap(t *testing.T) {
require.Equal(t, map[string]string{}, toStringMap([]NameStringValuePair{}))
require.Equal(t, map[string]string{}, toStringMap([]NameStringValuePair{{Name: "test"}}))
require.Equal(t, map[string]string{}, toStringMap([]NameStringValuePair{{Value: ptr("test")}}))
func TestCreateHeadersConfig(t *testing.T) {
tests := []struct {
name string
headers []NameStringValuePair
headersList *string
wantHeaders map[string]string
wantErr string
}{
{
name: "no headers",
headers: []NameStringValuePair{},
headersList: nil,
wantHeaders: map[string]string{},
},
{
name: "headerslist only",
headers: []NameStringValuePair{},
headersList: ptr("a=b,c=d"),
wantHeaders: map[string]string{
"a": "b",
"c": "d",
},
},
{
name: "headers only",
headers: []NameStringValuePair{
{
Name: "a",
Value: ptr("b"),
},
{
Name: "c",
Value: ptr("d"),
},
},
headersList: nil,
wantHeaders: map[string]string{
"a": "b",
"c": "d",
},
},
{
name: "both headers and headerslist",
headers: []NameStringValuePair{
{
Name: "a",
Value: ptr("b"),
},
},
headersList: ptr("c=d"),
wantHeaders: map[string]string{
"a": "b",
"c": "d",
},
},
{
name: "headers supersedes headerslist",
headers: []NameStringValuePair{
{
Name: "a",
Value: ptr("b"),
},
{
Name: "c",
Value: ptr("override"),
},
},
headersList: ptr("c=d"),
wantHeaders: map[string]string{
"a": "b",
"c": "override",
},
},
{
name: "invalid headerslist",
headersList: ptr("==="),
wantErr: "invalid headers list: invalid key: \"\"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
headersMap, err := createHeadersConfig(tt.headers, tt.headersList)
if tt.wantErr != "" {
require.Error(t, err)
require.Equal(t, tt.wantErr, err.Error())
} else {
require.NoError(t, err)
}
require.Equal(t, tt.wantHeaders, headersMap)
})
}
}

func ptr[T any](v T) *T {
Expand Down
16 changes: 12 additions & 4 deletions config/v0.3.0/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ func otlpHTTPLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 {
opts = append(opts, otlploghttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout)))
}
if len(otlpConfig.Headers) > 0 {
opts = append(opts, otlploghttp.WithHeaders(toStringMap(otlpConfig.Headers)))
headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList)
if err != nil {
return nil, err
}
if len(headersConfig) > 0 {
opts = append(opts, otlploghttp.WithHeaders(headersConfig))
}

tlsConfig, err := createTLSConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
Expand Down Expand Up @@ -200,8 +204,12 @@ func otlpGRPCLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 {
opts = append(opts, otlploggrpc.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout)))
}
if len(otlpConfig.Headers) > 0 {
opts = append(opts, otlploggrpc.WithHeaders(toStringMap(otlpConfig.Headers)))
headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList)
if err != nil {
return nil, err
}
if len(headersConfig) > 0 {
opts = append(opts, otlploggrpc.WithHeaders(headersConfig))
}

tlsConfig, err := createTLSConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
Expand Down
Loading