Skip to content
Open
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
26 changes: 26 additions & 0 deletions influx2otel/attribute_map_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package influx2otel

import (
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
)

func TestAttributeMapKey(t *testing.T) {
first := pcommon.NewMap()
first.PutStr("service.name", "api")
first.PutStr("region", "west")

second := pcommon.NewMap()
second.PutStr("region", "west")
second.PutStr("service.name", "api")

require.Equal(t, attributeMapKey(first), attributeMapKey(second))

boundaryA := pcommon.NewMap()
boundaryA.PutStr("a", "bc")
boundaryB := pcommon.NewMap()
boundaryB.PutStr("ab", "c")
require.NotEqual(t, attributeMapKey(boundaryA), attributeMapKey(boundaryB))
}
2 changes: 1 addition & 1 deletion influx2otel/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.25.0
require (
github.com/influxdata/influxdb-observability/common v0.5.8
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.101.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.101.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/pdata v1.8.0
go.opentelemetry.io/collector/semconv v0.101.0
Expand All @@ -18,6 +17,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.101.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.55.0 // indirect
Expand Down
33 changes: 25 additions & 8 deletions influx2otel/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"math"
"sort"
"strings"
"time"

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

func (c *LineProtocolToOtelMetrics) NewBatch() *MetricsBatch {
return &MetricsBatch{
rmByAttributes: make(map[[16]byte]pmetric.ResourceMetrics),
ilmByRMAttributesAndIL: make(map[[16]byte]map[string]pmetric.ScopeMetrics),
metricByRMIL: make(map[[16]byte]map[string]map[string]pmetric.Metric),
rmByAttributes: make(map[string]pmetric.ResourceMetrics),
ilmByRMAttributesAndIL: make(map[string]map[string]pmetric.ScopeMetrics),
metricByRMIL: make(map[string]map[string]map[string]pmetric.Metric),
histogramDataPointsByMDPK: make(map[pmetric.Metric]map[dataPointKey]pmetric.HistogramDataPoint),
summaryDataPointsByMDPK: make(map[pmetric.Metric]map[dataPointKey]pmetric.SummaryDataPoint),

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

type MetricsBatch struct {
rmByAttributes map[[16]byte]pmetric.ResourceMetrics
ilmByRMAttributesAndIL map[[16]byte]map[string]pmetric.ScopeMetrics
metricByRMIL map[[16]byte]map[string]map[string]pmetric.Metric
rmByAttributes map[string]pmetric.ResourceMetrics
ilmByRMAttributesAndIL map[string]map[string]pmetric.ScopeMetrics
metricByRMIL map[string]map[string]map[string]pmetric.Metric
histogramDataPointsByMDPK map[pmetric.Metric]map[dataPointKey]pmetric.HistogramDataPoint
summaryDataPointsByMDPK map[pmetric.Metric]map[dataPointKey]pmetric.SummaryDataPoint

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

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

func attributeMapKey(attributes pcommon.Map) string {
keys := make([]string, 0, attributes.Len())
attributes.Range(func(key string, _ pcommon.Value) bool {
keys = append(keys, key)
return true
})
sort.Strings(keys)

var key strings.Builder
for _, name := range keys {
value, _ := attributes.Get(name)
valueText := value.AsString()
fmt.Fprintf(&key, "%d:%s:%d:%d:%s", len(name), name, value.Type(), len(valueText), valueText)
}
return key.String()
}

func (b *MetricsBatch) lookupMetric(metricName string, tags map[string]string, vType common.InfluxMetricValueType) (pmetric.Metric, pcommon.Map, error) {
var ilName, ilVersion string
rAttributes := pcommon.NewMap()
Expand All @@ -98,7 +115,7 @@ func (b *MetricsBatch) lookupMetric(metricName string, tags map[string]string, v
}
}

rKey := pdatautil.MapHash(rAttributes)
rKey := attributeMapKey(rAttributes)
var resourceMetrics pmetric.ResourceMetrics
if rm, found := b.rmByAttributes[rKey]; found {
resourceMetrics = rm
Expand Down
3 changes: 1 addition & 2 deletions influx2otel/metrics_telegraf_prometheus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"time"

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

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

func newDataPointKey(ts time.Time, attributes pcommon.Map) dataPointKey {
return dataPointKey(fmt.Sprintf("%d:%s", ts.UnixNano(), pdatautil.MapHash(attributes)))
return dataPointKey(fmt.Sprintf("%d:%s", ts.UnixNano(), attributeMapKey(attributes)))
}

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