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

pkg/otlp: Add instrumentation_scope_metadata_as_tags #12393

Merged
merged 4 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions pkg/config/config_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3194,12 +3194,21 @@ api_key:
#
# resource_attributes_as_tags: false

## Deprecated - use `instrumentation_scope_metadata_as_tags` instead in favor of
## https://github.com/open-telemetry/opentelemetry-proto/releases/tag/v0.15.0
## If both are set, `instrumentation_scope_metadata_as_tags` takes priotiry.
## @param instrumentation_library_metadata_as_tags - boolean - optional - default: false
## @env DD_OTLP_CONFIG_METRICS_INSTRUMENTATION_LIBRARY_METADATA_AS_TAGS - boolean - optional - default: false
## Set to true to add metadata about the instrumentation library that created a metric.
#
# instrumentation_library_metadata_as_tags: false

## @param instrumentation_scope_metadata_as_tags - boolean - optional - default: false
## @env DD_OTLP_CONFIG_METRICS_INSTRUMENTATION_SCOPE_METADATA_AS_TAGS - boolean - optional - default: false
## Set to true to add metadata about the instrumentation scope that created a metric.
#
# instrumentation_scope_metadata_as_tags: false

## @param tag_cardinality - string - optional - default: low
## @env DD_OTLP_CONFIG_METRICS_TAG_CARDINALITY - string - optional - default: low
## Configure the level of granularity of tags to send for OTLP metrics. Choices are:
Expand Down
1 change: 1 addition & 0 deletions pkg/config/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func setupOTLPEnvironmentVariables(config Config) {
config.BindEnv(OTLPSection + ".metrics.delta_ttl")
config.BindEnv(OTLPSection + ".metrics.resource_attributes_as_tags")
config.BindEnv(OTLPSection + ".metrics.instrumentation_library_metadata_as_tags")
config.BindEnv(OTLPSection + ".metrics.instrumentation_scope_metadata_as_tags")
config.BindEnv(OTLPSection + ".metrics.tag_cardinality")
config.BindEnv(OTLPSection + ".metrics.histograms.mode")
config.BindEnv(OTLPSection + ".metrics.histograms.send_count_sum_metrics")
Expand Down
17 changes: 10 additions & 7 deletions pkg/otlp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ func TestFromEnvironmentVariables(t *testing.T) {
{
name: "HTTP + gRPC, metrics config",
env: map[string]string{
"DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_GRPC_ENDPOINT": "0.0.0.0:9995",
"DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_HTTP_ENDPOINT": "0.0.0.0:9996",
"DD_OTLP_CONFIG_METRICS_DELTA_TTL": "2400",
"DD_OTLP_CONFIG_METRICS_HISTOGRAMS_MODE": "counters",
"DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_GRPC_ENDPOINT": "0.0.0.0:9995",
"DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_HTTP_ENDPOINT": "0.0.0.0:9996",
"DD_OTLP_CONFIG_METRICS_DELTA_TTL": "2400",
"DD_OTLP_CONFIG_METRICS_HISTOGRAMS_MODE": "counters",
"DD_OTLP_CONFIG_METRICS_INSTRUMENTATION_SCOPE_METADATA_AS_TAGS": "true",
},
cfg: PipelineConfig{
OTLPReceiverConfig: map[string]interface{}{
Expand All @@ -226,9 +227,10 @@ func TestFromEnvironmentVariables(t *testing.T) {
TracesEnabled: true,
TracePort: 5003,
Metrics: map[string]interface{}{
"enabled": true,
"tag_cardinality": "low",
"delta_ttl": "2400",
"enabled": true,
"instrumentation_scope_metadata_as_tags": "true",
"tag_cardinality": "low",
"delta_ttl": "2400",
"histograms": map[string]interface{}{
"mode": "counters",
},
Expand Down Expand Up @@ -271,6 +273,7 @@ func TestFromAgentConfigMetrics(t *testing.T) {
"delta_ttl": 2400,
"resource_attributes_as_tags": true,
"instrumentation_library_metadata_as_tags": true,
"instrumentation_scope_metadata_as_tags": true,
"tag_cardinality": "orchestrator",
"histograms": map[string]interface{}{
"mode": "counters",
Expand Down
7 changes: 7 additions & 0 deletions pkg/otlp/internal/serializerexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,16 @@ type metricsExporterConfig struct {
// resource attributes into metric labels, which are then converted into tags
ResourceAttributesAsTags bool `mapstructure:"resource_attributes_as_tags"`

// Deprecated: Use InstrumentationScopeMetadataAsTags favor of in favor of
// https://github.com/open-telemetry/opentelemetry-proto/releases/tag/v0.15.0
// If both are set, InstrumentationScopeMetadataAsTags takes priority.
// InstrumentationLibraryMetadataAsTags, if set to true, adds the name and version of the
// instrumentation library that created a metric to the metric tags
InstrumentationLibraryMetadataAsTags bool `mapstructure:"instrumentation_library_metadata_as_tags"`

// InstrumentationScopeMetadataAsTags, if set to true, adds the name and version of the
// instrumentation scope that created a metric to the metric tags
InstrumentationScopeMetadataAsTags bool `mapstructure:"instrumentation_scope_metadata_as_tags"`
}

// Validate configuration
Expand Down
5 changes: 5 additions & 0 deletions pkg/otlp/internal/serializerexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func newDefaultConfig() config.Exporter {
ExporterConfig: metricsExporterConfig{
ResourceAttributesAsTags: false,
InstrumentationLibraryMetadataAsTags: false,
InstrumentationScopeMetadataAsTags: false,
},
TagCardinality: collectors.LowCardinalityString,
HistConfig: histogramConfig{
Expand Down Expand Up @@ -102,6 +103,10 @@ func translatorFromConfig(logger *zap.Logger, cfg *exporterConfig) (*translator.
options = append(options, translator.WithInstrumentationLibraryMetadataAsTags())
}

if cfg.Metrics.ExporterConfig.InstrumentationScopeMetadataAsTags {
keisku marked this conversation as resolved.
Show resolved Hide resolved
options = append(options, translator.WithInstrumentationLibraryMetadataAsTags())
}

var numberMode translator.NumberMode
switch cfg.Metrics.SumConfig.CumulativeMonotonicMode {
case CumulativeMonotonicSumModeRawValue:
Expand Down
10 changes: 7 additions & 3 deletions pkg/otlp/map_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"context"
"testing"

"github.com/DataDog/datadog-agent/pkg/otlp/internal/testutil"
"github.com/DataDog/datadog-agent/pkg/serializer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config"

"github.com/DataDog/datadog-agent/pkg/otlp/internal/testutil"
"github.com/DataDog/datadog-agent/pkg/serializer"
)

func TestNewMap(t *testing.T) {
Expand Down Expand Up @@ -74,6 +73,7 @@ func TestNewMap(t *testing.T) {
"delta_ttl": 2000,
"resource_attributes_as_tags": true,
"instrumentation_library_metadata_as_tags": true,
"instrumentation_scope_metadata_as_tags": true,
"histograms": map[string]interface{}{
"mode": "counters",
"send_count_sum_metrics": true,
Expand Down Expand Up @@ -108,6 +108,7 @@ func TestNewMap(t *testing.T) {
"delta_ttl": 2000,
"resource_attributes_as_tags": true,
"instrumentation_library_metadata_as_tags": true,
"instrumentation_scope_metadata_as_tags": true,
"histograms": map[string]interface{}{
"mode": "counters",
"send_count_sum_metrics": true,
Expand Down Expand Up @@ -181,6 +182,7 @@ func TestNewMap(t *testing.T) {
"delta_ttl": 1500,
"resource_attributes_as_tags": false,
"instrumentation_library_metadata_as_tags": false,
"instrumentation_scope_metadata_as_tags": false,
"histograms": map[string]interface{}{
"mode": "nobuckets",
"send_count_sum_metrics": true,
Expand Down Expand Up @@ -208,6 +210,7 @@ func TestNewMap(t *testing.T) {
"delta_ttl": 1500,
"resource_attributes_as_tags": false,
"instrumentation_library_metadata_as_tags": false,
"instrumentation_scope_metadata_as_tags": false,
"histograms": map[string]interface{}{
"mode": "nobuckets",
"send_count_sum_metrics": true,
Expand Down Expand Up @@ -249,6 +252,7 @@ func TestUnmarshal(t *testing.T) {
"delta_ttl": 2000,
"resource_attributes_as_tags": true,
"instrumentation_library_metadata_as_tags": true,
"instrumentation_scope_metadata_as_tags": true,
"histograms": map[string]interface{}{
"mode": "counters",
"send_count_sum_metrics": true,
Expand Down
34 changes: 34 additions & 0 deletions pkg/otlp/model/internal/instrumentationscope/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 instrumentationscope

import (
"github.com/DataDog/datadog-agent/pkg/otlp/model/internal/utils"
"go.opentelemetry.io/collector/pdata/pcommon"
)

const (
instrumentationScopeTag = "instrumentation_scope"
instrumentationScopeVersionTag = "instrumentation_scope_version"
)

// TagsFromInstrumentationScopeMetadata takes the name and version of
// the instrumentation scope and converts them to Datadog tags.
func TagsFromInstrumentationScopeMetadata(il pcommon.InstrumentationScope) []string {
return []string{
utils.FormatKeyValueTag(instrumentationScopeTag, il.Name()),
utils.FormatKeyValueTag(instrumentationScopeVersionTag, il.Version()),
}
}
45 changes: 45 additions & 0 deletions pkg/otlp/model/internal/instrumentationscope/metadata_test.go
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 instrumentationscope

import (
"fmt"
"testing"

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

func TestTagsFromInstrumentationScopeMetadata(t *testing.T) {
tests := []struct {
name string
version string
expectedTags []string
}{
{"test-il", "1.0.0", []string{fmt.Sprintf("%s:%s", instrumentationScopeTag, "test-il"), fmt.Sprintf("%s:%s", instrumentationScopeVersionTag, "1.0.0")}},
{"test-il", "", []string{fmt.Sprintf("%s:%s", instrumentationScopeTag, "test-il"), fmt.Sprintf("%s:%s", instrumentationScopeVersionTag, "n/a")}},
{"", "1.0.0", []string{fmt.Sprintf("%s:%s", instrumentationScopeTag, "n/a"), fmt.Sprintf("%s:%s", instrumentationScopeVersionTag, "1.0.0")}},
{"", "", []string{fmt.Sprintf("%s:%s", instrumentationScopeTag, "n/a"), fmt.Sprintf("%s:%s", instrumentationScopeVersionTag, "n/a")}},
}

for _, testInstance := range tests {
il := pcommon.NewInstrumentationScope()
il.SetName(testInstance.name)
il.SetVersion(testInstance.version)
tags := TagsFromInstrumentationScopeMetadata(il)

assert.ElementsMatch(t, testInstance.expectedTags, tags)
}
}
22 changes: 17 additions & 5 deletions pkg/otlp/model/translator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ import "fmt"

type translatorConfig struct {
// metrics export behavior
HistMode HistogramMode
SendCountSum bool
Quantiles bool
SendMonotonic bool
ResourceAttributesAsTags bool
HistMode HistogramMode
SendCountSum bool
Quantiles bool
SendMonotonic bool
ResourceAttributesAsTags bool
// Deprecated: use InstrumentationScopeMetadataAsTags instead in favor of
// https://github.com/open-telemetry/opentelemetry-proto/releases/tag/v0.15.0
// If both are set, InstrumentationScopeMetadataAsTags takes priority.
InstrumentationLibraryMetadataAsTags bool
InstrumentationScopeMetadataAsTags bool

// cache configuration
sweepInterval int64
Expand Down Expand Up @@ -85,6 +89,14 @@ func WithInstrumentationLibraryMetadataAsTags() Option {
}
}

// WithInstrumentationScopeMetadataAsTags sets instrumentation scope metadata as tags.
func WithInstrumentationScopeMetadataAsTags() Option {
return func(t *translatorConfig) error {
t.InstrumentationScopeMetadataAsTags = true
return nil
}
}

// HistogramMode is an export mode for OTLP Histogram metrics.
type HistogramMode string

Expand Down
5 changes: 4 additions & 1 deletion pkg/otlp/model/translator/metrics_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/DataDog/datadog-agent/pkg/otlp/model/attributes"
"github.com/DataDog/datadog-agent/pkg/otlp/model/internal/instrumentationlibrary"
"github.com/DataDog/datadog-agent/pkg/otlp/model/internal/instrumentationscope"
"github.com/DataDog/datadog-agent/pkg/quantile"
"github.com/DataDog/sketches-go/ddsketch"
"github.com/DataDog/sketches-go/ddsketch/mapping"
Expand Down Expand Up @@ -552,7 +553,9 @@ func (t *Translator) MapMetrics(ctx context.Context, md pmetric.Metrics, consume
metricsArray := ilm.Metrics()

var additionalTags []string
if t.cfg.InstrumentationLibraryMetadataAsTags {
if t.cfg.InstrumentationScopeMetadataAsTags {
additionalTags = append(attributeTags, instrumentationscope.TagsFromInstrumentationScopeMetadata(ilm.Scope())...)
} else if t.cfg.InstrumentationLibraryMetadataAsTags {
additionalTags = append(attributeTags, instrumentationlibrary.TagsFromInstrumentationLibraryMetadata(ilm.Scope())...)
} else {
additionalTags = attributeTags
Expand Down
Loading