| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package fanoutconsumer |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
|
| 11 |
"go.uber.org/multierr" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/consumer" |
| 14 |
"go.opentelemetry.io/collector/pdata/plog" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
func NewLogs(lcs []consumer.Logs) consumer.Logs { |
| 22 |
|
| 23 |
if len(lcs) == 1 && !lcs[0].Capabilities().MutatesData { |
| 24 |
return lcs[0] |
| 25 |
} |
| 26 |
|
| 27 |
lc := &logsConsumer{} |
| 28 |
for i := range lcs { |
| 29 |
if lcs[i].Capabilities().MutatesData { |
| 30 |
lc.mutable = append(lc.mutable, lcs[i]) |
| 31 |
} else { |
| 32 |
lc.readonly = append(lc.readonly, lcs[i]) |
| 33 |
} |
| 34 |
} |
| 35 |
return lc |
| 36 |
} |
| 37 |
|
| 38 |
type logsConsumer struct { |
| 39 |
mutable []consumer.Logs |
| 40 |
readonly []consumer.Logs |
| 41 |
} |
| 42 |
|
| 43 |
func (lsc *logsConsumer) Capabilities() consumer.Capabilities { |
| 44 |
|
| 45 |
return consumer.Capabilities{MutatesData: len(lsc.mutable) > 0 && len(lsc.readonly) == 0} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (lsc *logsConsumer) ConsumeLogs(ctx context.Context, ld plog.Logs) error { |
| 50 |
var errs error |
| 51 |
|
| 52 |
if len(lsc.mutable) > 0 { |
| 53 |
|
| 54 |
for i := 0; i < len(lsc.mutable)-1; i++ { |
| 55 |
errs = multierr.Append(errs, lsc.mutable[i].ConsumeLogs(ctx, cloneLogs(ld))) |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
lastConsumer := lsc.mutable[len(lsc.mutable)-1] |
| 61 |
if len(lsc.readonly) == 0 && !ld.IsReadOnly() { |
| 62 |
errs = multierr.Append(errs, lastConsumer.ConsumeLogs(ctx, ld)) |
| 63 |
} else { |
| 64 |
errs = multierr.Append(errs, lastConsumer.ConsumeLogs(ctx, cloneLogs(ld))) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
if len(lsc.readonly) > 1 && !ld.IsReadOnly() { |
| 70 |
ld.MarkReadOnly() |
| 71 |
} |
| 72 |
for _, lc := range lsc.readonly { |
| 73 |
errs = multierr.Append(errs, lc.ConsumeLogs(ctx, ld)) |
| 74 |
} |
| 75 |
|
| 76 |
return errs |
| 77 |
} |
| 78 |
|
| 79 |
func cloneLogs(ld plog.Logs) plog.Logs { |
| 80 |
clonedLogs := plog.NewLogs() |
| 81 |
ld.CopyTo(clonedLogs) |
| 82 |
return clonedLogs |
| 83 |
} |