Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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]
24 changes: 24 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 httpRecoverableErrorStatus(resp.StatusCode) {
err := fmt.Errorf("Elasticsearch request failed: %v", resp.Status)
componentstatus.ReportStatus(
cl.componentHost, componentstatus.NewRecoverableErrorEvent(err))
}

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 Expand Up @@ -169,3 +185,11 @@ func createElasticsearchBackoffFunc(config *RetrySettings) func(int) time.Durati
return expBackoff.NextBackOff()
}
}

func httpRecoverableErrorStatus(statusCode int) bool {
// Elasticsearch uses 409 conflict to report duplicates, which aren't really
// an error state, so those return false (but if we were already in an error
// state, we will still wait until we get an actual 200 OK before changing
// our state back).
return statusCode >= 300 && statusCode != http.StatusConflict
}
78 changes: 78 additions & 0 deletions exporter/elasticsearchexporter/esclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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: http.StatusUnauthorized, 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: http.StatusOK}, 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.31.1-0.20250501194116-727ae96d6214
go.opentelemetry.io/collector/component v1.31.1-0.20250501194116-727ae96d6214
go.opentelemetry.io/collector/component/componentstatus v0.125.0
go.opentelemetry.io/collector/component/componenttest v0.125.1-0.20250501194116-727ae96d6214
go.opentelemetry.io/collector/config/configauth v0.125.1-0.20250501194116-727ae96d6214
go.opentelemetry.io/collector/config/configcompression v1.31.1-0.20250501194116-727ae96d6214
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.