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

add error checks for elasticsearch exporters #9966

Closed
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
- `datadogexporter`: add error checks for datadog exporter (#9964)
- `datadogexporter`: Fix host aliases not being properly sent to the Datadog backend (#9748)
- `groupbyattrsprocessor`: copied aggregationtemporality when grouping metrics. (#9088)
- `elasticsearchexporter`: add error checks for elasticsearch exporters (#9966)
- `jaeger`: Update OTLP-Jaeger translation of span events according to the OTel Spec: use `event` log field instead
of `message` to represent OTel Span Event Name (#10273)
- `mongodbreceiver`: Fix issue where receiver startup could hang (#10111)
Expand Down
11 changes: 8 additions & 3 deletions exporter/elasticsearchexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

// Package elasticsearchexporter contains an opentelemetry-collector exporter
// for Elasticsearch.
// nolint:errcheck
package elasticsearchexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter"

import (
Expand Down Expand Up @@ -145,8 +144,14 @@ func (e *elasticsearchExporter) pushEvent(ctx context.Context, document []byte)
zap.NamedError("reason", err))

attempts++
body.Seek(0, io.SeekStart)
e.bulkIndexer.Add(ctx, item)
if _, seekerr := body.Seek(0, io.SeekStart); seekerr != nil {
e.logger.Error("failed to set the next offset.",
zap.NamedError("reason", seekerr))
}
if indexererr := e.bulkIndexer.Add(ctx, item); indexererr != nil {
e.logger.Error("failed to add item to indexer.",
zap.NamedError("reason", indexererr))
}

case resp.Status == 0 && err != nil:
// Encoding error. We didn't even attempt to send the event
Expand Down
5 changes: 3 additions & 2 deletions exporter/elasticsearchexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// nolint:errcheck
package elasticsearchexporter

import (
Expand Down Expand Up @@ -306,7 +305,9 @@ func newTestExporter(t *testing.T, url string, fns ...func(*Config)) *elasticsea
exporter, err := newExporter(zaptest.NewLogger(t), withTestExporterConfig(fns...)(url))
require.NoError(t, err)

t.Cleanup(func() { exporter.Shutdown(context.TODO()) })
t.Cleanup(func() {
require.NoError(t, exporter.Shutdown(context.TODO()))
})
return exporter
}

Expand Down
61 changes: 45 additions & 16 deletions exporter/elasticsearchexporter/internal/objmodel/objmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
// Ingest Node is used. But either way, we try to present only well formed
// document to Elasticsearch.

// nolint:errcheck
package objmodel // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/objmodel"

import (
Expand Down Expand Up @@ -250,17 +249,24 @@ func (doc *Document) iterJSON(v *json.Visitor, dedot bool) error {
}

func (doc *Document) iterJSONFlat(w *json.Visitor) error {
Copy link
Member

Choose a reason for hiding this comment

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

Name the return "argument" err and in the defer func do err = w.OnObjectFinished(), then instead of log.Fatal you will actually return an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if this is done the error returned from the defers will trigger the errcheck lint check as the errors returned from the defers are not checked

These are some related issues found
kisielk/errcheck#101
kubernetes-sigs/cluster-api#4586

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so, can you show me?

func (doc *Document) iterJSONFlat(w *json.Visitor) (err error) {
	err = w.OnObjectStart(-1, structform.AnyType)
	if err != nil {
		return 
	}
	defer func() {
		err = w.OnObjectFinished()
	}()
        ....
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done like this

defer func() error {
		err = w.OnObjectFinished()
		if err != nil {
			return err
		}
		return nil
	}()

getting this
Selection_005

Copy link
Member

Choose a reason for hiding this comment

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

@DiptoChakrabarty, please take a closer look at Bogdan's example. You cannot return the error from the deferred function, but you can assign the error to a return value on the calling function.

w.OnObjectStart(-1, structform.AnyType)
defer w.OnObjectFinished()
err := w.OnObjectStart(-1, structform.AnyType)
if err != nil {
return err
}
defer func() {
err = w.OnObjectFinished()
}()

for i := range doc.fields {
fld := &doc.fields[i]
if fld.value.IsEmpty() {
continue
}

w.OnKey(fld.key)
if err := fld.value.iterJSON(w, true); err != nil {
if err = w.OnKey(fld.key); err != nil {
return err
}
if err = fld.value.iterJSON(w, true); err != nil {
return err
}
}
Expand All @@ -272,8 +278,13 @@ func (doc *Document) iterJSONDedot(w *json.Visitor) error {
objPrefix := ""
level := 0

w.OnObjectStart(-1, structform.AnyType)
defer w.OnObjectFinished()
err := w.OnObjectStart(-1, structform.AnyType)
if err != nil {
return err
}
defer func() {
err = w.OnObjectFinished()
}()

for i := range doc.fields {
fld := &doc.fields[i]
Expand All @@ -298,13 +309,17 @@ func (doc *Document) iterJSONDedot(w *json.Visitor) error {

delta = delta[idx+1:]
level--
w.OnObjectFinished()
if err = w.OnObjectFinished(); err != nil {
return err
}
}

objPrefix = key[:L]
} else { // no common prefix, close all objects we reported so far.
for ; level > 0; level-- {
w.OnObjectFinished()
if err = w.OnObjectFinished(); err != nil {
return err
}
}
objPrefix = ""
}
Expand All @@ -321,19 +336,29 @@ func (doc *Document) iterJSONDedot(w *json.Visitor) error {
level++
objPrefix = key[:len(objPrefix)+idx+1]
fieldName := key[start : start+idx]
w.OnKey(fieldName)
w.OnObjectStart(-1, structform.AnyType)
if err = w.OnKey(fieldName); err != nil {
return err
}
if err = w.OnObjectStart(-1, structform.AnyType); err != nil {
return err
}
}

// report value
fieldName := key[len(objPrefix):]
w.OnKey(fieldName)
fld.value.iterJSON(w, true)
if err = w.OnKey(fieldName); err != nil {
return err
}
if err = fld.value.iterJSON(w, true); err != nil {
return err
}
}

// close all pending object levels
for ; level > 0; level-- {
w.OnObjectFinished()
if err = w.OnObjectFinished(); err != nil {
return err
}
}

return nil
Expand Down Expand Up @@ -453,13 +478,17 @@ func (v *Value) iterJSON(w *json.Visitor, dedot bool) error {
}
return v.doc.iterJSON(w, dedot)
case KindArr:
w.OnArrayStart(-1, structform.AnyType)
if err := w.OnArrayStart(-1, structform.AnyType); err != nil {
return err
}
for i := range v.arr {
if err := v.arr[i].iterJSON(w, dedot); err != nil {
return err
}
}
w.OnArrayFinished()
if err := w.OnArrayFinished(); err != nil {
return err
}
}

return nil
Expand Down