From f555dc75bc64038227cb317519ad9a1586fb7735 Mon Sep 17 00:00:00 2001 From: Pierre Massat Date: Wed, 25 May 2022 13:32:03 -0400 Subject: [PATCH] Add a method to transform the TraceContext into a map[string]interface{} --- go.mod | 2 +- interfaces_test.go | 5 ++--- tracing.go | 31 +++++++++++++++++++++++++++---- tracing_test.go | 9 ++++----- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 4f2df980..f7e673b2 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/interfaces_test.go b/interfaces_test.go index 5095005a..39eae50e 100644 --- a/interfaces_test.go +++ b/interfaces_test.go @@ -11,7 +11,6 @@ import ( "testing" "time" - "github.com/fatih/structs" "github.com/google/go-cmp/cmp" ) @@ -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(), }, }, }, diff --git a/tracing.go b/tracing.go index 8b64adff..1ff58ea1 100644 --- a/tracing.go +++ b/tracing.go @@ -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 @@ -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 } @@ -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, @@ -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 diff --git a/tracing_test.go b/tracing_test.go index 4dbfc656..69819da7 100644 --- a/tracing_test.go +++ b/tracing_test.go @@ -13,7 +13,6 @@ import ( "testing" "time" - "github.com/fatih/structs" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" ) @@ -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 @@ -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{ {