Debian Codegraph @

internal/fanoutconsumer/logs.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package fanoutconsumer contains implementations of Traces/Metrics/Logs consumers
5 // that fan out the data to multiple other consumers.
6 package fanoutconsumer // import "go.opentelemetry.io/collector/internal/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 // NewLogs wraps multiple log consumers in a single one.
18 // It fans out the incoming data to all the consumers, and does smart routing:
19 // - Clones only to the consumer that needs to mutate the data.
20 // - If all consumers needs to mutate the data one will get the original mutable data.
21 func NewLogs(lcs []consumer.Logs) consumer.Logs {
22 // Don't wrap if there is only one non-mutating consumer.
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 // If all consumers are mutating, then the original data will be passed to one of them.
45 return consumer.Capabilities{MutatesData: len(lsc.mutable) > 0 && len(lsc.readonly) == 0}
46 }
47
48 // ConsumeLogs exports the plog.Logs to all consumers wrapped by the current one.
49 func (lsc *logsConsumer) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
50 var errs error
51
52 if len(lsc.mutable) > 0 {
53 // Clone the data before sending to all mutating consumers except the last one.
54 for i := 0; i < len(lsc.mutable)-1; i++ {
55 errs = multierr.Append(errs, lsc.mutable[i].ConsumeLogs(ctx, cloneLogs(ld)))
56 }
57 // Send data as is to the last mutating consumer only if there are no other non-mutating consumers and the
58 // data is mutable. Never share the same data between a mutating and a non-mutating consumer since the
59 // non-mutating consumer may process data async and the mutating consumer may change the data before that.
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 // Mark the data as read-only if it will be sent to more than one read-only consumer.
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 }