import app from '@adonisjs/core/services/app';
import { defineConfig, Sentry } from '@rlanz/sentry';
import env from '../env.js';
import { version } from '../lib/version.js';
export const SENTRY_SPAN_NAME_IGNORE = 'ignore';
const enableSentry = env.get('SENTRY_ENV') !== 'test';
export default defineConfig({
    enabled: enableSentry,
    dsn: env.get('SENTRY_DSN'),
    environment: env.get('SENTRY_ENV'),
    release: version,
    tracesSampler: (samplingContext) => {
        const transactionName = samplingContext.name;
        if (transactionName === SENTRY_SPAN_NAME_IGNORE)
            return 0;
        else
            return 1;
    },
    debug: false,
    integrations: [
        Sentry.fsIntegration({
            recordFilePaths: true,
        }),
        Sentry.knexIntegration(),
        Sentry.pinoIntegration({ log: { levels: ['info', 'warn', 'error'] } }),
    ],
    enableLogs: true,
    beforeSendTransaction: async (transaction) => (transactionIsSlowerThan(transaction, 1) || forceTransaction(transaction)) &&
        !isDrHttp(transaction)
        ? transaction
        : null,
});
const transactionIsSlowerThan = (transaction, thresholdInSeconds) => {
    if (!transaction.timestamp || !transaction.start_timestamp)
        return true;
    const duration = transaction.timestamp - transaction.start_timestamp;
    return duration > thresholdInSeconds;
};
const forceTransaction = (transaction) => {
    const qs = transaction.request?.query_string;
    if (qs)
        if (typeof qs === 'string' && qs.includes('force_trace=true'))
            return true;
    return false;
};
const isDrHttp = (transaction) => {
    if (!transaction.transaction)
        return false;
    const dhHttpConfig = app.config.get('drhttp');
    if (!dhHttpConfig?.dsn)
        return false;
    const drHttpDsn = dhHttpConfig.dsn.split('@').at(1).split('/').at(0);
    if (!drHttpDsn)
        return false;
    if (transaction.transaction)
        return transaction.transaction.includes(drHttpDsn);
    return false;
};
//# sourceMappingURL=sentry.js.map