Skip to content

Commit

Permalink
Send unsupported registered span tags as data.custom map
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Slotin committed Apr 20, 2021
1 parent 5e6fb9e commit 82c1900
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
30 changes: 28 additions & 2 deletions json_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,33 @@ type batchInfo struct {
Size int `json:"s"`
}

// CustomSpanData holds user-defined span tags
type CustomSpanData struct {
Tags map[string]interface{} `json:"tags,omitempty"`
}

func filterCustomSpanTags(tags map[string]interface{}, st RegisteredSpanType) map[string]interface{} {
knownTags := st.TagsNames()
customTags := make(map[string]interface{})

for k, v := range tags {
if _, ok := knownTags[k]; ok {
continue
}

customTags[k] = v
}

return customTags
}

// SpanData contains fields to be sent in the `data` section of an OT span document. These fields are
// common for all span types.
type SpanData struct {
Service string `json:"service,omitempty"`
st RegisteredSpanType
Service string `json:"service,omitempty"`
Custom *CustomSpanData `json:"custom,omitempty"`

st RegisteredSpanType
}

// NewSpanData initializes a new span data from tracer span
Expand All @@ -180,6 +202,10 @@ func NewSpanData(span *spanS, st RegisteredSpanType) SpanData {
st: st,
}

if customTags := filterCustomSpanTags(span.Tags, st); len(customTags) > 0 {
data.Custom = &CustomSpanData{Tags: customTags}
}

return data
}

Expand Down
32 changes: 32 additions & 0 deletions json_span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,35 @@ func TestNewSDKSpanData(t *testing.T) {
},
}, data.Tags)
}

func TestSpanData_CustomTags(t *testing.T) {
recorder := instana.NewTestRecorder()
tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder)

sp := tracer.StartSpan("g.http", opentracing.Tags{
"http.host": "localhost",
"http.path": "/",
"custom.tag": "42",
"another.tag": true,
})
sp.Finish()

spans := recorder.GetQueuedSpans()
require.Len(t, spans, 1)

span := spans[0]
require.IsType(t, instana.HTTPSpanData{}, span.Data)

data := span.Data.(instana.HTTPSpanData)

assert.Equal(t, instana.HTTPSpanTags{
Host: "localhost",
Path: "/",
}, data.Tags)
assert.Equal(t, &instana.CustomSpanData{
Tags: map[string]interface{}{
"custom.tag": "42",
"another.tag": true,
},
}, data.Custom)
}

0 comments on commit 82c1900

Please sign in to comment.