-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathlogging.ts
68 lines (62 loc) · 1.87 KB
/
logging.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
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
import { Logging } from "@google-cloud/logging";
import { Request } from "express";
import type { GraphQLParams } from "express-graphql";
import PrettyError from "pretty-error";
import env from "../env";
// https://googleapis.dev/nodejs/logging/latest/
const logging = new Logging();
const log = logging.log("cloudfunctions.googleapis.com/cloud-functions");
// https://github.com/AriaMinaei/pretty-error#readme
const pe = new PrettyError();
/**
* Logs application errors to Google StackDriver when in production,
* otherwise just print them to the console using PrettyError formatter.
*/
export function reportError(
err: Error,
req: Request,
context?: GraphQLParams | Record<string, unknown>
): void {
if (!env.isProduction) {
console.error(pe.render(err));
return;
}
// E.g. us-central-example.cloudfunctions.net
const host = req.get("host") as string;
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
log.error(
log.entry(
{
resource: {
type: "cloud_function",
labels: {
region: host.substring(0, host.indexOf("-", host.indexOf("-") + 1)),
function_name: process.env.FUNCTION_TARGET as string,
},
},
httpRequest: {
requestMethod: req.method,
requestUrl: req.originalUrl,
userAgent: req.get("user-agent"),
remoteIp: req.ip,
referer: req.headers.referer,
latency: null,
},
labels: {
execution_id: req.get("function-execution-id") as string,
},
},
{
message: err.stack,
context: {
...context,
user: req.user
? { id: req.user.id, username: req.user.username }
: null,
},
}
)
);
}