| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package plog |
| 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/collector/pdata/internal" |
| 14 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 15 |
) |
| 16 |
|
| 17 |
func TestLogRecordCount(t *testing.T) { |
| 18 |
logs := NewLogs() |
| 19 |
assert.Equal(t, 0, logs.LogRecordCount()) |
| 20 |
|
| 21 |
rl := logs.ResourceLogs().AppendEmpty() |
| 22 |
assert.Equal(t, 0, logs.LogRecordCount()) |
| 23 |
|
| 24 |
ill := rl.ScopeLogs().AppendEmpty() |
| 25 |
assert.Equal(t, 0, logs.LogRecordCount()) |
| 26 |
|
| 27 |
ill.LogRecords().AppendEmpty() |
| 28 |
assert.Equal(t, 1, logs.LogRecordCount()) |
| 29 |
|
| 30 |
rms := logs.ResourceLogs() |
| 31 |
rms.EnsureCapacity(3) |
| 32 |
rms.AppendEmpty().ScopeLogs().AppendEmpty() |
| 33 |
illl := rms.AppendEmpty().ScopeLogs().AppendEmpty().LogRecords() |
| 34 |
for range 5 { |
| 35 |
illl.AppendEmpty() |
| 36 |
} |
| 37 |
// 5 + 1 (from rms.At(0) initialized first) |
| 38 |
assert.Equal(t, 6, logs.LogRecordCount()) |
| 39 |
} |
| 40 |
|
| 41 |
func TestLogRecordCountWithEmpty(t *testing.T) { |
| 42 |
assert.Zero(t, NewLogs().LogRecordCount()) |
| 43 |
assert.Zero(t, newLogs(&internal.ExportLogsServiceRequest{ |
| 44 |
ResourceLogs: []*internal.ResourceLogs{{}}, |
| 45 |
}, new(internal.State)).LogRecordCount()) |
| 46 |
assert.Zero(t, newLogs(&internal.ExportLogsServiceRequest{ |
| 47 |
ResourceLogs: []*internal.ResourceLogs{ |
| 48 |
{ |
| 49 |
ScopeLogs: []*internal.ScopeLogs{{}}, |
| 50 |
}, |
| 51 |
}, |
| 52 |
}, new(internal.State)).LogRecordCount()) |
| 53 |
assert.Equal(t, 1, newLogs(&internal.ExportLogsServiceRequest{ |
| 54 |
ResourceLogs: []*internal.ResourceLogs{ |
| 55 |
{ |
| 56 |
ScopeLogs: []*internal.ScopeLogs{ |
| 57 |
{ |
| 58 |
LogRecords: []*internal.LogRecord{{}}, |
| 59 |
}, |
| 60 |
}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
}, new(internal.State)).LogRecordCount()) |
| 64 |
} |
| 65 |
|
| 66 |
func TestReadOnlyLogsInvalidUsage(t *testing.T) { |
| 67 |
ld := NewLogs() |
| 68 |
assert.False(t, ld.IsReadOnly()) |
| 69 |
res := ld.ResourceLogs().AppendEmpty().Resource() |
| 70 |
res.Attributes().PutStr("k1", "v1") |
| 71 |
ld.MarkReadOnly() |
| 72 |
assert.True(t, ld.IsReadOnly()) |
| 73 |
assert.Panics(t, func() { res.Attributes().PutStr("k2", "v2") }) |
| 74 |
} |
| 75 |
|
| 76 |
func BenchmarkLogsUsage(b *testing.B) { |
| 77 |
ld := generateTestLogs() |
| 78 |
|
| 79 |
ts := pcommon.NewTimestampFromTime(time.Now()) |
| 80 |
|
| 81 |
b.ReportAllocs() |
| 82 |
|
| 83 |
for b.Loop() { |
| 84 |
for i := 0; i < ld.ResourceLogs().Len(); i++ { |
| 85 |
rl := ld.ResourceLogs().At(i) |
| 86 |
res := rl.Resource() |
| 87 |
res.Attributes().PutStr("foo", "bar") |
| 88 |
v, ok := res.Attributes().Get("foo") |
| 89 |
assert.True(b, ok) |
| 90 |
assert.Equal(b, "bar", v.Str()) |
| 91 |
v.SetStr("new-bar") |
| 92 |
assert.Equal(b, "new-bar", v.Str()) |
| 93 |
res.Attributes().Remove("foo") |
| 94 |
for j := 0; j < rl.ScopeLogs().Len(); j++ { |
| 95 |
sl := rl.ScopeLogs().At(j) |
| 96 |
sl.Scope().SetName("new_test_name") |
| 97 |
assert.Equal(b, "new_test_name", sl.Scope().Name()) |
| 98 |
for k := 0; k < sl.LogRecords().Len(); k++ { |
| 99 |
lr := sl.LogRecords().At(k) |
| 100 |
lr.Body().SetStr("new_body") |
| 101 |
assert.Equal(b, "new_body", lr.Body().Str()) |
| 102 |
lr.SetTimestamp(ts) |
| 103 |
assert.Equal(b, ts, lr.Timestamp()) |
| 104 |
} |
| 105 |
lr := sl.LogRecords().AppendEmpty() |
| 106 |
lr.Body().SetStr("another_log_record") |
| 107 |
lr.SetTimestamp(ts) |
| 108 |
lr.SetObservedTimestamp(ts) |
| 109 |
lr.SetSeverityText("info") |
| 110 |
lr.SetSeverityNumber(SeverityNumberInfo) |
| 111 |
lr.Attributes().PutStr("foo", "bar") |
| 112 |
lr.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}) |
| 113 |
sl.LogRecords().RemoveIf(func(lr LogRecord) bool { |
| 114 |
return lr.Body().Str() == "another_log_record" |
| 115 |
}) |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func BenchmarkLogsMarshalJSON(b *testing.B) { |
| 122 |
ld := generateTestLogs() |
| 123 |
encoder := &JSONMarshaler{} |
| 124 |
|
| 125 |
b.ReportAllocs() |
| 126 |
|
| 127 |
for b.Loop() { |
| 128 |
jsonBuf, err := encoder.MarshalLogs(ld) |
| 129 |
require.NoError(b, err) |
| 130 |
require.NotNil(b, jsonBuf) |
| 131 |
} |
| 132 |
} |