-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[connector/spanmetricsconnector]: Migrate from spanmtricsprocessor (#…
…18699) * Separate spanmetricsconnector from spanmetricsprocessor
- Loading branch information
Showing
21 changed files
with
2,706 additions
and
820 deletions.
There are no files selected for viewing
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
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: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 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( | ||
"connector.spanmetrics.PermissiveLabelSanitization", | ||
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 spanmetricsconnector. | ||
type Config struct { | ||
// LatencyHistogramBuckets is the list of durations representing latency histogram buckets. | ||
// See defaultLatencyHistogramBucketsMs in connector.go for the default value. | ||
LatencyHistogramBuckets []time.Duration `mapstructure:"latency_histogram_buckets"` | ||
|
||
// Dimensions defines the list of additional dimensions on top of the provided: | ||
// - service.name | ||
// - operation | ||
// - 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 connector.go for the default value. | ||
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 connector. | ||
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 | ||
} |
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,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: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 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()) | ||
} |
Oops, something went wrong.