Skip to content

Commit 5c9c85a

Browse files
committed
Fix receiver based routing overwriting data_stream.dataset
1 parent d78af32 commit 5c9c85a

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

.chloggen/elasticsearchexporter_dynamic-routing-default.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ subtext:
2020
Deprecate and make `{logs,metrics,traces}_dynamic_index` config no-op.
2121
Config validation error on `{logs,metrics,traces}_dynamic_index::enabled` and `{logs,metrics,traces}_index` set at the same time, as users who rely on dynamic index should not set `{logs,metrics,traces}_index`.
2222
Remove `elasticsearch.index.{prefix,suffix}` handling. Replace it with `elasticsearch.index` handling that uses attribute value as index directly. Users rely on the previously supported `elasticsearch.index.prefix` and `elasticsearch.index.suffix` should migrate to a transform processor that sets `elasticsearch.index`.
23+
Fix a bug where receiver-based routing overwrites data_stream.dataset.
2324

2425
# If your change doesn't affect end users or the exported elements of any package,
2526
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

exporter/elasticsearchexporter/data_stream_router.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func routeRecord(
166166
return elasticsearch.Index{Index: esIndex}, nil
167167
}
168168

169-
dataset, _ := getFromAttributes(elasticsearch.DataStreamDataset, defaultDataStreamDataset, recordAttr, scopeAttr, resourceAttr)
169+
dataset, datasetExists := getFromAttributes(elasticsearch.DataStreamDataset, defaultDataStreamDataset, recordAttr, scopeAttr, resourceAttr)
170170
namespace, _ := getFromAttributes(elasticsearch.DataStreamNamespace, defaultDataStreamNamespace, recordAttr, scopeAttr, resourceAttr)
171171

172172
dsType := defaultDSType
@@ -178,13 +178,16 @@ func routeRecord(
178178
}
179179
}
180180

181-
// Receiver-based routing
182-
// For example, hostmetricsreceiver (or hostmetricsreceiver.otel in the OTel output mode)
183-
// for the scope name
184-
// github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper
185-
if submatch := receiverRegex.FindStringSubmatch(scope.Name()); len(submatch) > 0 {
186-
receiverName := submatch[1]
187-
dataset = receiverName
181+
// Only use receiver-based routing if dataset is not specified.
182+
if !datasetExists {
183+
// Receiver-based routing
184+
// For example, hostmetricsreceiver (or hostmetricsreceiver.otel in the OTel output mode)
185+
// for the scope name
186+
// github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper
187+
if submatch := receiverRegex.FindStringSubmatch(scope.Name()); len(submatch) > 0 {
188+
receiverName := submatch[1]
189+
dataset = receiverName
190+
}
188191
}
189192

190193
// For dataset, the naming convention for datastream is expected to be "logs-[dataset].otel-[namespace]".

exporter/elasticsearchexporter/data_stream_router_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ func createRouteTests(dsType string) []routeTestCase {
6565
want: renderWantRoute(dsType, defaultDataStreamDataset, defaultDataStreamNamespace, MappingOTel),
6666
},
6767
{
68-
name: "otel with elasticsearch.index",
69-
mode: MappingOTel,
68+
name: "otel with elasticsearch.index",
69+
mode: MappingOTel,
70+
scopeName: "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/should/be/ignored",
7071
recordAttrs: map[string]any{
7172
"elasticsearch.index": "my-index",
7273
},
@@ -75,8 +76,9 @@ func createRouteTests(dsType string) []routeTestCase {
7576
},
7677
},
7778
{
78-
name: "otel with data_stream attrs",
79-
mode: MappingOTel,
79+
name: "otel with data_stream attrs",
80+
mode: MappingOTel,
81+
scopeName: "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/should/be/ignored",
8082
recordAttrs: map[string]any{
8183
"data_stream.dataset": "foo",
8284
"data_stream.namespace": "bar",

0 commit comments

Comments
 (0)