Преглед изворни кода

Added cohere session url to sentry event tags if cohere is present

jnfrati пре 4 година
родитељ
комит
47cc46a25c

+ 36 - 8
dashboard/src/shared/error_handling/logger.ts

@@ -1,19 +1,47 @@
 import * as Sentry from "@sentry/react";
+import Cohere from "cohere-js";
+import { isEmpty } from "lodash";
 
-type LogFunction = (error: Error) => void;
+type LogFunction = (error: Error, tags?: { [key: string]: string }) => void;
 type LogFunctions = {
   [key in Sentry.Severity]: LogFunction;
 };
 
-const logFunctionBuilder = (scope: string, severity: Sentry.Severity) => (
-  error: Error
+type LogFunctionBuilder = (
+  scope: string,
+  severity: Sentry.Severity
+) => LogFunction;
+
+const logFunctionBuilder: LogFunctionBuilder = (scope, severity) => (
+  error,
+  tags
 ) => {
-  Sentry.withScope((sentryScope) => {
-    sentryScope.setTag("scope", scope);
-    sentryScope.setLevel(severity);
+  if (process.env.ENABLE_COHERE) {
+    Cohere.getSessionUrl((sessionUrl) => {
+      Sentry.withScope((sentryScope) => {
+        sentryScope.setTag("scope", scope);
+        sentryScope.setTag("cohere_link", sessionUrl);
+        sentryScope.setLevel(severity);
+
+        if (!isEmpty(tags)) {
+          sentryScope.setTags(tags);
+        }
+
+        Sentry.captureException(error);
+      });
+    });
+  } else {
+    Sentry.withScope((sentryScope) => {
+      sentryScope.setTag("scope", scope);
+      sentryScope.setLevel(severity);
+
+      if (!isEmpty(tags)) {
+        sentryScope.setTags(tags);
+      }
 
-    Sentry.captureException(error);
-  });
+      Sentry.captureException(error);
+    });
+  }
 };
 
 function buildLogger(scope: string = "global") {

+ 19 - 0
dashboard/src/shared/error_handling/sentry/setup.ts

@@ -1,5 +1,6 @@
 import * as Sentry from "@sentry/react";
 import { Integrations } from "@sentry/tracing";
+import Cohere from "cohere-js";
 import CohereSentry from "cohere-sentry";
 
 const SENTRY_DSN = process.env.SENTRY_DSN;
@@ -19,4 +20,22 @@ export const SetupSentry = () => {
     // Check out https://docs.sentry.io/platforms/javascript/guides/react/configuration/sampling/ for a more refined sample rate
     tracesSampleRate: 1,
   });
+
+  if (process.env.ENABLE_COHERE) {
+    const sessionUrlListener = (sessionUrl: string) => {
+      Sentry.configureScope((scope) => {
+        scope.addEventProcessor((event) => {
+          event.tags = {
+            ...event.tags,
+            cohere_link: `${sessionUrl}${
+              event.timestamp ? `?ts=${event.timestamp * 1000}` : ""
+            }`,
+          };
+
+          return event;
+        });
+      });
+    };
+    Cohere.addSessionUrlListener(sessionUrlListener);
+  }
 };