-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
90 lines (84 loc) · 2.45 KB
/
context.ts
File metadata and controls
90 lines (84 loc) · 2.45 KB
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
/* eslint-disable no-console */
import { ClientInfo, createContext, LogEntry, LogTransport } from '@riddance/host/context'
import { FullConfiguration, Metadata } from '@riddance/host/registry'
import { randomUUID } from 'node:crypto'
import { SnsEventTransport } from './lib/sns.js'
export * from '@riddance/service/context'
export type AwsContext = {
getRemainingTimeInMillis(): number
readonly functionName: string
readonly functionVersion: string
readonly invokedFunctionArn: string
readonly memoryLimitInMB: number
readonly awsRequestId: string
readonly logGroupName: string
readonly logStreamName: string
callbackWaitsForEmptyEventLoop: boolean
}
class ConsoleLogger implements LogTransport {
sendEntries(entries: LogEntry[]) {
for (const entry of entries) {
switch (entry.level) {
case 'trace':
case 'debug':
console.debug(entry.json)
break
case 'info':
console.log(entry.json)
break
case 'warning':
console.warn(entry.json)
break
case 'error':
case 'fatal':
console.error(entry.json)
break
}
}
return undefined
}
}
const consoleLogger = new ConsoleLogger()
const hostInfo = {
instance: {
id: randomUUID().replaceAll('-', ''),
},
nodejs: {
version: process.version.substring(1),
},
function: {
version: process.env.AWS_LAMBDA_FUNCTION_VERSION,
executionEnvironment: process.env.AWS_EXECUTION_ENV,
},
}
export function createAwsContext(
context: AwsContext,
stageVariables: { [key: string]: string },
client: ClientInfo,
config?: FullConfiguration,
meta?: Metadata,
) {
const ctx = createContext(
client,
[consoleLogger],
new SnsEventTransport(),
{ default: 15 },
new AbortController(),
config,
meta,
{
...process.env,
...stageVariables,
} as { [key: string]: string },
)
ctx.log.enrichReserved({
host: hostInfo,
function: {
name: context.functionName,
version: context.functionVersion,
memoryLimit: context.memoryLimitInMB,
timeout: context.getRemainingTimeInMillis(),
},
})
return ctx
}