Skip to content

feat(core): Create template attributes in consoleLoggingIntegration - #17703

Merged
AbhiPrasad merged 2 commits into
developfrom
abhi-template-in-console
Sep 19, 2025
Merged

feat(core): Create template attributes in consoleLoggingIntegration#17703
AbhiPrasad merged 2 commits into
developfrom
abhi-template-in-console

Conversation

@AbhiPrasad

Copy link
Copy Markdown
Contributor

ref #16737

Right now if users use console.log like so:

console.log("here","is","my","log","statement");

The console logging integration will emit a log with log message "here is my log statement".

Some users would like it if we automatically paramaterized this into a template, given there are separate arguments being sent into the logging statement. So the above log statement would generate

{
"sentry.message.template": "here {} {} {} {}",
"sentry.message.parameter.0": "is",
"sentry.message.parameter.1": "my",
"sentry.message.parameter.2": "log",
"sentry.message.parameter.3": "statement",
}

This paramaterization is what this PR does, which provides a much better user experience.

One edge case that we need to watch out for is console substitution patterns like %s, %d, %i, %f, %o, %O, %c. Read more about this in the MDN docs. When encountering a console substitution pattern in the string, we elect to not generate string templates, as parsing the string to evaluate it gets too complicated client side.

@AbhiPrasad
AbhiPrasad requested a review from a teamSeptember 18, 2025 20:24
@AbhiPrasadAbhiPrasad self-assigned this Sep 18, 2025
@AbhiPrasad
AbhiPrasad requested review from RulaKhaled and mydea and removed request for a teamSeptember 18, 2025 20:24
@AbhiPrasadAbhiPrasad changed the title feat(core): Create template attributes in consoleLoggingIntegrationfeat(core): Create template attributes in consoleLoggingIntegrationSep 18, 2025
@github-actions

github-actionsBot commented Sep 18, 2025

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,813-9,201-4%
GET With Sentry1,37816%1,325+4%
GET With Sentry (error only)6,14870%6,064+1%
POST Baseline1,189-1,188+0%
POST With Sentry51343%528-3%
POST With Sentry (error only)1,07290%1,046+2%
MYSQL Baseline3,336-3,332+0%
MYSQL With Sentry47214%405+17%
MYSQL With Sentry (error only)2,72182%2,720+0%

View base workflow run

@Lms24Lms24 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can see how this is useful. Sounds like a good change to me!

@AbhiPrasad
AbhiPrasad enabled auto-merge (squash) September 19, 2025 15:48
});

return attributes;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: Console Template Attributes Formatting Issues

The createConsoleTemplateAttributes function has two issues. The sentry.message.template attribute includes an unintended trailing space when no followingArgs are present. Additionally, sentry.message.parameter attributes are stored as raw values instead of structured objects with value/type, and null/undefined values are not converted to their expected string representations.

Fix in CursorFix in Web

@AbhiPrasad
AbhiPrasad merged commit 0e0c711 into developSep 19, 2025
189 checks passed
@AbhiPrasad
AbhiPrasad deleted the abhi-template-in-console branch September 19, 2025 16:06
AbhiPrasad added a commit to getsentry/sentry-docs that referenced this pull request Sep 25, 2025
s1gr1d added a commit that referenced this pull request Mar 2, 2026
…ect as searchable attributes (#19534)
Aligns the Consola integration so object-first logs are structured and
fallback logs get template + parameters.
## Universal principle
- **Object-first** (first argument is a plain object): object keys
become log attributes, second argument (if string) is the message,
remaining arguments → sentry.message.parameter.{0, 1, 2, ...}.
- **Fallback** (first argument is not an object): message =
formatted(all args), args[1:] → sentry.message.template and
sentry.message.parameter.{0, 1, 2, ...} (same as console integration).
## Consola-specific behavior
- **Consola-merged**: For `consola.log({ message: "x", userId, action
})` Consola passes `args: ["x"]` and spreads the rest on the log object.
We detect this (single string in args + extra keys on logObj) and treat
it as one logical object: message = `args[0]`, attributes = extra keys.
- **Object-first** now applies to any plain object as first arg
(including objects with message or args keys), so e.g.
`consola.log.raw({ message: "raw-hello" })` produces attributes from the
object and an empty message.
- **Fallback** uses the same template/parameter pattern as the console
integration (no extraction of objects into top-level attributes; all
post-first args go into the formatted message and
`sentry.message.parameter.*`).
## Example
```ts
// Object-first
consola.log({ userId: 123, action: "login" }, "User logged in");
// → message: "User logged in", attributes: { userId: 123, action: "login" }
// With extra parameters
consola.log({ userId: 123 }, "User action", requestId, timestamp);
// → message: "User action", userId: 123, sentry.message.parameter.0: requestId, .1: timestamp
// Fallback (non-object first)
consola.log("Legacy log", { data: 1 }, 123);
// → message: "Legacy log {\"data\":1} 123", sentry.message.template: "Legacy log {} {}", sentry.message.parameter.0/1
```
Console String substitutions are not added as a template attribute
because parsing is too complicated on the client-side (see here:
#17703)
Closes#18593
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@AbhiPrasad@Lms24