This repository has been archived by the owner on May 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ext: Add LogFields helpers for keys from specification
Opentracing spec specify prefered names for log messages [1] This patch adds declaration for fields which are meaningful for golan Footnotes: [1] https://github.com/opentracing/specification/blob/master/semantic_conventions.md
- Loading branch information
Dmitry Monakhov
committed
Nov 27, 2019
1 parent
a7454ce
commit e014616
Showing
2 changed files
with
75 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,31 @@ | ||
package ext | ||
|
||
import ( | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/log" | ||
) | ||
|
||
// These constants define common logFields names recommended for better portability across | ||
// tracing systems and languages/platforms | ||
// More info: https://github.com/opentracing/specification/blob/master/semantic_conventions.md | ||
var ( | ||
LogEvent = stringLogName("event") | ||
LogMessage = stringLogName("message") | ||
LogStack = stringLogName("stack") | ||
|
||
// Span event represent error | ||
ErrorEvent = log.String(string(LogEvent), "error") | ||
) | ||
|
||
// Add error event info | ||
func LogError(span opentracing.Span, err error) { | ||
Error.Set(span, true) | ||
span.LogFields(ErrorEvent, log.String(string(LogMessage), err.Error())) | ||
} | ||
|
||
// Add log string to to the `span` | ||
func (logName stringLogName) Log(span opentracing.Span, value string) { | ||
span.LogFields(log.String(string(logName), value)) | ||
} | ||
|
||
type stringLogName string |
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,44 @@ | ||
package ext_test | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/opentracing/opentracing-go/ext" | ||
"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) | ||
|
||
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: "message", | ||
ValueKind: reflect.String, | ||
ValueString: err.Error(), | ||
}, | ||
}, fields) | ||
} |