Skip to content

Commit

Permalink
Add a method to transform the TraceContext into a map[string]interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
phacops committed May 25, 2022
1 parent 3196641 commit 49650f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
5 changes: 2 additions & 3 deletions interfaces_test.go
Expand Up @@ -11,7 +11,6 @@ import (
"testing"
"time"

"github.com/fatih/structs"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -142,13 +141,13 @@ func TestStructSnapshots(t *testing.T) {
StartTime: time.Unix(3, 0).UTC(),
Timestamp: time.Unix(5, 0).UTC(),
Contexts: map[string]Context{
"trace": structs.Map(TraceContext{
"trace": TraceContext{
TraceID: TraceIDFromHex("90d57511038845dcb4164a70fc3a7fdb"),
SpanID: SpanIDFromHex("f7f3fd754a9040eb"),
Op: "http.GET",
Description: "description",
Status: SpanStatusOK,
}),
}.Map(),
},
},
},
Expand Down
27 changes: 26 additions & 1 deletion tracing.go
Expand Up @@ -150,7 +150,7 @@ func StartSpan(ctx context.Context, operation string, options ...SpanOption) *Sp

// Update scope so that all events include a trace context, allowing
// Sentry to correlate errors to transactions/spans.
hubFromContext(ctx).Scope().SetContext("trace", structs.Map(span.traceContext()))
hubFromContext(ctx).Scope().SetContext("trace", span.traceContext().Map())

return &span
}
Expand Down Expand Up @@ -501,6 +501,31 @@ func (tc *TraceContext) MarshalJSON() ([]byte, error) {
})
}

func (tc TraceContext) Map() map[string]interface{} {
m := map[string]interface{}{
"trace_id": tc.TraceID,
"span_id": tc.SpanID,
}

if tc.ParentSpanID != [8]byte{} {
m["parent_span_id"] = tc.ParentSpanID
}

if tc.Op != "" {
m["op"] = tc.Op
}

if tc.Description != "" {
m["description"] = tc.Description
}

if tc.Status > 0 && tc.Status < maxSpanStatus {
m["status"] = tc.Status
}

return m
}

// Sampled signifies a sampling decision.
type Sampled int8

Expand Down
9 changes: 4 additions & 5 deletions tracing_test.go
Expand Up @@ -13,7 +13,6 @@ import (
"testing"
"time"

"github.com/fatih/structs"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
Expand Down Expand Up @@ -132,14 +131,14 @@ func TestStartSpan(t *testing.T) {
Type: transactionType,
Transaction: transaction,
Contexts: map[string]Context{
"trace": structs.Map(TraceContext{
"trace": TraceContext{
TraceID: span.TraceID,
SpanID: span.SpanID,
ParentSpanID: parentSpanID,
Op: op,
Description: description,
Status: status,
}),
}.Map(),
},
Tags: nil,
// TODO(tracing): the root span / transaction data field is
Expand Down Expand Up @@ -192,11 +191,11 @@ func TestStartChild(t *testing.T) {
Type: transactionType,
Transaction: "Test Transaction",
Contexts: map[string]Context{
"trace": structs.Map(TraceContext{
"trace": TraceContext{
TraceID: span.TraceID,
SpanID: span.SpanID,
Op: span.Op,
}),
}.Map(),
},
Spans: []*Span{
{
Expand Down

0 comments on commit 49650f9

Please sign in to comment.