import tracer from 'dd-trace';
export var Types;
(function (Types) {
    Types["ACTION_TRACK_RUNTIME"] = "action_track_runtime";
    Types["SYNC_TRACK_RUNTIME"] = "sync_script_track_runtime";
    Types["WEBHOOK_TRACK_RUNTIME"] = "webhook_track_runtime";
    Types["RUNNER_SDK"] = "nango.runner.sdk";
    Types["JOBS_CLEAN_ACTIVITY_LOGS"] = "nango.jobs.cron.cleanActivityLogs";
    Types["JOBS_DELETE_SYNCS_DATA"] = "nango.jobs.cron.deleteSyncsData";
    Types["JOBS_DELETE_SYNCS_DATA_JOBS"] = "nango.jobs.cron.deleteSyncsData.jobs";
    Types["JOBS_DELETE_SYNCS_DATA_SCHEDULES"] = "nango.jobs.cron.deleteSyncsData.schedules";
    Types["JOBS_DELETE_SYNCS_DATA_RECORDS"] = "nango.jobs.cron.deleteSyncsData.records";
    Types["JOBS_DELETE_SYNCS_DATA_DELETES"] = "nango.jobs.cron.deleteSyncsData.deletes";
    Types["PERSIST_RECORDS_COUNT"] = "nango.persist.records.count";
    Types["PERSIST_RECORDS_SIZE_IN_BYTES"] = "nango.persist.records.sizeInBytes";
    Types["AUTH_GET_ENV_BY_SECRET_KEY"] = "nango.auth.getEnvBySecretKey";
    Types["LOGS_LOG"] = "nango.logs.log";
})(Types || (Types = {}));
export function increment(metricName, value) {
    tracer.dogstatsd.increment(metricName, value ?? 1);
}
export function decrement(metricName, value) {
    tracer.dogstatsd.decrement(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's 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