Skip to content

Commit

Permalink
[connector/spanmetricsconnector]: Migrate from spanmtricsprocessor (#…
Browse files Browse the repository at this point in the history
…18699)

* Separate spanmetricsconnector from spanmetricsprocessor
  • Loading branch information
Cluas committed Feb 17, 2023
1 parent fdd01de commit e2d91dd
Show file tree
Hide file tree
Showing 21 changed files with 2,706 additions and 820 deletions.
4 changes: 2 additions & 2 deletions connector/spanmetricsconnector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The following is a simple example usage of the spanmetrics connector.

For configuration examples on other use cases, please refer to [More Examples](#more-examples).

The full list of settings exposed for this connector are documented [here](../../processor/spanmetricsprocessor/config.go).
The full list of settings exposed for this connector are documented [here](../../connector/spanmetricsconnector/config.go).

```yaml
receivers:
Expand Down Expand Up @@ -103,6 +103,6 @@ service:

### More Examples

For more example configuration covering various other use cases, please visit the [testdata directory](../../processor/spanmetricsprocessor/testdata).
For more example configuration covering various other use cases, please visit the [testdata directory](../../connector/spanmetricsconnector/testdata).

[development]: https://github.com/open-telemetry/opentelemetry-collector#development
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: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
}
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: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())
}
Loading

0 comments on commit e2d91dd

Please sign in to comment.