@itwin/presentation-opentelemetry
Version:
Utilities to enable OpenTelemetry tracing of Presentation requests
58 lines • 2.56 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Core
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportDiagnostics = exportDiagnostics;
const presentation_common_1 = require("@itwin/presentation-common");
const api_1 = require("@opentelemetry/api");
/**
* Export Presentation diagnostics as OpenTelemetry traces to the given context.
* @see [Diagnostics and OpenTelemetry]($docs/presentation/advanced/Diagnostics.md#diagnostics-and-opentelemetry)
* @beta
*/
function exportDiagnostics(diagnostics, ctx) {
for (const logs of diagnostics.logs ?? []) {
exportDiagnosticsLogs(logs, ctx);
}
}
function exportDiagnosticsLogs(logs, ctx) {
if (!logs.scopeCreateTimestamp || !logs.duration) {
return;
}
const span = api_1.trace.getTracer("iTwin.js Presentation").startActiveSpan(logs.scope, {
kind: api_1.SpanKind.INTERNAL,
attributes: { ...(logs.attributes ? logs.attributes : undefined) },
startTime: millisToHrTime(logs.scopeCreateTimestamp),
}, ctx ?? api_1.context.active(), (thisSpan) => {
for (const entry of logs.logs ?? []) {
if (presentation_common_1.DiagnosticsLogEntry.isMessage(entry)) {
const eventAttributes = {
category: entry.category,
...(entry.severity.dev ? { devSeverity: entry.severity.dev } : undefined),
...(entry.severity.editor ? { editorSeverity: entry.severity.editor } : undefined),
};
thisSpan.addEvent(entry.message, eventAttributes, millisToHrTime(entry.timestamp));
if (entry.severity.dev === "error") {
thisSpan.setStatus({ code: api_1.SpanStatusCode.ERROR, message: entry.message });
}
}
else {
exportDiagnosticsLogs(entry);
}
}
return thisSpan;
});
span.end(millisToHrTime(logs.scopeCreateTimestamp + logs.duration));
}
function millisToHrTime(millis) {
const hrTime = [0, 0];
hrTime[0] = Math.trunc(millis / 1000);
hrTime[1] = (millis - hrTime[0] * 1000) * 1e6;
return hrTime;
}
//# sourceMappingURL=Diagnostics.js.map