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

Commit

Permalink
ext: Add LogFields 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

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
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ext/fields.go
@@ -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
44 changes: 44 additions & 0 deletions ext/fields_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: "message",
ValueKind: reflect.String,
ValueString: err.Error(),
},
}, fields)
}

0 comments on commit e014616

Please sign in to comment.