Skip to content

Handle large log messages more efficiently #1604

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

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
67 changes: 41 additions & 26 deletions apps/webapp/app/v3/eventRepository.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ export type UpdateEventOptions = {
events?: SpanEvents;
};

type TaskEventSummary = Pick<
TaskEvent,
| "id"
| "spanId"
| "parentId"
| "runId"
| "idempotencyKey"
| "message"
| "style"
| "startTime"
| "duration"
| "isError"
| "isPartial"
| "isCancelled"
| "level"
| "events"
| "environmentType"
>;

export class EventRepository {
private readonly _flushScheduler: DynamicFlushScheduler<CreatableEvent>;
private _randomIdGenerator = new RandomIdGenerator();
Expand Down Expand Up @@ -383,32 +402,28 @@ export class EventRepository {

public async getTraceSummary(traceId: string): Promise<TraceSummary | undefined> {
return await startActiveSpan("getTraceSummary", async (span) => {
const events = await this.readReplica.taskEvent.findMany({
select: {
id: true,
spanId: true,
parentId: true,
runId: true,
idempotencyKey: true,
message: true,
style: true,
startTime: true,
duration: true,
isError: true,
isPartial: true,
isCancelled: true,
level: true,
events: true,
environmentType: true,
},
where: {
traceId,
},
orderBy: {
startTime: "asc",
},
take: env.MAXIMUM_TRACE_SUMMARY_VIEW_COUNT,
});
const events = await this.readReplica.$queryRaw<TaskEventSummary[]>`
SELECT
id,
"spanId",
"parentId",
"runId",
"idempotencyKey",
LEFT(message, 256) as message,
style,
"startTime",
duration,
"isError",
"isPartial",
"isCancelled",
level,
events,
"environmentType"
FROM "TaskEvent"
WHERE "traceId" = ${traceId}
ORDER BY "startTime" ASC
LIMIT ${env.MAXIMUM_TRACE_SUMMARY_VIEW_COUNT}
`;

let preparedEvents: Array<PreparedEvent> = [];
let rootSpanId: string | undefined;
Expand Down
4 changes: 3 additions & 1 deletion apps/webapp/app/v3/otlpExporter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ function convertLogsToCreateableEvents(resourceLog: ResourceLogs): Array<Creatab
traceId: binaryToHex(log.traceId),
spanId: eventRepository.generateSpanId(),
parentId: binaryToHex(log.spanId),
message: isStringValue(log.body) ? log.body.stringValue : `${log.severityText} log`,
message: isStringValue(log.body)
? log.body.stringValue.slice(0, 4096)
: `${log.severityText} log`,
Comment on lines +170 to +172
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Handle potential issues with multibyte characters when truncating

Using slice(0, 4096) on a string may cut through multibyte characters, potentially leading to malformed text. To properly handle multibyte characters, consider using methods that are Unicode-aware, such as substring or libraries that handle Unicode strings correctly.

Proposed change:

message: isStringValue(log.body)
  ? log.body.stringValue.substring(0, MAX_MESSAGE_LENGTH)
  : `${log.severityText} log`,

Alternatively, if the limit is intended to be in bytes, use a library that accurately handles UTF-8 strings.

isPartial: false,
kind: "INTERNAL" as const,
level: logLevelToEventLevel(log.severityNumber),
Expand Down
Loading