| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package event_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"os" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/google/go-cmp/cmp" |
| 18 |
"golang.org/x/exp/event" |
| 19 |
"golang.org/x/exp/event/adapter/logfmt" |
| 20 |
"golang.org/x/exp/event/eventtest" |
| 21 |
) |
| 22 |
|
| 23 |
var ( |
| 24 |
l1 = event.Int64("l1", 1) |
| 25 |
l2 = event.Int64("l2", 2) |
| 26 |
l3 = event.Int64("l3", 3) |
| 27 |
counter = event.NewCounter("hits", nil) |
| 28 |
gauge = event.NewFloatGauge("temperature", nil) |
| 29 |
latency = event.NewDuration("latency", nil) |
| 30 |
err = errors.New("an error") |
| 31 |
) |
| 32 |
|
| 33 |
func TestCommon(t *testing.T) { |
| 34 |
for _, test := range []struct { |
| 35 |
method string |
| 36 |
events func(context.Context) |
| 37 |
expect []event.Event |
| 38 |
}{{ |
| 39 |
method: "simple", |
| 40 |
events: func(ctx context.Context) { event.Log(ctx, "a message") }, |
| 41 |
expect: []event.Event{{ |
| 42 |
ID: 1, |
| 43 |
Kind: event.LogKind, |
| 44 |
Labels: []event.Label{event.String("msg", "a message")}, |
| 45 |
}}, |
| 46 |
}, { |
| 47 |
method: "log 1", |
| 48 |
events: func(ctx context.Context) { event.Log(ctx, "a message", l1) }, |
| 49 |
expect: []event.Event{{ |
| 50 |
ID: 1, |
| 51 |
Kind: event.LogKind, |
| 52 |
Labels: []event.Label{event.String("msg", "a message"), l1}, |
| 53 |
}}, |
| 54 |
}, { |
| 55 |
method: "log 2", |
| 56 |
events: func(ctx context.Context) { event.Log(ctx, "a message", l1, l2) }, |
| 57 |
expect: []event.Event{{ |
| 58 |
ID: 1, |
| 59 |
Kind: event.LogKind, |
| 60 |
Labels: []event.Label{event.String("msg", "a message"), l1, l2}, |
| 61 |
}}, |
| 62 |
}, { |
| 63 |
method: "log 3", |
| 64 |
events: func(ctx context.Context) { event.Log(ctx, "a message", l1, l2, l3) }, |
| 65 |
expect: []event.Event{{ |
| 66 |
ID: 1, |
| 67 |
Kind: event.LogKind, |
| 68 |
Labels: []event.Label{event.String("msg", "a message"), l1, l2, l3}, |
| 69 |
}}, |
| 70 |
}, { |
| 71 |
method: "logf", |
| 72 |
events: func(ctx context.Context) { event.Logf(ctx, "logf %s message", "to") }, |
| 73 |
expect: []event.Event{{ |
| 74 |
ID: 1, |
| 75 |
Kind: event.LogKind, |
| 76 |
Labels: []event.Label{event.String("msg", "logf to message")}, |
| 77 |
}}, |
| 78 |
}, { |
| 79 |
method: "error", |
| 80 |
events: func(ctx context.Context) { event.Error(ctx, "failed", err, l1) }, |
| 81 |
expect: []event.Event{{ |
| 82 |
ID: 1, |
| 83 |
Kind: event.LogKind, |
| 84 |
Labels: []event.Label{ |
| 85 |
event.String("msg", "failed"), |
| 86 |
event.Value("error", err), |
| 87 |
l1, |
| 88 |
}, |
| 89 |
}}, |
| 90 |
}, { |
| 91 |
method: "span", |
| 92 |
events: func(ctx context.Context) { |
| 93 |
ctx = event.Start(ctx, `span`) |
| 94 |
event.End(ctx) |
| 95 |
}, |
| 96 |
expect: []event.Event{{ |
| 97 |
ID: 1, |
| 98 |
Kind: event.StartKind, |
| 99 |
Labels: []event.Label{event.String("name", "span")}, |
| 100 |
}, { |
| 101 |
ID: 2, |
| 102 |
Parent: 1, |
| 103 |
Kind: event.EndKind, |
| 104 |
Labels: []event.Label{}, |
| 105 |
}}, |
| 106 |
}, { |
| 107 |
method: "span nested", |
| 108 |
events: func(ctx context.Context) { |
| 109 |
ctx = event.Start(ctx, "parent") |
| 110 |
defer event.End(ctx) |
| 111 |
child := event.Start(ctx, "child") |
| 112 |
defer event.End(child) |
| 113 |
event.Log(child, "message") |
| 114 |
}, |
| 115 |
expect: []event.Event{{ |
| 116 |
ID: 1, |
| 117 |
Kind: event.StartKind, |
| 118 |
Labels: []event.Label{event.String("name", "parent")}, |
| 119 |
}, { |
| 120 |
ID: 2, |
| 121 |
Parent: 1, |
| 122 |
Kind: event.StartKind, |
| 123 |
Labels: []event.Label{event.String("name", "child")}, |
| 124 |
}, { |
| 125 |
ID: 3, |
| 126 |
Parent: 2, |
| 127 |
Kind: event.LogKind, |
| 128 |
Labels: []event.Label{event.String("msg", "message")}, |
| 129 |
}, { |
| 130 |
ID: 4, |
| 131 |
Parent: 2, |
| 132 |
Kind: event.EndKind, |
| 133 |
Labels: []event.Label{}, |
| 134 |
}, { |
| 135 |
ID: 5, |
| 136 |
Parent: 1, |
| 137 |
Kind: event.EndKind, |
| 138 |
Labels: []event.Label{}, |
| 139 |
}}, |
| 140 |
}, { |
| 141 |
method: "counter", |
| 142 |
events: func(ctx context.Context) { counter.Record(ctx, 2, l1) }, |
| 143 |
expect: []event.Event{{ |
| 144 |
ID: 1, |
| 145 |
Kind: event.MetricKind, |
| 146 |
Labels: []event.Label{ |
| 147 |
event.Int64("metricValue", 2), |
| 148 |
event.Value("metric", counter), |
| 149 |
l1, |
| 150 |
}, |
| 151 |
}}, |
| 152 |
}, { |
| 153 |
method: "gauge", |
| 154 |
events: func(ctx context.Context) { gauge.Record(ctx, 98.6, l1) }, |
| 155 |
expect: []event.Event{{ |
| 156 |
ID: 1, |
| 157 |
Kind: event.MetricKind, |
| 158 |
Labels: []event.Label{ |
| 159 |
event.Float64("metricValue", 98.6), |
| 160 |
event.Value("metric", gauge), |
| 161 |
l1, |
| 162 |
}, |
| 163 |
}}, |
| 164 |
}, { |
| 165 |
method: "duration", |
| 166 |
events: func(ctx context.Context) { latency.Record(ctx, 3*time.Second, l1, l2) }, |
| 167 |
expect: []event.Event{{ |
| 168 |
ID: 1, |
| 169 |
Kind: event.MetricKind, |
| 170 |
Labels: []event.Label{ |
| 171 |
event.Duration("metricValue", 3*time.Second), |
| 172 |
event.Value("metric", latency), |
| 173 |
l1, l2, |
| 174 |
}, |
| 175 |
}}, |
| 176 |
}, { |
| 177 |
method: "annotate", |
| 178 |
events: func(ctx context.Context) { event.Annotate(ctx, l1) }, |
| 179 |
expect: []event.Event{{ |
| 180 |
ID: 1, |
| 181 |
Labels: []event.Label{l1}, |
| 182 |
}}, |
| 183 |
}, { |
| 184 |
method: "annotate 2", |
| 185 |
events: func(ctx context.Context) { event.Annotate(ctx, l1, l2) }, |
| 186 |
expect: []event.Event{{ |
| 187 |
ID: 1, |
| 188 |
Labels: []event.Label{l1, l2}, |
| 189 |
}}, |
| 190 |
}, { |
| 191 |
method: "multiple events", |
| 192 |
events: func(ctx context.Context) { |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
event.Log(ctx, "my event", event.Int64("myInt", 6)) |
| 200 |
event.Log(ctx, "string event", event.String("myString", "some string value")) |
| 201 |
}, |
| 202 |
expect: []event.Event{{ |
| 203 |
ID: 1, |
| 204 |
Kind: event.LogKind, |
| 205 |
Labels: []event.Label{ |
| 206 |
event.String("msg", "my event"), |
| 207 |
event.Int64("myInt", 6), |
| 208 |
}, |
| 209 |
}, { |
| 210 |
ID: 2, |
| 211 |
Kind: event.LogKind, |
| 212 |
Labels: []event.Label{ |
| 213 |
event.String("msg", "string event"), |
| 214 |
event.String("myString", "some string value"), |
| 215 |
}, |
| 216 |
}}, |
| 217 |
}} { |
| 218 |
t.Run(test.method, func(t *testing.T) { |
| 219 |
ctx, h := eventtest.NewCapture() |
| 220 |
test.events(ctx) |
| 221 |
if diff := cmp.Diff(test.expect, h.Got, eventtest.CmpOptions()...); diff != "" { |
| 222 |
t.Errorf("mismatch (-want, +got):\n%s", diff) |
| 223 |
} |
| 224 |
}) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func ExampleLog() { |
| 229 |
ctx := event.WithExporter(context.Background(), event.NewExporter(logfmt.NewHandler(os.Stdout), eventtest.ExporterOptions())) |
| 230 |
event.Log(ctx, "my event", event.Int64("myInt", 6)) |
| 231 |
event.Log(ctx, "error event", event.String("myString", "some string value")) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
func TestLogEventf(t *testing.T) { |
| 238 |
eventtest.TestBenchmark(t, eventPrint, eventLogf, eventtest.LogfOutput) |
| 239 |
} |
| 240 |
|
| 241 |
func TestLogEvent(t *testing.T) { |
| 242 |
eventtest.TestBenchmark(t, eventPrint, eventLog, eventtest.LogfmtOutput) |
| 243 |
} |
| 244 |
|
| 245 |
func TestTraceBuilder(t *testing.T) { |
| 246 |
|
| 247 |
|
| 248 |
ctx := event.WithExporter(context.Background(), event.NewExporter(&testTraceHandler{t: t}, eventtest.ExporterOptions())) |
| 249 |
ctx = event.Start(ctx, "s") |
| 250 |
val := ctx.Value("x") |
| 251 |
if val != 1 { |
| 252 |
t.Fatal("context not returned from Start") |
| 253 |
} |
| 254 |
event.End(ctx) |
| 255 |
} |
| 256 |
|
| 257 |
type testTraceHandler struct { |
| 258 |
t *testing.T |
| 259 |
} |
| 260 |
|
| 261 |
func (t *testTraceHandler) Event(ctx context.Context, ev *event.Event) context.Context { |
| 262 |
switch ev.Kind { |
| 263 |
case event.StartKind: |
| 264 |
return context.WithValue(ctx, "x", 1) |
| 265 |
case event.EndKind: |
| 266 |
val := ctx.Value("x") |
| 267 |
if val != 1 { |
| 268 |
t.t.Fatal("Start context not passed to End") |
| 269 |
} |
| 270 |
return ctx |
| 271 |
default: |
| 272 |
return ctx |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
func TestTraceDuration(t *testing.T) { |
| 277 |
|
| 278 |
dur := event.NewDuration("test", nil) |
| 279 |
want := time.Second |
| 280 |
|
| 281 |
check := func(t *testing.T, h *testTraceDurationHandler) { |
| 282 |
if !h.got.HasValue() { |
| 283 |
t.Fatal("no metric value") |
| 284 |
} |
| 285 |
got := h.got.Duration() |
| 286 |
if got != want { |
| 287 |
t.Fatalf("got %s, want %s", got, want) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
t.Run("returned builder", func(t *testing.T) { |
| 292 |
h := &testTraceDurationHandler{} |
| 293 |
ctx := event.WithExporter(context.Background(), event.NewExporter(h, eventtest.ExporterOptions())) |
| 294 |
ctx = event.Start(ctx, "s") |
| 295 |
time.Sleep(want) |
| 296 |
event.End(ctx, event.DurationMetric.Of(dur)) |
| 297 |
check(t, h) |
| 298 |
}) |
| 299 |
|
| 300 |
t.Run("separate builder", func(t *testing.T) { |
| 301 |
h := &testTraceDurationHandler{} |
| 302 |
ctx := event.WithExporter(context.Background(), event.NewExporter(h, eventtest.ExporterOptions())) |
| 303 |
ctx = event.Start(ctx, "s") |
| 304 |
time.Sleep(want) |
| 305 |
event.End(ctx, event.DurationMetric.Of(dur)) |
| 306 |
check(t, h) |
| 307 |
}) |
| 308 |
} |
| 309 |
|
| 310 |
type testTraceDurationHandler struct { |
| 311 |
got event.Label |
| 312 |
} |
| 313 |
|
| 314 |
func (t *testTraceDurationHandler) Event(ctx context.Context, ev *event.Event) context.Context { |
| 315 |
for _, l := range ev.Labels { |
| 316 |
if l.Name == string(event.MetricVal) { |
| 317 |
t.got = l |
| 318 |
} |
| 319 |
} |
| 320 |
return ctx |
| 321 |
} |
| 322 |
|
| 323 |
func BenchmarkBuildContext(b *testing.B) { |
| 324 |
|
| 325 |
c := event.NewCounter("c", nil) |
| 326 |
for _, depth := range []int{1, 5, 7, 10} { |
| 327 |
b.Run(fmt.Sprintf("depth %d", depth), func(b *testing.B) { |
| 328 |
ctx := event.WithExporter(context.Background(), event.NewExporter(nopHandler{}, eventtest.ExporterOptions())) |
| 329 |
for i := 0; i < depth; i++ { |
| 330 |
ctx = context.WithValue(ctx, i, i) |
| 331 |
} |
| 332 |
b.Run("direct", func(b *testing.B) { |
| 333 |
for i := 0; i < b.N; i++ { |
| 334 |
c.Record(ctx, 1) |
| 335 |
} |
| 336 |
}) |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
}) |
| 346 |
} |
| 347 |
} |