-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/otlp/model] Add option to send instrumentation library metadata …
…tags with metrics (#9472) Backport of open-telemetry/opentelemetry-collector-contrib#5431 to the `pkg/otlp/model` go module.
- Loading branch information
Showing
6 changed files
with
190 additions
and
32 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
pkg/otlp/model/internal/instrumentationlibrary/metadata.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 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 instrumentationlibrary | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/model/pdata" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/otlp/model/internal/utils" | ||
) | ||
|
||
const ( | ||
instrumentationLibraryTag = "instrumentation_library" | ||
instrumentationLibraryVersionTag = "instrumentation_library_version" | ||
) | ||
|
||
// TagsFromInstrumentationLibraryMetadata takes the name and version of | ||
// the instrumentation library and converts them to Datadog tags. | ||
func TagsFromInstrumentationLibraryMetadata(il pdata.InstrumentationLibrary) []string { | ||
return []string{ | ||
utils.FormatKeyValueTag(instrumentationLibraryTag, il.Name()), | ||
utils.FormatKeyValueTag(instrumentationLibraryVersionTag, il.Version()), | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
pkg/otlp/model/internal/instrumentationlibrary/metadata_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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 instrumentationlibrary | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/model/pdata" | ||
) | ||
|
||
func TestTagsFromInstrumentationLibraryMetadata(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
version string | ||
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, "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 { | ||
il := pdata.NewInstrumentationLibrary() | ||
il.SetName(testInstance.name) | ||
il.SetVersion(testInstance.version) | ||
tags := TagsFromInstrumentationLibraryMetadata(il) | ||
|
||
assert.ElementsMatch(t, testInstance.expectedTags, tags) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 { | ||
if value == "" { | ||
value = "n/a" | ||
} | ||
return fmt.Sprintf("%s:%s", key, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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 ( | ||
"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", "a.test.tag:a.test.value"}, | ||
{"a.test.tag", "", "a.test.tag:n/a"}, | ||
} | ||
|
||
for _, testInstance := range tests { | ||
assert.Equal(t, testInstance.expectedTag, FormatKeyValueTag(testInstance.key, testInstance.value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters