Просмотр исходного кода

feat(js): Add `endpoint` tag to all request errors (#50910)

This is a follow-up to https://github.com/getsentry/sentry/pull/49558, which added an `endpoint` tag to all error events with a request error as their cause. In this PR, the same tag is added to events whose primary error is a request error, by using a helper function in `beforeSend`.
Katie Byers 1 год назад
Родитель
Сommit
5caebfde44
2 измененных файлов с 66 добавлено и 1 удалено
  1. 53 1
      static/app/bootstrap/initializeSdk.spec.tsx
  2. 13 0
      static/app/bootstrap/initializeSdk.tsx

+ 53 - 1
static/app/bootstrap/initializeSdk.spec.tsx

@@ -1,6 +1,10 @@
 import {ERROR_MAP as origErrorMap} from 'sentry/utils/requestError/requestError';
 
-import {isEventWithFileUrl, isFilteredRequestErrorEvent} from './initializeSdk';
+import {
+  addEndpointTagToRequestError,
+  isEventWithFileUrl,
+  isFilteredRequestErrorEvent,
+} from './initializeSdk';
 
 const ERROR_MAP = {
   ...origErrorMap,
@@ -135,3 +139,51 @@ describe('isEventWithFileUrl', () => {
     expect(isEventWithFileUrl(event)).toBeFalsy();
   });
 });
+
+describe('addEndpointTagToRequestError', () => {
+  it('adds `endpoint` tag to events with matching message`', () => {
+    const event = {
+      exception: {
+        values: [{type: 'RequestError', value: 'GET /dogs/are/great/ 500'}],
+      },
+      tags: {},
+    };
+
+    addEndpointTagToRequestError(event);
+
+    expect(event.tags).toEqual({
+      endpoint: 'GET /dogs/are/great/',
+    });
+  });
+
+  it("doesn't add `endpoint` tag to events with non-matching message", () => {
+    const nonmatchingMessages = [
+      'RequestError with no endpoint for some reason',
+      'Some other stuff is wrong with endpoint /dogs/are/great/',
+      'This error has nothing to do with requests or endpoints at all',
+    ];
+
+    for (const msg of nonmatchingMessages) {
+      const event = {
+        exception: {
+          values: [{type: 'RequestError', value: msg}],
+        },
+        tags: {},
+      };
+
+      addEndpointTagToRequestError(event);
+
+      expect(event.tags).toEqual({});
+    }
+  });
+
+  it("doesn't add `endpoint` tag to events with no exception", () => {
+    const event = {
+      tags: {},
+    };
+
+    addEndpointTagToRequestError(event);
+
+    expect(event.tags).toEqual({});
+  });
+});

+ 13 - 0
static/app/bootstrap/initializeSdk.tsx

@@ -172,6 +172,7 @@ export function initializeSdk(config: Config, {routes}: {routes?: Function} = {}
       }
 
       handlePossibleUndefinedResponseBodyErrors(event);
+      addEndpointTagToRequestError(event);
 
       return event;
     },
@@ -292,3 +293,15 @@ function handlePossibleUndefinedResponseBodyErrors(event: Event): void {
       : ['UndefinedResponseBodyError as cause error'];
   }
 }
+
+export function addEndpointTagToRequestError(event: Event): void {
+  const errorMessage = event.exception?.values?.[0].value || '';
+
+  // The capturing group here turns `GET /dogs/are/great 500` into just `GET /dogs/are/great`
+  const requestErrorRegex = new RegExp('^([A-Za-z]+ (/[^/]+)+/) \\d+$');
+  const messageMatch = requestErrorRegex.exec(errorMessage);
+
+  if (messageMatch) {
+    event.tags = {...event.tags, endpoint: messageMatch[1]};
+  }
+}