Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5261989
adding first changes for metadata
gizas Nov 6, 2025
5b74827
comment
gizas Nov 6, 2025
5d3f323
adding tests
gizas Nov 6, 2025
32f7498
mod tidy and cahngelog
gizas Nov 6, 2025
fbeef6b
mod tidy and cahngelog
gizas Nov 6, 2025
00a18f3
format and adding tests for coverage
gizas Nov 7, 2025
e07c0aa
adding metadata keys in xexporterhelper.WithQueueBatch()
gizas Nov 20, 2025
1178536
merging with main
gizas Nov 20, 2025
331a703
update changelog
gizas Nov 20, 2025
5b40717
adding metadata tests
gizas Nov 20, 2025
a89aef2
updating test for xexporterhelper
gizas Nov 20, 2025
f74f3a4
updating test for xexporterhelper
gizas Nov 20, 2025
f4b629f
updating base_exporter and add checks for Partitioner and Mergectx
gizas Nov 24, 2025
c290ef6
Merge branch 'main' into otlphttp_mergge_metadata
gizas Nov 24, 2025
77be9c9
updating test for base_exporter
gizas Nov 24, 2025
0836ef7
updating with exporterhelper.WithQueueBatch
gizas Nov 26, 2025
c7746e5
Merge branch 'main' into otlphttp_mergge_metadata
gizas Nov 26, 2025
759ac81
removing exporterhelper.WithQueueBatch
gizas Nov 26, 2025
d233ec4
Merge branch 'otlphttp_mergge_metadata' of github.com:gizas/opentelem…
gizas Nov 26, 2025
bafdd11
updating chloggen message and kept only exporterhelper
gizas Nov 26, 2025
e197735
updating chloggen message and kept only exporterhelper
gizas Nov 26, 2025
5c4559b
Merge branch 'main' into otlphttp_mergge_metadata
gizas Nov 26, 2025
8cb459d
removing duplicate xexporterhelper/new_request_test.go test file
gizas Nov 26, 2025
8b766a3
gotidy
gizas Nov 26, 2025
513a1fd
Update exporter/exporterhelper/internal/queuebatch/config_test.go
gizas Nov 28, 2025
5502a54
Merge branch 'main' into otlphttp_mergge_metadata
gizas Nov 28, 2025
a341256
combine tests
gizas Nov 28, 2025
2fd73f9
updating description
gizas Nov 28, 2025
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
25 changes: 25 additions & 0 deletions .chloggen/otlphttp_mergge_metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: exporter/otlphttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Populate metadata into the OTLP HTTP exporter context

# One or more tracking issues or pull requests related to the change
issues: [14139]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
3 changes: 3 additions & 0 deletions exporter/otlphttpexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type Config struct {

// The encoding to export telemetry (default: "proto")
Encoding EncodingType `mapstructure:"encoding"`

// MetadataKeys is a list of metadata keys that will be used to partition the datainto batches if sending_queue is enabled
MetadataKeys []string `mapstructure:"metadata_keys"`
}

var _ component.Config = (*Config)(nil)
Expand Down
27 changes: 24 additions & 3 deletions exporter/otlphttpexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,21 @@ func createTraces(
return nil, err
}

qbs := xexporterhelper.NewTracesQueueBatchSettings()
if len(oCfg.MetadataKeys) > 0 {
partitioner := metadataKeysPartitioner{keys: oCfg.MetadataKeys}
qbs.Partitioner = partitioner
qbs.MergeCtx = partitioner.MergeCtx
}

return exporterhelper.NewTraces(ctx, set, cfg,
oce.pushTraces,
exporterhelper.WithStart(oce.start),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
// explicitly disable since we rely on http.Client timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutConfig{Timeout: 0}),
exporterhelper.WithRetry(oCfg.RetryConfig),
exporterhelper.WithQueue(oCfg.QueueConfig))
xexporterhelper.WithQueueBatch(oCfg.QueueConfig, qbs))
}

func createMetrics(
Expand All @@ -115,14 +122,21 @@ func createMetrics(
return nil, err
}

qbs := xexporterhelper.NewMetricsQueueBatchSettings()
if len(oCfg.MetadataKeys) > 0 {
partitioner := metadataKeysPartitioner{keys: oCfg.MetadataKeys}
qbs.Partitioner = partitioner
qbs.MergeCtx = partitioner.MergeCtx
}

return exporterhelper.NewMetrics(ctx, set, cfg,
oce.pushMetrics,
exporterhelper.WithStart(oce.start),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
// explicitly disable since we rely on http.Client timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutConfig{Timeout: 0}),
exporterhelper.WithRetry(oCfg.RetryConfig),
exporterhelper.WithQueue(oCfg.QueueConfig))
xexporterhelper.WithQueueBatch(oCfg.QueueConfig, qbs))
}

func createLogs(
Expand All @@ -140,14 +154,21 @@ func createLogs(
return nil, err
}

qbs := xexporterhelper.NewLogsQueueBatchSettings()
if len(oCfg.MetadataKeys) > 0 {
partitioner := metadataKeysPartitioner{keys: oCfg.MetadataKeys}
qbs.Partitioner = partitioner
qbs.MergeCtx = partitioner.MergeCtx
}

return exporterhelper.NewLogs(ctx, set, cfg,
oce.pushLogs,
exporterhelper.WithStart(oce.start),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
// explicitly disable since we rely on http.Client timeout logic.
exporterhelper.WithTimeout(exporterhelper.TimeoutConfig{Timeout: 0}),
exporterhelper.WithRetry(oCfg.RetryConfig),
exporterhelper.WithQueue(oCfg.QueueConfig))
xexporterhelper.WithQueueBatch(oCfg.QueueConfig, qbs))
}

func createProfiles(
Expand Down
29 changes: 29 additions & 0 deletions exporter/otlphttpexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/exporter/xexporter"
"go.opentelemetry.io/collector/internal/testutil"
Expand Down Expand Up @@ -49,6 +50,16 @@ func TestCreateMetrics(t *testing.T) {
oexp, err := factory.CreateMetrics(context.Background(), set, cfg)
require.NoError(t, err)
require.NotNil(t, oexp)

// Test with MetadataKeys and sending_queue enabled to cover partitioner creation
cfgWithKeys := factory.CreateDefaultConfig().(*Config)
cfgWithKeys.ClientConfig.Endpoint = "http://" + testutil.GetAvailableLocalAddress(t)
cfgWithKeys.MetadataKeys = []string{"key1", "key2"}
cfgWithKeys.QueueConfig.Enabled = true

oexpWithKeys, err := factory.CreateMetrics(context.Background(), set, cfgWithKeys)
require.NoError(t, err)
require.NotNil(t, oexpWithKeys)
}

func clientConfig(endpoint string, headers configopaque.MapList, tlsSetting configtls.ClientConfig, compression configcompression.Type) confighttp.ClientConfig {
Expand Down Expand Up @@ -159,6 +170,14 @@ func TestCreateTraces(t *testing.T) {
ClientConfig: clientConfig(endpoint, nil, configtls.ClientConfig{}, configCompression),
},
},
{
name: "WithMetadataKeys",
config: &Config{
ClientConfig: clientConfig(endpoint, nil, configtls.ClientConfig{}, configCompression),
MetadataKeys: []string{"key1", "key2"},
QueueConfig: exporterhelper.NewDefaultQueueConfig(),
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -197,6 +216,16 @@ func TestCreateLogs(t *testing.T) {
oexp, err := factory.CreateLogs(context.Background(), set, cfg)
require.NoError(t, err)
require.NotNil(t, oexp)

// Test with MetadataKeys and sending_queue enabled to cover partitioner creation
cfgWithKeys := factory.CreateDefaultConfig().(*Config)
cfgWithKeys.ClientConfig.Endpoint = "http://" + testutil.GetAvailableLocalAddress(t)
cfgWithKeys.MetadataKeys = []string{"key1", "key2"}
cfgWithKeys.QueueConfig.Enabled = true

oexpWithKeys, err := factory.CreateLogs(context.Background(), set, cfgWithKeys)
require.NoError(t, err)
require.NotNil(t, oexpWithKeys)
}

func TestCreateProfiles(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exporter/otlphttpexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.24.0
require (
github.com/stretchr/testify v1.11.1
go.opentelemetry.io/collector v0.139.0
go.opentelemetry.io/collector/client v1.45.0
go.opentelemetry.io/collector/component v1.45.0
go.opentelemetry.io/collector/component/componenttest v0.139.0
go.opentelemetry.io/collector/config/configcompression v1.45.0
Expand Down Expand Up @@ -58,7 +59,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.11.1 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/client v1.45.0 // indirect
go.opentelemetry.io/collector/config/configauth v1.45.0 // indirect
go.opentelemetry.io/collector/config/configmiddleware v1.45.0 // indirect
go.opentelemetry.io/collector/consumer/consumererror/xconsumererror v0.139.0 // indirect
Expand Down
72 changes: 72 additions & 0 deletions exporter/otlphttpexporter/partitioner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otlphttpexporter // import "go.opentelemetry.io/collector/exporter/otlphttpexporter"

import (
"bytes"
"context"
"fmt"
"slices"

"go.opentelemetry.io/collector/client"
"go.opentelemetry.io/collector/exporter/exporterhelper/xexporterhelper"
)

type metadataKeysPartitioner struct {
keys []string
}

func (p metadataKeysPartitioner) GetKey(
ctx context.Context,
_ xexporterhelper.Request,
) string {
var kb bytes.Buffer
meta := client.FromContext(ctx).Metadata

var afterFirst bool
for _, k := range p.keys {
if values := meta.Get(k); len(values) != 0 {
if afterFirst {
kb.WriteByte(0)
}
kb.WriteString(k)
afterFirst = true
for _, val := range values {
kb.WriteByte(0)
kb.WriteString(val)
}
}
}
return kb.String()
}

func (p metadataKeysPartitioner) MergeCtx(
ctx1, ctx2 context.Context,
) context.Context {
m1 := client.FromContext(ctx1).Metadata
m2 := client.FromContext(ctx2).Metadata

m := make(map[string][]string, len(p.keys))
for _, key := range p.keys {
v1 := m1.Get(key)
v2 := m2.Get(key)
if len(v1) == 0 && len(v2) == 0 {
continue
}

// Since the mergeCtx is based on partition key, we MUST have the same
// partition key-values in both the metadata. If they are not same then
// fail fast and dramatically.
if !slices.Equal(v1, v2) {
panic(fmt.Errorf(
"unexpected client metadata found when merging context for key %s", key,
))
}
m[key] = v1
}
return client.NewContext(
context.Background(),
client.Info{Metadata: client.NewMetadata(m)},
)
}
Loading
Loading