Skip to content

Commit

Permalink
add instrumentation_scope_metadata_as_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
keisku committed Jun 18, 2022
1 parent 24decfc commit 0382185
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 32 deletions.
6 changes: 6 additions & 0 deletions exporter/datadogexporter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,15 @@ type MetricsExporterConfig struct {
// resource attributes into metric labels, which are then converted into tags
ResourceAttributesAsTags bool `mapstructure:"resource_attributes_as_tags"`

// Deprecated: Use InstrumentationScopeMetadataAsTags instead in favor of https://github.com/open-telemetry/opentelemetry-proto/releases/tag/v0.15.0
// Both must not be enabled at the same time.
// 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"`
}

// TracesConfig defines the traces exporter specific configuration options
Expand Down
1 change: 1 addition & 0 deletions exporter/datadogexporter/config/warn_envvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func futureDefaultConfig() *Config {
ExporterConfig: MetricsExporterConfig{
ResourceAttributesAsTags: false,
InstrumentationLibraryMetadataAsTags: false,
InstrumentationScopeMetadataAsTags: false,
},
HistConfig: HistogramConfig{
Mode: "distributions",
Expand Down
26 changes: 17 additions & 9 deletions exporter/datadogexporter/example/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exporters:
## @params use_resource_metadata - boolean - optional - default: true
## Deprecated: [v0.49.0] Use `host_metadata::hostname_source` instead.
## This option will be removed in v0.52.0.
#
#
# use_resource_metadata: true

## @param only_metadata - boolean - optional - default: false
Expand Down Expand Up @@ -132,27 +132,35 @@ exporters:
#
# 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
## Both must not be enabled at the same time.
## @param instrumentation_library_metadata_as_tags - string - 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 - string - optional - default: false
## Set to true to add metadata about the instrumentation scope that created a metric.
#
# instrumentation_scope_metadata_as_tags: false

## @param histograms - custom object - optional
## Histograms specific configuration.
## @param mode - string - optional - default: distributions
## How to report histograms. Valid values are:
##
##
## - `distributions` to report metrics as Datadog distributions (recommended).
## - `nobuckets` to not report bucket metrics,
## - `counters` to report one metric per histogram bucket.
#
# mode: distributions

## @param send_count_sum_metrics - boolean - optional - default: false
## Whether to report sum and count as separate histogram metrics.
#
# send_count_sum_metrics: false

## @param sums - custom object - optional
## Sums specific configuration.
## @param cumulative_monotonic_mode - string - optional - default: to_delta
Expand All @@ -167,7 +175,7 @@ exporters:
## Summaries specific configuration.
## @param mode - string - optional - default: gauges
## How to report summaries. Valid values are:
##
##
## - `noquantiles` to not report quantile metrics
## - `gauges` to report one gauge metric per quantile.
#
Expand All @@ -188,15 +196,15 @@ exporters:
## A blacklist of regular expressions can be provided to disable certain traces based on their resource name
## all entries must be surrounded by double quotes and separated by commas.
#
# ignore_resources: ["(GET|POST) /healthcheck"]
# ignore_resources: ["(GET|POST) /healthcheck"]

## @param span_name_remappings - map of key/value pairs - optional
## A map of Datadog span operation name keys and preferred name valuues to update those names to. This can be used to
## automatically map Datadog Span Operation Names to an updated value, and is useful when a user wants to
## shorten or modify span names to something more user friendly in the case of instrumentation libraries with
## particularly verbose names.
#
# span_name_remappings:
# span_name_remappings:
# io.opentelemetry.javaagent.spring.client: spring.client
# instrumentation:express.server: express
# go.opentelemetry.io_contrib_instrumentation_net_http_otelhttp.client: http.client
Expand All @@ -207,7 +215,7 @@ exporters:
## https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/1909
#
# span_name_as_resource_name: true

## @param host_metadata - custom object - optional
## Host metadata specific configuration.
## Host metadata is the information used for populating the infrastructure list, the host map and providing host tags functionality within the Datadog app.
Expand Down Expand Up @@ -237,7 +245,7 @@ exporters:
## @param tags - list of strings - optional - default: empty list
## List of host tags to be sent as part of the host metadata.
## These tags will be attached to telemetry signals that have the host metadata hostname.
##
##
## To attach tags to telemetry signals regardless of the host, use a processor instead.
#
# tags: []
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/example/example_k8s_manifest.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This manifest file is meant as an example of how to deploy otel-agent as daemonset and otel-collector as a standalone service.
# Using this example should correctly identifies hostnames for individual k8s nodes.
# Using this example should correctly identifies hostnames for individual k8s nodes.
# This is meant as an example only and may differ depending on deployment scenario and specifics of the environment the collector is used.
---
# The k8sattributes processor may require additional
Expand Down
1 change: 1 addition & 0 deletions exporter/datadogexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (*factory) createDefaultConfig() config.Exporter {
ExporterConfig: ddconfig.MetricsExporterConfig{
ResourceAttributesAsTags: false,
InstrumentationLibraryMetadataAsTags: false,
InstrumentationScopeMetadataAsTags: false,
},
HistConfig: ddconfig.HistogramConfig{
Mode: "distributions",
Expand Down
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:https://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/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/model/internal/instrumentationscope"

import (
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/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()),
}
}
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:https://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 exporter/datadogexporter/internal/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
// Both must not be enabled at the same time.
InstrumentationLibraryMetadataAsTags bool
InstrumentationScopeMetadataAsTags bool

// cache configuration
sweepInterval int64
Expand Down Expand Up @@ -94,6 +98,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
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
"strconv"

"github.com/DataDog/datadog-agent/pkg/quantile"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/model/attributes"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/model/internal/instrumentationlibrary"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/model/internal/instrumentationscope"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
)

const metricName string = "metric name"
Expand All @@ -47,6 +47,7 @@ func New(logger *zap.Logger, options ...Option) (*Translator, error) {
SendMonotonic: true,
ResourceAttributesAsTags: false,
InstrumentationLibraryMetadataAsTags: false,
InstrumentationScopeMetadataAsTags: false,
sweepInterval: 1800,
deltaTTL: 3600,
fallbackHostnameProvider: &noHostProvider{},
Expand Down Expand Up @@ -427,7 +428,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

0 comments on commit 0382185

Please sign in to comment.