Skip to content

Commit 662a4c4

Browse files
authored
[chore] update code to use WithoutCancel (#10397)
This was introduced in go 1.21 and removes the need for a custom implementation of the context. Fixes #9049 Signed-off-by: Alex Boten <[email protected]>
1 parent 1889d58 commit 662a4c4

File tree

3 files changed

+5
-22
lines changed

3 files changed

+5
-22
lines changed

exporter/exporterhelper/obsexporter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (or *ObsReport) StartTracesOp(ctx context.Context) context.Context {
6969
// EndTracesOp completes the export operation that was started with StartTracesOp.
7070
func (or *ObsReport) EndTracesOp(ctx context.Context, numSpans int, err error) {
7171
numSent, numFailedToSend := toNumItems(numSpans, err)
72-
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeTraces, numSent, numFailedToSend)
72+
or.recordMetrics(context.WithoutCancel(ctx), component.DataTypeTraces, numSent, numFailedToSend)
7373
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentSpansKey, obsmetrics.FailedToSendSpansKey)
7474
}
7575

@@ -84,7 +84,7 @@ func (or *ObsReport) StartMetricsOp(ctx context.Context) context.Context {
8484
// StartMetricsOp.
8585
func (or *ObsReport) EndMetricsOp(ctx context.Context, numMetricPoints int, err error) {
8686
numSent, numFailedToSend := toNumItems(numMetricPoints, err)
87-
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeMetrics, numSent, numFailedToSend)
87+
or.recordMetrics(context.WithoutCancel(ctx), component.DataTypeMetrics, numSent, numFailedToSend)
8888
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentMetricPointsKey, obsmetrics.FailedToSendMetricPointsKey)
8989
}
9090

@@ -98,7 +98,7 @@ func (or *ObsReport) StartLogsOp(ctx context.Context) context.Context {
9898
// EndLogsOp completes the export operation that was started with StartLogsOp.
9999
func (or *ObsReport) EndLogsOp(ctx context.Context, numLogRecords int, err error) {
100100
numSent, numFailedToSend := toNumItems(numLogRecords, err)
101-
or.recordMetrics(noCancellationContext{Context: ctx}, component.DataTypeLogs, numSent, numFailedToSend)
101+
or.recordMetrics(context.WithoutCancel(ctx), component.DataTypeLogs, numSent, numFailedToSend)
102102
endSpan(ctx, err, numSent, numFailedToSend, obsmetrics.SentLogRecordsKey, obsmetrics.FailedToSendLogRecordsKey)
103103
}
104104

exporter/exporterhelper/queue_sender.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package exporterhelper // import "go.opentelemetry.io/collector/exporter/exporte
66
import (
77
"context"
88
"errors"
9-
"time"
109

1110
"go.opentelemetry.io/otel/attribute"
1211
"go.opentelemetry.io/otel/trace"
@@ -120,7 +119,7 @@ func (qs *queueSender) Shutdown(ctx context.Context) error {
120119
func (qs *queueSender) send(ctx context.Context, req Request) error {
121120
// Prevent cancellation and deadline to propagate to the context stored in the queue.
122121
// The grpc/http based receivers will cancel the request context after this function returns.
123-
c := noCancellationContext{Context: ctx}
122+
c := context.WithoutCancel(ctx)
124123

125124
span := trace.SpanFromContext(c)
126125
if err := qs.queue.Offer(c, req); err != nil {
@@ -131,19 +130,3 @@ func (qs *queueSender) send(ctx context.Context, req Request) error {
131130
span.AddEvent("Enqueued item.", trace.WithAttributes(qs.traceAttribute))
132131
return nil
133132
}
134-
135-
type noCancellationContext struct {
136-
context.Context
137-
}
138-
139-
func (noCancellationContext) Deadline() (deadline time.Time, ok bool) {
140-
return
141-
}
142-
143-
func (noCancellationContext) Done() <-chan struct{} {
144-
return nil
145-
}
146-
147-
func (noCancellationContext) Err() error {
148-
return nil
149-
}

exporter/exporterhelper/queue_sender_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func TestNoCancellationContext(t *testing.T) {
235235
require.True(t, ok)
236236
require.Equal(t, deadline, d)
237237

238-
nctx := noCancellationContext{Context: ctx}
238+
nctx := context.WithoutCancel(ctx)
239239
assert.NoError(t, nctx.Err())
240240
d, ok = nctx.Deadline()
241241
assert.False(t, ok)

0 commit comments

Comments
 (0)