|
14 | 14 |
|
15 | 15 | import * as express from 'express';
|
16 | 16 | import {FUNCTION_STATUS_HEADER_FIELD} from './types';
|
| 17 | +import {getCurrentContext, ExecutionContext} from './async_local_storage'; |
| 18 | +import {Buffer} from 'buffer'; |
| 19 | + |
| 20 | +export const EXECUTION_CONTEXT_LABELS_KEY = 'logging.googleapis.com/labels'; |
| 21 | +export const EXECUTION_CONTEXT_TRACE_KEY = 'logging.googleapis.com/trace'; |
| 22 | +export const EXECUTION_CONTEXT_SPAN_ID_KEY = 'logging.googleapis.com/spanId'; |
| 23 | +const SEVERITY = 'severity'; |
17 | 24 |
|
18 | 25 | /**
|
19 | 26 | * Logs an error message and sends back an error response to the incoming
|
@@ -60,3 +67,120 @@ export function sendCrashResponse({
|
60 | 67 | callback();
|
61 | 68 | }
|
62 | 69 | }
|
| 70 | + |
| 71 | +export function loggingHandlerAddExecutionContext() { |
| 72 | + interceptStdoutWrite(); |
| 73 | + interceptStderrWrite(); |
| 74 | +} |
| 75 | + |
| 76 | +function interceptStdoutWrite() { |
| 77 | + const originalStdoutWrite = process.stdout.write; |
| 78 | + process.stdout.write = (data, ...args) => { |
| 79 | + const {encoding, cb} = splitArgs(args); |
| 80 | + const modifiedData = getModifiedData(data, encoding); |
| 81 | + return originalStdoutWrite.apply(process.stdout, [modifiedData, cb]); |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function interceptStderrWrite() { |
| 86 | + const originalStderrWrite = process.stderr.write; |
| 87 | + process.stderr.write = (data, ...args) => { |
| 88 | + const {encoding, cb} = splitArgs(args); |
| 89 | + const modifiedData = getModifiedData(data, encoding, true); |
| 90 | + return originalStderrWrite.apply(process.stderr, [modifiedData, cb]); |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +export const errorHandler = ( |
| 95 | + err: Error | any, |
| 96 | + req: express.Request, |
| 97 | + res: express.Response, |
| 98 | + next: express.NextFunction |
| 99 | +) => { |
| 100 | + interceptStderrWrite(); |
| 101 | + res.status(500); |
| 102 | + res.render('error', {error: err}); |
| 103 | +}; |
| 104 | + |
| 105 | +export function splitArgs(args: any[]) { |
| 106 | + let encoding, cb; |
| 107 | + if ( |
| 108 | + args.length > 0 && |
| 109 | + (Buffer.isEncoding(args[0]) || typeof args[0] === 'undefined') |
| 110 | + ) { |
| 111 | + encoding = args[0]; |
| 112 | + args.shift(); |
| 113 | + } |
| 114 | + if (args.length > 0 && typeof args[0] === 'function') { |
| 115 | + cb = args[0]; |
| 116 | + } |
| 117 | + return {encoding: encoding, cb: cb}; |
| 118 | +} |
| 119 | + |
| 120 | +export function getModifiedData( |
| 121 | + data: Uint8Array | string, |
| 122 | + encoding?: BufferEncoding, |
| 123 | + stderr = false |
| 124 | +) { |
| 125 | + const currentContext = getCurrentContext(); |
| 126 | + if (!currentContext) { |
| 127 | + return data; |
| 128 | + } |
| 129 | + const {isJSON, processedData} = processData(data, encoding); |
| 130 | + let dataWithContext; |
| 131 | + if (isJSON) { |
| 132 | + dataWithContext = getJSONWithContext(processedData, currentContext); |
| 133 | + } else { |
| 134 | + dataWithContext = getTextWithContext(processedData, currentContext); |
| 135 | + } |
| 136 | + if (stderr) { |
| 137 | + dataWithContext[SEVERITY] = 'ERROR'; |
| 138 | + } |
| 139 | + |
| 140 | + return JSON.stringify(dataWithContext) + '\n'; |
| 141 | +} |
| 142 | + |
| 143 | +function getTextWithContext( |
| 144 | + data: Uint8Array | string, |
| 145 | + context: ExecutionContext |
| 146 | +) { |
| 147 | + return { |
| 148 | + message: data, |
| 149 | + [EXECUTION_CONTEXT_LABELS_KEY]: {execution_id: context.executionId}, |
| 150 | + [EXECUTION_CONTEXT_TRACE_KEY]: context.traceId, |
| 151 | + [EXECUTION_CONTEXT_SPAN_ID_KEY]: context.spanId, |
| 152 | + }; |
| 153 | +} |
| 154 | + |
| 155 | +function getJSONWithContext(json: any, context: ExecutionContext) { |
| 156 | + if (EXECUTION_CONTEXT_LABELS_KEY in json) { |
| 157 | + json[EXECUTION_CONTEXT_LABELS_KEY]['execution_id'] = context.executionId; |
| 158 | + } else { |
| 159 | + json[EXECUTION_CONTEXT_LABELS_KEY] = {execution_id: context.executionId}; |
| 160 | + } |
| 161 | + return { |
| 162 | + ...json, |
| 163 | + [EXECUTION_CONTEXT_TRACE_KEY]: context.traceId, |
| 164 | + [EXECUTION_CONTEXT_SPAN_ID_KEY]: context.spanId, |
| 165 | + }; |
| 166 | +} |
| 167 | + |
| 168 | +function processData(data: Uint8Array | string, encoding?: BufferEncoding) { |
| 169 | + let decodedData; |
| 170 | + try { |
| 171 | + if (data instanceof Uint8Array) { |
| 172 | + decodedData = Buffer.from(data.buffer).toString(); |
| 173 | + } else { |
| 174 | + decodedData = Buffer.from(data, encoding).toString(); |
| 175 | + } |
| 176 | + } catch (e) { |
| 177 | + // Failed to decode, treat it as simple text. |
| 178 | + return {isJSON: false, processedData: data}; |
| 179 | + } |
| 180 | + |
| 181 | + try { |
| 182 | + return {isJSON: true, processedData: JSON.parse(decodedData)}; |
| 183 | + } catch (e) { |
| 184 | + return {isJSON: false, processedData: decodedData}; |
| 185 | + } |
| 186 | +} |
0 commit comments