import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
import { GenericPoolInstrumentation } from '@opentelemetry/instrumentation-generic-pool';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis';
import { NetInstrumentation } from '@opentelemetry/instrumentation-net';
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';
import { PgInstrumentation } from '@opentelemetry/instrumentation-pg';
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';
const InstrumentationMap = {
    '@opentelemetry/instrumentation-http': HttpInstrumentation,
    '@opentelemetry/instrumentation-dns': DnsInstrumentation,
    '@opentelemetry/instrumentation-express': ExpressInstrumentation,
    '@opentelemetry/instrumentation-graphql': GraphQLInstrumentation,
    '@opentelemetry/instrumentation-undici': UndiciInstrumentation,
    '@opentelemetry/instrumentation-generic-pool': GenericPoolInstrumentation,
    '@opentelemetry/instrumentation-redis': RedisInstrumentation,
    '@opentelemetry/instrumentation-net': NetInstrumentation,
    '@opentelemetry/instrumentation-pg': PgInstrumentation,
    '@opentelemetry/instrumentation-pino': PinoInstrumentation,
};
let ignoreIncomingRequestHook = (req) => {
    return req.url === '/health' || req.url === '/metrics';
};
let spanNameHook;
export function setTelemetryHooks(hooks) {
    if ('ignoreIncomingRequestHook' in hooks) {
        ignoreIncomingRequestHook = hooks.ignoreIncomingRequestHook;
    }
    if ('spanNameHook' in hooks) {
        spanNameHook = hooks.spanNameHook;
    }
}
const defaultConfigs = {
    '@opentelemetry/instrumentation-http': {
        ignoreIncomingRequestHook(req) {
            if (ignoreIncomingRequestHook) {
                return ignoreIncomingRequestHook(req);
            }
            return false;
        },
    },
    '@opentelemetry/instrumentation-express': {
        spanNameHook(info, defaultName) {
            if (spanNameHook) {
                return spanNameHook(info, defaultName);
            }
            return defaultName;
        },
        ignoreLayers: [
            'middleware - serviceLogger',
            'middleware - jsonParser',
            'middleware - attachServiceLocals',
            'middleware - cookieParser',
            'middleware - corsMiddleware',
            'middleware - addReturnHeaders',
            'middleware - freezeQuery',
            'middleware - pathParamsMiddleware',
            'middleware - metadataMiddleware',
            'middleware - multipartMiddleware',
            'middleware - securityMiddleware',
        ],
    },
};
export function getAutoInstrumentations(inputConfigs = defaultConfigs) {
    const keys = Object.keys(InstrumentationMap);
    return keys
        .map((name) => {
        const Instance = InstrumentationMap[name];
        // Defaults are defined by the instrumentation itself
        const userConfig = inputConfigs[name] ?? {};
        try {
            return new Instance(userConfig);
        }
        catch (e) {
            // eslint-disable-next-line no-console
            console.error(`Failed to load ${name}`, e);
            return null;
        }
    })
        .filter((i) => !!i);
}
//# sourceMappingURL=instrumentations.js.map