-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmetrics.go
77 lines (66 loc) · 2.14 KB
/
metrics.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package processorhelper // import "go.opentelemetry.io/collector/processor/processorhelper"
import (
"context"
"errors"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pipeline"
"go.opentelemetry.io/collector/processor"
)
// ProcessMetricsFunc is a helper function that processes the incoming data and returns the data to be sent to the next component.
// If error is returned then returned data are ignored. It MUST not call the next component.
type ProcessMetricsFunc func(context.Context, pmetric.Metrics) (pmetric.Metrics, error)
type metrics struct {
component.StartFunc
component.ShutdownFunc
consumer.Metrics
}
// NewMetrics creates a processor.Metrics that ensure context propagation and the right tags are set.
func NewMetrics(
_ context.Context,
set processor.Settings,
_ component.Config,
nextConsumer consumer.Metrics,
metricsFunc ProcessMetricsFunc,
options ...Option,
) (processor.Metrics, error) {
if metricsFunc == nil {
return nil, errors.New("nil metricsFunc")
}
obs, err := newObsReport(set, pipeline.SignalMetrics)
if err != nil {
return nil, err
}
eventOptions := spanAttributes(set.ID)
bs := fromOptions(options)
metricsConsumer, err := consumer.NewMetrics(func(ctx context.Context, md pmetric.Metrics) error {
span := trace.SpanFromContext(ctx)
span.AddEvent("Start processing.", eventOptions)
pointsIn := md.DataPointCount()
var errFunc error
md, errFunc = metricsFunc(ctx, md)
span.AddEvent("End processing.", eventOptions)
if errFunc != nil {
obs.recordInOut(ctx, pointsIn, 0)
if errors.Is(errFunc, ErrSkipProcessingData) {
return nil
}
return errFunc
}
pointsOut := md.DataPointCount()
obs.recordInOut(ctx, pointsIn, pointsOut)
return nextConsumer.ConsumeMetrics(ctx, md)
}, bs.consumerOptions...)
if err != nil {
return nil, err
}
return &metrics{
StartFunc: bs.StartFunc,
ShutdownFunc: bs.ShutdownFunc,
Metrics: metricsConsumer,
}, nil
}