Skip to content
Merged
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
27 changes: 27 additions & 0 deletions .chloggen/elasticsearch-exporter-status.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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. filelogreceiver)
component: elasticsearchexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Report Elasticsearch request success / failure via componentstatus

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

# (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:

# 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]
16 changes: 16 additions & 0 deletions exporter/elasticsearchexporter/esclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package elasticsearchexporter // import "github.com/open-telemetry/opentelemetry
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"time"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/klauspost/compress/gzip"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componentstatus"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/sanitize"
Expand All @@ -26,6 +28,7 @@ type clientLogger struct {
*zap.Logger
logRequestBody bool
logResponseBody bool
componentHost component.Host
}

// LogRoundTrip should not modify the request or response, except for consuming and closing the body.
Expand Down Expand Up @@ -62,13 +65,25 @@ func (cl *clientLogger) LogRoundTrip(requ *http.Request, resp *http.Response, cl
zap.String("status", resp.Status),
)
zl.Debug("Request roundtrip completed.", fields...)
if resp.StatusCode == http.StatusOK {
// Success
componentstatus.ReportStatus(cl.componentHost, componentstatus.NewEvent(componentstatus.StatusOK))
} else if resp.StatusCode >= 300 {
// Error results
err := fmt.Errorf("Elasticsearch request failed: %v", resp.Status)
componentstatus.ReportStatus(
cl.componentHost, componentstatus.NewRecoverableErrorEvent(err))
}
Copy link

Choose a reason for hiding this comment

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

Are all of these recoverable? I would think a 401 would be non-recoverable without human intervention.

409s are not an error at all necessarily, when documents are truly duplicates. Is it worth special casing those? There should probably be an indicator they are happening but if someone explicitly is doing de-duplication via an _id in the document this will mark the exporter as degraded unnecessarily.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"non-recoverable" in the collector is very unforgiving, to the extent that almost nothing we could encounter truly satisfies its definition (which is that the component can never again return to a healthy state no matter what for the life of the process -- so its real meaning isn't "requires human intervention" it's "requires intervention and a restart of the process"). For example I've seen some setups where ingest credentials are synced to the ingest workers before they're actually activated upstream (I don't know why, but it shouldn't break us), or similarly a 401 can be a side effect of broken or unreliable proxy settings that can be resolved without restarting the client process.

Good point about 409s though, those should be telemetry numbers rather than a component error state, I will add a special case for that.

Copy link

Choose a reason for hiding this comment

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

Thanks, just special casing 409s makes sense to me then.

Copy link

Choose a reason for hiding this comment

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

Change looks good, probably worth a test case for 409s specifically since they are now meant not to trigger recovery from an error state.

Copy link

Choose a reason for hiding this comment

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

Thanks for the test!


case clientErr != nil:
fields = append(
fields,
zap.NamedError("reason", clientErr),
)
zl.Debug("Request failed.", fields...)
err := fmt.Errorf("Elasticsearch request failed: %w", clientErr)
componentstatus.ReportStatus(
cl.componentHost, componentstatus.NewRecoverableErrorEvent(err))
}

return nil
Expand Down Expand Up @@ -111,6 +126,7 @@ func newElasticsearchClient(
Logger: telemetry.Logger,
logRequestBody: config.LogRequestBody,
logResponseBody: config.LogResponseBody,
componentHost: host,
}

return elasticsearchv8.NewClient(elasticsearchv8.Config{
Expand Down
73 changes: 73 additions & 0 deletions exporter/elasticsearchexporter/esclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package elasticsearchexporter

import (
"io"
"net/http"
"net/url"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componentstatus"
"go.uber.org/zap"
)

func TestComponentStatus(t *testing.T) {
statusChan := make(chan *componentstatus.Event, 1)
reporter := &testStatusReporter{statusChan}
esLogger := clientLogger{
Logger: zap.New(nil),
logRequestBody: false,
logResponseBody: false,
componentHost: reporter,
}

// Pass in an error and make sure it's sent to the component status reporter
_ = esLogger.LogRoundTrip(nil, nil, io.EOF, time.Now(), 0)
select {
case event := <-statusChan:
assert.ErrorIs(t, event.Err(), io.EOF, "LogRoundTrip should report a component status error wrapping its error parameter")
assert.Equal(t, componentstatus.StatusRecoverableError, event.Status(), "LogRoundTrip on an error parameter should report a recoverable error")
default:
require.Fail(t, "LogRoundTrip with an error should report a recoverable error status")
}

// Pass in an http error status and make sure it's sent to the component status reporter
_ = esLogger.LogRoundTrip(&http.Request{URL: &url.URL{}}, &http.Response{StatusCode: 401, Status: "401 Unauthorized"}, nil, time.Now(), 0)
select {
case event := <-statusChan:
err := event.Err()
require.Error(t, err, "LogRoundTrip with an http error status should report a component status error")
assert.Contains(t, err.Error(), "401 Unauthorized", "LogRoundTrip with an http error status should include the status in its error state")
assert.Equal(t, componentstatus.StatusRecoverableError, event.Status(), "LogRoundTrip with an http error status should report a recoverable error")
default:
require.Fail(t, "LogRoundTrip with an http error code should report a recoverable error status")
}

// Pass in an http success status and make sure the component status returns to OK
_ = esLogger.LogRoundTrip(&http.Request{URL: &url.URL{}}, &http.Response{StatusCode: 200}, nil, time.Now(), 0)
select {
case event := <-statusChan:
assert.NoError(t, event.Err(), "LogRoundTrip with a success status shouldn't report a component status error")
assert.Equal(t, componentstatus.StatusOK, event.Status(), "LogRoundTrip with a success status should report component status OK")
default:
require.Fail(t, "LogRoundTrip with an http success should report component status OK")
}
}

type testStatusReporter struct {
statusChan chan *componentstatus.Event
}

func (tsr *testStatusReporter) Report(event *componentstatus.Event) {
tsr.statusChan <- event
}

func (tsr *testStatusReporter) GetExtensions() map[component.ID]component.Component {
return make(map[component.ID]component.Component)
}
1 change: 1 addition & 0 deletions exporter/elasticsearchexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/tidwall/gjson v1.18.0
go.opentelemetry.io/collector/client v1.30.1-0.20250422165940-c47951a8bf71
go.opentelemetry.io/collector/component v1.30.1-0.20250422165940-c47951a8bf71
go.opentelemetry.io/collector/component/componentstatus v0.124.1-0.20250422165940-c47951a8bf71
go.opentelemetry.io/collector/component/componenttest v0.124.1-0.20250422165940-c47951a8bf71
go.opentelemetry.io/collector/config/configauth v0.124.1-0.20250422165940-c47951a8bf71
go.opentelemetry.io/collector/config/configcompression v1.30.1-0.20250422165940-c47951a8bf71
Expand Down
2 changes: 2 additions & 0 deletions exporter/elasticsearchexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.