Skip to content

Commit

Permalink
add trace prefix and support for traceparent opentelemetry header (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shegox committed Sep 2, 2021
1 parent cde1a13 commit b1abecf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ The following are the environment variables you have to configure to run a priva
- `SLACK_TOKEN`: Optional.
- `REQUEST_TRACE_HEADER_NAME`: Use the value of an HTTP-header to set the name. E.g. the request id set by an ingress controller via `X-Req-Id`. If not set or no HTTP-header is present a random uuid is used.
- `LOG_TRACE_FIELD_NAME`: The log field to log the request id to. Defaults to `req_id`.
- `LOG_TRACE_PREFIX`: A prefix put before the traceId.

> **Hint:** For further reading on setting up MongoDB, check the "[Getting Started](http://docs.mongodb.org/manual/tutorial/getting-started/)" and [`db.createUser()` method](http://docs.mongodb.org/manual/reference/method/db.createUser).
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ module.exports = {
observability: {
request_trace_header_name: process.env.REQUEST_TRACE_HEADER_NAME,
log_trace_field_name: process.env.LOG_TRACE_FIELD_NAME || 'req_id',
trace_prefix: process.env.LOG_TRACE_PREFIX || '',
},

static: [
Expand Down
19 changes: 15 additions & 4 deletions src/server/services/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@ const formatter = (record, levelName) => {

// we use a custom Stdout writer, which injects for each call the request id
const wrappedStdout = {
write: entry => {
const logObject = JSON.parse(entry)
logObject[config.server.observability.log_trace_field_name] = rTracer.id();
process.stdout.write(JSON.stringify(logObject) + '\n');
write: entry => {
const logObject = JSON.parse(entry)
let traceId = rTracer.id()
if (traceId) {
if (config.server.observability.request_trace_header_name == 'traceparent') {
const traceParts = traceId.split('-');
// check if it conforms to version 00 of opentelemetry spec (https://www.w3.org/TR/trace-context)
// version "-" trace-id "-" parent-id "-" trace-flags
if (traceParts.length == 4 && traceParts[0] == '00') {
traceId = traceParts[1]
}
}
logObject[config.server.observability.log_trace_field_name] = `${config.server.observability.trace_prefix}${traceId}`;
}
process.stdout.write(JSON.stringify(logObject) + '\n');
}
}

const log = bunyan.createLogger({
Expand Down

0 comments on commit b1abecf

Please sign in to comment.