Skip to content

Commit

Permalink
ddtrace: tracer: add tests
Browse files Browse the repository at this point in the history
APPSEC-51698

Signed-off-by: Eliott Bouhana <eliott.bouhana@datadoghq.com>
  • Loading branch information
eliottness committed Feb 21, 2024
1 parent 1aa3487 commit c8613ca
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
4 changes: 2 additions & 2 deletions ddtrace/tracer/span_test.go
Expand Up @@ -364,7 +364,7 @@ func TestSpanSetTag(t *testing.T) {
span.SetTag("mapOfMap", mapOfMap)
assert.Equal(mapOfMap, span.MetaStruct["mapOfMap"])

// groupedStats is a struct that implements the msgp.Marshaler interface
// testMsgpStruct is a struct that implements the msgp.Marshaler interface
testValue := &testMsgpStruct{A: "test"}
span.SetTag("struct", testValue)
require.Equal(t, testValue, span.MetaStruct["struct"])
Expand All @@ -378,7 +378,7 @@ type testMsgpStruct struct {
A string
}

func (t *testMsgpStruct) MarshalMsg(b []byte) ([]byte, error) {
func (t *testMsgpStruct) MarshalMsg(_ []byte) ([]byte, error) {
return nil, nil
}

Expand Down
23 changes: 12 additions & 11 deletions ddtrace/tracer/transport_test.go
Expand Up @@ -25,17 +25,18 @@ import (
// getTestSpan returns a Span with different fields set
func getTestSpan() *span {
return &span{
TraceID: 42,
SpanID: 52,
ParentID: 42,
Type: "web",
Service: "high.throughput",
Name: "sending.events",
Resource: "SEND /data",
Start: 1481215590883401105,
Duration: 1000000000,
Meta: map[string]string{"http.host": "192.168.0.1"},
Metrics: map[string]float64{"http.monitor": 41.99},
TraceID: 42,
SpanID: 52,
ParentID: 42,
Type: "web",
Service: "high.throughput",
Name: "sending.events",
Resource: "SEND /data",
Start: 1481215590883401105,
Duration: 1000000000,
Meta: map[string]string{"http.host": "192.168.0.1"},
MetaStruct: map[string]any{"_dd.appsec.json": map[string]any{"triggers": []any{map[string]any{"id": "1"}}}},
Metrics: map[string]float64{"http.monitor": 41.99},
}
}

Expand Down
17 changes: 17 additions & 0 deletions ddtrace/tracer/writer.go
Expand Up @@ -219,6 +219,23 @@ func (h *logTraceWriter) encodeSpan(s *span) {
h.buf.WriteString(":")
h.marshalString(v)
}
h.buf.WriteString(`},"meta_struct":{`)
first = true
for k, v := range s.MetaStruct {
if first {
first = false
} else {
h.buf.WriteString(`,`)
}
h.marshalString(k)
h.buf.WriteString(":")
jsonValue, err := json.Marshal(v)
if err != nil {
log.Error("Error marshaling value %q: %v", v, err)
continue
}
h.marshalString(string(jsonValue))
}
h.buf.WriteString(`},"metrics":{`)
first = true
for k, v := range s.Metrics {
Expand Down
37 changes: 23 additions & 14 deletions ddtrace/tracer/writer_test.go
Expand Up @@ -113,7 +113,7 @@ func TestLogWriter(t *testing.T) {
v := struct{ Traces [][]map[string]interface{} }{}
d := json.NewDecoder(&buf)
err = d.Decode(&v)
assert.NoError(err)
assert.NoError(err, string(buf.Bytes()))
assert.Len(v.Traces, 20, "Expected 20 traces, but have %d", len(v.Traces))
for _, t := range v.Traces {
assert.Len(t, 2, "Expected 2 spans, but have %d", len(t))
Expand Down Expand Up @@ -153,17 +153,18 @@ func TestLogWriter(t *testing.T) {
h := newLogTraceWriter(cfg, statsd)
h.w = &buf
type jsonSpan struct {
TraceID string `json:"trace_id"`
SpanID string `json:"span_id"`
ParentID string `json:"parent_id"`
Name string `json:"name"`
Resource string `json:"resource"`
Error int32 `json:"error"`
Meta map[string]string `json:"meta"`
Metrics map[string]float64 `json:"metrics"`
Start int64 `json:"start"`
Duration int64 `json:"duration"`
Service string `json:"service"`
TraceID string `json:"trace_id"`
SpanID string `json:"span_id"`
ParentID string `json:"parent_id"`
Name string `json:"name"`
Resource string `json:"resource"`
Error int32 `json:"error"`
Meta map[string]string `json:"meta"`
MetaStruct map[string]any `json:"meta_struct"`
Metrics map[string]float64 `json:"metrics"`
Start int64 `json:"start"`
Duration int64 `json:"duration"`
Service string `json:"service"`
}
type jsonPayload struct {
Traces [][]jsonSpan `json:"traces"`
Expand All @@ -176,6 +177,11 @@ func TestLogWriter(t *testing.T) {
"env": "prod",
"version": "1.26.0",
},
MetaStruct: map[string]any{
"_dd.stack": map[string]string{
"0": "github.com/DataDog/dd-trace-go/v1/internal/tracer.TestLogWriter",
},
},
Metrics: map[string]float64{
"widgets": 1e26,
"zero": 0.0,
Expand All @@ -200,6 +206,9 @@ func TestLogWriter(t *testing.T) {
"env": "prod",
"version": "1.26.0",
},
MetaStruct: map[string]any{
"_dd.stack": "{\"0\":\"github.com/DataDog/dd-trace-go/v1/internal/tracer.TestLogWriter\"}",
},
Metrics: map[string]float64{
"widgets": 1e26,
"zero": 0.0,
Expand Down Expand Up @@ -233,7 +242,7 @@ func TestLogWriter(t *testing.T) {
w.encodeSpan(s)

str := w.buf.String()
assert.Equal(`{"trace_id":"1","span_id":"2","parent_id":"3","name":"name\n","resource":"\"res\"","error":0,"meta":{"query\n":"Select * from \n Where\nvalue"},"metrics":{"version\n":3},"start":12,"duration":0,"service":"srv\t"}`, str)
assert.Equal(`{"trace_id":"1","span_id":"2","parent_id":"3","name":"name\n","resource":"\"res\"","error":0,"meta":{"query\n":"Select * from \n Where\nvalue"},"meta_struct":{},"metrics":{"version\n":3},"start":12,"duration":0,"service":"srv\t"}`, str)
assert.NotContains(str, "\n")
assert.Contains(str, "\\n")
})
Expand Down Expand Up @@ -374,7 +383,7 @@ func TestTraceWriterFlushRetries(t *testing.T) {

sentCounts := map[string]int64{
"datadog.tracer.decode_error": 1,
"datadog.tracer.flush_bytes": 184,
"datadog.tracer.flush_bytes": 197,
"datadog.tracer.flush_traces": 1,
}
droppedCounts := map[string]int64{
Expand Down

0 comments on commit c8613ca

Please sign in to comment.