Debian Codegraph @

internal/fanoutconsumer/profiles.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/consumer/xconsumer"
13 "go.opentelemetry.io/collector/pdata/pprofile"
14 )
15
16 // NewProfiles wraps multiple profile consumers in a single one.
17 // It fans out the incoming data to all the consumers, and does smart routing:
18 // - Clones only to the consumer that needs to mutate the data.
19 // - If all consumers needs to mutate the data one will get the original mutable data.
20 func NewProfiles(tcs []xconsumer.Profiles) xconsumer.Profiles {
21 // Don't wrap if there is only one non-mutating consumer.
22 if len(tcs) == 1 && !tcs[0].Capabilities().MutatesData {
23 return tcs[0]
24 }
25
26 tc := &profilesConsumer{}
27 for i := range tcs {
28 if tcs[i].Capabilities().MutatesData {
29 tc.mutable = append(tc.mutable, tcs[i])
30 } else {
31 tc.readonly = append(tc.readonly, tcs[i])
32 }
33 }
34 return tc
35 }
36
37 type profilesConsumer struct {
38 mutable []xconsumer.Profiles
39 readonly []xconsumer.Profiles
40 }
41
42 func (tsc *profilesConsumer) Capabilities() consumer.Capabilities {
43 // If all consumers are mutating, then the original data will be passed to one of them.
44 return consumer.Capabilities{MutatesData: len(tsc.mutable) > 0 && len(tsc.readonly) == 0}
45 }
46
47 // ConsumeProfiles exports the pprofile.Profiles to all consumers wrapped by the current one.
48 func (tsc *profilesConsumer) ConsumeProfiles(ctx context.Context, td pprofile.Profiles) error {
49 var errs error
50
51 if len(tsc.mutable) > 0 {
52 // Clone the data before sending to all mutating consumers except the last one.
53 for i := 0; i < len(tsc.mutable)-1; i++ {
54 errs = multierr.Append(errs, tsc.mutable[i].ConsumeProfiles(ctx, cloneProfiles(td)))
55 }
56 // Send data as is to the last mutating consumer only if there are no other non-mutating consumers and the
57 // data is mutable. Never share the same data between a mutating and a non-mutating consumer since the
58 // non-mutating consumer may process data async and the mutating consumer may change the data before that.
59 lastConsumer := tsc.mutable[len(tsc.mutable)-1]
60 if len(tsc.readonly) == 0 && !td.IsReadOnly() {
61 errs = multierr.Append(errs, lastConsumer.ConsumeProfiles(ctx, td))
62 } else {
63 errs = multierr.Append(errs, lastConsumer.ConsumeProfiles(ctx, cloneProfiles(td)))
64 }
65 }
66
67 // Mark the data as read-only if it will be sent to more than one read-only consumer.
68 if len(tsc.readonly) > 1 && !td.IsReadOnly() {
69 td.MarkReadOnly()
70 }
71 for _, tc := range tsc.readonly {
72 errs = multierr.Append(errs, tc.ConsumeProfiles(ctx, td))
73 }
74
75 return errs
76 }
77
78 func cloneProfiles(td pprofile.Profiles) pprofile.Profiles {
79 clonedProfiles := pprofile.NewProfiles()
80 td.CopyTo(clonedProfiles)
81 return clonedProfiles
82 }