Skip to content

Commit 70b4b12

Browse files
ericallamnicktrn
andauthored
fix: Logging large objects is now much more performant and uses less memory (#2263)
* Logging large objects performance fixes * Add changeset * docs: new otel attribute count defaults * Scope the switch/case statement with braces * Add tests for nested map and sets --------- Co-authored-by: nicktrn <[email protected]>
1 parent 87c21ab commit 70b4b12

File tree

13 files changed

+638
-110
lines changed

13 files changed

+638
-110
lines changed

.changeset/chatty-snakes-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
fix: Logging large objects is now much more performant and uses less memory

apps/webapp/app/env.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ const EnvironmentSchema = z.object({
283283
PROD_OTEL_LOG_EXPORT_TIMEOUT_MILLIS: z.string().default("30000"),
284284
PROD_OTEL_LOG_MAX_QUEUE_SIZE: z.string().default("512"),
285285

286-
TRIGGER_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: z.string().default("256"),
287-
TRIGGER_OTEL_LOG_ATTRIBUTE_COUNT_LIMIT: z.string().default("256"),
286+
TRIGGER_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: z.string().default("1024"),
287+
TRIGGER_OTEL_LOG_ATTRIBUTE_COUNT_LIMIT: z.string().default("1024"),
288288
TRIGGER_OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: z.string().default("131072"),
289289
TRIGGER_OTEL_LOG_ATTRIBUTE_VALUE_LENGTH_LIMIT: z.string().default("131072"),
290290
TRIGGER_OTEL_SPAN_EVENT_COUNT_LIMIT: z.string().default("10"),

docs/self-hosting/env/webapp.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ mode: "wide"
105105
| `MAXIMUM_DEV_QUEUE_SIZE` | No || Max dev queue size. |
106106
| `MAXIMUM_DEPLOYED_QUEUE_SIZE` | No || Max deployed queue size. |
107107
| **OTel limits** | | | |
108-
| `TRIGGER_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT` | No | 256 | OTel span attribute count limit. |
109-
| `TRIGGER_OTEL_LOG_ATTRIBUTE_COUNT_LIMIT` | No | 256 | OTel log attribute count limit. |
108+
| `TRIGGER_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT` | No | 1024 | OTel span attribute count limit. |
109+
| `TRIGGER_OTEL_LOG_ATTRIBUTE_COUNT_LIMIT` | No | 1024 | OTel log attribute count limit. |
110110
| `TRIGGER_OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT` | No | 131072 | OTel span attribute value length limit. |
111111
| `TRIGGER_OTEL_LOG_ATTRIBUTE_VALUE_LENGTH_LIMIT` | No | 131072 | OTel log attribute value length limit. |
112112
| `TRIGGER_OTEL_SPAN_EVENT_COUNT_LIMIT` | No | 10 | OTel span event count limit. |

packages/cli-v3/src/entryPoints/dev-run-worker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
localsAPI,
1616
logger,
1717
LogLevel,
18+
OTEL_LOG_ATTRIBUTE_COUNT_LIMIT,
1819
resourceCatalog,
1920
runMetadata,
2021
runtime,
@@ -191,7 +192,8 @@ async function doBootstrap() {
191192
typeof config.enableConsoleLogging === "boolean" ? config.enableConsoleLogging : true,
192193
typeof config.disableConsoleInterceptor === "boolean"
193194
? config.disableConsoleInterceptor
194-
: false
195+
: false,
196+
OTEL_LOG_ATTRIBUTE_COUNT_LIMIT
195197
);
196198

197199
const configLogLevel = triggerLogLevel ?? config.logLevel ?? "info";
@@ -200,6 +202,7 @@ async function doBootstrap() {
200202
logger: otelLogger,
201203
tracer: tracer,
202204
level: logLevels.includes(configLogLevel as any) ? (configLogLevel as LogLevel) : "info",
205+
maxAttributeCount: OTEL_LOG_ATTRIBUTE_COUNT_LIMIT,
203206
});
204207

205208
logger.setGlobalTaskLogger(otelTaskLogger);

packages/cli-v3/src/entryPoints/managed-run-worker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
localsAPI,
1515
logger,
1616
LogLevel,
17+
OTEL_LOG_ATTRIBUTE_COUNT_LIMIT,
1718
resourceCatalog,
1819
runMetadata,
1920
runtime,
@@ -182,7 +183,8 @@ async function doBootstrap() {
182183
typeof config.enableConsoleLogging === "boolean" ? config.enableConsoleLogging : true,
183184
typeof config.disableConsoleInterceptor === "boolean"
184185
? config.disableConsoleInterceptor
185-
: false
186+
: false,
187+
OTEL_LOG_ATTRIBUTE_COUNT_LIMIT
186188
);
187189

188190
const configLogLevel = triggerLogLevel ?? config.logLevel ?? "info";
@@ -191,6 +193,7 @@ async function doBootstrap() {
191193
logger: otelLogger,
192194
tracer: tracer,
193195
level: logLevels.includes(configLogLevel as any) ? (configLogLevel as LogLevel) : "info",
196+
maxAttributeCount: OTEL_LOG_ATTRIBUTE_COUNT_LIMIT,
194197
});
195198

196199
logger.setGlobalTaskLogger(otelTaskLogger);

packages/core/src/v3/consoleInterceptor.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export class ConsoleInterceptor {
1111
constructor(
1212
private readonly logger: logsAPI.Logger,
1313
private readonly sendToStdIO: boolean,
14-
private readonly interceptingDisabled: boolean
14+
private readonly interceptingDisabled: boolean,
15+
private readonly maxAttributeCount?: number
1516
) {}
1617

1718
// Intercept the console and send logs to the OpenTelemetry logger
@@ -92,7 +93,10 @@ export class ConsoleInterceptor {
9293
severityNumber,
9394
severityText,
9495
body: getLogMessage(parsed.value, severityText),
95-
attributes: { ...this.#getAttributes(severityNumber), ...flattenAttributes(parsed.value) },
96+
attributes: {
97+
...this.#getAttributes(severityNumber),
98+
...flattenAttributes(parsed.value, undefined, this.maxAttributeCount),
99+
},
96100
timestamp,
97101
});
98102

packages/core/src/v3/limits.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ function getOtelEnvVarLimit(key: string, defaultValue: number) {
1313

1414
export const OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT = getOtelEnvVarLimit(
1515
"TRIGGER_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT",
16-
256
16+
1024
1717
);
1818
export const OTEL_LOG_ATTRIBUTE_COUNT_LIMIT = getOtelEnvVarLimit(
1919
"TRIGGER_OTEL_LOG_ATTRIBUTE_COUNT_LIMIT",
20-
256
20+
1024
2121
);
2222
export const OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT = getOtelEnvVarLimit(
2323
"TRIGGER_OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT",
@@ -51,10 +51,6 @@ export function imposeAttributeLimits(attributes: Attributes): Attributes {
5151
continue;
5252
}
5353

54-
if (Object.keys(newAttributes).length >= OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT) {
55-
break;
56-
}
57-
5854
newAttributes[key] = value;
5955
}
6056

packages/core/src/v3/logger/taskLogger.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type TaskLoggerConfig = {
1616
logger: Logger;
1717
tracer: TriggerTracer;
1818
level: LogLevel;
19+
maxAttributeCount?: number;
1920
};
2021

2122
export type TraceOptions = Prettify<
@@ -78,7 +79,12 @@ export class OtelTaskLogger implements TaskLogger {
7879
severityNumber: SeverityNumber,
7980
properties?: Record<string, unknown>
8081
) {
81-
let attributes: Attributes = { ...flattenAttributes(safeJsonProcess(properties)) };
82+
let attributes: Attributes = {};
83+
84+
if (properties) {
85+
// Use flattenAttributes directly - it now handles all non-JSON friendly values efficiently
86+
attributes = flattenAttributes(properties, undefined, this._config.maxAttributeCount);
87+
}
8288

8389
const icon = iconStringForSeverity(severityNumber);
8490
if (icon !== undefined) {
@@ -136,23 +142,3 @@ export class NoopTaskLogger implements TaskLogger {
136142
return {} as Span;
137143
}
138144
}
139-
140-
function safeJsonProcess(value?: Record<string, unknown>): Record<string, unknown> | undefined {
141-
try {
142-
return JSON.parse(JSON.stringify(value, jsonErrorReplacer));
143-
} catch {
144-
return value;
145-
}
146-
}
147-
148-
function jsonErrorReplacer(key: string, value: unknown) {
149-
if (value instanceof Error) {
150-
return {
151-
name: value.name,
152-
message: value.message,
153-
stack: value.stack,
154-
};
155-
}
156-
157-
return value;
158-
}

0 commit comments

Comments
 (0)