Skip to content

Commit

Permalink
Exposed additional APIs and logger for tracing context logging (#265)
Browse files Browse the repository at this point in the history
Co-authored-by: Vijay Nalawade <[email protected]>
  • Loading branch information
vijaynalawade and vnalawad-tibco authored Mar 15, 2022
1 parent c22e7e0 commit 85913f4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
14 changes: 11 additions & 3 deletions support/trace/context.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package trace

import "context"
import (
"context"

"github.com/project-flogo/core/support/log"
)

type key struct{}

var id = key{}


type TracingContext interface {
// TraceObject() returns underlying tracing implementation
TraceObject() interface{}
Expand All @@ -16,11 +19,16 @@ type TracingContext interface {
SetTag(tagKey string, tagValue interface{}) bool
// LogKV() allows you to log additional details about the entity being traced
LogKV(kvs map[string]interface{}) bool
// TraceID() returns trace ID
TraceID() string
// SpanID() returns span ID
SpanID() string
}

type Config struct {
Operation string
Tags map[string]interface{}
Logger log.Logger
}

func AppendTracingContext(goCtx context.Context, tracingContext TracingContext) context.Context {
Expand All @@ -30,4 +38,4 @@ func AppendTracingContext(goCtx context.Context, tracingContext TracingContext)
func ExtractTracingContext(goCtx context.Context) TracingContext {
tc, _ := goCtx.Value(id).(TracingContext)
return tc
}
}
20 changes: 14 additions & 6 deletions support/trace/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package trace

import (
"context"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

type TestTracingContext struct {
}

func (t TestTracingContext) TraceID() string {
return ""
}

func (t TestTracingContext) SpanID() string {
return ""
}

func (t TestTracingContext) TraceObject() interface{} {
Expand All @@ -31,20 +39,20 @@ func TestAppendTracingContext(t *testing.T) {
tCtx := &TestTracingContext{}

goCtx := AppendTracingContext(context.Background(), tCtx)
tc, ok := goCtx.Value(id).(TracingContext)
tc, ok := goCtx.Value(id).(TracingContext)

assert.True(t, ok)
assert.Equal(t, tCtx, tc)
assert.True(t, ok)
assert.Equal(t, tCtx, tc)
}

func TestExtractTracingContext(t *testing.T) {

tCtx := &TestTracingContext{}
goCtx := context.WithValue(nil, id, tCtx)
goCtx := context.WithValue(context.Background(), id, tCtx)

ttCtx := ExtractTracingContext(goCtx)
assert.Equal(t, tCtx, ttCtx)

ttCtx = ExtractTracingContext(context.Background())
assert.Nil(t, ttCtx)
}
}

1 comment on commit 85913f4

@d-sauer
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this give ability to enhance each log message with trace-id and span-id if engine receive those through e.g http listener with 'tracepatent' headers from OpenTelemetry?

Please sign in to comment.