-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathconfig.go
129 lines (103 loc) · 4.35 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package spanmetricsconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector"
import (
"errors"
"fmt"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pmetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector/internal/metrics"
)
const (
delta = "AGGREGATION_TEMPORALITY_DELTA"
cumulative = "AGGREGATION_TEMPORALITY_CUMULATIVE"
)
var defaultHistogramBucketsMs = []float64{
2, 4, 6, 8, 10, 50, 100, 200, 400, 800, 1000, 1400, 2000, 5000, 10_000, 15_000,
}
// 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 {
// Dimensions defines the list of additional dimensions on top of the provided:
// - service.name
// - span.kind
// - 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"`
ExcludeDimensions []string `mapstructure:"exclude_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"`
Histogram HistogramConfig `mapstructure:"histogram"`
// 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"`
// Exemplars defines the configuration for exemplars.
Exemplars ExemplarsConfig `mapstructure:"exemplars"`
}
type HistogramConfig struct {
Disable bool `mapstructure:"disable"`
Unit metrics.Unit `mapstructure:"unit"`
Exponential *ExponentialHistogramConfig `mapstructure:"exponential"`
Explicit *ExplicitHistogramConfig `mapstructure:"explicit"`
}
type ExemplarsConfig struct {
Enabled bool `mapstructure:"enabled"`
}
type ExponentialHistogramConfig struct {
MaxSize int32 `mapstructure:"max_size"`
}
type ExplicitHistogramConfig struct {
// Buckets is the list of durations representing explicit histogram buckets.
Buckets []time.Duration `mapstructure:"buckets"`
}
var _ component.ConfigValidator = (*Config)(nil)
// Validate checks if the processor configuration is valid
func (c Config) Validate() error {
err := validateDimensions(c.Dimensions)
if err != nil {
return err
}
if c.DimensionsCacheSize <= 0 {
return fmt.Errorf(
"invalid cache size: %v, the maximum number of the items in the cache should be positive",
c.DimensionsCacheSize,
)
}
if c.Histogram.Explicit != nil && c.Histogram.Exponential != nil {
return errors.New("use either `explicit` or `exponential` buckets histogram")
}
return nil
}
// 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
}
// validateDimensions checks duplicates for reserved dimensions and additional dimensions.
func validateDimensions(dimensions []Dimension) error {
labelNames := make(map[string]struct{})
for _, key := range []string{serviceNameKey, spanKindKey, statusCodeKey, spanNameKey} {
labelNames[key] = struct{}{}
}
for _, key := range dimensions {
if _, ok := labelNames[key.Name]; ok {
return fmt.Errorf("duplicate dimension name %s", key.Name)
}
labelNames[key.Name] = struct{}{}
}
return nil
}