This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add log/fields helpers for keys from specification (#226)
Opentracing spec specify prefered names for log messages [1] This patch adds declaration for fields which are meaningful for golang - log: add helpers function for spec keys - ext: add LogError() helper for error LogError() helper can not be declarated in log package because it depends opentrace.Span which result in cyclic depencency Footnotes: [1] https://github.com/opentracing/specification/blob/master/semantic_conventions.md#log-fields-table
- Loading branch information
1 parent
a7454ce
commit 17f6344
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ext | ||
|
||
import ( | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/log" | ||
) | ||
|
||
// LogError sets the error=true tag on the Span and logs err as an "error" event. | ||
func LogError(span opentracing.Span, err error, fields ...log.Field) { | ||
Error.Set(span, true) | ||
ef := []log.Field{ | ||
log.Event("error"), | ||
log.Error(err), | ||
} | ||
ef = append(ef, fields...) | ||
span.LogFields(ef...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ext_test | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/opentracing/opentracing-go/ext" | ||
"github.com/opentracing/opentracing-go/log" | ||
"github.com/opentracing/opentracing-go/mocktracer" | ||
) | ||
|
||
func TestLogError(t *testing.T) { | ||
tracer := mocktracer.New() | ||
span := tracer.StartSpan("my-trace") | ||
ext.Component.Set(span, "my-awesome-library") | ||
ext.SamplingPriority.Set(span, 1) | ||
err := fmt.Errorf("my error") | ||
ext.LogError(span, err, log.Message("my optional msg text")) | ||
|
||
span.Finish() | ||
|
||
rawSpan := tracer.FinishedSpans()[0] | ||
assert.Equal(t, map[string]interface{}{ | ||
"component": "my-awesome-library", | ||
"error": true, | ||
}, rawSpan.Tags()) | ||
|
||
assert.Equal(t, len(rawSpan.Logs()), 1) | ||
fields := rawSpan.Logs()[0].Fields | ||
assert.Equal(t, []mocktracer.MockKeyValue{ | ||
{ | ||
Key: "event", | ||
ValueKind: reflect.String, | ||
ValueString: "error", | ||
}, | ||
{ | ||
Key: "error", | ||
ValueKind: reflect.String, | ||
ValueString: err.Error(), | ||
}, | ||
{ | ||
Key: "message", | ||
ValueKind: reflect.String, | ||
ValueString: "my optional msg text", | ||
}, | ||
}, fields) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters