| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package logtest |
| 5 |
|
| 6 |
import ( |
| 7 |
"slices" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
"go.opentelemetry.io/otel/log" |
| 15 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 16 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 17 |
"go.opentelemetry.io/otel/sdk/resource" |
| 18 |
"go.opentelemetry.io/otel/trace" |
| 19 |
) |
| 20 |
|
| 21 |
func TestRecordFactoryEmpty(t *testing.T) { |
| 22 |
assert.Equal(t, sdklog.Record{}, RecordFactory{}.NewRecord()) |
| 23 |
} |
| 24 |
|
| 25 |
func TestRecordFactory(t *testing.T) { |
| 26 |
now := time.Now() |
| 27 |
observed := now.Add(time.Second) |
| 28 |
eventName := "testing.name" |
| 29 |
severity := log.SeverityDebug |
| 30 |
severityText := "DBG" |
| 31 |
body := log.StringValue("Message") |
| 32 |
attrs := []log.KeyValue{ |
| 33 |
log.Int("int", 1), |
| 34 |
log.String("str", "foo"), |
| 35 |
log.Float64("flt", 3.14), |
| 36 |
} |
| 37 |
traceID := trace.TraceID([16]byte{1}) |
| 38 |
spanID := trace.SpanID([8]byte{2}) |
| 39 |
traceFlags := trace.FlagsSampled |
| 40 |
dropped := 3 |
| 41 |
scope := instrumentation.Scope{ |
| 42 |
Name: t.Name(), |
| 43 |
} |
| 44 |
r := resource.NewSchemaless(attribute.Bool("works", true)) |
| 45 |
|
| 46 |
got := RecordFactory{ |
| 47 |
EventName: eventName, |
| 48 |
Timestamp: now, |
| 49 |
ObservedTimestamp: observed, |
| 50 |
Severity: severity, |
| 51 |
SeverityText: severityText, |
| 52 |
Body: body, |
| 53 |
Attributes: attrs, |
| 54 |
TraceID: traceID, |
| 55 |
SpanID: spanID, |
| 56 |
TraceFlags: traceFlags, |
| 57 |
DroppedAttributes: dropped, |
| 58 |
InstrumentationScope: &scope, |
| 59 |
Resource: r, |
| 60 |
}.NewRecord() |
| 61 |
|
| 62 |
assert.Equal(t, eventName, got.EventName()) |
| 63 |
assert.Equal(t, now, got.Timestamp()) |
| 64 |
assert.Equal(t, observed, got.ObservedTimestamp()) |
| 65 |
assert.Equal(t, severity, got.Severity()) |
| 66 |
assert.Equal(t, severityText, got.SeverityText()) |
| 67 |
assertBody(t, body, got) |
| 68 |
assertAttributes(t, attrs, got) |
| 69 |
assert.Equal(t, dropped, got.DroppedAttributes()) |
| 70 |
assert.Equal(t, traceID, got.TraceID()) |
| 71 |
assert.Equal(t, spanID, got.SpanID()) |
| 72 |
assert.Equal(t, traceFlags, got.TraceFlags()) |
| 73 |
assert.Equal(t, scope, got.InstrumentationScope()) |
| 74 |
assert.Equal(t, r, got.Resource()) |
| 75 |
} |
| 76 |
|
| 77 |
func TestRecordFactoryMultiple(t *testing.T) { |
| 78 |
now := time.Now() |
| 79 |
attrs := []log.KeyValue{ |
| 80 |
log.Int("int", 1), |
| 81 |
log.String("str", "foo"), |
| 82 |
log.Float64("flt", 3.14), |
| 83 |
} |
| 84 |
scope := instrumentation.Scope{ |
| 85 |
Name: t.Name(), |
| 86 |
} |
| 87 |
|
| 88 |
f := RecordFactory{ |
| 89 |
Timestamp: now, |
| 90 |
Attributes: attrs, |
| 91 |
DroppedAttributes: 1, |
| 92 |
InstrumentationScope: &scope, |
| 93 |
} |
| 94 |
|
| 95 |
record1 := f.NewRecord() |
| 96 |
|
| 97 |
f.Attributes = append(f.Attributes, log.Bool("added", true)) |
| 98 |
f.DroppedAttributes = 2 |
| 99 |
record2 := f.NewRecord() |
| 100 |
|
| 101 |
assert.Equal(t, now, record2.Timestamp()) |
| 102 |
assertAttributes(t, append(attrs, log.Bool("added", true)), record2) |
| 103 |
assert.Equal(t, 2, record2.DroppedAttributes()) |
| 104 |
assert.Equal(t, scope, record2.InstrumentationScope()) |
| 105 |
|
| 106 |
// Previously returned record is unharmed by the builder changes. |
| 107 |
assert.Equal(t, now, record1.Timestamp()) |
| 108 |
assertAttributes(t, attrs, record1) |
| 109 |
assert.Equal(t, 1, record1.DroppedAttributes()) |
| 110 |
assert.Equal(t, scope, record1.InstrumentationScope()) |
| 111 |
} |
| 112 |
|
| 113 |
func assertBody(t *testing.T, want log.Value, r sdklog.Record) { |
| 114 |
t.Helper() |
| 115 |
got := r.Body() |
| 116 |
if !got.Equal(want) { |
| 117 |
t.Errorf("Body value is not equal:\nwant: %v\ngot: %v", want, got) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func assertAttributes(t *testing.T, want []log.KeyValue, r sdklog.Record) { |
| 122 |
t.Helper() |
| 123 |
var got []log.KeyValue |
| 124 |
r.WalkAttributes(func(kv log.KeyValue) bool { |
| 125 |
got = append(got, kv) |
| 126 |
return true |
| 127 |
}) |
| 128 |
if !slices.EqualFunc(want, got, log.KeyValue.Equal) { |
| 129 |
t.Errorf("Attributes are not equal:\nwant: %v\ngot: %v", want, got) |
| 130 |
} |
| 131 |
} |