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

Remove context check on stdout exporters #5189

Merged
merged 22 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
509c602
Removed redundant code. Return no error.
prasad-shirodkar Apr 10, 2024
1cf07b6
Merge remote-tracking branch 'upstream/main' into remove_context_check
prasad-shirodkar Apr 17, 2024
326e7f6
handle cancellations
prasad-shirodkar Apr 17, 2024
3dae347
removed flaky deadline test case
prasad-shirodkar Apr 17, 2024
48d3eb8
added tests for ignoring context errors
prasad-shirodkar Apr 17, 2024
0d63602
Merge remote-tracking branch 'upstream/main' into remove_context_check
prasad-shirodkar Apr 17, 2024
7c88d49
updated changelog
prasad-shirodkar Apr 17, 2024
34908f3
renamed to TestExporterShutdownIgnoresContext
prasad-shirodkar Apr 17, 2024
7373862
updated changelog
prasad-shirodkar Apr 17, 2024
fa49342
removed DeadlineExceeded Ignored test case
prasad-shirodkar Apr 17, 2024
261659f
removed redundant test cases
prasad-shirodkar Apr 17, 2024
5edd3cb
removed flaky test case TestExporterShutdownHonorsTimeout
prasad-shirodkar Apr 17, 2024
a93250e
Merge remote-tracking branch 'upstream/main' into remove_context_check
prasad-shirodkar Apr 20, 2024
1909f88
simplified to background context
prasad-shirodkar Apr 20, 2024
c9f7c08
reverted
prasad-shirodkar Apr 20, 2024
477d2ed
Merge remote-tracking branch 'upstream/main' into remove_context_check
prasad-shirodkar Apr 22, 2024
0094a5a
Merge remote-tracking branch 'upstream/main' into remove_context_check
prasad-shirodkar Apr 23, 2024
638c349
Merge remote-tracking branch 'upstream/main' into remove_context_check
prasad-shirodkar Apr 24, 2024
53b3bba
Merge branch 'main' into remove_context_check
pellared Apr 25, 2024
e9b4df0
Update CHANGELOG.md
pellared Apr 25, 2024
450bc87
Merge branch 'main' into remove_context_check
pellared Apr 25, 2024
10b6e63
Update CHANGELOG.md
pellared Apr 25, 2024
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
16 changes: 6 additions & 10 deletions exporters/stdout/stdoutmetric/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ func (e *exporter) Aggregation(k metric.InstrumentKind) metric.Aggregation {
}

func (e *exporter) Export(ctx context.Context, data *metricdata.ResourceMetrics) error {
select {
case <-ctx.Done():
// Don't do anything if the context has already timed out.
return ctx.Err()
default:
// Context is still valid, continue.
if err := ctx.Err(); err != nil {
return err
}
if e.redactTimestamps {
redactTimestamps(data)
Expand All @@ -67,18 +63,18 @@ func (e *exporter) Export(ctx context.Context, data *metricdata.ResourceMetrics)
return e.encVal.Load().(encoderHolder).Encode(data)
}

func (e *exporter) ForceFlush(ctx context.Context) error {
func (e *exporter) ForceFlush(context.Context) error {
// exporter holds no state, nothing to flush.
return ctx.Err()
return nil
}

func (e *exporter) Shutdown(ctx context.Context) error {
func (e *exporter) Shutdown(context.Context) error {
e.shutdownOnce.Do(func() {
e.encVal.Store(encoderHolder{
encoder: shutdownEncoder{},
})
})
return ctx.Err()
return nil
}

func (e *exporter) MarshalLog() interface{} {
Expand Down
27 changes: 14 additions & 13 deletions exporters/stdout/stdoutmetric/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func testCtxErrHonored(factory func(*testing.T) func(context.Context) error) fun
t.Run("DeadlineExceeded", func(t *testing.T) {
innerCtx, innerCancel := context.WithTimeout(ctx, time.Nanosecond)
t.Cleanup(innerCancel)
<-innerCtx.Done()
pellared marked this conversation as resolved.
Show resolved Hide resolved

f := factory(t)
assert.ErrorIs(t, f(innerCtx), context.DeadlineExceeded)
Expand All @@ -56,18 +55,6 @@ func testCtxErrHonored(factory func(*testing.T) func(context.Context) error) fun
}

func TestExporterHonorsContextErrors(t *testing.T) {
t.Run("Shutdown", testCtxErrHonored(func(t *testing.T) func(context.Context) error {
pellared marked this conversation as resolved.
Show resolved Hide resolved
exp, err := stdoutmetric.New(testEncoderOption())
require.NoError(t, err)
return exp.Shutdown
}))

t.Run("ForceFlush", testCtxErrHonored(func(t *testing.T) func(context.Context) error {
exp, err := stdoutmetric.New(testEncoderOption())
require.NoError(t, err)
return exp.ForceFlush
}))

t.Run("Export", testCtxErrHonored(func(t *testing.T) func(context.Context) error {
exp, err := stdoutmetric.New(testEncoderOption())
require.NoError(t, err)
Expand All @@ -78,6 +65,20 @@ func TestExporterHonorsContextErrors(t *testing.T) {
}))
}

func TestExporterShutdown(t *testing.T) {
exporter, err := stdoutmetric.New(testEncoderOption())
assert.NoError(t, err)

assert.NoError(t, exporter.Shutdown(context.Background()))
}

func TestExporterForceFlush(t *testing.T) {
exporter, err := stdoutmetric.New(testEncoderOption())
assert.NoError(t, err)

assert.NoError(t, exporter.ForceFlush(context.Background()))
}

pellared marked this conversation as resolved.
Show resolved Hide resolved
func TestShutdownExporterReturnsShutdownErrorOnExport(t *testing.T) {
var (
data = new(metricdata.ResourceMetrics)
Expand Down
8 changes: 3 additions & 5 deletions exporters/stdout/stdouttrace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type Exporter struct {

// ExportSpans writes spans in json format to stdout.
func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
if err := ctx.Err(); err != nil {
return err
}
e.stoppedMu.RLock()
stopped := e.stopped
e.stoppedMu.RUnlock()
Expand Down Expand Up @@ -88,11 +91,6 @@ func (e *Exporter) Shutdown(ctx context.Context) error {
e.stopped = true
e.stoppedMu.Unlock()

select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}

Expand Down
40 changes: 31 additions & 9 deletions exporters/stdout/stdouttrace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,53 @@ func TestExporterExportSpan(t *testing.T) {
tests := []struct {
opts []stdouttrace.Option
expectNow time.Time
ctx context.Context
wantError error
pellared marked this conversation as resolved.
Show resolved Hide resolved
}{
{
opts: []stdouttrace.Option{stdouttrace.WithPrettyPrint()},
expectNow: now,
ctx: context.Background(),
},
{
opts: []stdouttrace.Option{stdouttrace.WithPrettyPrint(), stdouttrace.WithoutTimestamps()},
// expectNow is an empty time.Time
ctx: context.Background(),
},
{
opts: []stdouttrace.Option{},
ctx: func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}(),
wantError: context.Canceled,
},
{
opts: []stdouttrace.Option{},
ctx: func() context.Context {
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
t.Cleanup(cancel)
return ctx
}(),
wantError: context.DeadlineExceeded,
pellared marked this conversation as resolved.
Show resolved Hide resolved
},
}

ctx := context.Background()
for _, tt := range tests {
// write to buffer for testing
var b bytes.Buffer
ex, err := stdouttrace.New(append(tt.opts, stdouttrace.WithWriter(&b))...)
require.Nil(t, err)

err = ex.ExportSpans(ctx, tracetest.SpanStubs{ss, ss}.Snapshots())
require.Nil(t, err)
err = ex.ExportSpans(tt.ctx, tracetest.SpanStubs{ss, ss}.Snapshots())
assert.Equal(t, tt.wantError, err)

got := b.String()
wantone := expectedJSON(tt.expectNow)
assert.Equal(t, wantone+wantone, got)
if tt.wantError == nil {
got := b.String()
wantone := expectedJSON(tt.expectNow)
assert.Equal(t, wantone+wantone, got)
}
}
}

Expand Down Expand Up @@ -192,9 +215,8 @@ func TestExporterShutdownHonorsTimeout(t *testing.T) {

innerCtx, innerCancel := context.WithTimeout(ctx, time.Nanosecond)
defer innerCancel()
<-innerCtx.Done()
err = e.Shutdown(innerCtx)
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.NoError(t, err)
pellared marked this conversation as resolved.
Show resolved Hide resolved
}

func TestExporterShutdownHonorsCancel(t *testing.T) {
Expand All @@ -209,7 +231,7 @@ func TestExporterShutdownHonorsCancel(t *testing.T) {
innerCtx, innerCancel := context.WithCancel(ctx)
innerCancel()
err = e.Shutdown(innerCtx)
assert.ErrorIs(t, err, context.Canceled)
assert.NoError(t, err)
pellared marked this conversation as resolved.
Show resolved Hide resolved
}

func TestExporterShutdownNoError(t *testing.T) {
Expand Down