Skip to content

ref(ourlogs): Fix parameterize types for fmt #15600

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { logger } from './utils-hoist/logger';
import { uuid4 } from './utils-hoist/misc';
import { timestampInSeconds } from './utils-hoist/time';
import { GLOBAL_OBJ } from './utils-hoist/worldwide';
import { parameterize } from './utils/parameterize';
import { parameterizeAny } from './utils/parameterize';
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';

Expand Down Expand Up @@ -436,7 +436,7 @@ export const _experiment_log = {
* Sentry._experiment_log.fmt`This is a log statement with ${x} and ${y} params`
* ```
*/
fmt: parameterize,
fmt: parameterizeAny<string | number | null | object>,

/**
* A flexible utility to record a log with a custom level and send it to sentry.
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export { hasTracingEnabled } from './utils/hasSpansEnabled';
export { hasSpansEnabled } from './utils/hasSpansEnabled';
export { isSentryRequestUrl } from './utils/isSentryRequestUrl';
export { handleCallbackErrors } from './utils/handleCallbackErrors';
export { parameterize } from './utils/parameterize';
export { parameterize } from './utils/parameterize';

export { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress';
export {
convertSpanLinksForEnvelope,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types-hoist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export type {

export type { BrowserClientReplayOptions, BrowserClientProfilingOptions } from './browseroptions';
export type { CheckIn, MonitorConfig, FinishedCheckIn, InProgressCheckIn, SerializedCheckIn } from './checkin';
export type { ParameterizedString } from './parameterize';
export type { ParameterizedString, ParameterizedAnyValueString } from './parameterize';
export type { ContinuousProfiler, ProfilingIntegration, Profiler } from './profiling';
export type { ViewHierarchyData, ViewHierarchyWindow } from './view-hierarchy';
export type { LegacyCSPReport } from './csp';
6 changes: 4 additions & 2 deletions packages/core/src/types-hoist/parameterize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type ParameterizedString = string & {
export type ParameterizedAnyValueString<T> = string & {
__sentry_template_string__?: string;
__sentry_template_values__?: string[];
__sentry_template_values__?: T[];
};

export type ParameterizedString = ParameterizedAnyValueString<string>;
17 changes: 14 additions & 3 deletions packages/core/src/utils/parameterize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ParameterizedString } from '../types-hoist';
import type { ParameterizedAnyValueString } from '../types-hoist';

/**
* Tagged template function which returns parameterized representation of the message
Expand All @@ -9,8 +9,19 @@ import type { ParameterizedString } from '../types-hoist';
* @param values Expressions extracted from template string
* @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties
*/
export function parameterize(strings: TemplateStringsArray, ...values: string[]): ParameterizedString {
const formatted = new String(String.raw(strings, ...values)) as ParameterizedString;
export const parameterize = parameterizeAny<string>;

/**
* Tagged template function which returns parameterized representation of the message
* For example: parameterize`This is a log statement with ${x} and ${y} params`, would return:
* "__sentry_template_string__": 'This is a log statement with %s and %s params',
* "__sentry_template_values__": ['first', 'second']
* @param strings An array of string values splitted between expressions
* @param values Expressions extracted from template string, types provided by the generic.
* @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties
*/
export function parameterizeAny<T>(strings: TemplateStringsArray, ...values: T[]): ParameterizedAnyValueString<T> {
const formatted = new String(String.raw(strings, ...values)) as ParameterizedAnyValueString<T>;
formatted.__sentry_template_string__ = strings.join('\x00').replace(/%/g, '%%').replace(/\0/g, '%s');
formatted.__sentry_template_values__ = values;
return formatted;
Expand Down
Loading