@chirpier/chirpier-js 0.1.6 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +184 -108
- package/dist/__tests__/chirpier.test.js +435 -100
- package/dist/constants.d.ts +7 -6
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +5 -4
- package/dist/index.d.ts +129 -56
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +483 -203
- package/package.json +5 -5
- package/src/__tests__/chirpier.test.ts +296 -92
- package/src/constants.ts +7 -6
- package/src/index.ts +492 -206
|
@@ -9,8 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g =
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
14
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
15
|
function step(op) {
|
|
16
16
|
if (f) throw new TypeError("Generator is already executing.");
|
|
@@ -39,177 +39,512 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var fs_1 = __importDefault(require("fs"));
|
|
43
|
+
var os_1 = __importDefault(require("os"));
|
|
44
|
+
var path_1 = __importDefault(require("path"));
|
|
45
|
+
var axios_1 = __importDefault(require("axios"));
|
|
46
|
+
var axios_mock_adapter_1 = __importDefault(require("axios-mock-adapter"));
|
|
42
47
|
var index_1 = require("../index");
|
|
43
48
|
var constants_1 = require("../constants");
|
|
44
|
-
var axios_mock_adapter_1 = __importDefault(require("axios-mock-adapter"));
|
|
45
|
-
var axios_1 = __importDefault(require("axios"));
|
|
46
49
|
describe("Chirpier SDK", function () {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
expect(function () { return (0, index_1.monitor)(event); }).toThrow("Chirpier SDK is not initialized. Please call initialize() first.");
|
|
56
|
-
});
|
|
57
|
-
test("should initialize with default values", function () {
|
|
58
|
-
(0, index_1.initialize)({
|
|
59
|
-
key: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
|
|
60
|
-
logLevel: index_1.LogLevel.None,
|
|
61
|
-
});
|
|
62
|
-
var chirpier = index_1.Chirpier.getInstance({
|
|
63
|
-
key: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
|
|
64
|
-
});
|
|
65
|
-
// Setup mock server
|
|
66
|
-
var mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
67
|
-
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
68
|
-
expect(chirpier === null || chirpier === void 0 ? void 0 : chirpier["apiEndpoint"]).toBe(constants_1.DEFAULT_API_ENDPOINT);
|
|
69
|
-
expect(chirpier === null || chirpier === void 0 ? void 0 : chirpier["retries"]).toBe(constants_1.DEFAULT_RETRIES);
|
|
70
|
-
expect(chirpier === null || chirpier === void 0 ? void 0 : chirpier["timeout"]).toBe(constants_1.DEFAULT_TIMEOUT);
|
|
71
|
-
expect(chirpier === null || chirpier === void 0 ? void 0 : chirpier["batchSize"]).toBe(constants_1.DEFAULT_BATCH_SIZE);
|
|
72
|
-
expect(chirpier === null || chirpier === void 0 ? void 0 : chirpier["flushDelay"]).toBe(constants_1.DEFAULT_FLUSH_DELAY);
|
|
73
|
-
index_1.Chirpier.stop();
|
|
50
|
+
afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
51
|
+
return __generator(this, function (_a) {
|
|
52
|
+
switch (_a.label) {
|
|
53
|
+
case 0: return [4 /*yield*/, (0, index_1.stop)()];
|
|
54
|
+
case 1:
|
|
55
|
+
_a.sent();
|
|
56
|
+
return [2 /*return*/];
|
|
57
|
+
}
|
|
74
58
|
});
|
|
75
|
-
|
|
59
|
+
}); });
|
|
60
|
+
describe("Initialization", function () {
|
|
61
|
+
test("should throw error if logEvent is called before initialize", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
62
|
+
var log;
|
|
63
|
+
return __generator(this, function (_a) {
|
|
64
|
+
switch (_a.label) {
|
|
65
|
+
case 0:
|
|
66
|
+
log = {
|
|
67
|
+
event: "test-event",
|
|
68
|
+
value: 1,
|
|
69
|
+
};
|
|
70
|
+
return [4 /*yield*/, expect((0, index_1.logEvent)(log)).rejects.toThrow(index_1.ChirpierError)];
|
|
71
|
+
case 1:
|
|
72
|
+
_a.sent();
|
|
73
|
+
return [4 /*yield*/, expect((0, index_1.logEvent)(log)).rejects.toThrow("Chirpier SDK is not initialized. Please call initialize() first.")];
|
|
74
|
+
case 2:
|
|
75
|
+
_a.sent();
|
|
76
|
+
return [2 /*return*/];
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}); });
|
|
80
|
+
test("should initialize with default values", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
81
|
+
var mock;
|
|
82
|
+
return __generator(this, function (_a) {
|
|
83
|
+
switch (_a.label) {
|
|
84
|
+
case 0:
|
|
85
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
86
|
+
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
87
|
+
(0, index_1.initialize)({
|
|
88
|
+
key: "chp_test_default_key",
|
|
89
|
+
logLevel: 0 /* LogLevel.None */,
|
|
90
|
+
});
|
|
91
|
+
return [4 /*yield*/, (0, index_1.logEvent)({ event: "sdk.initialized", value: 1 })];
|
|
92
|
+
case 1:
|
|
93
|
+
_a.sent();
|
|
94
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
95
|
+
case 2:
|
|
96
|
+
_a.sent();
|
|
97
|
+
expect(mock.history.post[0].url).toBe(constants_1.DEFAULT_API_ENDPOINT);
|
|
98
|
+
return [2 /*return*/];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}); });
|
|
102
|
+
test("should initialize with custom apiEndpoint", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
103
|
+
var customEndpoint, mock;
|
|
104
|
+
return __generator(this, function (_a) {
|
|
105
|
+
switch (_a.label) {
|
|
106
|
+
case 0:
|
|
107
|
+
customEndpoint = "https://localhost:3001/v1.0/logs";
|
|
108
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
109
|
+
mock.onPost(customEndpoint).reply(200, { success: true });
|
|
110
|
+
(0, index_1.initialize)({
|
|
111
|
+
key: "chp_test_custom_endpoint",
|
|
112
|
+
apiEndpoint: customEndpoint,
|
|
113
|
+
logLevel: 0 /* LogLevel.None */,
|
|
114
|
+
});
|
|
115
|
+
return [4 /*yield*/, (0, index_1.logEvent)({ event: "sdk.custom-endpoint", value: 1 })];
|
|
116
|
+
case 1:
|
|
117
|
+
_a.sent();
|
|
118
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
119
|
+
case 2:
|
|
120
|
+
_a.sent();
|
|
121
|
+
expect(mock.history.post[0].url).toBe(customEndpoint);
|
|
122
|
+
return [2 /*return*/];
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}); });
|
|
126
|
+
test("should throw error for invalid apiEndpoint", function () {
|
|
76
127
|
expect(function () {
|
|
77
128
|
(0, index_1.initialize)({
|
|
78
|
-
key: "
|
|
79
|
-
|
|
129
|
+
key: "chp_test_invalid_endpoint",
|
|
130
|
+
apiEndpoint: "not-a-url",
|
|
80
131
|
});
|
|
81
|
-
}).toThrow(
|
|
132
|
+
}).toThrow("apiEndpoint must be a valid absolute URL");
|
|
133
|
+
});
|
|
134
|
+
test("should throw error for invalid key prefix", function () {
|
|
82
135
|
expect(function () {
|
|
83
136
|
(0, index_1.initialize)({
|
|
84
|
-
key: "
|
|
85
|
-
logLevel:
|
|
137
|
+
key: "invalid_key",
|
|
138
|
+
logLevel: 0 /* LogLevel.None */,
|
|
86
139
|
});
|
|
87
|
-
}).toThrow("Invalid API key:
|
|
140
|
+
}).toThrow("Invalid API key: must start with 'chp_'");
|
|
141
|
+
});
|
|
142
|
+
test("should load key from process environment", function () {
|
|
143
|
+
var previousKey = process.env.CHIRPIER_API_KEY;
|
|
144
|
+
process.env.CHIRPIER_API_KEY = "chp_env_key";
|
|
145
|
+
try {
|
|
146
|
+
(0, index_1.initialize)({ logLevel: 0 /* LogLevel.None */ });
|
|
147
|
+
expect(function () { return (0, index_1.initialize)({ logLevel: 0 /* LogLevel.None */ }); }).not.toThrow();
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
if (previousKey === undefined) {
|
|
151
|
+
delete process.env.CHIRPIER_API_KEY;
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
process.env.CHIRPIER_API_KEY = previousKey;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
test("should load key from .env fallback", function () {
|
|
159
|
+
var previousKey = process.env.CHIRPIER_API_KEY;
|
|
160
|
+
var previousCwd = process.cwd();
|
|
161
|
+
var tempDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "chirpier-js-"));
|
|
162
|
+
try {
|
|
163
|
+
delete process.env.CHIRPIER_API_KEY;
|
|
164
|
+
fs_1.default.writeFileSync(path_1.default.join(tempDir, ".env"), "CHIRPIER_API_KEY=chp_dotenv_key\n");
|
|
165
|
+
process.chdir(tempDir);
|
|
166
|
+
(0, index_1.initialize)({ logLevel: 0 /* LogLevel.None */ });
|
|
167
|
+
expect(function () { return (0, index_1.initialize)({ logLevel: 0 /* LogLevel.None */ }); }).not.toThrow();
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
process.chdir(previousCwd);
|
|
171
|
+
if (previousKey === undefined) {
|
|
172
|
+
delete process.env.CHIRPIER_API_KEY;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
process.env.CHIRPIER_API_KEY = previousKey;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
88
178
|
});
|
|
89
179
|
});
|
|
90
|
-
describe("
|
|
91
|
-
test("
|
|
92
|
-
var mock,
|
|
180
|
+
describe("logEvent", function () {
|
|
181
|
+
test("log should be sent", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
182
|
+
var mock, log;
|
|
93
183
|
return __generator(this, function (_a) {
|
|
94
184
|
switch (_a.label) {
|
|
95
185
|
case 0:
|
|
96
186
|
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
97
187
|
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
98
188
|
(0, index_1.initialize)({
|
|
99
|
-
key: "
|
|
100
|
-
logLevel:
|
|
189
|
+
key: "chp_log_send_key",
|
|
190
|
+
logLevel: 0 /* LogLevel.None */,
|
|
101
191
|
});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
192
|
+
log = {
|
|
193
|
+
agent: "api.worker",
|
|
194
|
+
event: "request.finished",
|
|
105
195
|
value: 1,
|
|
106
196
|
};
|
|
107
|
-
(0, index_1.
|
|
108
|
-
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
197
|
+
return [4 /*yield*/, (0, index_1.logEvent)(log)];
|
|
109
198
|
case 1:
|
|
110
|
-
_a.sent();
|
|
199
|
+
_a.sent();
|
|
200
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
201
|
+
case 2:
|
|
202
|
+
_a.sent();
|
|
111
203
|
expect(mock.history.post.length).toBe(1);
|
|
112
204
|
expect(mock.history.post[0].url).toBe(constants_1.DEFAULT_API_ENDPOINT);
|
|
113
205
|
expect(JSON.parse(mock.history.post[0].data)).toEqual([
|
|
114
206
|
{
|
|
115
|
-
|
|
116
|
-
|
|
207
|
+
log_id: expect.any(String),
|
|
208
|
+
agent: "api.worker",
|
|
209
|
+
event: "request.finished",
|
|
117
210
|
value: 1,
|
|
118
211
|
},
|
|
119
212
|
]);
|
|
120
|
-
|
|
121
|
-
mock.reset();
|
|
122
|
-
index_1.Chirpier.stop();
|
|
213
|
+
expect(JSON.parse(mock.history.post[0].data)[0].log_id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
|
|
123
214
|
return [2 /*return*/];
|
|
124
215
|
}
|
|
125
216
|
});
|
|
126
217
|
}); });
|
|
127
|
-
test("should
|
|
128
|
-
var mock,
|
|
218
|
+
test("should preserve provided log_id", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
219
|
+
var mock, payload;
|
|
129
220
|
return __generator(this, function (_a) {
|
|
130
221
|
switch (_a.label) {
|
|
131
222
|
case 0:
|
|
132
223
|
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
133
224
|
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
134
225
|
(0, index_1.initialize)({
|
|
135
|
-
key: "
|
|
136
|
-
logLevel:
|
|
226
|
+
key: "chp_log_id_key",
|
|
227
|
+
logLevel: 0 /* LogLevel.None */,
|
|
137
228
|
});
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
consoleSpy = jest.spyOn(console, "debug");
|
|
144
|
-
return [4 /*yield*/, (0, index_1.monitor)(invalidEvent)];
|
|
229
|
+
return [4 /*yield*/, (0, index_1.logEvent)({
|
|
230
|
+
log_id: "9f97d65f-fb30-4062-b4d0-8617c03fe4f6",
|
|
231
|
+
event: "request.finished",
|
|
232
|
+
value: 1,
|
|
233
|
+
})];
|
|
145
234
|
case 1:
|
|
146
235
|
_a.sent();
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
mock.
|
|
151
|
-
|
|
152
|
-
index_1.Chirpier.stop();
|
|
236
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
237
|
+
case 2:
|
|
238
|
+
_a.sent();
|
|
239
|
+
payload = JSON.parse(mock.history.post[0].data);
|
|
240
|
+
expect(payload[0].log_id).toBe("9f97d65f-fb30-4062-b4d0-8617c03fe4f6");
|
|
153
241
|
return [2 /*return*/];
|
|
154
242
|
}
|
|
155
243
|
});
|
|
156
244
|
}); });
|
|
157
|
-
test("
|
|
158
|
-
var mock,
|
|
245
|
+
test("agent whitespace should be omitted", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
246
|
+
var mock, payload;
|
|
159
247
|
return __generator(this, function (_a) {
|
|
160
248
|
switch (_a.label) {
|
|
161
249
|
case 0:
|
|
162
250
|
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
163
251
|
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
164
252
|
(0, index_1.initialize)({
|
|
165
|
-
key: "
|
|
166
|
-
logLevel:
|
|
253
|
+
key: "chp_log_whitespace_agent",
|
|
254
|
+
logLevel: 0 /* LogLevel.None */,
|
|
167
255
|
});
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
256
|
+
return [4 /*yield*/, (0, index_1.logEvent)({
|
|
257
|
+
agent: " ",
|
|
258
|
+
event: "metric.tick",
|
|
259
|
+
value: 42,
|
|
260
|
+
})];
|
|
261
|
+
case 1:
|
|
262
|
+
_a.sent();
|
|
263
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
264
|
+
case 2:
|
|
265
|
+
_a.sent();
|
|
266
|
+
payload = JSON.parse(mock.history.post[0].data);
|
|
267
|
+
expect(payload[0].agent).toBeUndefined();
|
|
268
|
+
return [2 /*return*/];
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
}); });
|
|
272
|
+
test("should support meta payload", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
273
|
+
var mock, payload;
|
|
274
|
+
return __generator(this, function (_a) {
|
|
275
|
+
switch (_a.label) {
|
|
276
|
+
case 0:
|
|
277
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
278
|
+
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
279
|
+
(0, index_1.initialize)({
|
|
280
|
+
key: "chp_log_meta_key",
|
|
281
|
+
logLevel: 0 /* LogLevel.None */,
|
|
282
|
+
});
|
|
283
|
+
return [4 /*yield*/, (0, index_1.logEvent)({
|
|
284
|
+
agent: "api.worker",
|
|
285
|
+
event: "request.finished",
|
|
286
|
+
value: 200,
|
|
287
|
+
meta: {
|
|
288
|
+
path: "/v1.0/logs",
|
|
289
|
+
status: "ok",
|
|
290
|
+
},
|
|
291
|
+
})];
|
|
292
|
+
case 1:
|
|
293
|
+
_a.sent();
|
|
175
294
|
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
295
|
+
case 2:
|
|
296
|
+
_a.sent();
|
|
297
|
+
payload = JSON.parse(mock.history.post[0].data);
|
|
298
|
+
expect(payload[0].meta.path).toBe("/v1.0/logs");
|
|
299
|
+
return [2 /*return*/];
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}); });
|
|
303
|
+
test("should support occurred_at timestamp", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
304
|
+
var mock, occurredAt, payload;
|
|
305
|
+
return __generator(this, function (_a) {
|
|
306
|
+
switch (_a.label) {
|
|
307
|
+
case 0:
|
|
308
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
309
|
+
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
310
|
+
(0, index_1.initialize)({
|
|
311
|
+
key: "chp_log_occurred_at_key",
|
|
312
|
+
logLevel: 0 /* LogLevel.None */,
|
|
313
|
+
});
|
|
314
|
+
occurredAt = new Date(Date.now() - 2 * 60 * 60 * 1000);
|
|
315
|
+
return [4 /*yield*/, (0, index_1.logEvent)({
|
|
316
|
+
event: "request.finished",
|
|
317
|
+
value: 1,
|
|
318
|
+
occurred_at: occurredAt,
|
|
319
|
+
})];
|
|
176
320
|
case 1:
|
|
177
|
-
_a.sent();
|
|
321
|
+
_a.sent();
|
|
322
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
323
|
+
case 2:
|
|
324
|
+
_a.sent();
|
|
325
|
+
payload = JSON.parse(mock.history.post[0].data);
|
|
326
|
+
expect(payload[0].occurred_at).toBe(occurredAt.toISOString());
|
|
327
|
+
return [2 /*return*/];
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}); });
|
|
331
|
+
test("should throw error for invalid log", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
332
|
+
var mock;
|
|
333
|
+
return __generator(this, function (_a) {
|
|
334
|
+
switch (_a.label) {
|
|
335
|
+
case 0:
|
|
336
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
337
|
+
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
338
|
+
(0, index_1.initialize)({
|
|
339
|
+
key: "chp_invalid_log_key",
|
|
340
|
+
logLevel: 3 /* LogLevel.Debug */,
|
|
341
|
+
});
|
|
342
|
+
return [4 /*yield*/, expect((0, index_1.logEvent)({
|
|
343
|
+
event: "",
|
|
344
|
+
value: 0,
|
|
345
|
+
})).rejects.toThrow(index_1.ChirpierError)];
|
|
346
|
+
case 1:
|
|
347
|
+
_a.sent();
|
|
348
|
+
return [4 /*yield*/, expect((0, index_1.logEvent)({
|
|
349
|
+
event: "too-old",
|
|
350
|
+
value: 1,
|
|
351
|
+
occurred_at: new Date(Date.now() - 31 * 24 * 60 * 60 * 1000),
|
|
352
|
+
})).rejects.toThrow(index_1.ChirpierError)];
|
|
353
|
+
case 2:
|
|
354
|
+
_a.sent();
|
|
355
|
+
return [4 /*yield*/, expect((0, index_1.logEvent)({
|
|
356
|
+
event: "too-future",
|
|
357
|
+
value: 1,
|
|
358
|
+
occurred_at: new Date(Date.now() + 25 * 60 * 60 * 1000),
|
|
359
|
+
})).rejects.toThrow(index_1.ChirpierError)];
|
|
360
|
+
case 3:
|
|
361
|
+
_a.sent();
|
|
362
|
+
return [4 /*yield*/, expect((0, index_1.logEvent)({
|
|
363
|
+
log_id: "not-a-uuid",
|
|
364
|
+
event: "bad-log-id",
|
|
365
|
+
value: 1,
|
|
366
|
+
})).rejects.toThrow(index_1.ChirpierError)];
|
|
367
|
+
case 4:
|
|
368
|
+
_a.sent();
|
|
369
|
+
expect(mock.history.post.length).toBe(0);
|
|
370
|
+
return [2 /*return*/];
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}); });
|
|
374
|
+
test("should batch logs and flush when batch size is reached", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
375
|
+
var mock;
|
|
376
|
+
return __generator(this, function (_a) {
|
|
377
|
+
switch (_a.label) {
|
|
378
|
+
case 0:
|
|
379
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
380
|
+
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
381
|
+
(0, index_1.initialize)({
|
|
382
|
+
key: "chp_batch_key",
|
|
383
|
+
logLevel: 0 /* LogLevel.None */,
|
|
384
|
+
});
|
|
385
|
+
return [4 /*yield*/, (0, index_1.logEvent)({ event: "batch.event", value: 1 })];
|
|
386
|
+
case 1:
|
|
387
|
+
_a.sent();
|
|
388
|
+
return [4 /*yield*/, (0, index_1.logEvent)({ event: "batch.event", value: 2 })];
|
|
389
|
+
case 2:
|
|
390
|
+
_a.sent();
|
|
391
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
392
|
+
case 3:
|
|
393
|
+
_a.sent();
|
|
178
394
|
expect(mock.history.post.length).toBe(1);
|
|
179
395
|
expect(JSON.parse(mock.history.post[0].data).length).toBe(2);
|
|
180
|
-
mock.reset();
|
|
181
|
-
index_1.Chirpier.stop();
|
|
182
396
|
return [2 /*return*/];
|
|
183
397
|
}
|
|
184
398
|
});
|
|
185
399
|
}); });
|
|
186
|
-
|
|
187
|
-
|
|
400
|
+
});
|
|
401
|
+
describe("Client API", function () {
|
|
402
|
+
test("createClient supports direct logging", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
403
|
+
var mock, client;
|
|
404
|
+
return __generator(this, function (_a) {
|
|
405
|
+
switch (_a.label) {
|
|
406
|
+
case 0:
|
|
407
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
408
|
+
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
409
|
+
client = (0, index_1.createClient)({ key: "chp_direct_client_key" });
|
|
410
|
+
return [4 /*yield*/, client.log({
|
|
411
|
+
event: "direct.client.log",
|
|
412
|
+
value: 1,
|
|
413
|
+
})];
|
|
414
|
+
case 1:
|
|
415
|
+
_a.sent();
|
|
416
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
417
|
+
case 2:
|
|
418
|
+
_a.sent();
|
|
419
|
+
expect(mock.history.post.length).toBe(1);
|
|
420
|
+
expect(JSON.parse(mock.history.post[0].data)[0].event).toBe("direct.client.log");
|
|
421
|
+
return [4 /*yield*/, client.shutdown()];
|
|
422
|
+
case 3:
|
|
423
|
+
_a.sent();
|
|
424
|
+
return [2 /*return*/];
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}); });
|
|
428
|
+
test("flush should force delivery of queued logs", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
429
|
+
var mock;
|
|
188
430
|
return __generator(this, function (_a) {
|
|
189
431
|
switch (_a.label) {
|
|
190
432
|
case 0:
|
|
191
433
|
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
192
434
|
mock.onPost(constants_1.DEFAULT_API_ENDPOINT).reply(200, { success: true });
|
|
193
435
|
(0, index_1.initialize)({
|
|
194
|
-
key: "
|
|
195
|
-
logLevel:
|
|
436
|
+
key: "chp_flush_key",
|
|
437
|
+
logLevel: 0 /* LogLevel.None */,
|
|
438
|
+
flushDelay: 10000,
|
|
196
439
|
});
|
|
197
|
-
event
|
|
198
|
-
group_id: "bfd9299d-817a-452f-bc53-6e154f2281fc",
|
|
199
|
-
stream_name: "test-stream",
|
|
200
|
-
value: 1,
|
|
201
|
-
};
|
|
202
|
-
(0, index_1.monitor)(event);
|
|
203
|
-
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })];
|
|
440
|
+
return [4 /*yield*/, (0, index_1.logEvent)({ event: "queued.before.flush", value: 1 })];
|
|
204
441
|
case 1:
|
|
205
|
-
_a.sent();
|
|
442
|
+
_a.sent();
|
|
443
|
+
expect(mock.history.post.length).toBe(0);
|
|
444
|
+
return [4 /*yield*/, (0, index_1.flush)()];
|
|
445
|
+
case 2:
|
|
446
|
+
_a.sent();
|
|
206
447
|
expect(mock.history.post.length).toBe(1);
|
|
207
|
-
expect(JSON.parse(mock.history.post[0].data).
|
|
208
|
-
mock.reset();
|
|
209
|
-
index_1.Chirpier.stop();
|
|
448
|
+
expect(JSON.parse(mock.history.post[0].data)[0].event).toBe("queued.before.flush");
|
|
210
449
|
return [2 /*return*/];
|
|
211
450
|
}
|
|
212
451
|
});
|
|
213
452
|
}); });
|
|
453
|
+
test("getEventLogs uses servicer endpoint with period, limit, and offset", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
454
|
+
var mock, client;
|
|
455
|
+
return __generator(this, function (_a) {
|
|
456
|
+
switch (_a.label) {
|
|
457
|
+
case 0:
|
|
458
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
459
|
+
mock.onGet("https://api.chirpier.co/v1.0/events/evt_123/logs?period=hour&limit=25&offset=10").reply(200, []);
|
|
460
|
+
client = (0, index_1.createClient)({ key: "chp_client_logs_key" });
|
|
461
|
+
_a.label = 1;
|
|
462
|
+
case 1:
|
|
463
|
+
_a.trys.push([1, , 3, 5]);
|
|
464
|
+
return [4 /*yield*/, client.getEventLogs("evt_123", { period: "hour", limit: 25, offset: 10 })];
|
|
465
|
+
case 2:
|
|
466
|
+
_a.sent();
|
|
467
|
+
expect(mock.history.get[0].url).toBe("https://api.chirpier.co/v1.0/events/evt_123/logs?period=hour&limit=25&offset=10");
|
|
468
|
+
return [3 /*break*/, 5];
|
|
469
|
+
case 3: return [4 /*yield*/, client.shutdown()];
|
|
470
|
+
case 4:
|
|
471
|
+
_a.sent();
|
|
472
|
+
return [7 /*endfinally*/];
|
|
473
|
+
case 5: return [2 /*return*/];
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
}); });
|
|
477
|
+
test("getAlertDeliveries uses pagination params", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
478
|
+
var mock, client;
|
|
479
|
+
return __generator(this, function (_a) {
|
|
480
|
+
switch (_a.label) {
|
|
481
|
+
case 0:
|
|
482
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
483
|
+
mock.onGet("https://api.chirpier.co/v1.0/alerts/alrt_123/deliveries?kind=test&limit=20&offset=5").reply(200, []);
|
|
484
|
+
client = (0, index_1.createClient)({ key: "chp_client_alert_key" });
|
|
485
|
+
_a.label = 1;
|
|
486
|
+
case 1:
|
|
487
|
+
_a.trys.push([1, , 3, 5]);
|
|
488
|
+
return [4 /*yield*/, client.getAlertDeliveries("alrt_123", { kind: "test", limit: 20, offset: 5 })];
|
|
489
|
+
case 2:
|
|
490
|
+
_a.sent();
|
|
491
|
+
expect(mock.history.get[0].url).toBe("https://api.chirpier.co/v1.0/alerts/alrt_123/deliveries?kind=test&limit=20&offset=5");
|
|
492
|
+
return [3 /*break*/, 5];
|
|
493
|
+
case 3: return [4 /*yield*/, client.shutdown()];
|
|
494
|
+
case 4:
|
|
495
|
+
_a.sent();
|
|
496
|
+
return [7 /*endfinally*/];
|
|
497
|
+
case 5: return [2 /*return*/];
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}); });
|
|
501
|
+
test("archiveAlert posts to servicer endpoint", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
502
|
+
var mock, client;
|
|
503
|
+
return __generator(this, function (_a) {
|
|
504
|
+
switch (_a.label) {
|
|
505
|
+
case 0:
|
|
506
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
507
|
+
mock.onPost("https://api.chirpier.co/v1.0/alerts/alrt_123/archive").reply(200, {});
|
|
508
|
+
client = (0, index_1.createClient)({ key: "chp_client_alert_key" });
|
|
509
|
+
_a.label = 1;
|
|
510
|
+
case 1:
|
|
511
|
+
_a.trys.push([1, , 3, 5]);
|
|
512
|
+
return [4 /*yield*/, client.archiveAlert("alrt_123")];
|
|
513
|
+
case 2:
|
|
514
|
+
_a.sent();
|
|
515
|
+
expect(mock.history.post[0].url).toBe("https://api.chirpier.co/v1.0/alerts/alrt_123/archive");
|
|
516
|
+
return [3 /*break*/, 5];
|
|
517
|
+
case 3: return [4 /*yield*/, client.shutdown()];
|
|
518
|
+
case 4:
|
|
519
|
+
_a.sent();
|
|
520
|
+
return [7 /*endfinally*/];
|
|
521
|
+
case 5: return [2 /*return*/];
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
}); });
|
|
525
|
+
test("testWebhook posts to servicer endpoint", function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
526
|
+
var mock, client;
|
|
527
|
+
return __generator(this, function (_a) {
|
|
528
|
+
switch (_a.label) {
|
|
529
|
+
case 0:
|
|
530
|
+
mock = new axios_mock_adapter_1.default(axios_1.default);
|
|
531
|
+
mock.onPost("https://api.chirpier.co/v1.0/webhooks/whk_123/test").reply(200);
|
|
532
|
+
client = (0, index_1.createClient)({ key: "chp_client_webhook_key" });
|
|
533
|
+
_a.label = 1;
|
|
534
|
+
case 1:
|
|
535
|
+
_a.trys.push([1, , 3, 5]);
|
|
536
|
+
return [4 /*yield*/, client.testWebhook("whk_123")];
|
|
537
|
+
case 2:
|
|
538
|
+
_a.sent();
|
|
539
|
+
expect(mock.history.post[0].url).toBe("https://api.chirpier.co/v1.0/webhooks/whk_123/test");
|
|
540
|
+
return [3 /*break*/, 5];
|
|
541
|
+
case 3: return [4 /*yield*/, client.shutdown()];
|
|
542
|
+
case 4:
|
|
543
|
+
_a.sent();
|
|
544
|
+
return [7 /*endfinally*/];
|
|
545
|
+
case 5: return [2 /*return*/];
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}); });
|
|
214
549
|
});
|
|
215
550
|
});
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export declare const DEFAULT_RETRIES
|
|
2
|
-
export declare const DEFAULT_TIMEOUT
|
|
3
|
-
export declare const DEFAULT_BATCH_SIZE
|
|
4
|
-
export declare const DEFAULT_FLUSH_DELAY
|
|
5
|
-
export declare const MAX_QUEUE_SIZE
|
|
6
|
-
export declare const DEFAULT_API_ENDPOINT
|
|
1
|
+
export declare const DEFAULT_RETRIES: 15;
|
|
2
|
+
export declare const DEFAULT_TIMEOUT: 5000;
|
|
3
|
+
export declare const DEFAULT_BATCH_SIZE: 500;
|
|
4
|
+
export declare const DEFAULT_FLUSH_DELAY: 500;
|
|
5
|
+
export declare const MAX_QUEUE_SIZE: 5000;
|
|
6
|
+
export declare const DEFAULT_API_ENDPOINT: "https://logs.chirpier.co/v1.0/logs";
|
|
7
|
+
export declare const DEFAULT_SERVICER_ENDPOINT: "https://api.chirpier.co/v1.0";
|
|
7
8
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,eAAe,EAAG,EAAW,CAAC;AAC3C,eAAO,MAAM,eAAe,EAAG,IAAa,CAAC;AAC7C,eAAO,MAAM,kBAAkB,EAAG,GAAY,CAAC;AAC/C,eAAO,MAAM,mBAAmB,EAAG,GAAY,CAAC;AAChD,eAAO,MAAM,cAAc,EAAG,IAAa,CAAC;AAC5C,eAAO,MAAM,oBAAoB,EAAG,oCAA6C,CAAC;AAClF,eAAO,MAAM,yBAAyB,EAAG,8BAAuC,CAAC"}
|