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 f555dc7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,6 @@ module github.com/getsentry/sentry-go
go 1.18

require (
github.com/fatih/structs v1.1.0
github.com/gin-gonic/gin v1.7.7
github.com/go-errors/errors v1.0.1
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab
Expand All @@ -27,6 +26,7 @@ require (
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
Expand Down
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
31 changes: 27 additions & 4 deletions tracing.go
Expand Up @@ -10,8 +10,6 @@ import (
"regexp"
"strings"
"time"

"github.com/fatih/structs"
)

// A Span is the building block of a Sentry transaction. Spans build up a tree
Expand Down Expand Up @@ -150,7 +148,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 @@ -332,7 +330,7 @@ func (s *Span) toEvent() *Event {
Type: transactionType,
Transaction: hub.Scope().Transaction(),
Contexts: map[string]Context{
"trace": structs.Map(s.traceContext()),
"trace": s.traceContext().Map(),
},
Tags: s.Tags,
Extra: s.Data,
Expand Down Expand Up @@ -501,6 +499,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 f555dc7

Please sign in to comment.