"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OtelLifecycleHandler = void 0;
exports.countOutputItems = countOutputItems;
exports.countInputItems = countInputItems;
const decorators_1 = require("@n8n/decorators");
const di_1 = require("@n8n/di");
const execution_level_tracer_1 = require("./execution-level-tracer");
const otel_config_1 = require("./otel.config");
const tracing_context_1 = require("./tracing-context");
let OtelLifecycleHandler = class OtelLifecycleHandler {
    constructor(tracer, traceContextService, config) {
        this.tracer = tracer;
        this.traceContextService = traceContextService;
        this.config = config;
    }
    async onWorkflowStart(ctx) {
        const parentExecutionId = ctx.executionData?.parentExecution?.executionId;
        const tracingContext = parentExecutionId
            ?
                await this.traceContextService.get(parentExecutionId)
            :
                await this.traceContextService.get(ctx.executionId);
        const spanContext = this.tracer.startWorkflow({
            executionId: ctx.executionId,
            tracingContext,
            workflow: {
                id: ctx.workflow.id,
                name: ctx.workflow.name,
                versionId: ctx.workflow.versionId,
                nodeCount: ctx.workflow.nodes.length,
            },
        });
        await this.traceContextService.persist(ctx.executionId, spanContext);
    }
    async onWorkflowResume(ctx) {
        const previousWorkflowExecution = await this.traceContextService.get(ctx.executionId);
        this.tracer.startWorkflow({
            executionId: ctx.executionId,
            linkTo: previousWorkflowExecution,
            workflow: {
                id: ctx.workflow.id,
                name: ctx.workflow.name,
                versionId: ctx.workflow.versionId,
                nodeCount: ctx.workflow.nodes.length,
            },
        });
    }
    onWorkflowEnd(ctx) {
        this.tracer.endWorkflow({
            executionId: ctx.executionId,
            status: ctx.runData.status,
            mode: ctx.runData.mode,
            error: ctx.runData.data.resultData.error,
            isRetry: ctx.runData.mode === 'retry',
            retryOf: ctx.retryOf,
        });
    }
    onNodeStart(ctx) {
        if (!this.config.includeNodeSpans)
            return;
        const node = ctx.workflow.nodes.find((n) => n.name === ctx.nodeName);
        if (!node)
            return;
        this.tracer.startNode({
            executionId: ctx.executionId,
            node,
        });
    }
    onNodeEnd(ctx) {
        if (!this.config.includeNodeSpans)
            return;
        const node = ctx.workflow.nodes.find((n) => n.name === ctx.nodeName);
        if (!node)
            return;
        const customAttributes = ctx.taskData.metadata?.tracing
            ? Object.fromEntries(Object.entries(ctx.taskData.metadata.tracing).map(([key, value]) => [key, String(value)]))
            : undefined;
        this.tracer.endNode({
            executionId: ctx.executionId,
            node,
            inputItemCount: countInputItems(ctx),
            outputItemCount: countOutputItems(ctx.taskData.data),
            error: ctx.taskData.error ?? undefined,
            customAttributes,
        });
    }
};
exports.OtelLifecycleHandler = OtelLifecycleHandler;
__decorate([
    (0, decorators_1.OnLifecycleEvent)('workflowExecuteBefore'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], OtelLifecycleHandler.prototype, "onWorkflowStart", null);
__decorate([
    (0, decorators_1.OnLifecycleEvent)('workflowExecuteResume'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], OtelLifecycleHandler.prototype, "onWorkflowResume", null);
__decorate([
    (0, decorators_1.OnLifecycleEvent)('workflowExecuteAfter'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], OtelLifecycleHandler.prototype, "onWorkflowEnd", null);
__decorate([
    (0, decorators_1.OnLifecycleEvent)('nodeExecuteBefore'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], OtelLifecycleHandler.prototype, "onNodeStart", null);
__decorate([
    (0, decorators_1.OnLifecycleEvent)('nodeExecuteAfter'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], OtelLifecycleHandler.prototype, "onNodeEnd", null);
exports.OtelLifecycleHandler = OtelLifecycleHandler = __decorate([
    (0, di_1.Service)(),
    __metadata("design:paramtypes", [execution_level_tracer_1.ExecutionLevelTracer,
        tracing_context_1.TraceContextService,
        otel_config_1.OtelConfig])
], OtelLifecycleHandler);
function countOutputItems(data) {
    if (!data?.main)
        return 0;
    return data.main.reduce((sum, branch) => sum + (branch?.length ?? 0), 0);
}
function countInputItems(ctx) {
    const runData = ctx.executionData.resultData.runData;
    let total = 0;
    for (const source of ctx.taskData.source) {
        if (!source)
            continue;
        const sourceRuns = runData[source.previousNode];
        if (!sourceRuns)
            continue;
        const run = sourceRuns[source.previousNodeRun ?? 0];
        if (!run?.data?.main)
            continue;
        const branch = run.data.main[source.previousNodeOutput ?? 0];
        total += branch?.length ?? 0;
    }
    return total;
}
//# sourceMappingURL=otel-lifecycle-handler.js.map