(dc): change logging to json

This commit is contained in:
Marcel Arndt
2025-04-11 11:58:19 +02:00
parent 46631db5b2
commit dde00abe54
16 changed files with 3674 additions and 3092 deletions
@@ -1,4 +1,4 @@
import { Controller, Get, Query } from '@nestjs/common';
import { Controller, Get, Logger, Query } from '@nestjs/common';
import { PrismaService } from './core/database/prisma.service';
import { AnomaliesService } from './modules/feat-anomalies/anomalies.service';
import { CalculationService } from './modules/feat-calculation/calculation.service';
@@ -7,6 +7,8 @@ import { TicketService } from './modules/feat-tickets/tickets.service';
@Controller()
export class AppController {
private logger = new Logger(AppController.name);
constructor(
private readonly importer: ImporterService,
private readonly anomaliesService: AnomaliesService,
@@ -17,6 +19,8 @@ export class AppController {
@Get('etl')
etl(@Query('from') from: string, @Query('to') to: string) {
this.logger.log(`Start ETL for interval from=${from} to=${to}`);
if (!from || !to) {
return {
error: 'from and to must be defined',
@@ -146,7 +150,6 @@ export class AppController {
revenue: await this.calculationService.calculateTourRevenue(tour),
});
}
console.log(this.calculationService.getHealthInsurancesWithNoTenant());
return toursWithRevenue;
}
}
@@ -1,38 +0,0 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/*instrumentation.ts*/
const { NodeSDK } = require('@opentelemetry/sdk-node');
const {
getNodeAutoInstrumentations,
} = require('@opentelemetry/auto-instrumentations-node');
const {
OTLPTraceExporter,
} = require('@opentelemetry/exporter-trace-otlp-proto'); // Annahme: gRPC/Proto Exporter für Backend
const {
OTLPMetricExporter,
} = require('@opentelemetry/exporter-metrics-otlp-proto'); // Annahme: gRPC/Proto Exporter für Backend
const { PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { resourceFromAttributes } = require('@opentelemetry/resources');
const { ATTR_SERVICE_NAME } = require('@opentelemetry/semantic-conventions');
const otelEndpoint =
process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ??
'http://monitoring_alloy:4318';
const sdk = new NodeSDK({
resource: resourceFromAttributes({
[ATTR_SERVICE_NAME]: 'avicenna.data-connector',
namespace: 'avicenna',
}),
traceExporter: new OTLPTraceExporter({
url: `${otelEndpoint}/v1/traces`,
}),
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
url: `${otelEndpoint}/v1/metrics`,
}),
}),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
console.log('OTel SDK started');
+4 -1
View File
@@ -1,8 +1,11 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConsoleLogger } from '@nestjs/common';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule, {
logger: new ConsoleLogger({ json: true }),
});
app.enableCors();
await app.listen(3000);
@@ -39,7 +39,7 @@ export class ManagerResolver {
@Subscription(() => TimePerTourUpdateObjectType)
timePerTourUpdated() {
return this.timePerTourKpiService.pubSub.asyncIterator(
return this.timePerTourKpiService.pubSub.asyncIterableIterator(
'timePerTourUpdated',
);
}
@@ -92,7 +92,6 @@ export class TicketLifecycleService {
private async validateCreatedTickets(): Promise<void> {
const tickets = await this.repository.findAllCreated();
this.logger.debug(`Found ${tickets.length} with state CREATED`);
for (const ticket of tickets) {
const {
@@ -362,7 +362,6 @@ export class TicketRepository {
const dwellTimesForTicketState = ticketWithState
.map(({ state }) => {
console.log(state);
const stateToCalculateIdx = state.findIndex(
({ state }) => state === ticketState,
);
@@ -77,6 +77,6 @@ export class TicketsResolver {
@Subscription(() => TicketsUpdatedObjectType)
ticketsUpdated() {
return this.ticketsService.pubSub.asyncIterator('ticketsUpdated');
return this.ticketsService.pubSub.asyncIterableIterator('ticketsUpdated');
}
}
@@ -82,7 +82,7 @@ export class DriverCertificateDetector extends AnomalyDetector {
};
}
} catch (error) {
this.logger.error(error, anomaly.id);
this.logger.error(error, { anomalyId: anomaly.id });
return anomaly;
}
}
@@ -68,7 +68,7 @@ export class EmployeesLoggedInDetector extends AnomalyDetector {
return anomaly;
}
} catch (error) {
this.logger.error(error, anomaly.id);
this.logger.error(error, { anomalyId: anomaly.id });
return anomaly;
}
}
@@ -200,12 +200,11 @@ export class EmployeesLoggedInDetector extends AnomalyDetector {
return false;
}
if (!employee.personnelNumber) {
this.logger.warn(
`Employee has no personnelNumber`,
employee.id,
employee.surname,
employee.name,
);
this.logger.warn(`Employee has no personnelNumber`, {
id: employee.id,
surname: employee.surname,
name: employee.name,
});
return false;
}
return true;
@@ -313,13 +312,12 @@ export class EmployeesLoggedInDetector extends AnomalyDetector {
return [false, undefined];
}
} catch (error) {
this.logger.error(
error,
datetimeToCheck.toISOString(),
this.logger.error(error, {
timeInQuestion: datetimeToCheck.toISOString(),
workTime,
firstClockedIn,
lastClockedOut,
);
});
return [false, undefined];
}
}
@@ -131,12 +131,11 @@ export class OneTourAtATimeDetector extends AnomalyDetector {
tourListInterval,
);
} catch (error) {
this.logger.error(
'tourListInterval',
error,
this.logger.error(error, {
what: 'tourListInterval',
tourListInterval,
tourInterval,
);
});
return false;
}
},
@@ -299,7 +298,7 @@ export class OneTourAtATimeDetector extends AnomalyDetector {
}
return { ...anomaly };
} catch (error) {
this.logger.error(error, anomaly.id);
this.logger.error(error, { anomalyId: anomaly.id });
return anomaly;
}
}
@@ -66,7 +66,7 @@ export class WorkingEmployeeNotInDistributionSystemDetector extends AnomalyDetec
} catch (error) {
this.logger.warn(
`Employee does not exist! PersonnelNumber: ${personnelNumber}`,
error,
{ error },
);
return;
}
@@ -51,7 +51,7 @@ export class CalculationService {
new Date(startDate),
);
if (!tariff) {
this.logger.error(
this.logger.warn(
`No tariff found for tour ${tour.operationId} (${tour.healthInsurance})`,
);
return 0;
@@ -163,11 +163,10 @@ export class CalculationService {
),
);
if (!billingInformation) {
this.logger.error(
"Couldn't find billing information",
this.logger.warn("Couldn't find billing information", {
date: date.toISOString(),
ordinanceType,
date.toISOString(),
);
});
throw new Error("Couldn't find billing information");
}
return billingInformation[1];
@@ -12,7 +12,6 @@ export class PlannedTimeLoader {
const deletedPlannedTimes = stagingPlannedTimes.filter(
({ deleted }) => deleted,
);
console.log(deletedPlannedTimes.length);
const plannedTimesToInsert = stagingPlannedTimes.filter(
({ id }) =>
!!id &&