Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions .chloggen/49208-opensearch-retry-transient-errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Use this changelog template to create an entry for release notes.

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

# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
component: exporter/opensearch

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Retry transient OpenSearch failures instead of dropping them as permanent errors

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [49208]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks like it also fixes #45147 and is closely related to #45120. Worth adding them to issues: so they get closed/linked


# (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: |
Indexer-level failures such as connection refused, timeouts, and DNS errors were wrapped as
permanent errors, so exporterhelper dropped the batch even with retry_on_failure enabled. They
are now surfaced as retryable, and per-item transport errors (net.OpError) are retried as well,
while encoding and bad-document errors remain permanent.

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# 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]
18 changes: 15 additions & 3 deletions exporter/opensearchexporter/log_bulk_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"errors"
"net"
"time"

"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
Expand Down Expand Up @@ -47,7 +48,11 @@ func (lbi *logBulkIndexer) close(ctx context.Context) {

func (lbi *logBulkIndexer) onIndexerError(_ context.Context, indexerErr error) {
if indexerErr != nil {
lbi.appendPermanentError(consumererror.NewPermanent(indexerErr))
// Indexer-level errors are transport/flush failures (connection refused,
// timeout, DNS). They are transient, so surface them as a retryable error
// and let exporterhelper's retry_on_failure resend the batch instead of
// dropping it as permanent.
lbi.errs = append(lbi.errs, indexerErr)
}
}

Expand Down Expand Up @@ -128,8 +133,15 @@ func (lbi *logBulkIndexer) processItemFailure(resp opensearchapi.BulkRespItem, i
// Non-recoverable OpenSearch error while indexing document
lbi.appendPermanentError(responseAsError(resp))
default:
// Encoding error. We didn't even attempt to send the event
lbi.appendPermanentError(itemErr)
// A transport error (e.g. net.OpError) reported per item is transient and
// should be retried; anything else here is an encoding error we never sent,
// which is permanent.
var netErr *net.OpError
if errors.As(itemErr, &netErr) {
lbi.appendRetryLogError(itemErr, logs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This only retries *net.OpError. Should a more exhaustive list (e.g. context.DeadlineExceeded, context.Canceled) be covered? Or, assuming we have a safe retry/back-off configuration, should we consider defaulting to retryable and reserve Permanent for errors we can positively ID as non-recoverable?

} else {
lbi.appendPermanentError(itemErr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On a flush failure, doesn't opensearchutil fire both OnFailure (per item, via handleBulkError) and OnError with the same error? Since IsPermanent is errors.As over the joined error, wouldn't this Permanent branch override the retryable onIndexerError fix and drop the batch anyway? See code here and here

}
}
}

Expand Down
37 changes: 37 additions & 0 deletions exporter/opensearchexporter/log_bulk_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package opensearchexporter

import (
"errors"
"net"
"testing"
"time"

"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
)
Expand Down Expand Up @@ -60,6 +62,41 @@ func TestProcessItemFailure(t *testing.T) {
}
}

func TestOnIndexerErrorIsRetryable(t *testing.T) {
lbi := &logBulkIndexer{}
// A transport failure surfaces through the bulk indexer's OnError callback.
lbi.onIndexerError(t.Context(), &net.OpError{Op: "dial", Err: errors.New("connection refused")})
err := lbi.joinedError()
if err == nil {
t.Fatal("expected an error")
}
if consumererror.IsPermanent(err) {
t.Error("indexer-level transport error must be retryable, not permanent (otherwise retry_on_failure silently drops the batch)")
}
}

func TestProcessItemFailureTransportErrorRetryable(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we drive a real flush via httptest and assert on the joined error, rather than hand-feeding *net.OpError? That would exercise the actual error types the transport produces. Something like:

func TestProcessItemFailureTransportRetryable(t *testing.T) {
	tests := []struct {
		name    string
		handler http.HandlerFunc // nil => point at a dead port
	}{
		{"connection refused", nil},
		{"connection drop mid-request", func(w http.ResponseWriter, _ *http.Request) {
			conn, _, _ := w.(http.Hijacker).Hijack()
			conn.Close()
		}},
		{"reset after read", func(w http.ResponseWriter, r *http.Request) {
			io.Copy(io.Discard, r.Body)
			conn, _, _ := w.(http.Hijacker).Hijack()
			if tcp, ok := conn.(*net.TCPConn); ok {
				tcp.SetLinger(0) // force RST
			}
			conn.Close()
		}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			endpoint := "http://127.0.0.1:1" // dead port
			if tt.handler != nil {
				srv := httptest.NewServer(tt.handler)
				defer srv.Close()
				endpoint = srv.URL
			}
			client, _ := newOpenSearchClient(endpoint, http.DefaultClient, zap.NewNop())
			idx := newLogBulkIndexer("create", model, "")
			require.NoError(t, idx.start(client))
			idx.submit(context.Background(), logs, ir, cfg, time.Now())
			idx.close(context.Background())

			err := idx.joinedError()
			require.Error(t, err)
			require.False(t, consumererror.IsPermanent(err),
				"transient flush failure must be retryable, else retry_on_failure drops the batch")
		})
	}
}

// A per-item transport error (net.OpError) with no HTTP status is transient.
lbi := &logBulkIndexer{}
lbi.processItemFailure(opensearchapi.BulkRespItem{Status: 0}, &net.OpError{Op: "read", Err: errors.New("i/o timeout")}, plog.NewLogs())
if len(lbi.errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(lbi.errs))
}
if consumererror.IsPermanent(lbi.errs[0]) {
t.Error("per-item transport error must be retryable, not permanent")
}

// A genuine encoding error (never sent) stays permanent.
lbi2 := &logBulkIndexer{}
lbi2.processItemFailure(opensearchapi.BulkRespItem{Status: 0}, errors.New("encode failed"), plog.NewLogs())
if len(lbi2.errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(lbi2.errs))
}
if !consumererror.IsPermanent(lbi2.errs[0]) {
t.Error("encoding error must remain permanent")
}
}

func TestNewLogBulkIndexerWithPipeline(t *testing.T) {
tests := []struct {
name string
Expand Down
4 changes: 3 additions & 1 deletion exporter/opensearchexporter/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ tests:
expect_consumer_error: true
config:
http:
endpoint: https://opensearch.example.com:9200
endpoint: https://opensearch.example.com:9200
retry_on_failure:
enabled: false
18 changes: 15 additions & 3 deletions exporter/opensearchexporter/trace_bulk_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"errors"
"net"
"slices"
"time"

Expand Down Expand Up @@ -49,7 +50,11 @@ func (tbi *traceBulkIndexer) close(ctx context.Context) {

func (tbi *traceBulkIndexer) onIndexerError(_ context.Context, indexerErr error) {
if indexerErr != nil {
tbi.appendPermanentError(consumererror.NewPermanent(indexerErr))
// Indexer-level errors are transport/flush failures (connection refused,
// timeout, DNS). They are transient, so surface them as a retryable error
// and let exporterhelper's retry_on_failure resend the batch instead of
// dropping it as permanent.
tbi.errs = append(tbi.errs, indexerErr)
}
}

Expand Down Expand Up @@ -130,8 +135,15 @@ func (tbi *traceBulkIndexer) processItemFailure(resp opensearchapi.BulkRespItem,
// Non-recoverable OpenSearch error while indexing document
tbi.appendPermanentError(responseAsError(resp))
default:
// Encoding error. We didn't even attempt to send the event
tbi.appendPermanentError(itemErr)
// A transport error (e.g. net.OpError) reported per item is transient and
// should be retried; anything else here is an encoding error we never sent,
// which is permanent.
var netErr *net.OpError
if errors.As(itemErr, &netErr) {
tbi.appendRetryTraceError(itemErr, traces)
} else {
tbi.appendPermanentError(itemErr)
}
}
}

Expand Down
34 changes: 34 additions & 0 deletions exporter/opensearchexporter/trace_bulk_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package opensearchexporter

import (
"errors"
"net"
"testing"
"time"

"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
)
Expand Down Expand Up @@ -60,6 +62,38 @@ func TestTraceProcessItemFailure(t *testing.T) {
}
}

func TestTraceOnIndexerErrorIsRetryable(t *testing.T) {
tbi := &traceBulkIndexer{}
tbi.onIndexerError(t.Context(), &net.OpError{Op: "dial", Err: errors.New("connection refused")})
err := tbi.joinedError()
if err == nil {
t.Fatal("expected an error")
}
if consumererror.IsPermanent(err) {
t.Error("indexer-level transport error must be retryable, not permanent")
}
}

func TestTraceProcessItemFailureTransportErrorRetryable(t *testing.T) {
tbi := &traceBulkIndexer{}
tbi.processItemFailure(opensearchapi.BulkRespItem{Status: 0}, &net.OpError{Op: "read", Err: errors.New("i/o timeout")}, ptrace.NewTraces())
if len(tbi.errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(tbi.errs))
}
if consumererror.IsPermanent(tbi.errs[0]) {
t.Error("per-item transport error must be retryable, not permanent")
}

tbi2 := &traceBulkIndexer{}
tbi2.processItemFailure(opensearchapi.BulkRespItem{Status: 0}, errors.New("encode failed"), ptrace.NewTraces())
if len(tbi2.errs) != 1 {
t.Fatalf("expected 1 error, got %d", len(tbi2.errs))
}
if !consumererror.IsPermanent(tbi2.errs[0]) {
t.Error("encoding error must remain permanent")
}
}

func TestNewTraceBulkIndexerWithPipeline(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading