Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Add log/fields helpers for keys from specification (#226)
Browse files Browse the repository at this point in the history
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
dmonakhov authored and yurishkuro committed Dec 5, 2019
1 parent a7454ce commit 17f6344
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ext/field.go
@@ -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...)
}
50 changes: 50 additions & 0 deletions ext/field_test.go
@@ -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)
}
10 changes: 10 additions & 0 deletions log/field.go
Expand Up @@ -143,6 +143,16 @@ func Object(key string, obj interface{}) Field {
}
}

// Event creates a string-valued Field for span logs with key="event" and value=val.
func Event(val string) Field {
return String("event", val)
}

// Message creates a string-valued Field for span logs with key="message" and value=val.
func Message(val string) Field {
return String("message", val)
}

// LazyLogger allows for user-defined, late-bound logging of arbitrary data
type LazyLogger func(fv Encoder)

Expand Down
8 changes: 8 additions & 0 deletions log/field_test.go
Expand Up @@ -34,6 +34,14 @@ func TestFieldString(t *testing.T) {
field: Noop(),
expected: ":<nil>",
},
{
field: Event("test"),
expected: "event:test",
},
{
field: Message("test2"),
expected: "message:test2",
},
}
for i, tc := range testCases {
if str := tc.field.String(); str != tc.expected {
Expand Down

0 comments on commit 17f6344

Please sign in to comment.