-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathexporter_test.go
272 lines (222 loc) · 7.55 KB
/
exporter_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package mezmoexporter
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
var buildInfo = component.BuildInfo{
Version: "1.0",
}
func createSimpleLogData(numberOfLogs int) plog.Logs {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty() // Add an empty ResourceLogs
rl := logs.ResourceLogs().AppendEmpty()
rl.ScopeLogs().AppendEmpty() // Add an empty ScopeLogs
sl := rl.ScopeLogs().AppendEmpty()
for i := 0; i < numberOfLogs; i++ {
ts := pcommon.Timestamp(int64(i) * time.Millisecond.Nanoseconds())
logRecord := sl.LogRecords().AppendEmpty()
logRecord.Body().SetStr("10byteslog")
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.SetTimestamp(ts)
}
return logs
}
func createMinimalAttributesLogData(numberOfLogs int) plog.Logs {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty()
rl := logs.ResourceLogs().AppendEmpty()
rl.ScopeLogs().AppendEmpty()
sl := rl.ScopeLogs().AppendEmpty()
for i := 0; i < numberOfLogs; i++ {
logRecord := sl.LogRecords().AppendEmpty()
logRecord.Body().SetStr("minimal attribute log")
}
return logs
}
// Creates a logs set that exceeds the maximum message side we can send in one HTTP POST
func createMaxLogData() plog.Logs {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty() // Add an empty ResourceLogs
rl := logs.ResourceLogs().AppendEmpty()
rl.ScopeLogs().AppendEmpty() // Add an empty ScopeLogs
sl := rl.ScopeLogs().AppendEmpty()
var lineLen = maxMessageSize
var lineCnt = (maxBodySize / lineLen) * 2
for i := 0; i < lineCnt; i++ {
ts := pcommon.Timestamp(int64(i) * time.Millisecond.Nanoseconds())
logRecord := sl.LogRecords().AppendEmpty()
logRecord.Body().SetStr(randString(maxMessageSize))
logRecord.SetTimestamp(ts)
}
return logs
}
func createSizedPayloadLogData(payloadSize int) plog.Logs {
logs := plog.NewLogs()
logs.ResourceLogs().AppendEmpty() // Add an empty ResourceLogs
rl := logs.ResourceLogs().AppendEmpty()
rl.ScopeLogs().AppendEmpty() // Add an empty ScopeLogs
sl := rl.ScopeLogs().AppendEmpty()
maxMsg := randString(payloadSize)
ts := pcommon.Timestamp(0)
logRecord := sl.LogRecords().AppendEmpty()
logRecord.Body().SetStr(maxMsg)
logRecord.SetTimestamp(ts)
return logs
}
type testServer struct {
instance *httptest.Server
url string
}
type httpAssertionCallback func(req *http.Request, body mezmoLogBody) (int, string)
type testServerParams struct {
t *testing.T
assertionsCallback httpAssertionCallback
}
// Creates an HTTP server to test log delivery payloads by applying a set of
// assertions through the assertCB function.
func createHTTPServer(params *testServerParams) testServer {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
params.t.Fatal(err)
}
var logBody mezmoLogBody
if err = json.Unmarshal(body, &logBody); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
}
statusCode, responseBody := params.assertionsCallback(r, logBody)
w.WriteHeader(statusCode)
if len(responseBody) > 0 {
_, err = w.Write([]byte(responseBody))
assert.NoError(params.t, err)
}
}))
serverURL, err := url.Parse(httpServer.URL)
assert.NoError(params.t, err)
server := testServer{
instance: httpServer,
url: serverURL.String(),
}
return server
}
func createExporter(t *testing.T, config *Config, logger *zap.Logger) *mezmoExporter {
exporter := newLogsExporter(config, componenttest.NewNopTelemetrySettings(), buildInfo, logger)
require.NotNil(t, exporter)
err := exporter.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
return exporter
}
func createLogger() (*zap.Logger, *observer.ObservedLogs) {
core, logObserver := observer.New(zap.DebugLevel)
logger := zap.New(core)
return logger, logObserver
}
func TestLogsExporter(t *testing.T) {
httpServerParams := testServerParams{
t: t,
assertionsCallback: func(req *http.Request, _ mezmoLogBody) (int, string) {
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
assert.Equal(t, "mezmo-otel-exporter/"+buildInfo.Version, req.Header.Get("User-Agent"))
return http.StatusOK, ""
},
}
server := createHTTPServer(&httpServerParams)
defer server.instance.Close()
log, _ := createLogger()
config := &Config{
IngestURL: server.url,
}
exporter := createExporter(t, config, log)
t.Run("Test simple log data", func(t *testing.T) {
var logs = createSimpleLogData(3)
err := exporter.pushLogData(context.Background(), logs)
require.NoError(t, err)
})
t.Run("Test max message size", func(t *testing.T) {
var logs = createSizedPayloadLogData(maxMessageSize)
err := exporter.pushLogData(context.Background(), logs)
require.NoError(t, err)
})
t.Run("Test max body size", func(t *testing.T) {
var logs = createMaxLogData()
err := exporter.pushLogData(context.Background(), logs)
require.NoError(t, err)
})
}
func TestAddsRequiredAttributes(t *testing.T) {
httpServerParams := testServerParams{
t: t,
assertionsCallback: func(req *http.Request, body mezmoLogBody) (int, string) {
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
assert.Equal(t, "mezmo-otel-exporter/"+buildInfo.Version, req.Header.Get("User-Agent"))
lines := body.Lines
for _, line := range lines {
assert.True(t, line.Timestamp > 0)
assert.Equal(t, line.Level, "info")
assert.Equal(t, line.App, "")
assert.Equal(t, line.Line, "minimal attribute log")
}
return http.StatusOK, ""
},
}
server := createHTTPServer(&httpServerParams)
defer server.instance.Close()
log, _ := createLogger()
config := &Config{
IngestURL: server.url,
}
exporter := createExporter(t, config, log)
logs := createMinimalAttributesLogData(4)
err := exporter.pushLogData(context.Background(), logs)
require.NoError(t, err)
}
func Test404IngestError(t *testing.T) {
log, logObserver := createLogger()
httpServerParams := testServerParams{
t: t,
assertionsCallback: func(_ *http.Request, _ mezmoLogBody) (int, string) {
return http.StatusNotFound, `{"foo":"bar"}`
},
}
server := createHTTPServer(&httpServerParams)
defer server.instance.Close()
config := &Config{
IngestURL: fmt.Sprintf("%s/foobar", server.url),
}
exporter := createExporter(t, config, log)
logs := createSizedPayloadLogData(1)
err := exporter.pushLogData(context.Background(), logs)
require.NoError(t, err)
assert.Equal(t, logObserver.Len(), 2)
logLine := logObserver.All()[0]
assert.Equal(t, logLine.Message, "got http status (/foobar): 404 Not Found")
assert.Equal(t, logLine.Level, zapcore.ErrorLevel)
logLine = logObserver.All()[1]
assert.Equal(t, logLine.Message, "http response")
assert.Equal(t, logLine.Level, zapcore.DebugLevel)
responseField := logLine.Context[0]
assert.Equal(t, responseField.Key, "response")
assert.Equal(t, responseField.String, `{"foo":"bar"}`)
}