-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathattributes_metric.go
96 lines (88 loc) · 3.17 KB
/
attributes_metric.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package attributesprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/attributesprocessor"
import (
"context"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/attraction"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/expr"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
)
type metricAttributesProcessor struct {
logger *zap.Logger
attrProc *attraction.AttrProc
skipExpr expr.BoolExpr[ottlmetric.TransformContext]
}
// newMetricAttributesProcessor returns a processor that modifies attributes of a
// metric record. To construct the attributes processors, the use of the factory
// methods are required in order to validate the inputs.
func newMetricAttributesProcessor(logger *zap.Logger, attrProc *attraction.AttrProc, skipExpr expr.BoolExpr[ottlmetric.TransformContext]) *metricAttributesProcessor {
return &metricAttributesProcessor{
logger: logger,
attrProc: attrProc,
skipExpr: skipExpr,
}
}
func (a *metricAttributesProcessor) processMetrics(ctx context.Context, md pmetric.Metrics) (pmetric.Metrics, error) {
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rs := rms.At(i)
resource := rs.Resource()
ilms := rs.ScopeMetrics()
for j := 0; j < ilms.Len(); j++ {
ils := ilms.At(j)
scope := ils.Scope()
metrics := ils.Metrics()
for k := 0; k < metrics.Len(); k++ {
m := metrics.At(k)
if a.skipExpr != nil {
skip, err := a.skipExpr.Eval(ctx, ottlmetric.NewTransformContext(m, metrics, scope, resource, ils, rs))
if err != nil {
return md, err
}
if skip {
continue
}
}
a.processMetricAttributes(ctx, m)
}
}
}
return md, nil
}
// Attributes are provided for each log and trace, but not at the metric level
// Need to process attributes for every data point within a metric.
func (a *metricAttributesProcessor) processMetricAttributes(ctx context.Context, m pmetric.Metric) {
// This is a lot of repeated code, but since there is no single parent superclass
// between metric data types, we can't use polymorphism.
//exhaustive:enforce
switch m.Type() {
case pmetric.MetricTypeGauge:
dps := m.Gauge().DataPoints()
for i := 0; i < dps.Len(); i++ {
a.attrProc.Process(ctx, a.logger, dps.At(i).Attributes())
}
case pmetric.MetricTypeSum:
dps := m.Sum().DataPoints()
for i := 0; i < dps.Len(); i++ {
a.attrProc.Process(ctx, a.logger, dps.At(i).Attributes())
}
case pmetric.MetricTypeHistogram:
dps := m.Histogram().DataPoints()
for i := 0; i < dps.Len(); i++ {
a.attrProc.Process(ctx, a.logger, dps.At(i).Attributes())
}
case pmetric.MetricTypeExponentialHistogram:
dps := m.ExponentialHistogram().DataPoints()
for i := 0; i < dps.Len(); i++ {
a.attrProc.Process(ctx, a.logger, dps.At(i).Attributes())
}
case pmetric.MetricTypeSummary:
dps := m.Summary().DataPoints()
for i := 0; i < dps.Len(); i++ {
a.attrProc.Process(ctx, a.logger, dps.At(i).Attributes())
}
case pmetric.MetricTypeEmpty:
}
}