Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/datadogexporter] Take hostname into account for cache #6223

Merged
merged 3 commits into from
Dec 2, 2021
Merged
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
99 changes: 99 additions & 0 deletions exporter/datadogexporter/internal/translator/dimensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package translator

import (
"fmt"
"sort"
"strings"

"go.opentelemetry.io/collector/model/pdata"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/translator/utils"
)

const (
dimensionSeparator = string(byte(0))
)

type metricsDimensions struct {
name string
tags []string
host string
}

// getTags maps an attributeMap into a slice of Datadog tags
func getTags(labels pdata.AttributeMap) []string {
tags := make([]string, 0, labels.Len())
labels.Range(func(key string, value pdata.AttributeValue) bool {
v := value.AsString()
tags = append(tags, utils.FormatKeyValueTag(key, v))
return true
})
return tags
}

// AddTags to metrics dimensions.
func (m *metricsDimensions) AddTags(tags ...string) metricsDimensions {
// defensively copy the tags
newTags := make([]string, 0, len(tags)+len(m.tags))
newTags = append(newTags, tags...)
newTags = append(newTags, m.tags...)
return metricsDimensions{
name: m.name,
tags: newTags,
host: m.host,
}
}

// WithAttributeMap creates a new metricDimensions struct with additional tags from attributes.
func (m *metricsDimensions) WithAttributeMap(labels pdata.AttributeMap) metricsDimensions {
return m.AddTags(getTags(labels)...)
}

// WithSuffix creates a new dimensions struct with an extra name suffix.
func (m *metricsDimensions) WithSuffix(suffix string) metricsDimensions {
return metricsDimensions{
name: fmt.Sprintf("%s.%s", m.name, suffix),
host: m.host,
tags: m.tags,
}
}

// Uses a logic similar to what is done in the span processor to build metric keys:
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/b2327211df976e0a57ef0425493448988772a16b/processor/spanmetricsprocessor/processor.go#L353-L387
// TODO: make this a public util function?
func concatDimensionValue(metricKeyBuilder *strings.Builder, value string) {
metricKeyBuilder.WriteString(value)
metricKeyBuilder.WriteString(dimensionSeparator)
}

// String maps dimensions to a string to use as an identifier.
// The tags order does not matter.
func (m *metricsDimensions) String() string {
var metricKeyBuilder strings.Builder

dimensions := make([]string, len(m.tags))
copy(dimensions, m.tags)

dimensions = append(dimensions, fmt.Sprintf("name:%s", m.name))
dimensions = append(dimensions, fmt.Sprintf("host:%s", m.host))
sort.Strings(dimensions)

for _, dim := range dimensions {
concatDimensionValue(&metricKeyBuilder, dim)
}
return metricKeyBuilder.String()
}
100 changes: 100 additions & 0 deletions exporter/datadogexporter/internal/translator/dimensions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package translator

import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/model/pdata"
)

func TestWithAttributeMap(t *testing.T) {
attributes := pdata.NewAttributeMapFromMap(map[string]pdata.AttributeValue{
"key1": pdata.NewAttributeValueString("val1"),
"key2": pdata.NewAttributeValueString("val2"),
"key3": pdata.NewAttributeValueString(""),
})

dims := metricsDimensions{}
assert.ElementsMatch(t,
dims.WithAttributeMap(attributes).tags,
[...]string{"key1:val1", "key2:val2", "key3:n/a"},
)
}

func TestMetricDimensionsString(t *testing.T) {
getKey := func(name string, tags []string, host string) string {
dims := metricsDimensions{name: name, tags: tags, host: host}
return dims.String()
}
metricName := "metric.name"
hostOne := "host-one"
hostTwo := "host-two"
noTags := getKey(metricName, []string{}, hostOne)
someTags := getKey(metricName, []string{"key1:val1", "key2:val2"}, hostOne)
sameTags := getKey(metricName, []string{"key2:val2", "key1:val1"}, hostOne)
diffTags := getKey(metricName, []string{"key3:val3"}, hostOne)
diffHost := getKey(metricName, []string{"key1:val1", "key2:val2"}, hostTwo)

assert.NotEqual(t, noTags, someTags)
assert.NotEqual(t, someTags, diffTags)
assert.Equal(t, someTags, sameTags)
assert.NotEqual(t, someTags, diffHost)
}

func TestMetricDimensionsStringNoTagsChange(t *testing.T) {
// The original metricDimensionsToMapKey had an issue where:
// - if the capacity of the tags array passed to it was higher than its length
// - and the metric name is earlier (in alphabetical order) than one of the tags
// then the original tag array would be modified (without a reallocation, since there is enough capacity),
// and would contain a tag labeled as the metric name, while the final tag (in alphabetical order)
// would get left out.
// This test checks that this doesn't happen anymore.

originalTags := make([]string, 2, 3)
originalTags[0] = "key1:val1"
originalTags[1] = "key2:val2"

dims := metricsDimensions{
name: "a.metric.name",
tags: originalTags,
}

_ = dims.String()
assert.Equal(t, []string{"key1:val1", "key2:val2"}, originalTags)

}

var testDims metricsDimensions = metricsDimensions{
name: "test.metric",
tags: []string{"key:val"},
host: "host",
}

func TestWithSuffix(t *testing.T) {
dimsSuf1 := testDims.WithSuffix("suffixOne")
dimsSuf2 := testDims.WithSuffix("suffixTwo")

assert.Equal(t, "test.metric", testDims.name)
assert.Equal(t, "test.metric.suffixOne", dimsSuf1.name)
assert.Equal(t, "test.metric.suffixTwo", dimsSuf2.name)
}

func TestAddTags(t *testing.T) {
dimsWithTags := testDims.AddTags("key1:val1", "key2:val2")
assert.ElementsMatch(t, []string{"key:val", "key1:val1", "key2:val2"}, dimsWithTags.tags)
assert.ElementsMatch(t, []string{"key:val"}, testDims.tags)
}
Loading