| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package test |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"encoding/json" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 15 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 16 |
|
| 17 |
"go.opentelemetry.io/otel/trace/internal/telemetry" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
y2k = time.Unix(0, time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano()) // No location. |
| 22 |
|
| 23 |
attrsA = []telemetry.Attr{ |
| 24 |
telemetry.String("user", "Alice"), |
| 25 |
telemetry.Bool("admin", true), |
| 26 |
telemetry.Int64("floor", -2), |
| 27 |
telemetry.Float64("impact", 0.21362), |
| 28 |
telemetry.Slice( |
| 29 |
"reports", |
| 30 |
telemetry.StringValue("Bob"), |
| 31 |
telemetry.StringValue("Dave"), |
| 32 |
), |
| 33 |
telemetry.Map( |
| 34 |
"favorites", |
| 35 |
telemetry.String("food", "hot dog"), |
| 36 |
telemetry.Int("number", 13), |
| 37 |
), |
| 38 |
telemetry.Bytes( |
| 39 |
"secret", |
| 40 |
[]byte("NUI4RUZGRjc5ODAzODEwM0QyNjlCNjMzODEzRkM2MEM="), |
| 41 |
), |
| 42 |
} |
| 43 |
pAttrsA = func() pcommon.Map { |
| 44 |
m := pcommon.NewMap() |
| 45 |
m.PutStr("user", "Alice") |
| 46 |
m.PutBool("admin", true) |
| 47 |
m.PutInt("floor", -2) |
| 48 |
m.PutDouble("impact", 0.21362) |
| 49 |
|
| 50 |
s := m.PutEmptySlice("reports") |
| 51 |
s.AppendEmpty().SetStr("Bob") |
| 52 |
s.AppendEmpty().SetStr("Dave") |
| 53 |
|
| 54 |
fav := m.PutEmptyMap("favorites") |
| 55 |
fav.PutStr("food", "hot dog") |
| 56 |
fav.PutInt("number", 13) |
| 57 |
|
| 58 |
sec := m.PutEmptyBytes("secret") |
| 59 |
sec.FromRaw([]byte("NUI4RUZGRjc5ODAzODEwM0QyNjlCNjMzODEzRkM2MEM=")) |
| 60 |
|
| 61 |
return m |
| 62 |
}() |
| 63 |
|
| 64 |
link = &telemetry.SpanLink{ |
| 65 |
TraceID: telemetry.TraceID{0x2}, |
| 66 |
SpanID: telemetry.SpanID{0x1}, |
| 67 |
TraceState: "test=green", |
| 68 |
Attrs: []telemetry.Attr{telemetry.Int("queue", 17)}, |
| 69 |
DroppedAttrs: 8, |
| 70 |
Flags: 1, |
| 71 |
} |
| 72 |
pLink = func() ptrace.SpanLink { |
| 73 |
l := ptrace.NewSpanLink() |
| 74 |
l.SetTraceID(pcommon.TraceID{0x2}) |
| 75 |
l.SetSpanID(pcommon.SpanID{0x1}) |
| 76 |
l.TraceState().FromRaw("test=green") |
| 77 |
l.Attributes().PutInt("queue", 17) |
| 78 |
l.SetDroppedAttributesCount(8) |
| 79 |
l.SetFlags(1) |
| 80 |
return l |
| 81 |
}() |
| 82 |
|
| 83 |
event = &telemetry.SpanEvent{ |
| 84 |
Time: y2k.Add(10 * time.Microsecond), |
| 85 |
Name: "span.event", |
| 86 |
Attrs: []telemetry.Attr{telemetry.Float64("impact", 0.4372)}, |
| 87 |
DroppedAttrs: 2, |
| 88 |
} |
| 89 |
prevent = func() ptrace.SpanEvent { |
| 90 |
e := ptrace.NewSpanEvent() |
| 91 |
e.SetTimestamp(pcommon.NewTimestampFromTime(y2k.Add(10 * time.Microsecond))) |
| 92 |
e.SetName("span.event") |
| 93 |
e.Attributes().PutDouble("impact", 0.4372) |
| 94 |
e.SetDroppedAttributesCount(2) |
| 95 |
return e |
| 96 |
}() |
| 97 |
|
| 98 |
spanA = &telemetry.Span{ |
| 99 |
TraceID: [16]byte{0x1}, |
| 100 |
SpanID: [8]byte{0x2}, |
| 101 |
TraceState: "test=a", |
| 102 |
ParentSpanID: [8]byte{0x1}, |
| 103 |
Flags: 1, |
| 104 |
Name: "span.a", |
| 105 |
Kind: telemetry.SpanKindClient, |
| 106 |
StartTime: y2k, |
| 107 |
EndTime: y2k.Add(time.Second), |
| 108 |
Attrs: attrsA, |
| 109 |
DroppedAttrs: 2, |
| 110 |
Events: []*telemetry.SpanEvent{event}, |
| 111 |
DroppedEvents: 3, |
| 112 |
Links: []*telemetry.SpanLink{link}, |
| 113 |
DroppedLinks: 4, |
| 114 |
Status: &telemetry.Status{ |
| 115 |
Message: "okay", |
| 116 |
Code: telemetry.StatusCodeOK, |
| 117 |
}, |
| 118 |
} |
| 119 |
pSpanA = func() ptrace.Span { |
| 120 |
s := ptrace.NewSpan() |
| 121 |
s.SetTraceID(pcommon.TraceID([16]byte{0x1})) |
| 122 |
s.SetSpanID(pcommon.SpanID([8]byte{0x2})) |
| 123 |
|
| 124 |
ts := s.TraceState() |
| 125 |
ts.FromRaw("test=a") |
| 126 |
|
| 127 |
s.SetParentSpanID(pcommon.SpanID([8]byte{0x1})) |
| 128 |
s.SetFlags(1) |
| 129 |
s.SetName("span.a") |
| 130 |
s.SetKind(ptrace.SpanKindClient) |
| 131 |
s.SetStartTimestamp(pcommon.NewTimestampFromTime(y2k)) |
| 132 |
s.SetEndTimestamp(pcommon.NewTimestampFromTime(y2k.Add(time.Second))) |
| 133 |
pAttrsA.CopyTo(s.Attributes()) |
| 134 |
s.SetDroppedAttributesCount(2) |
| 135 |
prevent.CopyTo(s.Events().AppendEmpty()) |
| 136 |
s.SetDroppedEventsCount(3) |
| 137 |
pLink.CopyTo(s.Links().AppendEmpty()) |
| 138 |
s.SetDroppedLinksCount(4) |
| 139 |
|
| 140 |
stat := s.Status() |
| 141 |
stat.SetMessage("okay") |
| 142 |
stat.SetCode(ptrace.StatusCodeOk) |
| 143 |
|
| 144 |
return s |
| 145 |
}() |
| 146 |
schema100 = "http://go.opentelemetry.io/schema/v1.0.0" |
| 147 |
|
| 148 |
scope = &telemetry.Scope{ |
| 149 |
Name: "go.opentelemetry.io/otel/trace/internal/telemetry/test", |
| 150 |
Version: "v0.0.1", |
| 151 |
Attrs: []telemetry.Attr{telemetry.String("department", "ops")}, |
| 152 |
DroppedAttrs: 1, |
| 153 |
} |
| 154 |
pScope = func() pcommon.InstrumentationScope { |
| 155 |
s := pcommon.NewInstrumentationScope() |
| 156 |
s.SetName("go.opentelemetry.io/otel/trace/internal/telemetry/test") |
| 157 |
s.SetVersion("v0.0.1") |
| 158 |
s.Attributes().PutStr("department", "ops") |
| 159 |
s.SetDroppedAttributesCount(1) |
| 160 |
return s |
| 161 |
}() |
| 162 |
|
| 163 |
scopeSpans = &telemetry.ScopeSpans{ |
| 164 |
Scope: scope, |
| 165 |
Spans: []*telemetry.Span{spanA}, |
| 166 |
SchemaURL: schema100, |
| 167 |
} |
| 168 |
pScopeSpans = func() ptrace.ScopeSpans { |
| 169 |
s := ptrace.NewScopeSpans() |
| 170 |
pSpanA.CopyTo(s.Spans().AppendEmpty()) |
| 171 |
pScope.CopyTo(s.Scope()) |
| 172 |
s.SetSchemaUrl(schema100) |
| 173 |
return s |
| 174 |
}() |
| 175 |
|
| 176 |
res = telemetry.Resource{ |
| 177 |
Attrs: []telemetry.Attr{ |
| 178 |
telemetry.String("host", "hal"), |
| 179 |
telemetry.Int("id", 42), |
| 180 |
}, |
| 181 |
DroppedAttrs: 100, |
| 182 |
} |
| 183 |
press = func() pcommon.Resource { |
| 184 |
r := pcommon.NewResource() |
| 185 |
r.Attributes().PutStr("host", "hal") |
| 186 |
r.Attributes().PutInt("id", 42) |
| 187 |
r.SetDroppedAttributesCount(100) |
| 188 |
return r |
| 189 |
}() |
| 190 |
|
| 191 |
resSpans = &telemetry.ResourceSpans{ |
| 192 |
Resource: res, |
| 193 |
SchemaURL: schema100, |
| 194 |
ScopeSpans: []*telemetry.ScopeSpans{scopeSpans}, |
| 195 |
} |
| 196 |
pResSpans = func() ptrace.ResourceSpans { |
| 197 |
rs := ptrace.NewResourceSpans() |
| 198 |
press.CopyTo(rs.Resource()) |
| 199 |
pScopeSpans.CopyTo(rs.ScopeSpans().AppendEmpty()) |
| 200 |
rs.SetSchemaUrl(schema100) |
| 201 |
return rs |
| 202 |
}() |
| 203 |
|
| 204 |
traces = telemetry.Traces{ |
| 205 |
ResourceSpans: []*telemetry.ResourceSpans{ |
| 206 |
resSpans, |
| 207 |
}, |
| 208 |
} |
| 209 |
pTraces = func() ptrace.Traces { |
| 210 |
traces := ptrace.NewTraces() |
| 211 |
pResSpans.CopyTo(traces.ResourceSpans().AppendEmpty()) |
| 212 |
return traces |
| 213 |
}() |
| 214 |
) |
| 215 |
|
| 216 |
func TestDecode(t *testing.T) { |
| 217 |
var enc ptrace.JSONMarshaler |
| 218 |
b, err := enc.MarshalTraces(pTraces) |
| 219 |
require.NoError(t, err) |
| 220 |
|
| 221 |
t.Log(string(b)) // This helps when test fails to understand what is being decoded. |
| 222 |
|
| 223 |
var got telemetry.Traces |
| 224 |
dec := json.NewDecoder(bytes.NewReader(b)) |
| 225 |
require.NoError(t, dec.Decode(&got)) |
| 226 |
|
| 227 |
assert.Equal(t, traces, got) |
| 228 |
} |
| 229 |
|
| 230 |
func TestEncode(t *testing.T) { |
| 231 |
var buf bytes.Buffer |
| 232 |
enc := json.NewEncoder(&buf) |
| 233 |
require.NoError(t, enc.Encode(traces)) |
| 234 |
|
| 235 |
data := buf.Bytes() |
| 236 |
t.Log(string(data)) // This helps when test fails to understand what how the data has been encoded. |
| 237 |
|
| 238 |
var dec ptrace.JSONUnmarshaler |
| 239 |
got, err := dec.UnmarshalTraces(data) |
| 240 |
require.NoError(t, err) |
| 241 |
|
| 242 |
assert.Equal(t, pTraces, got) |
| 243 |
} |