Skip to content

Commit

Permalink
feat(backend): use pino logger for json formatting, add vector sink s…
Browse files Browse the repository at this point in the history
…ignoz conf
  • Loading branch information
dthib committed Dec 3, 2024
1 parent 5be034f commit 95ff7a3
Show file tree
Hide file tree
Showing 5 changed files with 459 additions and 237 deletions.
22 changes: 22 additions & 0 deletions backend/sink-signoz.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[transforms.parse]
inputs = [ "pipe"]
type = "remap"
source = '''
.parsed_timestamp = parse_timestamp!(.timestamp, format: "%Y-%m-%dT%H:%M:%S.%fZ")
if is_timestamp(.parsed_timestamp) {
.timestamp = to_unix_timestamp(.parsed_timestamp)
} else {
.timestamp = to_unix_timestamp(now())
}
. = parse_json(slice!(.message, start: 44)) ?? .
'''

[sinks.hook]
type = "http"
inputs = ["parse"]
encoding.codec = "json"
uri = "${SIGNOZ_URI}"
method = "post"

[sinks.hook.request.headers]
signoz-access-token = "${SIGNOZ_TOKEN}"
28 changes: 28 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { SentryModule } from '@sentry/nestjs/setup';
import { LoggerModule } from 'nestjs-pino';
import { AuthModule } from './auth/auth.module';
import { CollectivitesModule } from './collectivites/collectivites.module';
import { CommonModule } from './common/common.module';
Expand All @@ -17,6 +18,33 @@ import { TrpcRouter } from './trpc/trpc.router';

@Module({
imports: [
LoggerModule.forRoot({
pinoHttp: {
messageKey: process.env.NODE_ENV === 'production' ? 'message' : 'msg',
formatters:
process.env.NODE_ENV === 'production'
? {
level: (label, number) => {
// Compliant with https://opentelemetry.io/docs/specs/otel/logs/data-model/
return {
severity_number: number,
severity_text: label,
};
},
}
: undefined,
transport:
process.env.NODE_ENV === 'production'
? undefined // Default configuration to console in json
: {
target: 'pino-pretty',
options: {
singleLine: true,
colorize: true,
},
},
},
}),
SentryModule.forRoot(),
ConfigModule.forRoot({
ignoreEnvFile: process.env.NODE_ENV === 'production', // In production, environment variables are set by the deployment
Expand Down
8 changes: 5 additions & 3 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
import './common/services/sentry.service';
// Other imports
import { patchNestjsSwagger, ZodValidationPipe } from '@anatine/zod-nestjs';
import { Logger } from '@nestjs/common';
import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { Logger } from 'nestjs-pino';
import { AppModule } from './app.module';
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
import { initApplicationCredentials } from './common/services/gcloud.helper';
import { TrpcRouter } from './trpc/trpc.router';

const logger = new Logger('main');
const port = process.env.PORT || 8080;
logger.log(`Launching NestJS app on port ${port}`);

async function bootstrap() {
initApplicationCredentials();

const app = await NestFactory.create(AppModule, {
logger: ['fatal', 'error', 'warn', 'log'], // No debug by default
});
const logger = app.get(Logger);
logger.log(`Launching NestJS app on port ${port}`);
app.useLogger(logger);

const { httpAdapter } = app.get(HttpAdapterHost);

app.enableCors();
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@
"leaflet-defaulticon-compatibility": "^0.1.2",
"lodash": "^4.17.21",
"luxon": "^3.5.0",
"nestjs-pino": "^4.1.0",
"next": "^14.2.16",
"nodemailer": "^6.9.14",
"pino-http": "^10.3.0",
"pino-pretty": "^13.0.0",
"postgres": "^3.4.4",
"posthog-js": "^1.131.3",
"react": "18.3.1",
Expand Down
Loading

0 comments on commit 95ff7a3

Please sign in to comment.