| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/log" |
| 14 |
) |
| 15 |
|
| 16 |
var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 17 |
|
| 18 |
func TestRecordEventName(t *testing.T) { |
| 19 |
const text = "testing text" |
| 20 |
|
| 21 |
var r log.Record |
| 22 |
r.SetEventName(text) |
| 23 |
assert.Equal(t, text, r.EventName()) |
| 24 |
} |
| 25 |
|
| 26 |
func TestRecordTimestamp(t *testing.T) { |
| 27 |
var r log.Record |
| 28 |
r.SetTimestamp(y2k) |
| 29 |
assert.Equal(t, y2k, r.Timestamp()) |
| 30 |
} |
| 31 |
|
| 32 |
func TestRecordObservedTimestamp(t *testing.T) { |
| 33 |
var r log.Record |
| 34 |
r.SetObservedTimestamp(y2k) |
| 35 |
assert.Equal(t, y2k, r.ObservedTimestamp()) |
| 36 |
} |
| 37 |
|
| 38 |
func TestRecordSeverity(t *testing.T) { |
| 39 |
var r log.Record |
| 40 |
r.SetSeverity(log.SeverityInfo) |
| 41 |
assert.Equal(t, log.SeverityInfo, r.Severity()) |
| 42 |
} |
| 43 |
|
| 44 |
func TestRecordSeverityText(t *testing.T) { |
| 45 |
const text = "testing text" |
| 46 |
|
| 47 |
var r log.Record |
| 48 |
r.SetSeverityText(text) |
| 49 |
assert.Equal(t, text, r.SeverityText()) |
| 50 |
} |
| 51 |
|
| 52 |
func TestRecordBody(t *testing.T) { |
| 53 |
body := log.StringValue("testing body value") |
| 54 |
|
| 55 |
var r log.Record |
| 56 |
r.SetBody(body) |
| 57 |
assert.Equal(t, body, r.Body()) |
| 58 |
} |
| 59 |
|
| 60 |
func TestRecordAttributes(t *testing.T) { |
| 61 |
attrs := []log.KeyValue{ |
| 62 |
log.String("k1", "str"), |
| 63 |
log.Float64("k2", 1.0), |
| 64 |
log.Int("k3", 2), |
| 65 |
log.Bool("k4", true), |
| 66 |
log.Bytes("k5", []byte{1}), |
| 67 |
log.Slice("k6", log.IntValue(3)), |
| 68 |
log.Map("k7", log.Bool("sub1", true)), |
| 69 |
log.String("k8", "str"), |
| 70 |
log.Float64("k9", 1.0), |
| 71 |
log.Int("k10", 2), |
| 72 |
log.Bool("k11", true), |
| 73 |
log.Bytes("k12", []byte{1}), |
| 74 |
log.Slice("k13", log.IntValue(3)), |
| 75 |
log.Map("k14", log.Bool("sub1", true)), |
| 76 |
{}, // Empty. |
| 77 |
} |
| 78 |
|
| 79 |
var r log.Record |
| 80 |
r.AddAttributes(attrs...) |
| 81 |
require.Equal(t, len(attrs), r.AttributesLen()) |
| 82 |
|
| 83 |
t.Run("Correctness", func(t *testing.T) { |
| 84 |
var i int |
| 85 |
r.WalkAttributes(func(kv log.KeyValue) bool { |
| 86 |
assert.Equal(t, attrs[i], kv) |
| 87 |
i++ |
| 88 |
return true |
| 89 |
}) |
| 90 |
}) |
| 91 |
|
| 92 |
t.Run("WalkAttributes/Filtering", func(t *testing.T) { |
| 93 |
for i := 1; i <= len(attrs); i++ { |
| 94 |
var j int |
| 95 |
r.WalkAttributes(func(log.KeyValue) bool { |
| 96 |
j++ |
| 97 |
return j < i |
| 98 |
}) |
| 99 |
assert.Equal(t, i, j, "number of attributes walked incorrect") |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
func TestRecordAllocationLimits(t *testing.T) { |
| 105 |
const runs = 5 |
| 106 |
|
| 107 |
// Assign testing results to external scope so the compiler doesn't |
| 108 |
// optimize away the testing statements. |
| 109 |
var ( |
| 110 |
tStamp time.Time |
| 111 |
sev log.Severity |
| 112 |
text string |
| 113 |
body log.Value |
| 114 |
n int |
| 115 |
attr log.KeyValue |
| 116 |
) |
| 117 |
|
| 118 |
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { |
| 119 |
var r log.Record |
| 120 |
r.SetTimestamp(y2k) |
| 121 |
tStamp = r.Timestamp() |
| 122 |
}), "Timestamp") |
| 123 |
|
| 124 |
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { |
| 125 |
var r log.Record |
| 126 |
r.SetObservedTimestamp(y2k) |
| 127 |
tStamp = r.ObservedTimestamp() |
| 128 |
}), "ObservedTimestamp") |
| 129 |
|
| 130 |
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { |
| 131 |
var r log.Record |
| 132 |
r.SetSeverity(log.SeverityDebug) |
| 133 |
sev = r.Severity() |
| 134 |
}), "Severity") |
| 135 |
|
| 136 |
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { |
| 137 |
var r log.Record |
| 138 |
r.SetSeverityText("severity text") |
| 139 |
text = r.SeverityText() |
| 140 |
}), "SeverityText") |
| 141 |
|
| 142 |
bodyVal := log.BoolValue(true) |
| 143 |
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { |
| 144 |
var r log.Record |
| 145 |
r.SetBody(bodyVal) |
| 146 |
body = r.Body() |
| 147 |
}), "Body") |
| 148 |
|
| 149 |
attrVal := []log.KeyValue{log.Bool("k", true), log.Int("i", 1)} |
| 150 |
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { |
| 151 |
var r log.Record |
| 152 |
r.AddAttributes(attrVal...) |
| 153 |
n = r.AttributesLen() |
| 154 |
r.WalkAttributes(func(kv log.KeyValue) bool { |
| 155 |
attr = kv |
| 156 |
return true |
| 157 |
}) |
| 158 |
}), "Attributes") |
| 159 |
|
| 160 |
// Convince the linter these values are used. |
| 161 |
_, _, _, _, _, _ = tStamp, sev, text, body, n, attr |
| 162 |
} |
| 163 |
|
| 164 |
func TestRecordClone(t *testing.T) { |
| 165 |
now0 := time.Now() |
| 166 |
sev0 := log.SeverityInfo |
| 167 |
text0 := "text" |
| 168 |
val0 := log.BoolValue(true) |
| 169 |
attr0 := log.Bool("0", true) |
| 170 |
|
| 171 |
r0 := log.Record{} |
| 172 |
r0.SetTimestamp(now0) |
| 173 |
r0.SetObservedTimestamp(now0) |
| 174 |
r0.SetSeverity(sev0) |
| 175 |
r0.SetSeverityText(text0) |
| 176 |
r0.SetBody(val0) |
| 177 |
r0.AddAttributes(attr0) |
| 178 |
|
| 179 |
// Clone and modify the clone |
| 180 |
now1 := now0.Add(time.Second) |
| 181 |
sev1 := log.SeverityDebug |
| 182 |
text1 := "string" |
| 183 |
val1 := log.IntValue(1) |
| 184 |
attr1 := log.Int64("1", 2) |
| 185 |
|
| 186 |
r1 := r0.Clone() |
| 187 |
r1.SetTimestamp(now1) |
| 188 |
r1.SetObservedTimestamp(now1) |
| 189 |
r1.SetSeverity(sev1) |
| 190 |
r1.SetSeverityText(text1) |
| 191 |
r1.SetBody(val1) |
| 192 |
r1.AddAttributes(attr1) |
| 193 |
|
| 194 |
// Assertions on original record (r0) |
| 195 |
assert.Equal(t, now0, r0.Timestamp()) |
| 196 |
assert.Equal(t, now0, r0.ObservedTimestamp()) |
| 197 |
assert.Equal(t, sev0, r0.Severity()) |
| 198 |
assert.Equal(t, text0, r0.SeverityText()) |
| 199 |
assert.True(t, val0.Equal(r0.Body())) |
| 200 |
|
| 201 |
var r0Attrs []log.KeyValue |
| 202 |
r0.WalkAttributes(func(kv log.KeyValue) bool { |
| 203 |
r0Attrs = append(r0Attrs, kv) |
| 204 |
return true |
| 205 |
}) |
| 206 |
assert.Contains(t, r0Attrs, attr0) |
| 207 |
assert.NotContains(t, r0Attrs, attr1) |
| 208 |
|
| 209 |
// Assertions on cloned record (r1) |
| 210 |
assert.Equal(t, now1, r1.Timestamp()) |
| 211 |
assert.Equal(t, now1, r1.ObservedTimestamp()) |
| 212 |
assert.Equal(t, sev1, r1.Severity()) |
| 213 |
assert.Equal(t, text1, r1.SeverityText()) |
| 214 |
assert.True(t, val1.Equal(r1.Body())) |
| 215 |
|
| 216 |
var r1Attrs []log.KeyValue |
| 217 |
r1.WalkAttributes(func(kv log.KeyValue) bool { |
| 218 |
r1Attrs = append(r1Attrs, kv) |
| 219 |
return true |
| 220 |
}) |
| 221 |
assert.Contains(t, r1Attrs, attr0) |
| 222 |
assert.Contains(t, r1Attrs, attr1) |
| 223 |
} |
| 224 |
|
| 225 |
func TestRecordErr(t *testing.T) { |
| 226 |
tests := []struct { |
| 227 |
name string |
| 228 |
fn func(*log.Record) |
| 229 |
want error |
| 230 |
}{ |
| 231 |
{ |
| 232 |
name: "zero value", |
| 233 |
}, |
| 234 |
{ |
| 235 |
name: "set error", |
| 236 |
fn: func(r *log.Record) { |
| 237 |
r.SetErr(assert.AnError) |
| 238 |
}, |
| 239 |
want: assert.AnError, |
| 240 |
}, |
| 241 |
{ |
| 242 |
name: "clear error", |
| 243 |
fn: func(r *log.Record) { |
| 244 |
r.SetErr(assert.AnError) |
| 245 |
r.SetErr(nil) |
| 246 |
}, |
| 247 |
}, |
| 248 |
} |
| 249 |
|
| 250 |
for _, tt := range tests { |
| 251 |
t.Run(tt.name, func(t *testing.T) { |
| 252 |
var r log.Record |
| 253 |
if tt.fn != nil { |
| 254 |
tt.fn(&r) |
| 255 |
} |
| 256 |
assert.Equal(t, tt.want, r.Err()) |
| 257 |
}) |
| 258 |
} |
| 259 |
} |