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 3 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 @@ -55,6 +55,7 @@
### 🧰 Bug fixes 🧰

- `datadogexporter`: add error checks for datadog exporter (#9964)
- `elasticsearchexporter`: add error checks for elasticsearch exporters (#9966)
- `k8sclusterreceiver`: Fix the receiver to work with 1.19 and 1.20 k8s API versions (#9523)
- `azuremonitorexporter`: Fix log exporter bug related to incorrectly mapping SpanId (#9579)
- `mysqlreceiver`: Fix attribute values mismatch with its definition (#9688)
Expand Down
13 changes: 10 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,16 @@ func (e *elasticsearchExporter) pushEvent(ctx context.Context, document []byte)
zap.NamedError("reason", err))

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

@bogdandrutu bogdandrutu May 12, 2022

Choose a reason for hiding this comment

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

I prefer this (and everywhere else) to inline this:

if indexererr := e.bulkIndexer.Add(ctx, item); indexererr != nil {
   ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

have done this will push as all improvements suggested are finalised

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 @@ -313,7 +312,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
80 changes: 63 additions & 17 deletions exporter/elasticsearchexporter/internal/objmodel/objmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
// 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 (
"io"
"log"
"math"
"sort"
"strings"
Expand Down Expand Up @@ -250,17 +250,28 @@ 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 {
log.Fatalln(err)
}
defer func() {
err = w.OnObjectFinished()
if err != nil {
log.Fatalln(err)
}
}()

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 {
err = w.OnKey(fld.key)
if err != nil {
log.Fatalln(err)
}
if err = fld.value.iterJSON(w, true); err != nil {
return err
}
}
Expand All @@ -272,8 +283,16 @@ 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 {
log.Fatalln(err)
}
defer func() {
err = w.OnObjectFinished()
if err != nil {
log.Fatalln(err)
}
}()

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

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

objPrefix = key[:L]
} else { // no common prefix, close all objects we reported so far.
for ; level > 0; level-- {
w.OnObjectFinished()
err = w.OnObjectFinished()
if err != nil {
log.Fatalln(err)
}
}
objPrefix = ""
}
Expand All @@ -321,19 +346,34 @@ 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)
err = w.OnKey(fieldName)
if err != nil {
log.Fatalln(err)
}
err = w.OnObjectStart(-1, structform.AnyType)
if err != nil {
log.Fatalln(err)
}
}

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

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

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

return nil
Expand Down