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
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 golan

- 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
Dmitry Monakhov committed Nov 28, 2019
1 parent a7454ce commit c747d25
Show file tree
Hide file tree
Showing 4 changed files with 88 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 add error event record for the Span
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...)
}
44 changes: 44 additions & 0 deletions ext/field_test.go
@@ -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: "error",
ValueKind: reflect.String,
ValueString: err.Error(),
},
}, fields)
}
15 changes: 15 additions & 0 deletions log/field.go
Expand Up @@ -143,6 +143,21 @@ func Object(key string, obj interface{}) Field {
}
}

// Event adds a string-valued field with name "value" to a Span.LogFields() record
func Event(val string) Field {
return String("event", val)
}

// Message adds a string-valued field with name "event" to a Span.LogFields() record
func Message(val string) Field {
return String("message", val)
}

// Stack adds a string-valued field with name "stack" to a Span.LogFields() record
func Stack(val string) Field {
return String("stack", val)
}

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

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

0 comments on commit c747d25

Please sign in to comment.