Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter][batcher] MergedContext implemented with SpanLink #12318

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/merged_context.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: exporterhelper

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Link batcher context to all batched request's span contexts.

# One or more tracking issues or pull requests related to the change
issues: [12212, 8122]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
4 changes: 2 additions & 2 deletions exporter/exporterhelper/internal/batch_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func TestBatchSender_BatchCancelled(t *testing.T) {
require.NoError(t, be.Shutdown(context.Background()))
})
}
runTest("enable_queue_batcher", true)
// When queue_batcher is enabled, we don't cancel the whole batch when the first request is canceled.
runTest("disable_queue_batcher", false)
}

Expand Down Expand Up @@ -622,7 +622,7 @@ func TestBatchSenderWithTimeout(t *testing.T) {
assert.EqualValues(t, 12, sink.ItemsCount())
})
}
runTest("enable_queue_batcher", true)
// When queue_batcher is enabled, we don't propagate context deadline.
runTest("disable_queue_batcher", false)
}

Expand Down
31 changes: 31 additions & 0 deletions exporter/exporterhelper/internal/batcher/batch_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package batcher // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/batcher"
import (
"context"

"go.opentelemetry.io/otel/trace"
)

type traceContextKeyType int

const batchSpanLinksKey traceContextKeyType = iota

// LinksFromContext returns a list of trace links registered in the context.
func LinksFromContext(ctx context.Context) []trace.Link {
if ctx == nil {
return []trace.Link{}
}

Check warning on line 19 in exporter/exporterhelper/internal/batcher/batch_context.go

View check run for this annotation

Codecov / codecov/patch

exporter/exporterhelper/internal/batcher/batch_context.go#L18-L19

Added lines #L18 - L19 were not covered by tests
if links, ok := ctx.Value(batchSpanLinksKey).([]trace.Link); ok {
return links
}
return []trace.Link{trace.LinkFromContext(ctx)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the batcher is disabled (or when it is enabled but only one request ends up in the batch), contextWithMergedLinks is never called, and we pass the parent context through directly to the obsreportsender. I think this is fine, but because the latter calls LinksFromContext, the parent span ends up as both the parent AND as a link.

It's not a big deal, but I think it would be better to create links only when we cut the trace, which is to say, when calling contextWithMergedLinks.

}

func contextWithMergedLinks(ctx1 context.Context, ctx2 context.Context) context.Context {
return context.WithValue(
context.Background(),
batchSpanLinksKey,
append(LinksFromContext(ctx1), LinksFromContext(ctx2)...))
}
39 changes: 39 additions & 0 deletions exporter/exporterhelper/internal/batcher/batch_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package batcher

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/collector/component/componenttest"
)

func TestBatchContextLink(t *testing.T) {
tracerProvider := componenttest.NewTelemetry().NewTelemetrySettings().TracerProvider
tracer := tracerProvider.Tracer("go.opentelemetry.io/collector/exporter/exporterhelper")

ctx1 := context.Background()

ctx2, span2 := tracer.Start(ctx1, "span2")
defer span2.End()

ctx3, span3 := tracer.Start(ctx1, "span3")
defer span3.End()

ctx4, span4 := tracer.Start(ctx1, "span4")
defer span4.End()

batchContext := contextWithMergedLinks(ctx2, ctx3)
batchContext = contextWithMergedLinks(batchContext, ctx4)

actualLinks := LinksFromContext(batchContext)
require.Len(t, actualLinks, 3)
require.Equal(t, trace.SpanContextFromContext(ctx2), actualLinks[0].SpanContext)
require.Equal(t, trace.SpanContextFromContext(ctx3), actualLinks[1].SpanContext)
require.Equal(t, trace.SpanContextFromContext(ctx4), actualLinks[2].SpanContext)
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ func (qb *defaultBatcher) Consume(ctx context.Context, req request.Request, done
// - Last result may not have enough data to be flushed.

// Logic on how to deal with the current batch:
// TODO: Deal with merging Context.
qb.currentBatch.req = reqList[0]
qb.currentBatch.done = append(qb.currentBatch.done, done)
qb.currentBatch.ctx = contextWithMergedLinks(qb.currentBatch.ctx, ctx)

// Save the "currentBatch" if we need to flush it, because we want to execute flush without holding the lock, and
// cannot unlock and re-lock because we are not done processing all the responses.
var firstBatch *batch
Expand Down
6 changes: 5 additions & 1 deletion exporter/exporterhelper/internal/obs_report_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/batcher"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/metadata"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
"go.opentelemetry.io/collector/pipeline"
Expand Down Expand Up @@ -95,7 +96,10 @@ func (ors *obsReportSender[K]) Send(ctx context.Context, req K) error {
// StartOp creates the span used to trace the operation. Returning
// the updated context and the created span.
func (ors *obsReportSender[K]) startOp(ctx context.Context) context.Context {
ctx, _ = ors.tracer.Start(ctx, ors.spanName, ors.spanAttrs)
ctx, _ = ors.tracer.Start(ctx,
ors.spanName,
ors.spanAttrs,
trace.WithLinks(batcher.LinksFromContext(ctx)...))
return ctx
}

Expand Down
8 changes: 8 additions & 0 deletions exporter/exporterhelper/internal/queue_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"context"
"errors"

"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter/exporterbatcher"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/batcher"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/metadata"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
"go.opentelemetry.io/collector/exporter/exporterqueue"
"go.opentelemetry.io/collector/featuregate"
Expand Down Expand Up @@ -123,7 +125,13 @@ func NewQueueSender(
// Have to read the number of items before sending the request since the request can
// be modified by the downstream components like the batcher.
itemsCount := req.ItemsCount()

// TODO: move start of span to enqueue instead to dequeue.
// Figure out how to preserve span context across persistent storage.
ctx, _ = metadata.Tracer(qSet.ExporterSettings.TelemetrySettings).Start(ctx, "exporter/enqueue")
err := next.Send(ctx, req)
trace.SpanFromContext(ctx).End()

if err != nil {
qSet.ExporterSettings.Logger.Error("Exporting failed. Dropping data."+exportFailureMessage,
zap.Error(err), zap.Int("dropped_items", itemsCount))
Expand Down
Loading