-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.ts
336 lines (306 loc) · 8.54 KB
/
conf.ts
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { compileThrowing } from "./utils";
/**
* Parse a value from the environment by running it through a set of
* parsing functions.
*
* @param key The environment variable name.
* @param firstFn The first function that takes the raw string in the
* environment.
* @param fns The rest of the functions to run the value through.
* @returns The final validated value, or null if the variable wasn't
* found.
*/
const fromEnv = <T>(
key: string,
firstFn: (value: string) => T,
...fns: ((value: T) => T)[]
): null | T => {
const raw = process.env[key];
if (typeof raw === "undefined") {
return null;
}
let value: T;
try {
value = firstFn(raw);
} catch (err) {
throw new Error(
`The configured value for the ${key} variable is not valid: ${err}`
);
}
for (const fn of fns) {
try {
value = fn(value);
} catch (err) {
throw new Error(
`The configured value for the ${key} variable is not valid: ${err}`
);
}
}
return value;
};
/**
* The execution environment.
*/
export const NODE_ENV = (fromEnv(
"NODE_ENV",
(s) => s.toLowerCase(),
compileThrowing({ enum: ["production", "development", "test"] })
) ?? "production") as "production" | "development" | "test";
/**
* The minimum log level.
*/
export const LOG_LEVEL = (fromEnv(
"LOG_LEVEL",
(s) => s.toLowerCase(),
compileThrowing({ enum: ["debug", "info", "warn", "error"] })
) ?? (NODE_ENV === "test" ? "error" : "info")) as
| "debug"
| "info"
| "warn"
| "error";
/**
* The maximum size of a serialized data message, in bytes.
*/
export const PARSE_BUFFER_SIZE: number =
fromEnv(
"PARSE_BUFFER_SIZE",
JSON.parse,
compileThrowing({ type: "integer", minimum: 32 })
) ?? (NODE_ENV === "test" ? 32 : 1048576); // 32 bytes or 1 Mib
/**
* The standard PATH variable, split on ':'.
*/
export const PATH: string[] = (process.env.PATH ?? "")
.split(":")
.filter((p) => p.length > 0)
.concat(NODE_ENV === "test" ? ["./stream-jsonnet"] : []);
/**
* The time to wait after the input drains before closing the
* pipeline.
*/
export const INPUT_DRAIN_TIMEOUT: number =
fromEnv(
"INPUT_DRAIN_TIMEOUT",
JSON.parse,
compileThrowing({ type: "number", exclusiveMinimum: 0 })
) ?? 1; // 1 second
/**
* The time to wait between each self health check. Set to 0 to
* disable self health checks.
*/
export const HEALTH_CHECK_INTERVAL: number =
fromEnv(
"HEALTH_CHECK_INTERVAL",
JSON.parse,
compileThrowing({ type: "number", minimum: 0 })
) ?? 5; // 5 seconds
/**
* An URI that will receive an HTTP request with dead events: events
* that couldn't be fully processed.
*/
export const DEAD_LETTER_TARGET: string | null = fromEnv(
"DEAD_LETTER_TARGET",
compileThrowing({ type: "string", pattern: "^https?://\\S+$" })
);
/**
* An HTTP method to use for the request that forwards dead events.
*/
export const DEAD_LETTER_TARGET_METHOD = (fromEnv(
"DEAD_LETTER_TARGET_METHOD",
(s) => s.toUpperCase(),
compileThrowing({ enum: ["POST", "PUT", "PATCH"] })
) ?? "POST") as "POST" | "PUT" | "PATCH";
/**
* A mapping of HTTP headers to use when sending dead events to a
* remote service.
*/
export const DEAD_LETTER_TARGET_HEADERS = (fromEnv(
"DEAD_LETTER_TARGET_HEADERS",
JSON.parse,
compileThrowing({
type: "object",
properties: {},
additionalProperties: {
anyOf: [{ type: "string" }, { type: "number" }, { type: "boolean" }],
},
})
) ?? {}) as {
[key: string]: string | number | boolean;
};
/**
* The default port used for listening for HTTP requests.
*/
export const HTTP_SERVER_DEFAULT_PORT: number =
fromEnv(
"HTTP_SERVER_DEFAULT_PORT",
JSON.parse,
compileThrowing({ type: "integer", minimum: 1, maximum: 65535 })
) ?? 8000;
/**
* The address used to listen for HTTP requests. Default is to listen
* on all interfaces.
*/
export const HTTP_SERVER_LISTEN_ADDRESS: string =
fromEnv(
"HTTP_SERVER_LISTEN_ADDRESS",
compileThrowing({ type: "string", minLength: 1 })
) ?? "0.0.0.0";
/**
* The TCP backlog size for the HTTP server.
*/
export const HTTP_SERVER_LISTEN_BACKLOG: number =
fromEnv(
"HTTP_SERVER_LISTEN_BACKLOG",
JSON.parse,
compileThrowing({ type: "integer", minimum: 1 })
) ?? 511;
/**
* The endpoint which will expose the application's health status.
*/
export const HTTP_SERVER_HEALTH_ENDPOINT: string =
fromEnv(
"HTTP_SERVER_HEALTH_ENDPOINT",
compileThrowing({ type: "string", pattern: "^/\\S*$" })
) ?? "/healthz";
/**
* The time to wait between poll requests when using the `poll` input
* form without an explicit `poll.seconds` option set.
*/
export const POLL_INPUT_DEFAULT_INTERVAL: number =
fromEnv(
"POLL_INPUT_DEFAULT_INTERVAL",
JSON.parse,
compileThrowing({ type: "number", exclusiveMinimum: 0 })
) ?? 5; // 5 seconds
/**
* The port used to expose prometheus metric.
*/
export const METRICS_EXPOSITION_PORT: number =
fromEnv(
"METRICS_EXPOSITION_PORT",
JSON.parse,
compileThrowing({ type: "integer", minimum: 1, maximum: 65535 })
) ?? 8001;
/**
* The endpoint which will expose prometheus metrics. Set to the empty
* string to disable metrics exposition.
*/
export const METRICS_EXPOSITION_PATH: string =
fromEnv(
"METRICS_EXPOSITION_PATH",
compileThrowing({ type: "string", pattern: "^(/\\S*)?$" })
) ?? "/metrics";
/**
* The prefix used in prometheus metric names.
*/
export const METRICS_NAME_PREFIX: string =
fromEnv(
"METRICS_NAME_PREFIX",
compileThrowing({ type: "string", pattern: "[A-Za-z]\\w*" })
) ?? "cdp_";
/**
* The timeout used for emitted HTTP requests.
*/
export const HTTP_CLIENT_TIMEOUT: number =
fromEnv(
"HTTP_CLIENT_TIMEOUT",
JSON.parse,
compileThrowing({ type: "number", exclusiveMinimum: 0 })
) ?? 60; // 60 seconds
/**
* Whether to reject invalid TLS certificates. Defaults to `true`.
*/
export const HTTP_CLIENT_REJECT_UNAUTHORIZED = !["false", "no", "0"].includes(
(process.env.HTTP_CLIENT_REJECT_UNAUTHORIZED ?? "yes").toLowerCase()
);
/**
* The upper limit of redirects accepted when emitting HTTP requests.
*/
export const HTTP_CLIENT_MAX_REDIRECTS: number =
fromEnv(
"HTTP_CLIENT_MAX_REDIRECTS",
JSON.parse,
compileThrowing({ type: "integer", minimum: 0 })
) ?? 10;
/**
* The upper limit of response sizes received for HTTP requests.
*/
export const HTTP_CLIENT_MAX_CONTENT_LENGTH: number =
fromEnv(
"HTTP_CLIENT_MAX_CONTENT_LENGTH",
JSON.parse,
compileThrowing({ type: "integer", minimum: 32 })
) ?? 52428800; // 50 MiB
/**
* The maximum number of additional request attempts for each HTTP
* response received with status 5xx.
*/
export const HTTP_CLIENT_MAX_RETRIES: number =
fromEnv(
"HTTP_CLIENT_MAX_RETRIES",
JSON.parse,
compileThrowing({ type: "integer", minimum: 0 })
) ?? 4;
/**
* The constant factor used in HTTP requests exponential backoff, in
* seconds.
*/
export const HTTP_CLIENT_BACKOFF_FACTOR: number =
fromEnv(
"HTTP_CLIENT_BACKOFF_FACTOR",
JSON.parse,
compileThrowing({ type: "number", minimum: 0 })
) ?? (NODE_ENV === "test" ? 0 : 1); // 0 or 1 second
/**
* The maximum amount of concurrent HTTP requests to execute in a
* forwarding step function.
*/
export const HTTP_CLIENT_DEFAULT_CONCURRENCY: number =
fromEnv(
"HTTP_CLIENT_DEFAULT_CONCURRENCY",
JSON.parse,
compileThrowing({ type: "integer", minimum: 1 })
) ?? 10;
/**
* Time interval, in seconds, between measurements taken to apply
* backpressure to input forms.
*/
export const BACKPRESSURE_INTERVAL: number =
fromEnv(
"BACKPRESSURE_INTERVAL",
JSON.parse,
compileThrowing({ type: "number", exclusiveMinimum: 0 })
) ?? 5;
/**
* Backpressure limit against the rss metric, measured in bytes.
*/
export const BACKPRESSURE_RSS: number | null = fromEnv(
"BACKPRESSURE_RSS",
JSON.parse,
compileThrowing({ type: "integer", minimum: 1 })
);
/**
* Backpressure limit against the heapTotal metric, measured in bytes.
*/
export const BACKPRESSURE_HEAP_TOTAL: number | null = fromEnv(
"BACKPRESSURE_HEAP_TOTAL",
JSON.parse,
compileThrowing({ type: "integer", minimum: 1 })
);
/**
* Backpressure limit against the heapUsed metric, measured in bytes.
*/
export const BACKPRESSURE_HEAP_USED: number | null = fromEnv(
"BACKPRESSURE_HEAP_USED",
JSON.parse,
compileThrowing({ type: "integer", minimum: 1 })
);
/**
* Backpressure limit against the queued events metric, measured in
* count of events.
*/
export const BACKPRESSURE_QUEUED_EVENTS: number | null = fromEnv(
"BACKPRESSURE_QUEUED_EVENTS",
JSON.parse,
compileThrowing({ type: "integer", minimum: 1 })
);