import tracer from 'dd-trace';
export var Types;
(function (Types) {
    Types["ACTION_EXECUTION"] = "nango.jobs.actionExecution";
    Types["ACTION_SUCCESS"] = "nango.orch.action.success";
    Types["ACTION_FAILURE"] = "nango.orch.action.failure";
    Types["ACTION_TRACK_RUNTIME"] = "action_track_runtime";
    Types["AUTH_GET_ENV_BY_CONNECT_SESSION"] = "nango.auth.getEnvByConnectSession";
    Types["AUTH_GET_ENV_BY_SECRET_KEY"] = "nango.auth.getEnvBySecretKey";
    Types["AUTH_GET_ENV_BY_CONNECT_SESSION_OR_SECRET_KEY"] = "nango.auth.getEnvByConnectSessionOrSecretKey";
    Types["AUTH_GET_ENV_BY_CONNECT_SESSION_OR_PUBLIC_KEY"] = "nango.auth.getEnvByConnectSessionOrPublicKey";
    Types["AUTH_SESSION"] = "nango.auth.session";
    Types["GET_CONNECTION"] = "nango.server.getConnection";
    Types["JOBS_DELETE_SYNCS_DATA"] = "nango.jobs.cron.deleteSyncsData";
    Types["JOBS_DELETE_SYNCS_DATA_DELETES"] = "nango.jobs.cron.deleteSyncsData.deletes";
    Types["JOBS_DELETE_SYNCS_DATA_JOBS"] = "nango.jobs.cron.deleteSyncsData.jobs";
    Types["JOBS_DELETE_SYNCS_DATA_RECORDS"] = "nango.jobs.cron.deleteSyncsData.records";
    Types["JOBS_DELETE_SYNCS_DATA_SCHEDULES"] = "nango.jobs.cron.deleteSyncsData.schedules";
    Types["LOGS_LOG"] = "nango.logs.log";
    Types["PERSIST_RECORDS_COUNT"] = "nango.persist.records.count";
    Types["PERSIST_RECORDS_SIZE_IN_BYTES"] = "nango.persist.records.sizeInBytes";
    Types["ON_EVENT_SCRIPT_EXECUTION"] = "nango.jobs.onEventScriptExecution";
    Types["ON_EVENT_SCRIPT_RUNTIME"] = "nango.jobs.onEventScriptRuntime";
    Types["ON_EVENT_SCRIPT_SUCCESS"] = "nango.orch.onEventScript.success";
    Types["ON_EVENT_SCRIPT_FAILURE"] = "nango.orch.onEventScript.failure";
    Types["PROXY"] = "nango.server.proxyCall";
    Types["PROXY_SUCCESS"] = "nango.server.proxy.success";
    Types["PROXY_FAILURE"] = "nango.server.proxy.failure";
    Types["REFRESH_CONNECTIONS"] = "nango.server.cron.refreshConnections";
    Types["REFRESH_CONNECTIONS_FAILED"] = "nango.server.cron.refreshConnections.failed";
    Types["REFRESH_CONNECTIONS_SUCCESS"] = "nango.server.cron.refreshConnections.success";
    Types["RUNNER_SDK"] = "nango.runner.sdk";
    Types["RUNNER_INVALID_ACTION_INPUT"] = "nango.runner.invalidActionInput";
    Types["RUNNER_INVALID_ACTION_OUTPUT"] = "nango.runner.invalidActionOutput";
    Types["RUNNER_INVALID_SYNCS_RECORDS"] = "nango.runner.invalidSyncsRecords";
    Types["SYNC_EXECUTION"] = "nango.jobs.syncExecution";
    Types["SYNC_TRACK_RUNTIME"] = "sync_script_track_runtime";
    Types["SYNC_SUCCESS"] = "nango.orch.sync.success";
    Types["SYNC_FAILURE"] = "nango.orch.sync.failure";
    Types["WEBHOOK_EXECUTION"] = "nango.jobs.webhookExecution";
    Types["WEBHOOK_TRACK_RUNTIME"] = "webhook_track_runtime";
    Types["WEBHOOK_SUCCESS"] = "nango.orch.webhook.success";
    Types["WEBHOOK_FAILURE"] = "nango.orch.webhook.failure";
    Types["ORCH_TASKS_CREATED"] = "nango.orch.tasks.created";
    Types["ORCH_TASKS_STARTED"] = "nango.orch.tasks.started";
    Types["ORCH_TASKS_SUCCEEDED"] = "nango.orch.tasks.succeeded";
    Types["ORCH_TASKS_FAILED"] = "nango.orch.tasks.failed";
    Types["ORCH_TASKS_EXPIRED"] = "nango.orch.tasks.expired";
    Types["ORCH_TASKS_CANCELLED"] = "nango.orch.tasks.cancelled";
})(Types || (Types = {}));
export function increment(metricName, value = 1, dimensions) {
    tracer.dogstatsd.increment(metricName, value, dimensions ?? {});
}
export function decrement(metricName, value = 1, dimensions) {
    tracer.dogstatsd.decrement(metricName, value, dimensions ?? {});
}
export function gauge(metricName, value) {
    tracer.dogstatsd.gauge(metricName, value ?? 1);
}
export function duration(metricName, value) {
    tracer.dogstatsd.distribution(metricName, value);
}
export function time(metricName, func) {
    const computeDuration = (start) => {
        const durationComponents = process.hrtime(start);
        const seconds = durationComponents[0];
        const nanoseconds = durationComponents[1];
        const total = seconds * 1000 + nanoseconds / 1e6;
        duration(metricName, total);
    };
    // This function should handle both async/sync function
    // So it try/catch regular execution and use .then() for async
    // @ts-expect-error can't fix this
    return function wrapped(...args) {
        const start = process.hrtime();
        try {
            const res = func(...args);
            if (res[Symbol.toStringTag] === 'Promise') {
                return res.then((v) => {
                    computeDuration(start);
                    return v;
                }, (err) => {
                    computeDuration(start);
                    throw err;
                });
            }
            return res;
        }
        catch (err) {
            computeDuration(start);
            throw err;
        }
    };
}
//# sourceMappingURL=metrics.js.map