-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
logsdata_to_logservice_test.go
106 lines (92 loc) · 3.16 KB
/
logsdata_to_logservice_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package alibabacloudlogserviceexporter
import (
"sort"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
)
func fillComplexAttributeValueMap(m pcommon.Map) {
m.PutBool("result", true)
m.PutStr("status", "ok")
m.PutDouble("value", 1.3)
m.PutInt("code", 200)
m.PutEmpty("null")
m.PutEmptySlice("array").AppendEmpty().SetStr("array")
m.PutEmptyMap("map").PutStr("data", "hello world")
m.PutStr("status", "ok")
}
func createLogData(numberOfLogs int) plog.Logs {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty() // Add an empty ResourceLogs
rl := logs.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().PutStr("resouceKey", "resourceValue")
rl.Resource().Attributes().PutStr(conventions.AttributeServiceName, "test-log-service-exporter")
rl.Resource().Attributes().PutStr(conventions.AttributeHostName, "test-host")
sl := rl.ScopeLogs().AppendEmpty()
sl.Scope().SetName("collector")
sl.Scope().SetVersion("v0.1.0")
for i := 0; i < numberOfLogs; i++ {
ts := pcommon.Timestamp(int64(i) * time.Millisecond.Nanoseconds())
logRecord := sl.LogRecords().AppendEmpty()
switch i {
case 0:
// do nothing, left body null
case 1:
logRecord.Body().SetBool(true)
case 2:
logRecord.Body().SetInt(2.0)
case 3:
logRecord.Body().SetDouble(3.0)
case 4:
logRecord.Body().SetStr("4")
case 5:
fillComplexAttributeValueMap(logRecord.Attributes().PutEmptyMap("map-value"))
logRecord.Body().SetStr("log contents")
case 6:
logRecord.Attributes().PutEmptySlice("array-value").AppendEmpty().SetStr("array")
logRecord.Body().SetStr("log contents")
default:
logRecord.Body().SetStr("log contents")
}
logRecord.Attributes().PutStr(conventions.AttributeServiceName, "myapp")
logRecord.Attributes().PutStr("my-label", "myapp-type")
logRecord.Attributes().PutStr(conventions.AttributeHostName, "myhost")
logRecord.Attributes().PutStr("custom", "custom")
logRecord.Attributes().PutEmpty("null-value")
logRecord.SetTimestamp(ts)
}
return logs
}
func TestLogsDataToLogService(t *testing.T) {
totalLogCount := 10
validLogCount := totalLogCount - 1
gotLogs := logDataToLogService(createLogData(10))
assert.Equal(t, len(gotLogs), 9)
gotLogPairs := make([][]logKeyValuePair, 0, len(gotLogs))
for _, log := range gotLogs {
pairs := make([]logKeyValuePair, 0, len(log.Contents))
for _, content := range log.Contents {
pairs = append(pairs, logKeyValuePair{
Key: content.GetKey(),
Value: content.GetValue(),
})
}
gotLogPairs = append(gotLogPairs, pairs)
}
wantLogs := make([][]logKeyValuePair, 0, validLogCount)
resultLogFile := "./testdata/logservice_log_data.json"
if err := loadFromJSON(resultLogFile, &wantLogs); err != nil {
t.Errorf("Failed load log key value pairs from %q: %v", resultLogFile, err)
return
}
for j := 0; j < validLogCount; j++ {
sort.Sort(logKeyValuePairs(gotLogPairs[j]))
sort.Sort(logKeyValuePairs(wantLogs[j]))
assert.Equal(t, wantLogs[j], gotLogPairs[j])
}
}