| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func NewProfiles(tcs []xconsumer.Profiles) xconsumer.Profiles { |
| 21 |
|
| 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 |
|
| 44 |
return consumer.Capabilities{MutatesData: len(tsc.mutable) > 0 && len(tsc.readonly) == 0} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func (tsc *profilesConsumer) ConsumeProfiles(ctx context.Context, td pprofile.Profiles) error { |
| 49 |
var errs error |
| 50 |
|
| 51 |
if len(tsc.mutable) > 0 { |
| 52 |
|
| 53 |
for i := 0; i < len(tsc.mutable)-1; i++ { |
| 54 |
errs = multierr.Append(errs, tsc.mutable[i].ConsumeProfiles(ctx, cloneProfiles(td))) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
} |