From 03212ef8ebec641b1453f8216564f32b34316e0a Mon Sep 17 00:00:00 2001 From: Joshua MacDonald Date: Wed, 5 Jun 2024 09:15:43 -0700 Subject: [PATCH] Avoid work for already-canceled requests (#213) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is consistent with gRPC behavior for unary exporters, as with for HTTP exporters. It also helps us test with realistic conditions in the new integration test of #210. https://grpc.io/docs/guides/deadlines/ explains why the default behavior is the way it is for unary gRPC. This is also the case for Golang's net/http client, which is used for the OTLP/HTTP exporter. Therefore, it seems the right thing to do for Arrow as well. Further, if we were to support extending the deadline on purpose, it would belong in the exporterhelper code (IMO). --------- Co-authored-by: Laurent Quérel --- .../exporter/otelarrowexporter/internal/arrow/exporter.go | 8 ++++++++ .../otelarrowexporter/internal/arrow/exporter_test.go | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/collector/exporter/otelarrowexporter/internal/arrow/exporter.go b/collector/exporter/otelarrowexporter/internal/arrow/exporter.go index 4b9af2de..213a5a28 100644 --- a/collector/exporter/otelarrowexporter/internal/arrow/exporter.go +++ b/collector/exporter/otelarrowexporter/internal/arrow/exporter.go @@ -257,6 +257,14 @@ func (e *Exporter) runArrowStream(ctx context.Context, dc doneCancel, state *str // // consumer should fall back to standard OTLP, (true, nil) func (e *Exporter) SendAndWait(ctx context.Context, data any) (bool, error) { + // If the incoming context is already canceled, return the + // same error condition a unary gRPC or HTTP exporter would do. + select { + case <-ctx.Done(): + return false, status.Errorf(codes.Canceled, "context done before send: %v", ctx.Err()) + default: + } + errCh := make(chan error, 1) // Note that if the OTLP exporter's gRPC Headers field was diff --git a/collector/exporter/otelarrowexporter/internal/arrow/exporter_test.go b/collector/exporter/otelarrowexporter/internal/arrow/exporter_test.go index 7047d740..8769d1e8 100644 --- a/collector/exporter/otelarrowexporter/internal/arrow/exporter_test.go +++ b/collector/exporter/otelarrowexporter/internal/arrow/exporter_test.go @@ -284,6 +284,13 @@ func TestArrowExporterTimeout(t *testing.T) { require.True(t, is, "is a gRPC status") require.Equal(t, codes.Canceled, stat.Code()) + // Repeat the request, will get immediate timeout. + sent, err = tc.exporter.SendAndWait(ctx, twoTraces) + stat, is = status.FromError(err) + require.True(t, is, "is a gRPC status error: %v", err) + require.Equal(t, "context done before send: context canceled", stat.Message()) + require.Equal(t, codes.Canceled, stat.Code()) + require.NoError(t, tc.exporter.Shutdown(ctx)) }) }