Skip to content

Commit 5680e22

Browse files
authored
Merge branch 'main' into main
2 parents 9b11d06 + 7ce882e commit 5680e22

File tree

5 files changed

+81
-76
lines changed

5 files changed

+81
-76
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package elasticsearch // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/elasticsearch"
5+
6+
import (
7+
"slices"
8+
9+
"go.opentelemetry.io/collector/pdata/pcommon"
10+
)
11+
12+
const (
13+
MappingHintsAttrKey = "elasticsearch.mapping.hints"
14+
)
15+
16+
type MappingHint string
17+
18+
const (
19+
HintAggregateMetricDouble MappingHint = "aggregate_metric_double"
20+
HintDocCount MappingHint = "_doc_count"
21+
)
22+
23+
type MappingHintGetter struct {
24+
hints []MappingHint
25+
}
26+
27+
// NewMappingHintGetter creates a new MappingHintGetter
28+
func NewMappingHintGetter(attr pcommon.Map) (g MappingHintGetter) {
29+
v, ok := attr.Get(MappingHintsAttrKey)
30+
if !ok || v.Type() != pcommon.ValueTypeSlice {
31+
return
32+
}
33+
slice := v.Slice()
34+
g.hints = slices.Grow(g.hints, slice.Len())
35+
for i := range slice.Len() {
36+
g.hints = append(g.hints, MappingHint(slice.At(i).Str()))
37+
}
38+
return
39+
}
40+
41+
// HasMappingHint checks whether the getter contains the requested mapping hint
42+
func (g MappingHintGetter) HasMappingHint(hint MappingHint) bool {
43+
return slices.Contains(g.hints, hint)
44+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package elasticsearchexporter
4+
package elasticsearch
55

66
import (
77
"testing"
@@ -14,74 +14,74 @@ func TestHasHint(t *testing.T) {
1414
tests := []struct {
1515
name string
1616
attrsFunc func() pcommon.Map
17-
hint mappingHint
17+
hint MappingHint
1818
want bool
1919
}{
2020
{
2121
name: "empty map",
2222
attrsFunc: pcommon.NewMap,
23-
hint: hintAggregateMetricDouble,
23+
hint: HintAggregateMetricDouble,
2424
want: false,
2525
},
2626
{
2727
name: "bad type",
2828
attrsFunc: func() pcommon.Map {
2929
m := pcommon.NewMap()
30-
m.PutBool(mappingHintsAttrKey, true)
30+
m.PutBool(MappingHintsAttrKey, true)
3131
return m
3232
},
33-
hint: hintAggregateMetricDouble,
33+
hint: HintAggregateMetricDouble,
3434
want: false,
3535
},
3636
{
3737
name: "bad inner type",
3838
attrsFunc: func() pcommon.Map {
3939
m := pcommon.NewMap()
40-
s := m.PutEmptySlice(mappingHintsAttrKey)
40+
s := m.PutEmptySlice(MappingHintsAttrKey)
4141
s.AppendEmpty().SetBool(true)
4242
return m
4343
},
44-
hint: hintAggregateMetricDouble,
44+
hint: HintAggregateMetricDouble,
4545
want: false,
4646
},
4747
{
4848
name: "hit",
4949
attrsFunc: func() pcommon.Map {
5050
m := pcommon.NewMap()
51-
s := m.PutEmptySlice(mappingHintsAttrKey)
52-
s.AppendEmpty().SetStr(string(hintAggregateMetricDouble))
51+
s := m.PutEmptySlice(MappingHintsAttrKey)
52+
s.AppendEmpty().SetStr(string(HintAggregateMetricDouble))
5353
return m
5454
},
55-
hint: hintAggregateMetricDouble,
55+
hint: HintAggregateMetricDouble,
5656
want: true,
5757
},
5858
{
5959
name: "hit 2nd",
6060
attrsFunc: func() pcommon.Map {
6161
m := pcommon.NewMap()
62-
s := m.PutEmptySlice(mappingHintsAttrKey)
63-
s.AppendEmpty().SetStr(string(hintDocCount))
64-
s.AppendEmpty().SetStr(string(hintAggregateMetricDouble))
62+
s := m.PutEmptySlice(MappingHintsAttrKey)
63+
s.AppendEmpty().SetStr(string(HintDocCount))
64+
s.AppendEmpty().SetStr(string(HintAggregateMetricDouble))
6565
return m
6666
},
67-
hint: hintAggregateMetricDouble,
67+
hint: HintAggregateMetricDouble,
6868
want: true,
6969
},
7070
{
7171
name: "miss",
7272
attrsFunc: func() pcommon.Map {
7373
m := pcommon.NewMap()
74-
s := m.PutEmptySlice(mappingHintsAttrKey)
75-
s.AppendEmpty().SetStr(string(hintDocCount))
74+
s := m.PutEmptySlice(MappingHintsAttrKey)
75+
s.AppendEmpty().SetStr(string(HintDocCount))
7676
return m
7777
},
78-
hint: hintAggregateMetricDouble,
78+
hint: HintAggregateMetricDouble,
7979
want: false,
8080
},
8181
}
8282
for _, tt := range tests {
8383
t.Run(tt.name, func(t *testing.T) {
84-
assert.Equal(t, tt.want, newMappingHintGetter(tt.attrsFunc()).HasMappingHint(tt.hint))
84+
assert.Equal(t, tt.want, NewMappingHintGetter(tt.attrsFunc()).HasMappingHint(tt.hint))
8585
})
8686
}
8787
}

exporter/elasticsearchexporter/mapping_hint.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

exporter/elasticsearchexporter/model.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"go.opentelemetry.io/collector/pdata/ptrace"
2222
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
2323

24+
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/elasticsearch"
2425
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/exphistogram"
2526
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/objmodel"
2627
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
@@ -101,7 +102,7 @@ type dataPoint interface {
101102
Value() (pcommon.Value, error)
102103
DynamicTemplate(pmetric.Metric) string
103104
DocCount() uint64
104-
HasMappingHint(mappingHint) bool
105+
HasMappingHint(elasticsearch.MappingHint) bool
105106
Metric() pmetric.Metric
106107
}
107108

@@ -263,12 +264,12 @@ func (m *encodeModel) encodeMetrics(resource pcommon.Resource, resourceSchemaURL
263264

264265
type summaryDataPoint struct {
265266
pmetric.SummaryDataPoint
266-
mappingHintGetter
267+
elasticsearch.MappingHintGetter
267268
metric pmetric.Metric
268269
}
269270

270271
func newSummaryDataPoint(metric pmetric.Metric, dp pmetric.SummaryDataPoint) summaryDataPoint {
271-
return summaryDataPoint{SummaryDataPoint: dp, mappingHintGetter: newMappingHintGetter(dp.Attributes()), metric: metric}
272+
return summaryDataPoint{SummaryDataPoint: dp, MappingHintGetter: elasticsearch.NewMappingHintGetter(dp.Attributes()), metric: metric}
272273
}
273274

274275
func (dp summaryDataPoint) Value() (pcommon.Value, error) {
@@ -295,16 +296,16 @@ func (dp summaryDataPoint) Metric() pmetric.Metric {
295296

296297
type exponentialHistogramDataPoint struct {
297298
pmetric.ExponentialHistogramDataPoint
298-
mappingHintGetter
299+
elasticsearch.MappingHintGetter
299300
metric pmetric.Metric
300301
}
301302

302303
func newExponentialHistogramDataPoint(metric pmetric.Metric, dp pmetric.ExponentialHistogramDataPoint) exponentialHistogramDataPoint {
303-
return exponentialHistogramDataPoint{ExponentialHistogramDataPoint: dp, mappingHintGetter: newMappingHintGetter(dp.Attributes()), metric: metric}
304+
return exponentialHistogramDataPoint{ExponentialHistogramDataPoint: dp, MappingHintGetter: elasticsearch.NewMappingHintGetter(dp.Attributes()), metric: metric}
304305
}
305306

306307
func (dp exponentialHistogramDataPoint) Value() (pcommon.Value, error) {
307-
if dp.HasMappingHint(hintAggregateMetricDouble) {
308+
if dp.HasMappingHint(elasticsearch.HintAggregateMetricDouble) {
308309
vm := pcommon.NewValueMap()
309310
m := vm.Map()
310311
m.PutDouble("sum", dp.Sum())
@@ -331,7 +332,7 @@ func (dp exponentialHistogramDataPoint) Value() (pcommon.Value, error) {
331332
}
332333

333334
func (dp exponentialHistogramDataPoint) DynamicTemplate(_ pmetric.Metric) string {
334-
if dp.HasMappingHint(hintAggregateMetricDouble) {
335+
if dp.HasMappingHint(elasticsearch.HintAggregateMetricDouble) {
335336
return "summary"
336337
}
337338
return "histogram"
@@ -347,16 +348,16 @@ func (dp exponentialHistogramDataPoint) Metric() pmetric.Metric {
347348

348349
type histogramDataPoint struct {
349350
pmetric.HistogramDataPoint
350-
mappingHintGetter
351+
elasticsearch.MappingHintGetter
351352
metric pmetric.Metric
352353
}
353354

354355
func newHistogramDataPoint(metric pmetric.Metric, dp pmetric.HistogramDataPoint) histogramDataPoint {
355-
return histogramDataPoint{HistogramDataPoint: dp, mappingHintGetter: newMappingHintGetter(dp.Attributes()), metric: metric}
356+
return histogramDataPoint{HistogramDataPoint: dp, MappingHintGetter: elasticsearch.NewMappingHintGetter(dp.Attributes()), metric: metric}
356357
}
357358

358359
func (dp histogramDataPoint) Value() (pcommon.Value, error) {
359-
if dp.HasMappingHint(hintAggregateMetricDouble) {
360+
if dp.HasMappingHint(elasticsearch.HintAggregateMetricDouble) {
360361
vm := pcommon.NewValueMap()
361362
m := vm.Map()
362363
m.PutDouble("sum", dp.Sum())
@@ -367,7 +368,7 @@ func (dp histogramDataPoint) Value() (pcommon.Value, error) {
367368
}
368369

369370
func (dp histogramDataPoint) DynamicTemplate(_ pmetric.Metric) string {
370-
if dp.HasMappingHint(hintAggregateMetricDouble) {
371+
if dp.HasMappingHint(elasticsearch.HintAggregateMetricDouble) {
371372
return "summary"
372373
}
373374
return "histogram"
@@ -431,12 +432,12 @@ func histogramToValue(dp pmetric.HistogramDataPoint) (pcommon.Value, error) {
431432

432433
type numberDataPoint struct {
433434
pmetric.NumberDataPoint
434-
mappingHintGetter
435+
elasticsearch.MappingHintGetter
435436
metric pmetric.Metric
436437
}
437438

438439
func newNumberDataPoint(metric pmetric.Metric, dp pmetric.NumberDataPoint) numberDataPoint {
439-
return numberDataPoint{NumberDataPoint: dp, mappingHintGetter: newMappingHintGetter(dp.Attributes()), metric: metric}
440+
return numberDataPoint{NumberDataPoint: dp, MappingHintGetter: elasticsearch.NewMappingHintGetter(dp.Attributes()), metric: metric}
440441
}
441442

442443
func (dp numberDataPoint) Value() (pcommon.Value, error) {
@@ -724,7 +725,7 @@ func metricOTelHash(dp dataPoint, unit string) uint32 {
724725

725726
hasher.Write([]byte(unit))
726727

727-
mapHashExcludeReservedAttrs(hasher, dp.Attributes(), mappingHintsAttrKey)
728+
mapHashExcludeReservedAttrs(hasher, dp.Attributes(), elasticsearch.MappingHintsAttrKey)
728729

729730
return hasher.Sum32()
730731
}

exporter/elasticsearchexporter/pdata_serializer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"go.opentelemetry.io/collector/pdata/pcommon"
1616
"go.opentelemetry.io/collector/pdata/plog"
1717
"go.opentelemetry.io/collector/pdata/ptrace"
18+
19+
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/elasticsearch"
1820
)
1921

2022
const tsLayout = "2006-01-02T15:04:05.000000000Z"
@@ -68,7 +70,7 @@ func serializeDataPoints(v *json.Visitor, dataPoints []dataPoint, validationErro
6870
// TODO here's potential for more optimization by directly serializing the value instead of allocating a pcommon.Value
6971
// the tradeoff is that this would imply a duplicated logic for the ECS mode
7072
value, err := dp.Value()
71-
if dp.HasMappingHint(hintDocCount) {
73+
if dp.HasMappingHint(elasticsearch.HintDocCount) {
7274
docCount = dp.DocCount()
7375
}
7476
if err != nil {
@@ -296,7 +298,7 @@ func writeAttributes(v *json.Visitor, attributes pcommon.Map, stringifyMapValues
296298
_ = v.OnObjectStart(-1, structform.AnyType)
297299
attributes.Range(func(k string, val pcommon.Value) bool {
298300
switch k {
299-
case dataStreamType, dataStreamDataset, dataStreamNamespace, mappingHintsAttrKey:
301+
case dataStreamType, dataStreamDataset, dataStreamNamespace, elasticsearch.MappingHintsAttrKey:
300302
return true
301303
}
302304
if isGeoAttribute(k, val) {

0 commit comments

Comments
 (0)