-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.mts
170 lines (145 loc) · 4.05 KB
/
index.test.mts
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
import assert from "node:assert";
import process from "node:process";
import {
describe,
test,
} from "node:test";
import { setTimeout as sleep } from "node:timers/promises";
import {
CloudWatchLogs,
type InputLogEvent,
} from "@aws-sdk/client-cloudwatch-logs";
import { layout as jsonLayout } from "log4js-layout-json";
import Level from "log4js/lib/levels.js";
import LoggingEvent from "log4js/lib/LoggingEvent.js";
import {
CloudwatchAppender,
Config,
createLogEventHandler,
LogBuffer,
} from "./dist/index.js";
describe("LogBuffer", () => {
const logbufferConfig: Config = {
batchSize: 5,
bufferTimeout: 500,
accessKeyId: process.env.ACCESSKEY_ID!,
secretAccessKey: process.env.SECRET_ACCESS_KEY!,
logGroupName: "",
logStreamName: "",
};
test("should release logs when batch size is reached", () => {
const mockCallback = (logs: Array<InputLogEvent>) => {
assert.equal(logs.length, 5);
};
const logbuffer = new LogBuffer(logbufferConfig, mockCallback);
for (let i = 0; i < 5; i++) {
logbuffer.push(`log message ${i}`);
}
});
test("should release logs when buffer timeout is reached", async () => {
const mockCallback = (logs: Array<InputLogEvent>) => {
assert.equal(logs.length, 3);
};
const logbuffer = new LogBuffer(logbufferConfig, mockCallback);
for (let i = 0; i < 3; i++) {
logbuffer.push(`log message ${i}`);
}
// Wait for buffer timeout
await sleep(600);
});
});
function makeLogEvent() {
return new LoggingEvent(
"default",
new Level(20000, "INFO", "green"),
["test"],
{ sub: "test" },
undefined,
);
}
describe("AWS Integration", () => {
const config: Config = {
batchSize: 10,
bufferTimeout: 1_000,
accessKeyId: process.env.ACCESSKEY_ID!,
secretAccessKey: process.env.SECRET_ACCESS_KEY!,
region: "eu-central-1",
logGroupName: "prod",
logStreamName: "bar",
// createResources: true
};
const cloudwatchClient = new CloudWatchLogs({
region: config.region,
credentials: {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
},
});
const logbuffer = new LogBuffer(
config,
createLogEventHandler(cloudwatchClient, config),
);
test("fill batch size", async () => {
const layout = jsonLayout();
const appender = new CloudwatchAppender(
config,
layout,
logbuffer,
cloudwatchClient,
);
const append = appender.appenderFunction();
const startTime = Date.now();
// NOTE: batch is pushed after 10 events
for (let i = 0; i < 10; i++) {
const logEvent = makeLogEvent();
append(logEvent);
}
// NOTE: wait for 2s to ensure all events are processed
await sleep(2_000);
// NOTE: fetch log events, if events are not appearing, increase sleep duration
const data = await cloudwatchClient.getLogEvents({
startTime: startTime,
logStreamName: config.logStreamName,
logGroupName: config.logGroupName,
});
assert.equal(data.events?.length, 10);
for (const e of data.events!) {
const data = JSON.parse(e.message!);
assert.equal(data.category, "default");
assert.equal(data.level, "INFO");
assert.equal(data.msg, "test");
}
});
test("wait for buffer timeout", async () => {
const layout = jsonLayout();
const appender = new CloudwatchAppender(
config,
layout,
logbuffer,
cloudwatchClient,
);
const startTime = Date.now();
const append = appender.appenderFunction();
// NOTE: batch is pushed after 10 events
for (let i = 0; i < 5; i++) {
const logEvent = makeLogEvent();
append(logEvent);
}
// NOTE: wait for 2s for buffer timeout
await sleep(2_000);
// NOTE: fetch log events, if events are not appearing, increase sleep duration
const data = await cloudwatchClient.getLogEvents({
startTime: startTime,
logStreamName: config.logStreamName,
logGroupName: config.logGroupName,
});
assert.equal(data.events?.length, 5);
for (const e of data.events!) {
const data = JSON.parse(e.message!);
assert.equal(data.category, "default");
assert.equal(data.level, "INFO");
assert.equal(data.msg, "test");
assert.equal(data.sub, "test"); // from context
}
});
});