forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonlog_test.go
116 lines (109 loc) · 3.26 KB
/
jsonlog_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
107
108
109
110
111
112
113
114
115
116
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package logzioexporter
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configcompression"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
)
// Logs
func generateLogRecordWithNestedBody() plog.LogRecord {
lr := plog.NewLogRecord()
fillLogOne(lr)
return lr
}
func generateLogRecordWithMultiTypeValues() plog.LogRecord {
lr := plog.NewLogRecord()
fillLogTwo(lr)
return lr
}
func TestConvertLogRecordToJSON(t *testing.T) {
type convertLogRecordToJSONTest struct {
log plog.LogRecord
resource pcommon.Resource
expected map[string]any
}
var convertLogRecordToJSONTests = []convertLogRecordToJSONTest{
{generateLogRecordWithNestedBody(),
pcommon.NewResource(),
map[string]any{
"23": float64(45),
"app": "server",
"foo": "bar",
"instance_num": float64(1),
"level": "Info",
"message": "hello there",
"@timestamp": TestLogTimeUnixMilli,
"nested": map[string]any{"number": float64(499), "string": "v1"},
"spanID": "0102040800000000",
"traceID": "08040201000000000000000000000000",
},
},
{generateLogRecordWithMultiTypeValues(),
pcommon.NewResource(),
map[string]any{
"bool": true,
"customer": "acme",
"env": "dev",
"level": "Info",
"@timestamp": TestLogTimeUnixMilli,
"message": "something happened",
"number": float64(64),
},
},
}
for _, test := range convertLogRecordToJSONTests {
output := convertLogRecordToJSON(test.log, test.log.Attributes())
require.Equal(t, output, test.expected)
}
}
func TestSetTimeStamp(t *testing.T) {
var recordedRequests []byte
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
recordedRequests, _ = io.ReadAll(req.Body)
rw.WriteHeader(http.StatusOK)
}))
ld := generateLogsOneEmptyTimestamp()
cfg := &Config{
Region: "us",
Token: "token",
ClientConfig: confighttp.ClientConfig{
Endpoint: server.URL,
Compression: configcompression.TypeGzip,
},
}
var err error
params := exportertest.NewNopCreateSettings()
exporter, err := createLogsExporter(context.Background(), params, cfg)
require.NoError(t, err)
err = exporter.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
ctx := context.Background()
err = exporter.ConsumeLogs(ctx, ld)
require.NoError(t, err)
err = exporter.Shutdown(ctx)
require.NoError(t, err)
var jsonLog map[string]any
var jsonLogNoTimestamp map[string]any
decoded, _ := gUnzipData(recordedRequests)
requests := strings.Split(string(decoded), "\n")
require.NoError(t, json.Unmarshal([]byte(requests[0]), &jsonLog))
require.NoError(t, json.Unmarshal([]byte(requests[1]), &jsonLogNoTimestamp))
if jsonLogNoTimestamp["@timestamp"] != nil {
t.Fatalf("did not expect @timestamp")
}
if jsonLog["@timestamp"] == nil {
t.Fatalf("@timestamp does not exist")
}
}