/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module RpcInterface
 */
// cspell:ignore calltrace
import { assert, Logger, SpanKind, Tracing } from "@itwin/core-bentley";
import { RpcInvocation } from "@itwin/core-common";
import { AsyncLocalStorage } from "async_hooks";
import { BackendLoggerCategory } from "../BackendLoggerCategory";
import { IModelHost } from "../IModelHost";
/* eslint-disable @typescript-eslint/no-deprecated */
/**
 * Utility for tracing Rpc activity processing. When multiple Rpc requests are being processed asynchronously, this
 * class can be used to correlate the current calltrace with the originating RpcActivity. This is used for automatic appending
 * of RpcActivity to log messages emitted during Rpc processing. It may also be used to retrieve the user accessToken
 * from the RpcActivity.
 * @public
 */
export class RpcTrace {
    static _storage = new AsyncLocalStorage();
    /** Get the [RpcActivity]($common) for the currently executing async, or `undefined` if there is no
     * RpcActivity in the current call stack.
     * */
    static get currentActivity() {
        return RpcTrace._storage.getStore();
    }
    /** Get the [RpcActivity]($common) for the currently executing async. Asserts that the RpcActivity
     * exists in the current call stack.
     * */
    static get expectCurrentActivity() {
        assert(undefined !== RpcTrace.currentActivity);
        return RpcTrace.currentActivity;
    }
    /** Start the processing of an RpcActivity. */
    static async run(activity, fn) {
        return RpcTrace._storage.run(activity, fn);
    }
    /** Start the processing of an RpcActivity inside an OpenTelemetry span */
    static async runWithSpan(activity, fn) {
        return Tracing.withSpan(activity.rpcMethod ?? "unknown RPC method", async () => RpcTrace.run(activity, fn), {
            attributes: {
                ...Logger.getMetaData(), // add default metadata
                ...RpcInvocation.sanitizeForLog(activity), // override with the correct RpcActivity
            },
            kind: SpanKind.INTERNAL,
        });
    }
}
/** @internal */
export function initializeTracing(enableOpenTelemetry = false) {
    RpcInvocation.runActivity = async (activity, fn) => RpcTrace.run(activity, fn); // redirect the invocation processing to the tracer
    if (enableOpenTelemetry) {
        try {
            // eslint-disable-next-line @typescript-eslint/no-require-imports
            const api = require("@opentelemetry/api");
            const tracer = api.trace.getTracer("@itwin/core-backend", IModelHost.backendVersion);
            Tracing.enableOpenTelemetry(tracer, api);
            RpcInvocation.runActivity = async (activity, fn) => RpcTrace.runWithSpan(activity, fn); // wrap invocation in an OpenTelemetry span in addition to RpcTrace
        }
        catch (e) {
            Logger.logWarning(BackendLoggerCategory.IModelHost, "Failed to initialize OpenTelemetry");
            Logger.logError(BackendLoggerCategory.IModelHost, e);
        }
    }
    // set up static logger metadata to include current RpcActivity information for logs during rpc processing
    Logger.staticMetaData.set("rpc", () => RpcInvocation.sanitizeForLog(RpcTrace.currentActivity));
}
//# sourceMappingURL=tracing.js.map