Debian Codegraph @

receiver/receiverhelper/obsreport.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 //go:generate mdatagen metadata.yaml
5
6 package receiverhelper // import "go.opentelemetry.io/collector/receiver/receiverhelper"
7
8 import (
9 "context"
10
11 "go.opentelemetry.io/otel/attribute"
12 "go.opentelemetry.io/otel/codes"
13 "go.opentelemetry.io/otel/metric"
14 "go.opentelemetry.io/otel/trace"
15
16 "go.opentelemetry.io/collector/component"
17 "go.opentelemetry.io/collector/consumer/consumererror"
18 "go.opentelemetry.io/collector/pipeline"
19 "go.opentelemetry.io/collector/receiver"
20 "go.opentelemetry.io/collector/receiver/receiverhelper/internal"
21 "go.opentelemetry.io/collector/receiver/receiverhelper/internal/metadata"
22 )
23
24 // ObsReport is a helper to add observability to a receiver.
25 type ObsReport struct {
26 spanNamePrefix string
27 transport string
28 longLivedCtx bool
29 tracer trace.Tracer
30
31 otelAttrs metric.MeasurementOption
32 telemetryBuilder *metadata.TelemetryBuilder
33 }
34
35 // ObsReportSettings are settings for creating an ObsReport.
36 type ObsReportSettings struct {
37 ReceiverID component.ID
38 Transport string
39 // LongLivedCtx when true indicates that the context passed in the call
40 // outlives the individual receive operation.
41 // Typically the long lived context is associated to a connection,
42 // eg.: a gRPC stream, for which many batches of data are received in individual
43 // operations without a corresponding new context per operation.
44 LongLivedCtx bool
45 ReceiverCreateSettings receiver.Settings
46
47 // prevent unkeyed literal initialization
48 _ struct{}
49 }
50
51 // NewObsReport creates a new ObsReport.
52 func NewObsReport(cfg ObsReportSettings) (*ObsReport, error) {
53 return newReceiver(cfg)
54 }
55
56 func newReceiver(cfg ObsReportSettings) (*ObsReport, error) {
57 telemetryBuilder, err := metadata.NewTelemetryBuilder(cfg.ReceiverCreateSettings.TelemetrySettings)
58 if err != nil {
59 return nil, err
60 }
61 return &ObsReport{
62 spanNamePrefix: internal.ReceiverKey + internal.SpanNameSep + cfg.ReceiverID.String(),
63 transport: cfg.Transport,
64 longLivedCtx: cfg.LongLivedCtx,
65 tracer: cfg.ReceiverCreateSettings.TracerProvider.Tracer(cfg.ReceiverID.String()),
66
67 otelAttrs: metric.WithAttributeSet(attribute.NewSet(
68 attribute.String(internal.ReceiverKey, cfg.ReceiverID.String()),
69 attribute.String(internal.TransportKey, cfg.Transport),
70 )),
71 telemetryBuilder: telemetryBuilder,
72 }, nil
73 }
74
75 // StartTracesOp is called when a request is received from a client.
76 // The returned context should be used in other calls to the obsreport functions
77 // dealing with the same receive operation.
78 func (rec *ObsReport) StartTracesOp(operationCtx context.Context) context.Context {
79 return rec.startOp(operationCtx, internal.ReceiveTraceDataOperationSuffix)
80 }
81
82 // EndTracesOp completes the receive operation that was started with
83 // StartTracesOp.
84 func (rec *ObsReport) EndTracesOp(
85 receiverCtx context.Context,
86 format string,
87 numReceivedSpans int,
88 err error,
89 ) {
90 rec.endOp(receiverCtx, format, numReceivedSpans, err, pipeline.SignalTraces)
91 }
92
93 // StartLogsOp is called when a request is received from a client.
94 // The returned context should be used in other calls to the obsreport functions
95 // dealing with the same receive operation.
96 func (rec *ObsReport) StartLogsOp(operationCtx context.Context) context.Context {
97 return rec.startOp(operationCtx, internal.ReceiverLogsOperationSuffix)
98 }
99
100 // EndLogsOp completes the receive operation that was started with
101 // StartLogsOp.
102 func (rec *ObsReport) EndLogsOp(
103 receiverCtx context.Context,
104 format string,
105 numReceivedLogRecords int,
106 err error,
107 ) {
108 rec.endOp(receiverCtx, format, numReceivedLogRecords, err, pipeline.SignalLogs)
109 }
110
111 // StartMetricsOp is called when a request is received from a client.
112 // The returned context should be used in other calls to the obsreport functions
113 // dealing with the same receive operation.
114 func (rec *ObsReport) StartMetricsOp(operationCtx context.Context) context.Context {
115 return rec.startOp(operationCtx, internal.ReceiverMetricsOperationSuffix)
116 }
117
118 // EndMetricsOp completes the receive operation that was started with
119 // StartMetricsOp.
120 func (rec *ObsReport) EndMetricsOp(
121 receiverCtx context.Context,
122 format string,
123 numReceivedPoints int,
124 err error,
125 ) {
126 rec.endOp(receiverCtx, format, numReceivedPoints, err, pipeline.SignalMetrics)
127 }
128
129 // startOp creates the span used to trace the operation. Returning
130 // the updated context with the created span.
131 func (rec *ObsReport) startOp(receiverCtx context.Context, operationSuffix string) context.Context {
132 var ctx context.Context
133 var span trace.Span
134 spanName := rec.spanNamePrefix + operationSuffix
135 if !rec.longLivedCtx {
136 ctx, span = rec.tracer.Start(receiverCtx, spanName)
137 } else {
138 // Since the receiverCtx is long lived do not use it to start the span.
139 // This way this trace ends when the EndTracesOp is called.
140 // Here is safe to ignore the returned context since it is not used below.
141 _, span = rec.tracer.Start(context.Background(), spanName, trace.WithLinks(trace.Link{
142 SpanContext: trace.SpanContextFromContext(receiverCtx),
143 }))
144
145 ctx = trace.ContextWithSpan(receiverCtx, span)
146 }
147
148 if rec.transport != "" {
149 span.SetAttributes(attribute.String(internal.TransportKey, rec.transport))
150 }
151 return ctx
152 }
153
154 // endOp records the observability signals at the end of an operation.
155 func (rec *ObsReport) endOp(
156 receiverCtx context.Context,
157 format string,
158 numReceivedItems int,
159 err error,
160 signal pipeline.Signal,
161 ) {
162 numAccepted := numReceivedItems
163 numRefused := 0
164 numFailedErrors := 0
165 if err != nil {
166 numAccepted = 0
167 // If gate is enabled, we distinguish between refused and failed.
168 if NewReceiverMetricsGate.IsEnabled() {
169 if consumererror.IsDownstream(err) {
170 numRefused = numReceivedItems
171 } else {
172 numFailedErrors = numReceivedItems
173 }
174 } else {
175 // When the gate is disabled, all errors are considered "refused".
176 numRefused = numReceivedItems
177 }
178 }
179
180 span := trace.SpanFromContext(receiverCtx)
181
182 rec.recordMetrics(receiverCtx, signal, numAccepted, numRefused, numFailedErrors)
183
184 // The new otelcol_receiver_requests metric is only emitted when the feature gate is enabled.
185 if NewReceiverMetricsGate.IsEnabled() {
186 var outcome string
187 switch {
188 case err == nil:
189 outcome = "success"
190 case consumererror.IsDownstream(err):
191 outcome = "refused"
192 default:
193 outcome = "failure"
194 }
195 rec.telemetryBuilder.ReceiverRequests.Add(receiverCtx, 1, rec.otelAttrs, metric.WithAttributeSet(attribute.NewSet(attribute.String("outcome", outcome))))
196 }
197
198 // end span according to errors
199 if span.IsRecording() {
200 var acceptedItemsKey, refusedItemsKey, failedItemsKey string
201 switch signal {
202 case pipeline.SignalTraces:
203 acceptedItemsKey = internal.AcceptedSpansKey
204 refusedItemsKey = internal.RefusedSpansKey
205 failedItemsKey = internal.FailedSpansKey
206 case pipeline.SignalMetrics:
207 acceptedItemsKey = internal.AcceptedMetricPointsKey
208 refusedItemsKey = internal.RefusedMetricPointsKey
209 failedItemsKey = internal.FailedMetricPointsKey
210 case pipeline.SignalLogs:
211 acceptedItemsKey = internal.AcceptedLogRecordsKey
212 refusedItemsKey = internal.RefusedLogRecordsKey
213 failedItemsKey = internal.FailedLogRecordsKey
214 }
215
216 span.SetAttributes(
217 attribute.String(internal.FormatKey, format),
218 attribute.Int64(acceptedItemsKey, int64(numAccepted)),
219 attribute.Int64(refusedItemsKey, int64(numRefused)),
220 attribute.Int64(failedItemsKey, int64(numFailedErrors)),
221 )
222 if err != nil {
223 span.SetStatus(codes.Error, err.Error())
224 }
225 }
226 span.End()
227 }
228
229 func (rec *ObsReport) recordMetrics(receiverCtx context.Context, signal pipeline.Signal, numAccepted, numRefused, numFailedErrors int) {
230 var acceptedMeasure, refusedMeasure, failedMeasure metric.Int64Counter
231 switch signal {
232 case pipeline.SignalTraces:
233 acceptedMeasure = rec.telemetryBuilder.ReceiverAcceptedSpans
234 refusedMeasure = rec.telemetryBuilder.ReceiverRefusedSpans
235 failedMeasure = rec.telemetryBuilder.ReceiverFailedSpans
236 case pipeline.SignalMetrics:
237 acceptedMeasure = rec.telemetryBuilder.ReceiverAcceptedMetricPoints
238 refusedMeasure = rec.telemetryBuilder.ReceiverRefusedMetricPoints
239 failedMeasure = rec.telemetryBuilder.ReceiverFailedMetricPoints
240 case pipeline.SignalLogs:
241 acceptedMeasure = rec.telemetryBuilder.ReceiverAcceptedLogRecords
242 refusedMeasure = rec.telemetryBuilder.ReceiverRefusedLogRecords
243 failedMeasure = rec.telemetryBuilder.ReceiverFailedLogRecords
244 }
245
246 acceptedMeasure.Add(receiverCtx, int64(numAccepted), rec.otelAttrs)
247 refusedMeasure.Add(receiverCtx, int64(numRefused), rec.otelAttrs)
248 failedMeasure.Add(receiverCtx, int64(numFailedErrors), rec.otelAttrs)
249 }