Skip to content

Commit

Permalink
feat: add logs to trace route (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: GALLLASMILAN <gallas.milan@gmail.com>
  • Loading branch information
GALLLASMILAN authored Dec 11, 2024
1 parent e012ad1 commit 007869e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/trace/trace.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { StatusCodes } from 'http-status-codes';

import { withResultsResponse, withResultResponse, Tags } from '../utils/swagger.js';
import { ExportTraceServiceRequest__Output } from '../types/generated/opentelemetry/proto/collector/trace/v1/ExportTraceServiceRequest.js';
import { getModuleLogger } from '../utils/logger-factories.js';

import {
traceSchema,
Expand All @@ -31,6 +32,8 @@ import {
} from './trace.dto.js';
import { createTrace, getTrace, getTraces, traceProtobufBufferParser } from './trace.service.js';

const logger = getModuleLogger('trace');

const module: FastifyPluginAsyncJsonSchemaToTs = async (app) => {
// Custom parser for "application/x-protobuf"
app.addContentTypeParser(
Expand Down Expand Up @@ -89,6 +92,17 @@ const module: FastifyPluginAsyncJsonSchemaToTs = async (app) => {
}
},
async ({ body }, res) => {
logger.debug(
body.resourceSpans.flatMap((resourceSpan) =>
resourceSpan.scopeSpans.flatMap((scopeSpan) => {
return {
scopeName: scopeSpan.scope?.name,
spanNames: scopeSpan.spans.map((span) => span.name)
};
})
),
'incoming trace data'
);
await createTrace(body);

res.code(StatusCodes.CREATED);
Expand Down
1 change: 1 addition & 0 deletions src/trace/trace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export function traceProtobufBufferParser(
)
);
}
logger.debug({ base64: payload.toString().slice(0, 100) }, 'trace buffer base64 data');
if (!Buffer.isBuffer(payload)) {
return done(
new ErrorWithProps(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ export const errorPlugin: FastifyPluginAsync = fp.default(async (app) => {
message: error.message
});
} else {
this.log.error(error);
reply.status(StatusCodes.INTERNAL_SERVER_ERROR).send({
code: ErrorWithPropsCodes.INTERNAL_SERVER_ERROR,
message: 'An unspecified error occurred'
});
}
this.log.error(error);
});
});

Expand Down
1 change: 1 addition & 0 deletions src/utils/logger-factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getLogger } from './logger.js';
import { QueueName } from './queue.js';

export const getServiceLogger = (service: string) => getLogger().child({ service });
export const getModuleLogger = (module: string) => getLogger().child({ module });

export const getQueueLogger = ({ service, queue }: { service: string; queue: QueueName }) =>
getServiceLogger(service).child({ queue });

0 comments on commit 007869e

Please sign in to comment.