"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.N8nInstrumentation = void 0;
const backend_common_1 = require("@n8n/backend-common");
const decorators_1 = require("@n8n/decorators");
const di_1 = require("@n8n/di");
const api_1 = require("@opentelemetry/api");
const node_end_handler_1 = require("./handlers/node-end.handler");
const node_start_handler_1 = require("./handlers/node-start.handler");
const workflow_end_handler_1 = require("./handlers/workflow-end.handler");
const workflow_start_handler_1 = require("./handlers/workflow-start.handler");
const otel_config_1 = require("./otel.config");
const span_registry_1 = require("./span-registry");
const TRACER_NAME = 'n8n-workflow';
let N8nInstrumentation = class N8nInstrumentation {
    constructor(workflowStartHandler, workflowEndHandler, nodeStartHandler, nodeEndHandler, config, logger) {
        this.config = config;
        this.logger = logger;
        this.spans = new span_registry_1.SpanRegistry();
        this.tracer = api_1.trace.getTracer(TRACER_NAME);
        this.loggedFailureEvents = new Set();
        this.lifecycleDispatchers = {
            workflowExecuteBefore: (ctx) => workflowStartHandler.handle(ctx, this.spans, this.tracer),
            workflowExecuteAfter: (ctx) => workflowEndHandler.handle(ctx, this.spans),
            nodeExecuteBefore: (ctx) => nodeStartHandler.handle(ctx, this.spans, this.tracer),
            nodeExecuteAfter: (ctx) => nodeEndHandler.handle(ctx, this.spans),
        };
    }
    onWorkflowStart(ctx) {
        this.executeLifecycleHandler('workflowExecuteBefore', ctx);
    }
    onWorkflowEnd(ctx) {
        this.executeLifecycleHandler('workflowExecuteAfter', ctx);
    }
    onNodeStart(ctx) {
        if (!this.config.includeNodeSpans)
            return;
        this.executeLifecycleHandler('nodeExecuteBefore', ctx);
    }
    onNodeEnd(ctx) {
        if (!this.config.includeNodeSpans)
            return;
        this.executeLifecycleHandler('nodeExecuteAfter', ctx);
    }
    executeLifecycleHandler(event, ctx) {
        try {
            this.lifecycleDispatchers[event](ctx);
        }
        catch (error) {
            if (this.loggedFailureEvents.has(event))
                return;
            this.loggedFailureEvents.add(event);
            this.logger.error('Failed to process OpenTelemetry span data', {
                event,
                error: error instanceof Error ? error.message : String(error),
            });
        }
    }
};
exports.N8nInstrumentation = N8nInstrumentation;
__decorate([
    (0, decorators_1.OnLifecycleEvent)('workflowExecuteBefore'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], N8nInstrumentation.prototype, "onWorkflowStart", null);
__decorate([
    (0, decorators_1.OnLifecycleEvent)('workflowExecuteAfter'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], N8nInstrumentation.prototype, "onWorkflowEnd", null);
__decorate([
    (0, decorators_1.OnLifecycleEvent)('nodeExecuteBefore'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], N8nInstrumentation.prototype, "onNodeStart", null);
__decorate([
    (0, decorators_1.OnLifecycleEvent)('nodeExecuteAfter'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], N8nInstrumentation.prototype, "onNodeEnd", null);
exports.N8nInstrumentation = N8nInstrumentation = __decorate([
    (0, di_1.Service)(),
    __metadata("design:paramtypes", [workflow_start_handler_1.WorkflowStartHandler,
        workflow_end_handler_1.WorkflowEndHandler,
        node_start_handler_1.NodeStartHandler,
        node_end_handler_1.NodeEndHandler,
        otel_config_1.OtelConfig,
        backend_common_1.Logger])
], N8nInstrumentation);
//# sourceMappingURL=n8n-instrumentation.js.map