Skip to content

Commit

Permalink
Add tags formatting util
Browse files Browse the repository at this point in the history
  • Loading branch information
KSerrania committed Sep 25, 2021
1 parent 860e412 commit 57a1e0d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package instrumentationlibrary

import (
"fmt"

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

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

const (
Expand All @@ -29,7 +29,7 @@ const (
// the instrumentation library and converts them to Datadog tags.
func TagsFromInstrumentationLibraryMetadata(il pdata.InstrumentationLibrary) []string {
return []string{
fmt.Sprintf("%s:%s", instrumentationLibraryTag, il.Name()),
fmt.Sprintf("%s:%s", instrumentationLibraryVersionTag, il.Version()),
utils.FormatKeyValueTag(instrumentationLibraryTag, il.Name()),
utils.FormatKeyValueTag(instrumentationLibraryVersionTag, il.Version()),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func TestTagsFromInstrumentationLibraryMetadata(t *testing.T) {
expectedTags []string
}{
{"test-il", "1.0.0", []string{fmt.Sprintf("%s:%s", instrumentationLibraryTag, "test-il"), fmt.Sprintf("%s:%s", instrumentationLibraryVersionTag, "1.0.0")}},
{"test-il", "", []string{fmt.Sprintf("%s:%s", instrumentationLibraryTag, "test-il"), fmt.Sprintf("%s:%s", instrumentationLibraryVersionTag, "")}},
{"", "1.0.0", []string{fmt.Sprintf("%s:%s", instrumentationLibraryTag, ""), fmt.Sprintf("%s:%s", instrumentationLibraryVersionTag, "1.0.0")}},
{"test-il", "", []string{fmt.Sprintf("%s:%s", instrumentationLibraryTag, "test-il"), fmt.Sprintf("%s:%s", instrumentationLibraryVersionTag, "n/a")}},
{"", "1.0.0", []string{fmt.Sprintf("%s:%s", instrumentationLibraryTag, "n/a"), fmt.Sprintf("%s:%s", instrumentationLibraryVersionTag, "1.0.0")}},
{"", "", []string{fmt.Sprintf("%s:%s", instrumentationLibraryTag, "n/a"), fmt.Sprintf("%s:%s", instrumentationLibraryVersionTag, "n/a")}},
}

for _, testInstance := range tests {
fmt.Println(testInstance)
il := pdata.NewInstrumentationLibrary()
il.SetName(testInstance.name)
il.SetVersion(testInstance.version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/instrumentationlibrary"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metrics"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/sketches"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/utils"
)

const metricName string = "metric name"
Expand Down Expand Up @@ -65,11 +66,7 @@ func getTags(labels pdata.AttributeMap) []string {
tags := make([]string, 0, labels.Len())
labels.Range(func(key string, value pdata.AttributeValue) bool {
v := value.AsString()
if v == "" {
// Tags can't end with ":" so we replace empty values with "n/a"
v = "n/a"
}
tags = append(tags, fmt.Sprintf("%s:%s", key, v))
tags = append(tags, utils.FormatKeyValueTag(key, v))
return true
})
return tags
Expand Down
29 changes: 29 additions & 0 deletions exporter/datadogexporter/internal/utils/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 utils

import (
"fmt"
)

// FormatKeyValueTag takes a key-value pair, and creates a tag string out of it
// Tags can't end with ":" so we replace empty values with "n/a"
func FormatKeyValueTag(key, value string) string {
// Should this check for empty keys, and return an error in that case?
if value == "" {
value = "n/a"
}
return fmt.Sprintf("%s:%s", key, value)
}
37 changes: 37 additions & 0 deletions exporter/datadogexporter/internal/utils/tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 utils

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFormatKeyValueTag(t *testing.T) {
tests := []struct {
key string
value string
expectedTag string
}{
{"a.test.tag", "a.test.value", fmt.Sprintf("%s:%s", "a.test.tag", "a.test.value")},
{"a.test.tag", "", fmt.Sprintf("%s:%s", "a.test.tag", "n/a")},
}

for _, testInstance := range tests {
assert.Equal(t, testInstance.expectedTag, FormatKeyValueTag(testInstance.key, testInstance.value))
}
}

0 comments on commit 57a1e0d

Please sign in to comment.