Skip to content

Commit 292c6bf

Browse files
committed
influx2otel: Avoid runtime dependency on pdatautil
Collector contrib imports influx2otel from its InfluxDB receiver, while influx2otel imports pdatautil from Collector contrib. This forms a package-level dependency cycle when the repositories are packaged as complete source trees. Use a deterministic, length-delimited attribute key for the two internal lookup maps instead. Keep pdatautil as an indirect test dependency through pdatatest. Signed-off-by: HNO3Miracle <xiangao.or@isrc.iscas.ac.cn>
1 parent 5301efa commit 292c6bf

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package influx2otel
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"go.opentelemetry.io/collector/pdata/pcommon"
8+
)
9+
10+
func TestAttributeMapKey(t *testing.T) {
11+
first := pcommon.NewMap()
12+
first.PutStr("service.name", "api")
13+
first.PutStr("region", "west")
14+
15+
second := pcommon.NewMap()
16+
second.PutStr("region", "west")
17+
second.PutStr("service.name", "api")
18+
19+
require.Equal(t, attributeMapKey(first), attributeMapKey(second))
20+
21+
boundaryA := pcommon.NewMap()
22+
boundaryA.PutStr("a", "bc")
23+
boundaryB := pcommon.NewMap()
24+
boundaryB.PutStr("ab", "c")
25+
require.NotEqual(t, attributeMapKey(boundaryA), attributeMapKey(boundaryB))
26+
}

influx2otel/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.25.0
55
require (
66
github.com/influxdata/influxdb-observability/common v0.5.8
77
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.101.0
8-
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.101.0
98
github.com/stretchr/testify v1.9.0
109
go.opentelemetry.io/collector/pdata v1.8.0
1110
go.opentelemetry.io/collector/semconv v0.101.0
@@ -18,6 +17,7 @@ require (
1817
github.com/json-iterator/go v1.1.12 // indirect
1918
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2019
github.com/modern-go/reflect2 v1.0.2 // indirect
20+
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.101.0 // indirect
2121
github.com/pmezard/go-difflib v1.0.0 // indirect
2222
go.uber.org/multierr v1.11.0 // indirect
2323
golang.org/x/net v0.55.0 // indirect

influx2otel/metrics.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"fmt"
66
"math"
77
"sort"
8+
"strings"
89
"time"
910

10-
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
1111
"go.opentelemetry.io/collector/pdata/pcommon"
1212
"go.opentelemetry.io/collector/pdata/pmetric"
1313
semconv "go.opentelemetry.io/collector/semconv/v1.16.0"
@@ -27,9 +27,9 @@ func NewLineProtocolToOtelMetrics(logger common.Logger) (*LineProtocolToOtelMetr
2727

2828
func (c *LineProtocolToOtelMetrics) NewBatch() *MetricsBatch {
2929
return &MetricsBatch{
30-
rmByAttributes: make(map[[16]byte]pmetric.ResourceMetrics),
31-
ilmByRMAttributesAndIL: make(map[[16]byte]map[string]pmetric.ScopeMetrics),
32-
metricByRMIL: make(map[[16]byte]map[string]map[string]pmetric.Metric),
30+
rmByAttributes: make(map[string]pmetric.ResourceMetrics),
31+
ilmByRMAttributesAndIL: make(map[string]map[string]pmetric.ScopeMetrics),
32+
metricByRMIL: make(map[string]map[string]map[string]pmetric.Metric),
3333
histogramDataPointsByMDPK: make(map[pmetric.Metric]map[dataPointKey]pmetric.HistogramDataPoint),
3434
summaryDataPointsByMDPK: make(map[pmetric.Metric]map[dataPointKey]pmetric.SummaryDataPoint),
3535

@@ -38,9 +38,9 @@ func (c *LineProtocolToOtelMetrics) NewBatch() *MetricsBatch {
3838
}
3939

4040
type MetricsBatch struct {
41-
rmByAttributes map[[16]byte]pmetric.ResourceMetrics
42-
ilmByRMAttributesAndIL map[[16]byte]map[string]pmetric.ScopeMetrics
43-
metricByRMIL map[[16]byte]map[string]map[string]pmetric.Metric
41+
rmByAttributes map[string]pmetric.ResourceMetrics
42+
ilmByRMAttributesAndIL map[string]map[string]pmetric.ScopeMetrics
43+
metricByRMIL map[string]map[string]map[string]pmetric.Metric
4444
histogramDataPointsByMDPK map[pmetric.Metric]map[dataPointKey]pmetric.HistogramDataPoint
4545
summaryDataPointsByMDPK map[pmetric.Metric]map[dataPointKey]pmetric.SummaryDataPoint
4646

@@ -74,6 +74,23 @@ func (b *MetricsBatch) AddPoint(measurement string, tags map[string]string, fiel
7474

7575
var errValueTypeUnknown = errors.New("value type unknown")
7676

77+
func attributeMapKey(attributes pcommon.Map) string {
78+
keys := make([]string, 0, attributes.Len())
79+
attributes.Range(func(key string, _ pcommon.Value) bool {
80+
keys = append(keys, key)
81+
return true
82+
})
83+
sort.Strings(keys)
84+
85+
var key strings.Builder
86+
for _, name := range keys {
87+
value, _ := attributes.Get(name)
88+
valueText := value.AsString()
89+
fmt.Fprintf(&key, "%d:%s:%d:%d:%s", len(name), name, value.Type(), len(valueText), valueText)
90+
}
91+
return key.String()
92+
}
93+
7794
func (b *MetricsBatch) lookupMetric(metricName string, tags map[string]string, vType common.InfluxMetricValueType) (pmetric.Metric, pcommon.Map, error) {
7895
var ilName, ilVersion string
7996
rAttributes := pcommon.NewMap()
@@ -98,7 +115,7 @@ func (b *MetricsBatch) lookupMetric(metricName string, tags map[string]string, v
98115
}
99116
}
100117

101-
rKey := pdatautil.MapHash(rAttributes)
118+
rKey := attributeMapKey(rAttributes)
102119
var resourceMetrics pmetric.ResourceMetrics
103120
if rm, found := b.rmByAttributes[rKey]; found {
104121
resourceMetrics = rm

influx2otel/metrics_telegraf_prometheus_v2.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
1110
"go.opentelemetry.io/collector/pdata/pcommon"
1211

1312
"github.com/influxdata/influxdb-observability/common"
@@ -64,7 +63,7 @@ func (b *MetricsBatch) inferMetricValueTypeV2(vType common.InfluxMetricValueType
6463
type dataPointKey string
6564

6665
func newDataPointKey(ts time.Time, attributes pcommon.Map) dataPointKey {
67-
return dataPointKey(fmt.Sprintf("%d:%s", ts.UnixNano(), pdatautil.MapHash(attributes)))
66+
return dataPointKey(fmt.Sprintf("%d:%s", ts.UnixNano(), attributeMapKey(attributes)))
6867
}
6968

7069
func (b *MetricsBatch) convertGaugeV2(tags map[string]string, fields map[string]interface{}, ts time.Time) error {

0 commit comments

Comments
 (0)