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

[connector/spanmetricsconnector]: Migrate from spanmtricsprocessor #18699

Merged
merged 10 commits into from
Feb 17, 2023
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
16 changes: 16 additions & 0 deletions .chloggen/spanmetricsconnector-independent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: new_component
Cluas marked this conversation as resolved.
Show resolved Hide resolved

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: spanmetricsconnector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: From spanmetricsprocessor and conform to otel specifications

# One or more tracking issues related to the change
issues: [18697]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
80 changes: 80 additions & 0 deletions connector/spanmetricsconnector/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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 spanmetricsconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector"

import (
"time"

"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pmetric"
)

const (
delta = "AGGREGATION_TEMPORALITY_DELTA"
cumulative = "AGGREGATION_TEMPORALITY_CUMULATIVE"
)

var dropSanitizationGate = featuregate.GlobalRegistry().MustRegister(
"processor.spanmetrics.PermissiveLabelSanitization",
kovrus marked this conversation as resolved.
Show resolved Hide resolved
featuregate.StageAlpha,
featuregate.WithRegisterDescription("Controls whether to change labels starting with '_' to 'key_'"),
)

// Dimension defines the dimension name and optional default value if the Dimension is missing from a span attribute.
type Dimension struct {
Name string `mapstructure:"name"`
Default *string `mapstructure:"default"`
}

// Config defines the configuration options for spanmetricsprocessor.
kovrus marked this conversation as resolved.
Show resolved Hide resolved
type Config struct {
// LatencyHistogramBuckets is the list of durations representing latency histogram buckets.
// See defaultLatencyHistogramBucketsMs in processor.go for the default value.
kovrus marked this conversation as resolved.
Show resolved Hide resolved
LatencyHistogramBuckets []time.Duration `mapstructure:"latency_histogram_buckets"`

// Dimensions defines the list of additional dimensions on top of the provided:
// - service.name
// - span.name
Cluas marked this conversation as resolved.
Show resolved Hide resolved
// - span.kind
// - status.code
// The dimensions will be fetched from the span's attributes. Examples of some conventionally used attributes:
// https://github.com/open-telemetry/opentelemetry-collector/blob/main/model/semconv/opentelemetry.go.
Dimensions []Dimension `mapstructure:"dimensions"`

// DimensionsCacheSize defines the size of cache for storing Dimensions, which helps to avoid cache memory growing
// indefinitely over the lifetime of the collector.
// Optional. See defaultDimensionsCacheSize in processor.go for the default value.
kovrus marked this conversation as resolved.
Show resolved Hide resolved
DimensionsCacheSize int `mapstructure:"dimensions_cache_size"`

AggregationTemporality string `mapstructure:"aggregation_temporality"`

// skipSanitizeLabel if enabled, labels that start with _ are not sanitized
skipSanitizeLabel bool

// MetricsEmitInterval is the time period between when metrics are flushed or emitted to the configured MetricsExporter.
MetricsFlushInterval time.Duration `mapstructure:"metrics_flush_interval"`

// Namespace is the namespace of the metrics emitted by the processor.
kovrus marked this conversation as resolved.
Show resolved Hide resolved
Namespace string `mapstructure:"namespace"`
}

// GetAggregationTemporality converts the string value given in the config into a AggregationTemporality.
// Returns cumulative, unless delta is correctly specified.
func (c Config) GetAggregationTemporality() pmetric.AggregationTemporality {
if c.AggregationTemporality == delta {
return pmetric.AggregationTemporalityDelta
}
return pmetric.AggregationTemporalityCumulative
}
92 changes: 92 additions & 0 deletions connector/spanmetricsconnector/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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 spanmetricsconnector

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/otelcol/otelcoltest"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/receivertest"
)

func TestLoadConfig(t *testing.T) {
t.Parallel()

defaultMethod := "GET"
// Need to set this gate to load connector configs
require.NoError(t, featuregate.GlobalRegistry().Set("service.connectors", true))
defer func() {
require.NoError(t, featuregate.GlobalRegistry().Set("service.connectors", false))
}()

// Prepare
factories, err := otelcoltest.NopFactories()
require.NoError(t, err)

factories.Receivers["nop"] = receivertest.NewNopFactory()
factories.Connectors[typeStr] = NewFactory()
factories.Exporters["nop"] = exportertest.NewNopFactory()

// Test
simpleCfg, err := otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-simplest-connector.yaml"), factories)
require.NoError(t, err)
require.NotNil(t, simpleCfg)
assert.Equal(t,
&Config{
AggregationTemporality: cumulative,
DimensionsCacheSize: defaultDimensionsCacheSize,
skipSanitizeLabel: dropSanitizationGate.IsEnabled(),
MetricsFlushInterval: 15 * time.Second,
},
simpleCfg.Connectors[component.NewID(typeStr)],
)

fullCfg, err := otelcoltest.LoadConfigAndValidate(filepath.Join("testdata", "config-full-connector.yaml"), factories)
require.NoError(t, err)
require.NotNil(t, fullCfg)
assert.Equal(t,
&Config{
LatencyHistogramBuckets: []time.Duration{100000, 1000000, 2000000, 6000000, 10000000, 100000000, 250000000},
Dimensions: []Dimension{
{Name: "http.method", Default: &defaultMethod},
{Name: "http.status_code", Default: (*string)(nil)},
},
AggregationTemporality: delta,
DimensionsCacheSize: 1500,
skipSanitizeLabel: dropSanitizationGate.IsEnabled(),
MetricsFlushInterval: 30 * time.Second,
},
fullCfg.Connectors[component.NewID(typeStr)],
)
}

func TestGetAggregationTemporality(t *testing.T) {
cfg := &Config{AggregationTemporality: delta}
assert.Equal(t, pmetric.AggregationTemporalityDelta, cfg.GetAggregationTemporality())

cfg = &Config{AggregationTemporality: cumulative}
assert.Equal(t, pmetric.AggregationTemporalityCumulative, cfg.GetAggregationTemporality())

cfg = &Config{}
assert.Equal(t, pmetric.AggregationTemporalityCumulative, cfg.GetAggregationTemporality())
}
Loading