Distrito Telefónica. Hub de Innovación y Talento
Activación de la monitorización del rendimiento de Sentry
import * as Sentry from "@sentry/nextjs";
Sentry.init({
tracesSampleRate: 1.0,
});
// sentry.client.config.ts
import * as Sentry from "@sentry/nextjs";
import getConfig from 'next/config';
Sentry.init({
tracesSampleRate: 0.1,
});
Pantalla principal de la pestaña de rendimiento en Sentry
Hacer visibles los parámetros de las cadenas de consulta
Lista de transacciones
/ sentry.client.config.ts
import * as Sentry from "@sentry/nextjs";
import {beforeNavigate} from './utils/sentry'
Sentry.init({
// ...
integrations: [
new Sentry.BrowserTracing({
beforeNavigate,
}),
],
});// utils/sentry.ts
// Sentry strips query parameters from the url on navigation events. We get them back by listening to next router. https://github.com/getsentry/sentry-javascript/pull/8278
let currentNavigationPath: string =
typeof window !== 'undefined' ? window.location.pathname + window.location.search : '';
let nextNavigationPath: string =
typeof window !== 'undefined' ? window.location.pathname + window.location.search : '';
Router.events.on('routeChangeStart', (navigationTarget: string) => {
nextNavigationPath = navigationTarget;
});
export const beforeNavigate = (context: TransactionContext): TransactionContext => {
context.name = removeJwtFromQueryParams(nextNavigationPath);
if (typeof context.tags?.from === 'string') {
context.tags.from = removeJwtFromQueryParams(currentNavigationPath);
}
currentNavigationPath = nextNavigationPath; // Once we have completed the navigation, we can update the current path
return context;
};
Parámetros de URL variables
Breadcrumbs
// sentry.client.config.ts
import * as Sentry from "@sentry/nextjs";
import {beforeBreadcrumb, beforeSendTransaction} from './utils/sentry'
Sentry.init({
// ...
beforeBreadcrumb,
beforeSendTransaction,
});
export const beforeBreadcrumb = (breadcrumb: Sentry.Breadcrumb): Sentry.Breadcrumb => {
if (breadcrumb.category === 'navigation') {
breadcrumb.data.from = removeJwtFromQueryParams(breadcrumb.data.from);
breadcrumb.data.to = removeJwtFromQueryParams(breadcrumb.data.to);
return breadcrumb;
}
if (breadcrumb.category === 'fetch') {
breadcrumb.data.url = removeJwtFromApiPath(breadcrumb.data.url);
return breadcrumb;
}
return breadcrumb;
};
export const beforeSendTransaction = (transaction: TransactionEvent): TransactionEvent => {
if (transaction?.request?.url) {
transaction.request.url = removeJwtFromQueryParams(transaction.request.url);
}
if (transaction.request?.headers?.Referer) {
transaction.request.headers.Referer = removeJwtFromQueryParams(transaction.request.headers.Referer);
}
transaction.spans = transaction.spans?.map(parameterizeHttpRequests);
return transaction;
};
const parameterizeHttpRequests = (span: Span): Span => {
if (span.op === 'http.client') {
if (span.data.url) {
span.data.url = removeJwtFromApiPath(span.data.url);
}
if (span.description) {
span.description = removeJwtFromApiPath(span.description);
}
}
return span;
};
const removeJwtFromQueryParams = (urlPath: string) => {
if (urlPath.includes('?')) {
const [path, queryString] = urlPath.split('?');
const searchParams = new URLSearchParams(queryString);
searchParams.set('jwt', '-');
return `${path}?${searchParams.toString()}`;
}
return urlPath;
};
Resultado final
Resultado final de la pestaña principal de rendimiento
Detalles de la transacción
Detalles del evento