Debian Codegraph @

service/internal/obsconsumer/traces.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package obsconsumer // import "go.opentelemetry.io/collector/service/internal/obsconsumer"
5
6 import (
7 "context"
8
9 "go.uber.org/zap"
10
11 "go.opentelemetry.io/collector/consumer"
12 "go.opentelemetry.io/collector/consumer/consumererror"
13 "go.opentelemetry.io/collector/internal/telemetry"
14 "go.opentelemetry.io/collector/pdata/ptrace"
15 )
16
17 var (
18 _ consumer.Traces = obsTraces{}
19 tracesMarshaler = &ptrace.ProtoMarshaler{}
20 )
21
22 func NewTraces(cons consumer.Traces, set Settings, opts ...Option) consumer.Traces {
23 if !telemetry.NewPipelineTelemetryGate.IsEnabled() {
24 return cons
25 }
26
27 o := options{}
28 for _, opt := range opts {
29 opt(&o)
30 }
31
32 consumerSet := Settings{
33 ItemCounter: set.ItemCounter,
34 SizeCounter: set.SizeCounter,
35 Logger: set.Logger.With(telemetry.ToZapFields(o.staticDataPointAttributes)...),
36 }
37
38 return obsTraces{
39 consumer: cons,
40 set: consumerSet,
41 compiledOptions: o.compile(),
42 }
43 }
44
45 type obsTraces struct {
46 consumer consumer.Traces
47 set Settings
48 compiledOptions
49 }
50
51 // ConsumeTraces measures telemetry before calling ConsumeTraces because the data may be mutated downstream
52 func (c obsTraces) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
53 // Use a pointer to so that deferred function can depend on the result of ConsumeTraces
54 attrs := &c.withSuccessAttrs
55
56 itemCount := td.SpanCount()
57 defer func() {
58 c.set.ItemCounter.Add(ctx, int64(itemCount), *attrs)
59 }()
60
61 if isEnabled(ctx, c.set.SizeCounter) {
62 byteCount := int64(tracesMarshaler.TracesSize(td))
63 defer func() {
64 c.set.SizeCounter.Add(ctx, byteCount, *attrs)
65 }()
66 }
67
68 err := c.consumer.ConsumeTraces(ctx, td)
69 if err != nil {
70 if consumererror.IsDownstream(err) {
71 attrs = &c.withRefusedAttrs
72 } else {
73 attrs = &c.withFailureAttrs
74 err = consumererror.NewDownstream(err)
75 }
76 if c.set.Logger.Core().Enabled(zap.DebugLevel) {
77 c.set.Logger.Debug("Traces pipeline component had an error", zap.Error(err), zap.Int("item count", itemCount))
78 }
79 }
80 return err
81 }
82
83 func (c obsTraces) Capabilities() consumer.Capabilities {
84 return c.consumer.Capabilities()
85 }