Skip to content

Commit 3b3f9a5

Browse files
Ace NassriJustinBeckwith
andauthored
chore(functions/logging): exclude trace code from sample (#2326)
* chore(functions/logging): exclude HTTP-specific trace code from sample * Fix region tag * Apply Adam's suggested fix Co-authored-by: Justin Beckwith <justin.beckwith@gmail.com>
1 parent 5b9f898 commit 3b3f9a5

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

run/logging-manual/app.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ app.get('/', (req, res) => {
1212
// [START run_manual_logging]
1313

1414
// Uncomment and populate this variable in your code:
15-
// $project = 'The project ID of your Cloud Run service';
15+
// $project = 'The project ID of your function or Cloud Run service';
1616

1717
// Build structured log messages as an object.
1818
const globalLogFields = {};
1919

2020
// Add log correlation to nest all log messages beneath request log in Log Viewer.
21-
const traceHeader = req.header('X-Cloud-Trace-Context');
22-
if (traceHeader && project) {
23-
const [trace] = traceHeader.split('/');
24-
globalLogFields[
25-
'logging.googleapis.com/trace'
26-
] = `projects/${project}/traces/${trace}`;
21+
// (This only works for HTTP-based invocations where `req` is defined.)
22+
if (typeof req !== 'undefined') {
23+
const traceHeader = req.header('X-Cloud-Trace-Context');
24+
if (traceHeader && project) {
25+
const [trace] = traceHeader.split('/');
26+
globalLogFields[
27+
'logging.googleapis.com/trace'
28+
] = `projects/${project}/traces/${trace}`;
29+
}
2730
}
2831

2932
// Complete a structured log entry.

0 commit comments

Comments
 (0)