Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poc tracing #562

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"json": false,
"colorize": true
}
},
"tracing": {
"enabled": true,
"traceExportURL": "http://localhost:4318/v1/traces"
}
},
"rendering": {
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"@grpc/grpc-js": "^1.8.22",
"@grpc/proto-loader": "^0.7.2",
"@hapi/boom": "^10.0.0",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/auto-instrumentations-node": "^0.49.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.52.1",
"@opentelemetry/resources": "^1.25.1",
"@opentelemetry/sdk-node": "^0.52.1",
"@opentelemetry/semantic-conventions": "^1.25.1",
"@puppeteer/browsers": "^1.6.0",
"chokidar": "^3.5.2",
"dompurify": "^2.4.0",
Expand All @@ -54,7 +60,7 @@
"@types/jest": "^29.5.12",
"@types/jsdom": "20.0.0",
"@types/multer": "1.4.7",
"@types/node": "^18.7.18",
"@types/node": "^22.1.0",
"@types/pixelmatch": "^5.2.6",
"@types/supertest": "^2.0.15",
"@typescript-eslint/eslint-plugin": "5.37.0",
Expand Down
8 changes: 8 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './tracing'; // FIXIT: ideally this should changed by a function initializeTracing

import * as path from 'path';
import * as _ from 'lodash';
import * as fs from 'fs';
Expand Down Expand Up @@ -68,6 +70,12 @@ async function main() {

const logger = new ConsoleLogger(config.service.logging);

if (config.service.tracing.enabled) {
// FIXIT: not working probably because it is being initialized before the server
// initializeTracing(logger, config.service.tracing.exporterURL);
logger.info("tracing is enabled");
}

const sanitizer = createSanitizer();
const server = new HttpServer(config, logger, sanitizer);
await server.start();
Expand Down
16 changes: 16 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as winston from 'winston';
import { LoggingConfig } from './service/config';
import {context, trace} from "@opentelemetry/api";

export interface LogWriter {
write(message, encoding);
Expand Down Expand Up @@ -45,10 +46,25 @@ export class ConsoleLogger implements Logger {
transports.push(new winston.transports.Console(options));
}

// Create a custom format to include trace context in logs
const traceContextFormat = winston.format((info) => {
const span = trace.getSpan(context.active());
if (span) {
const spanContext = span.spanContext();
info.trace_id = spanContext.traceId;
info.span_id = spanContext.spanId;
}
return info;
});

this.logger = winston.createLogger({
level: config.level,
exitOnError: false,
transports: transports,
format: winston.format.combine(
traceContextFormat(),
winston.format.json()
),
});

this.errorWriter = {
Expand Down
10 changes: 10 additions & 0 deletions src/service/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export interface MetricsConfig {
requestDurationBuckets: number[];
}

export interface TracesConfig {
enabled: boolean;
exporterURL: string;
}

export interface ConsoleLoggerConfig {
level?: string;
json: boolean;
Expand All @@ -29,6 +34,7 @@ export interface ServiceConfig {
metrics: MetricsConfig;
logging: LoggingConfig;
security: SecurityConfig;
tracing: TracesConfig;
};
rendering: RenderingConfig;
}
Expand All @@ -53,6 +59,10 @@ export const defaultServiceConfig: ServiceConfig = {
security: {
authToken: '-',
},
tracing: {
enabled: false,
exporterURL: 'http://localhost:4318/v1/traces',
}
},
rendering: defaultRenderingConfig,
};
Expand Down
40 changes: 40 additions & 0 deletions src/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {NodeSDK} from '@opentelemetry/sdk-node';
import {getNodeAutoInstrumentations} from '@opentelemetry/auto-instrumentations-node';
import {OTLPTraceExporter} from '@opentelemetry/exporter-trace-otlp-http';
import {SEMRESATTRS_SERVICE_NAME} from '@opentelemetry/semantic-conventions';
import {Resource} from '@opentelemetry/resources';
import {ConsoleLogger, Logger} from "./logger";
import {defaultServiceConfig, ServiceConfig} from "./service/config";

const traceExporterURL = 'http://localhost:4318/v1/traces';
let config: ServiceConfig = defaultServiceConfig;


// For troubleshooting, set the log level to DiagLogLevel.DEBUG
// const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
// diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const log = new ConsoleLogger(config.service.logging);
// export function initializeTracing(log: Logger, traceExporterURL: string) {
log.info('Starting tracing', 'traceExporterURL', traceExporterURL);
const traceExporter = new OTLPTraceExporter({
url: traceExporterURL, // Change to your Jaeger or OTLP endpoint
});

const sdk = new NodeSDK({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'image-renderer-service',
}),
traceExporter,
instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();

process.on('SIGTERM', () => {
sdk.shutdown()
.then(() => log.debug('Tracing terminated'))
.catch((error) => log.error('Error terminating tracing', error))
.finally(() => process.exit(0));
});
// }
Loading