Debian Codegraph @

service/internal/obsconsumer/profiles.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/consumer/xconsumer"
14 "go.opentelemetry.io/collector/internal/telemetry"
15 "go.opentelemetry.io/collector/pdata/pprofile"
16 )
17
18 var (
19 _ xconsumer.Profiles = obsProfiles{}
20 profilesMarshaler = pprofile.ProtoMarshaler{}
21 )
22
23 func NewProfiles(cons xconsumer.Profiles, set Settings, opts ...Option) xconsumer.Profiles {
24 if !telemetry.NewPipelineTelemetryGate.IsEnabled() {
25 return cons
26 }
27
28 o := options{}
29 for _, opt := range opts {
30 opt(&o)
31 }
32
33 consumerSet := Settings{
34 ItemCounter: set.ItemCounter,
35 SizeCounter: set.SizeCounter,
36 Logger: set.Logger.With(telemetry.ToZapFields(o.staticDataPointAttributes)...),
37 }
38
39 return obsProfiles{
40 consumer: cons,
41 set: consumerSet,
42 compiledOptions: o.compile(),
43 }
44 }
45
46 type obsProfiles struct {
47 consumer xconsumer.Profiles
48 set Settings
49 compiledOptions
50 }
51
52 // ConsumeProfiles measures telemetry before calling ConsumeProfiles because the data may be mutated downstream
53 func (c obsProfiles) ConsumeProfiles(ctx context.Context, pd pprofile.Profiles) error {
54 // Measure before calling ConsumeProfiles because the data may be mutated downstream
55 attrs := &c.withSuccessAttrs
56
57 itemCount := pd.SampleCount()
58 defer func() {
59 c.set.ItemCounter.Add(ctx, int64(itemCount), *attrs)
60 }()
61
62 if isEnabled(ctx, c.set.SizeCounter) {
63 byteCount := int64(profilesMarshaler.ProfilesSize(pd))
64 defer func() {
65 c.set.SizeCounter.Add(ctx, byteCount, *attrs)
66 }()
67 }
68
69 err := c.consumer.ConsumeProfiles(ctx, pd)
70 if err != nil {
71 if consumererror.IsDownstream(err) {
72 attrs = &c.withRefusedAttrs
73 } else {
74 attrs = &c.withFailureAttrs
75 err = consumererror.NewDownstream(err)
76 }
77 if c.set.Logger.Core().Enabled(zap.DebugLevel) {
78 c.set.Logger.Debug("Profiles pipeline component had an error", zap.Error(err), zap.Int("item count", itemCount))
79 }
80 }
81 return err
82 }
83
84 func (c obsProfiles) Capabilities() consumer.Capabilities {
85 return c.consumer.Capabilities()
86 }