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

feat(utils): Add parameterize function #9145

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
db66295
Added parametrize function
AleshaOleg Sep 29, 2023
e15d5f1
Populate logentry parameter if it's exist for eventFromMessage functions
AleshaOleg Oct 11, 2023
7b27a1e
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg Oct 11, 2023
e4c4d4c
Renamed parametrize to parameterize and moved ParameterizedString typ…
AleshaOleg Oct 13, 2023
8fa0552
Added integration tests
AleshaOleg Oct 13, 2023
d83411a
Fixing tests and improvements
AleshaOleg Oct 13, 2023
70e6864
isParameterized -> isParameterizedString in docs
AleshaOleg Oct 13, 2023
cbd0805
Fixed lint errors
AleshaOleg Oct 14, 2023
1020b14
Minor optimization
AleshaOleg Oct 14, 2023
99c4fbb
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg Oct 14, 2023
3cbe665
Minor improvements, reverted prisma update
AleshaOleg Oct 16, 2023
de6e723
Fixed typo
AleshaOleg Oct 16, 2023
28758b6
Fixed typo
AleshaOleg Oct 16, 2023
17a4bbd
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg Oct 16, 2023
2c3393f
prisma@3.12.0 -> prisma@^3.12.0
AleshaOleg Oct 16, 2023
f6347a3
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg Oct 16, 2023
cefa81c
prisma@^3.12.0 -> prisma@3.12.0
AleshaOleg Oct 17, 2023
e80f23a
prisma@3.12.0 -> prisma@^3.12.0
AleshaOleg Oct 17, 2023
3b67901
Merge branch 'develop' of github.com:getsentry/sentry-javascript into…
AleshaOleg Oct 17, 2023
2789d04
prisma@3.12.0 -> prisma@^3.12.0
AleshaOleg Oct 17, 2023
3e3b441
Merge branch 'develop' of github.com:AleshaOleg/sentry-javascript int…
AleshaOleg Nov 30, 2023
0d67997
Fixed Biome errors
AleshaOleg Dec 1, 2023
dcd1549
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg Dec 21, 2023
ca8a58a
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg Dec 30, 2023
8c8bf10
Skipping test for some bundles
AleshaOleg Dec 31, 2023
a631c55
Prevent running test for tracing builds
AleshaOleg Jan 2, 2024
aa3c9d1
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg Jan 2, 2024
de11d01
Skipping all test for bundle builds
AleshaOleg Jan 4, 2024
5aeedf7
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg Jan 4, 2024
f68be1f
Test migration to dev-packages folder
AleshaOleg Jan 4, 2024
5681add
Merge branch 'develop' of https://github.com/getsentry/sentry-javascr…
AleshaOleg Jan 4, 2024
4b0c53c
Merge branch 'develop' into feature/added-parametrize-function
AleshaOleg Jan 5, 2024
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
17 changes: 13 additions & 4 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,11 @@ export function eventFromUnknownInput(
*/
export function eventFromString(
stackParser: StackParser,
input: string,
input: string & { __sentry_template_string__?: string; __sentry_template_values__?: string[] },
Copy link
Member

Choose a reason for hiding this comment

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

Can we extract ParemeterizedString from this PR as a type export it from @sentry/types? Then we can reuse it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried to replace it everywhere where I think it makes sense. Thanks for the tip!

syntheticException?: Error,
attachStacktrace?: boolean,
): Event {
const event: Event = {
message: input,
};
const event: Event = {};

if (attachStacktrace && syntheticException) {
const frames = parseStackFrames(stackParser, syntheticException);
Expand All @@ -282,6 +280,17 @@ export function eventFromString(
}
}

const { __sentry_template_string__, __sentry_template_values__ } = input;

if (__sentry_template_string__ && __sentry_template_values__) {
event.logentry = {
message: __sentry_template_string__,
params: __sentry_template_values__,
};
return event;
}

event.message = input;
return event;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import type { User } from './user';
export interface Event {
event_id?: string;
message?: string;
logentry?: {
message?: string;
params?: string[];
};
timestamp?: number;
start_timestamp?: number;
// eslint-disable-next-line deprecation/deprecation
Expand Down
16 changes: 13 additions & 3 deletions packages/utils/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function eventFromUnknownInput(
*/
export function eventFromMessage(
stackParser: StackParser,
message: string,
input: string & { __sentry_template_string__?: string; __sentry_template_values__?: string[] },
// eslint-disable-next-line deprecation/deprecation
level: Severity | SeverityLevel = 'info',
hint?: EventHint,
Expand All @@ -128,7 +128,6 @@ export function eventFromMessage(
const event: Event = {
event_id: hint && hint.event_id,
level,
message,
};

if (attachStacktrace && hint && hint.syntheticException) {
Expand All @@ -137,13 +136,24 @@ export function eventFromMessage(
event.exception = {
values: [
{
value: message,
value: input,
stacktrace: { frames },
},
],
};
}
}

const { __sentry_template_string__, __sentry_template_values__ } = input;

if (__sentry_template_string__ && __sentry_template_values__) {
event.logentry = {
message: __sentry_template_string__,
params: __sentry_template_values__,
};
return event;
}

event.message = input;
return event;
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export * from './url';
export * from './userIntegrations';
export * from './cache';
export * from './eventbuilder';
export * from './parametrize';
export * from './anr';
20 changes: 20 additions & 0 deletions packages/utils/src/parametrize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export interface ParamerizedString extends String {
__sentry_template_string__: string;
__sentry_template_values__: string[];
}

/**
* Tagged template function which returns paramaterized representation of the message
* For example: parametrize`This is a log statement with ${x} and ${y} params`, would return:
* "__sentry_template_string__": "My raw message with interpreted strings like %s",
* "__sentry_template_values__": ["this"]
* @param strings An array of string values splitted between expressions
* @param values Expressions extracted from template string
* @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties
*/
export function parametrize(strings: TemplateStringsArray, ...values: string[]): ParamerizedString {
const formatted = new String(String.raw(strings, ...values)) as ParamerizedString;
formatted.__sentry_template_string__ = strings.join('\x00').replace(/%/g, '%%').replace(/\0/g, '%s');
formatted.__sentry_template_values__ = values;
return formatted;
}
26 changes: 26 additions & 0 deletions packages/utils/test/parametrize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ParamerizedString } from '../src/parametrize';
import { parametrize } from '../src/parametrize';

describe('parametrize()', () => {
test('works with empty string', () => {
const string = new String() as ParamerizedString;
string.__sentry_template_string__ = '';
string.__sentry_template_values__ = [];

const formatted = parametrize``;
expect(formatted.__sentry_template_string__).toEqual('');
expect(formatted.__sentry_template_values__).toEqual([]);
});

test('works as expected with template literals', () => {
const x = 'first';
const y = 'second';
const string = new String() as ParamerizedString;
string.__sentry_template_string__ = 'This is a log statement with %s and %s params';
string.__sentry_template_values__ = ['first', 'second'];

const formatted = parametrize`This is a log statement with ${x} and ${y} params`;
expect(formatted.__sentry_template_string__).toEqual(string.__sentry_template_string__);
expect(formatted.__sentry_template_values__).toEqual(string.__sentry_template_values__);
});
});