Debian Codegraph @

internal/fanoutconsumer/metrics.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package fanoutconsumer // import "go.opentelemetry.io/collector/internal/fanoutconsumer"
5
6 import (
7 "context"
8
9 "go.uber.org/multierr"
10
11 "go.opentelemetry.io/collector/consumer"
12 "go.opentelemetry.io/collector/pdata/pmetric"
13 )
14
15 // NewMetrics wraps multiple metrics consumers in a single one.
16 // It fans out the incoming data to all the consumers, and does smart routing:
17 // - Clones only to the consumer that needs to mutate the data.
18 // - If all consumers needs to mutate the data one will get the original mutable data.
19 func NewMetrics(mcs []consumer.Metrics) consumer.Metrics {
20 // Don't wrap if there is only one non-mutating consumer.
21 if len(mcs) == 1 && !mcs[0].Capabilities().MutatesData {
22 return mcs[0]
23 }
24
25 mc := &metricsConsumer{}
26 for i := range mcs {
27 if mcs[i].Capabilities().MutatesData {
28 mc.mutable = append(mc.mutable, mcs[i])
29 } else {
30 mc.readonly = append(mc.readonly, mcs[i])
31 }
32 }
33 return mc
34 }
35
36 type metricsConsumer struct {
37 mutable []consumer.Metrics
38 readonly []consumer.Metrics
39 }
40
41 func (msc *metricsConsumer) Capabilities() consumer.Capabilities {
42 // If all consumers are mutating, then the original data will be passed to one of them.
43 return consumer.Capabilities{MutatesData: len(msc.mutable) > 0 && len(msc.readonly) == 0}
44 }
45
46 // ConsumeMetrics exports the pmetric.Metrics to all consumers wrapped by the current one.
47 func (msc *metricsConsumer) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
48 var errs error
49
50 if len(msc.mutable) > 0 {
51 // Clone the data before sending to all mutating consumers except the last one.
52 for i := 0; i < len(msc.mutable)-1; i++ {
53 errs = multierr.Append(errs, msc.mutable[i].ConsumeMetrics(ctx, cloneMetrics(md)))
54 }
55 // Send data as is to the last mutating consumer only if there are no other non-mutating consumers and the
56 // data is mutable. Never share the same data between a mutating and a non-mutating consumer since the
57 // non-mutating consumer may process data async and the mutating consumer may change the data before that.
58 lastConsumer := msc.mutable[len(msc.mutable)-1]
59 if len(msc.readonly) == 0 && !md.IsReadOnly() {
60 errs = multierr.Append(errs, lastConsumer.ConsumeMetrics(ctx, md))
61 } else {
62 errs = multierr.Append(errs, lastConsumer.ConsumeMetrics(ctx, cloneMetrics(md)))
63 }
64 }
65
66 // Mark the data as read-only if it will be sent to more than one read-only consumer.
67 if len(msc.readonly) > 1 && !md.IsReadOnly() {
68 md.MarkReadOnly()
69 }
70 for _, mc := range msc.readonly {
71 errs = multierr.Append(errs, mc.ConsumeMetrics(ctx, md))
72 }
73
74 return errs
75 }
76
77 func cloneMetrics(md pmetric.Metrics) pmetric.Metrics {
78 clonedMetrics := pmetric.NewMetrics()
79 md.CopyTo(clonedMetrics)
80 return clonedMetrics
81 }