@nestjs-labs/nestjs-pino-extra
Version:
Enhanced nestjs-pino with OpenTelemetry, Loki, file rotation and enterprise features
72 lines • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLokiOptions = getLokiOptions;
exports.getOtelOptions = getOtelOptions;
exports.extractLogConfig = extractLogConfig;
exports.getPinoHttpFromConfig = getPinoHttpFromConfig;
exports.getParamsFromConfig = getParamsFromConfig;
exports.getNestjsPinoModuleOptions = getNestjsPinoModuleOptions;
const pino_http_extra_1 = require("@nestjs-labs/pino-http-extra");
function getLokiOptions(configService) {
const host = configService.get('LOG_LOKI_HOST');
const username = configService.get('LOG_LOKI_USERNAME');
const password = configService.get('LOG_LOKI_PASSWORD');
const labels = configService
.get('LOG_LOKI_LABELS')
?.split(',')
?.reduce((acc, label) => {
const [key, value] = label.split('=');
acc[key] = value;
return acc;
}, {});
return host
? {
host,
basicAuth: username && password ? { username, password } : undefined,
labels,
}
: undefined;
}
function getOtelOptions(configService) {
const spanIdKey = configService.get('OTEL_SPAN_ID_KEY') ?? 'spanId';
const traceIdKey = configService.get('OTEL_TRACE_ID_KEY') ?? 'traceId';
return { spanIdKey, traceIdKey };
}
function extractLogConfig(configService) {
const app = configService.get('OTLP_SERVICE_NAME') ?? 'app';
const level = configService.get('LOG_LEVEL') ?? 'info';
const filename = configService.get('LOG_FILE');
const lokiOptions = getLokiOptions(configService);
const otelOptions = getOtelOptions(configService);
const validLevels = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
if (!validLevels.includes(level)) {
throw new Error(`Invalid LOG_LEVEL: ${level}. Must be one of: ${validLevels.join(', ')}`);
}
return {
app,
filename,
level,
loki: lokiOptions,
spanIdKey: otelOptions.spanIdKey,
traceIdKey: otelOptions.traceIdKey,
};
}
function getPinoHttpFromConfig(config) {
return [
(0, pino_http_extra_1.getPinoHttpOption)(config.level, config.spanIdKey, config.traceIdKey),
(0, pino_http_extra_1.getMultiDestinationStream)(config.app, config.level, config.filename, config.loki),
];
}
function getParamsFromConfig(config, overrides) {
const pinoHttp = getPinoHttpFromConfig(config);
const params = {
pinoHttp,
exclude: [{ method: 0, path: '/health' }],
};
return Object.assign(params, overrides);
}
function getNestjsPinoModuleOptions(configService, overrides = {}) {
const config = extractLogConfig(configService);
return getParamsFromConfig(config, overrides);
}
//# sourceMappingURL=module-option.js.map