-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 3 commits
b7c3514
d812fcd
a7e2364
a96859f
65beac5
3ead7f7
4332284
3aa8c1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -250,17 +250,28 @@ func (doc *Document) iterJSON(v *json.Visitor, dedot bool) error { | |
} | ||
|
||
func (doc *Document) iterJSONFlat(w *json.Visitor) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name the return "argument" There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()
}()
....
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
|
@@ -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] | ||
|
@@ -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 = "" | ||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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