1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-10 22:31:50 +02:00

log/logtest: improve Transform usage in the example (#6938)

This commit is contained in:
Robert Pająk
2025-07-07 20:48:19 +02:00
committed by GitHub
parent 9af7382dce
commit bd5e1346ce

View File

@@ -20,20 +20,18 @@ func Example() {
// Emit a log record (code under test). // Emit a log record (code under test).
l := rec.Logger("Example") l := rec.Logger("Example")
ctx := context.Background()
r := log.Record{} r := log.Record{}
r.SetTimestamp(time.Now()) r.SetTimestamp(time.Now())
r.SetSeverity(log.SeverityInfo) r.SetSeverity(log.SeverityInfo)
r.SetBody(log.StringValue("Hello there")) r.SetBody(log.StringValue("Hello there"))
r.AddAttributes(log.String("foo", "bar")) r.AddAttributes(log.String("foo", "bar"))
r.AddAttributes(log.Int("n", 1)) r.AddAttributes(log.Int("n", 1))
l.Emit(ctx, r) l.Emit(context.Background(), r)
// Verify that the expected and actual log records match. // Verify that the expected and actual log records match.
want := logtest.Recording{ want := logtest.Recording{
logtest.Scope{Name: "Example"}: []logtest.Record{ logtest.Scope{Name: "Example"}: []logtest.Record{
{ {
Context: context.Background(),
Severity: log.SeverityInfo, Severity: log.SeverityInfo,
Body: log.StringValue("Hello there"), Body: log.StringValue("Hello there"),
Attributes: []log.KeyValue{ Attributes: []log.KeyValue{
@@ -45,9 +43,11 @@ func Example() {
} }
got := rec.Result() got := rec.Result()
logtest.AssertEqual(t, want, got, logtest.AssertEqual(t, want, got,
// Ignore Timestamps. logtest.Transform(func(r logtest.Record) logtest.Record {
logtest.Transform(func(time.Time) time.Time { r = r.Clone()
return time.Time{} r.Context = nil // Ignore context.
r.Timestamp = time.Time{} // Ignore timestamp.
return r
}), }),
) )
// Output: // Output: