/utils/telemetryConsole
The telemetryConsole module exports the ConsoleTraceProcessor which logs one object per trace to the console.
The telemetry module registers the processor with the OTEL_TRACES_EXPORTER=console process environment variable.
A trace is logged once the root span of the trace ends. For a request the root span is the request span. The logged object has a summary of the count and total duration for each span name, and the span tree with nested children.
{
name: 'GET /api/workspace{/:key}',
start: 0,
duration: 41.2,
attributes: { 'faas.coldstart': true, 'xyz.workspace.key': 'locale', ... },
summary: {
'workspace.getLocale': { count: 1, duration: 38.6 },
'workspace.composeObj': { count: 12, duration: 30.1 },
},
children: [ { name: 'workspace.getLocale', start: 1.8, duration: 38.6, children: [...] } ]
}
Start times and durations are in milliseconds. The start of each span is relative to the start of the root span. The summary duration of nested spans with the same name will be counted more than once, eg. a nested getLocale.
Traces whose root span is shorter than the minDuration option are not logged. The telemetry module assigns the minDuration from the OTEL_CONSOLE_MIN_DURATION process environment variable, eg. 500 to only log requests which take 500ms or longer.
The trace is logged as indented JSON if the process output is an interactive terminal. Otherwise, eg. in Vercel runtime logs, every trace is logged as a single line of JSON.
Spans which end after their root span, eg. a nonblocking query, are logged on their own if their trace was logged.
The resource attributes with the process metadata are only logged with the first trace of the process.
With the html option every logged trace is also written to an HTML report file with the most recent traces. The telemetry module assigns the html option from the OTEL_CONSOLE_HTML process environment variable in a local process. Keep the report open in a browser and reload the page after a request.
Traces are only written to the HTML report and not logged to the console with the log option set to false. The telemetry module assigns the log option false with the OTEL_TRACES_EXPORTER=html process environment variable. Spans which end after their root span are not written to the report and therefore not shown at all.
- Source
Requires
- module:node:fs
- module:node:inspector
- module:node:path
- module:/utils/traceReport
Classes
Methods
(inner) log(node)
Logs the node with console.log, which is patched by varlock to redact sensitive values.
The node object is logged as is while a debugger is attached so the object can be expanded in the debug console. Otherwise the node is logged as a JSON string, since nested objects would be truncated as [Object] in the terminal. JSON is used instead of util.inspect which prefixes every object without prototype with [Object: null prototype].
The JSON is indented for an interactive terminal. Log output which is not a terminal, eg. Vercel runtime logs or a log drain, gets one line of JSON per trace.
| Name | Type | Description |
|---|---|---|
node | Object | The span node to log. |
(inner) nullObject(properties) → {Object}
Assigns the properties to an object created without prototype. The logged objects are plain data and expanding a logged object in the debug console does not show the Object prototype.
| Name | Type | Description |
|---|---|---|
properties | Object |
Object with null prototype.
- Type:
- Object
(inner) onEnd(span)
Collects the ended span. The trace is logged if the span is the root span of the trace and the root span duration is not shorter than the minDuration.
| Name | Type | Description |
|---|---|---|
span | ReadableSpan | The ended span. |
(inner) onStart(span)
Records the precise start time of the span.
| Name | Type | Description |
|---|---|---|
span | Span | The started span. |
- Source
(inner) spanNode(span, originopt) → {Object}
Creates a compact node for the span. Empty properties are omitted.
The start is the time in milliseconds from the origin, eg. the start of the root span. The start of a span logged on its own is 0.
| Name | Type | Attributes | Description |
|---|---|---|---|
span | ReadableSpan | ||
origin | number | <optional> | The origin time in milliseconds. Defaults to the span start time. |
Span node.
- Type:
- Object
(inner) startTime(span) → {number}
Returns the precise start time recorded by the onStart method. The span startTime is used for spans which have not been started with the processor.
| Name | Type | Description |
|---|---|---|
span | ReadableSpan |
Start time in milliseconds.
- Type:
- number
(inner) traceTree(root, spans) → {Object}
Creates the span tree for the root span. Children are sorted by their start time.
The summary counts the spans and sums their duration by span name.
| Name | Type | Description |
|---|---|---|
root | ReadableSpan | The root span. |
spans | Array.<ReadableSpan> | All ended spans of the trace including the root span. |
The root span node with summary and nested children.
- Type:
- Object
(inner) writeReport(tree)
Adds the trace to the most recent traces and writes the HTML report file from the traceReport module. The report is written synchronously and is meant for a local process only.
| Name | Type | Description |
|---|---|---|
tree | Object | The logged trace. |
(inner) writeReportFile(retryopt)
Writes the HTML report file with the most recent traces. A failed write is retried once after the RETRY_DELAY. The file may be locked for a moment on Windows while another process, eg. a browser or live server, reads the file. A pending retry is cancelled by the next write.
A write which also fails on retry is warned about. Each distinct error is only warned about once.
| Name | Type | Attributes | Description |
|---|---|---|---|
retry | boolean | <optional> | The write is a retry of a failed write. |