forked from jriguera/otel-helloworldprocessor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.go
53 lines (48 loc) · 1.45 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
package contextprocessor
import (
"context"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)
type contextMetricsProcessor struct {
contextProcessor
nextConsumer consumer.Metrics
}
func NewContextMetricsProcessor(
logger *zap.Logger,
nextConsumer consumer.Metrics,
eventOptions trace.SpanStartEventOption,
actions []ActionConfig) (*contextMetricsProcessor, error) {
aRunner := NewActionsRunner()
for _, action := range actions {
if err := aRunner.AddAction(action); err != nil {
return nil, err
}
}
return &contextMetricsProcessor{
contextProcessor: contextProcessor{
logger: logger,
actionsRunner: aRunner,
eventOptions: eventOptions,
},
nextConsumer: nextConsumer,
}, nil
}
// implements https://pkg.go.dev/go.opentelemetry.io/collector/consumer#Metrics
func (ctxt *contextMetricsProcessor) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) (err error) {
span := trace.SpanFromContext(ctx)
span.AddEvent("Start processing.", ctxt.eventOptions)
rms := md.ResourceMetrics()
for i := 0; i < rms.Len() && err == nil; i++ {
rm := rms.At(i)
attrs := rm.Resource().Attributes()
newCtx := ctxt.actionsRunner.Apply(ctx, attrs)
newMd := pmetric.NewMetrics()
rm.CopyTo(newMd.ResourceMetrics().AppendEmpty())
err = ctxt.nextConsumer.ConsumeMetrics(newCtx, newMd)
}
span.AddEvent("End processing.", ctxt.eventOptions)
return err
}