Repository navigation
Expand file tree
/
Copy pathlogging.js
More file actions
147 lines (132 loc) · 5.21 KB
/
Copy pathlogging.js
File metadata and controls
147 lines (132 loc) · 5.21 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
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
'use strict';
const { performance } = require('node:perf_hooks');
const IDENTIFIER_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
const HTTP_METHODS = new Set(['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH']);
const CONTEXT_FIELDS = [
'functionName', 'functionRequestId', 'functionInvocationId',
'x-ms-client-request-id', 'x-ms-correlation-id', 'msCorrelationIdSource',
'omittedIdFields', 'channel', 'evaluation', 'providerName', 'providerAuthMode',
'providerTenantId', 'functionOutboundClientId', 'functionOutboundManagedIdentityClientId',
];
function safeIdentifier(value) {
return typeof value === 'string' && IDENTIFIER_PATTERN.test(value) ? value : null;
}
function createRequestContext(context, requestId, msRequestId, headerCorrelationId) {
const clientId = safeIdentifier(msRequestId);
const correlationId = safeIdentifier(headerCorrelationId);
const omittedIdFields = [];
if (msRequestId != null && !clientId && String(msRequestId).trim()) {
omittedIdFields.push('x-ms-client-request-id');
}
if (headerCorrelationId != null && !correlationId && String(headerCorrelationId).trim()) {
omittedIdFields.push('x-ms-correlation-id');
}
return Object.freeze({
sink: context,
started: performance.now(),
functionName: 'SendOtp',
functionRequestId: requestId,
functionInvocationId: safeIdentifier(context.invocationId),
'x-ms-client-request-id': clientId,
'x-ms-correlation-id': correlationId,
msCorrelationIdSource: correlationId ? 'header' : 'none',
omittedIdFields: Object.freeze(omittedIdFields),
channel: null,
evaluation: null,
providerName: null,
providerAuthMode: null,
providerTenantId: null,
functionOutboundClientId: null,
functionOutboundManagedIdentityClientId: null,
});
}
function extendRequestContext(context, fields) {
return Object.freeze({ ...context, ...fields });
}
function payloadContext(context, payload) {
const hasPayloadCorrelationId = typeof payload.correlationId === 'string'
&& payload.correlationId.length > 0;
const payloadCorrelationId = hasPayloadCorrelationId
? safeIdentifier(payload.correlationId) : null;
const omittedIdFields = context.omittedIdFields
.filter((field) => field !== 'x-ms-correlation-id');
if (hasPayloadCorrelationId && !payloadCorrelationId) {
omittedIdFields.push('x-ms-correlation-id');
}
return extendRequestContext(context, {
'x-ms-correlation-id': hasPayloadCorrelationId
? payloadCorrelationId : context['x-ms-correlation-id'],
msCorrelationIdSource: hasPayloadCorrelationId
? 'envelope' : context['x-ms-correlation-id'] ? 'header' : 'none',
omittedIdFields: Object.freeze(omittedIdFields),
channel: payload.channelName,
evaluation: payload.isEvaluation,
});
}
function providerContext(context, provider) {
return extendRequestContext(context, {
providerName: provider.name,
providerAuthMode: provider.authenticationMode,
});
}
function credentialContext(context, config) {
const fields = {
providerTenantId: config.providerTenantId,
functionOutboundClientId: config.outboundClientId,
functionOutboundManagedIdentityClientId: config.outboundManagedIdentityClientId,
};
const omittedIdFields = [...context.omittedIdFields];
const safeFields = {};
for (const [name, value] of Object.entries(fields)) {
safeFields[name] = safeIdentifier(value);
if (value != null && !safeFields[name] && String(value).trim()) {
omittedIdFields.push(name);
}
}
return extendRequestContext(context, {
...safeFields,
omittedIdFields: Object.freeze([...new Set(omittedIdFields)]),
});
}
function emit(context, eventName, details = {}, level = 'log') {
const base = Object.fromEntries(CONTEXT_FIELDS.map((field) => [field, context[field] ?? null]));
context.sink[level](JSON.stringify({
logType: 'service',
eventName,
...base,
...details,
elapsedMs: Math.floor(performance.now() - context.started),
}));
}
function requestFailed(context, failureStage, failureReason, httpStatus) {
emit(context, 'request_failed', { failureStage, failureReason, httpStatus },
httpStatus >= 500 ? 'error' : 'warn');
}
function requestCompleted(context, httpStatus, result) {
emit(context, 'request_completed', { httpStatus, result });
}
function unexpectedError(context, httpStatus = 500) {
emit(context, 'unexpected_error', { httpStatus }, 'error');
}
function normalizeHttpMethod(method) {
const normalized = typeof method === 'string' ? method.toUpperCase() : '';
return HTTP_METHODS.has(normalized) ? normalized : 'other';
}
function sanitizeEndpoint(value) {
const url = new URL(value);
return `${url.protocol}//${url.host}${url.pathname}`;
}
module.exports = {
safeIdentifier,
createRequestContext,
extendRequestContext,
payloadContext,
providerContext,
credentialContext,
emit,
requestFailed,
requestCompleted,
unexpectedError,
normalizeHttpMethod,
sanitizeEndpoint,
};