From 1ce8b46ee57f80cdb0d54e438f09706feac63a65 Mon Sep 17 00:00:00 2001
From: Jenn Mueng <30991498+jennmueng@users.noreply.github.com>
Date: Tue, 8 Jun 2021 13:08:41 +0700
Subject: [PATCH] feat: Add event origin tags and also move sdkInfo into an
 integration (#47)

---
 src/index.ts                    | 34 +---------------------------
 src/integrations/eventorigin.ts | 28 +++++++++++++++++++++++
 src/integrations/index.ts       |  2 ++
 src/integrations/sdkinfo.ts     | 39 +++++++++++++++++++++++++++++++++
 src/sdk.ts                      |  3 +++
 5 files changed, 73 insertions(+), 33 deletions(-)
 create mode 100644 src/integrations/eventorigin.ts
 create mode 100644 src/integrations/index.ts
 create mode 100644 src/integrations/sdkinfo.ts

diff --git a/src/index.ts b/src/index.ts
index 6164edfc..108cc79a 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -16,7 +16,6 @@ export {
   User,
 } from '@sentry/types';
 
-import { addGlobalEventProcessor } from '@sentry/core';
 export {
   addGlobalEventProcessor,
   addBreadcrumb,
@@ -37,37 +36,6 @@ export {
   withScope,
 } from '@sentry/core';
 
-import { SDK_NAME, SDK_VERSION } from './version';
-
+export { SDK_NAME, SDK_VERSION } from './version';
 export { CapacitorOptions } from './options';
-
 export { init, nativeCrash } from './sdk';
-
-/**
- * Adds the SDK info. Make sure this is called after @sentry/capacitor's so this is the top-level SDK.
- */
-function createCapacitorEventProcessor(): void {
-  if (addGlobalEventProcessor) {
-    addGlobalEventProcessor(event => {
-      event.platform = event.platform || 'javascript';
-      event.sdk = {
-        ...event.sdk,
-        name: SDK_NAME,
-        packages: [
-          ...((event.sdk && event.sdk.packages) || []),
-          {
-            name: 'npm:@sentry/capacitor',
-            version: SDK_VERSION,
-          },
-        ],
-        version: SDK_VERSION,
-      };
-
-      return event;
-    });
-  }
-}
-
-createCapacitorEventProcessor();
-
-export { SDK_NAME, SDK_VERSION };
diff --git a/src/integrations/eventorigin.ts b/src/integrations/eventorigin.ts
new file mode 100644
index 00000000..cd8f6623
--- /dev/null
+++ b/src/integrations/eventorigin.ts
@@ -0,0 +1,28 @@
+import { EventProcessor, Integration } from '@sentry/types';
+
+/** Default EventOrigin instrumentation */
+export class EventOrigin implements Integration {
+  /**
+   * @inheritDoc
+   */
+  public static id: string = 'EventOrigin';
+
+  /**
+   * @inheritDoc
+   */
+  public name: string = EventOrigin.id;
+
+  /**
+   * @inheritDoc
+   */
+  public setupOnce(addGlobalEventProcessor: (e: EventProcessor) => void): void {
+    addGlobalEventProcessor(event => {
+      event.tags = event.tags ?? {};
+
+      event.tags['event.origin'] = 'javascript';
+      event.tags['event.environment'] = 'javascript';
+
+      return event;
+    });
+  }
+}
diff --git a/src/integrations/index.ts b/src/integrations/index.ts
new file mode 100644
index 00000000..b98d0aaf
--- /dev/null
+++ b/src/integrations/index.ts
@@ -0,0 +1,2 @@
+export { EventOrigin } from './eventorigin';
+export { SdkInfo } from './sdkinfo';
diff --git a/src/integrations/sdkinfo.ts b/src/integrations/sdkinfo.ts
new file mode 100644
index 00000000..f1cd7ea4
--- /dev/null
+++ b/src/integrations/sdkinfo.ts
@@ -0,0 +1,39 @@
+import { EventProcessor, Integration } from '@sentry/types';
+
+import { SDK_NAME, SDK_VERSION } from '../version';
+
+/** Default SdkInfo instrumentation */
+export class SdkInfo implements Integration {
+  /**
+   * @inheritDoc
+   */
+  public static id: string = 'SdkInfo';
+
+  /**
+   * @inheritDoc
+   */
+  public name: string = SdkInfo.id;
+
+  /**
+   * @inheritDoc
+   */
+  public setupOnce(addGlobalEventProcessor: (e: EventProcessor) => void): void {
+    addGlobalEventProcessor(event => {
+      event.platform = event.platform || 'javascript';
+      event.sdk = {
+        ...event.sdk,
+        name: SDK_NAME,
+        packages: [
+          ...((event.sdk && event.sdk.packages) || []),
+          {
+            name: 'npm:@sentry/capacitor',
+            version: SDK_VERSION,
+          },
+        ],
+        version: SDK_VERSION,
+      };
+
+      return event;
+    });
+  }
+}
diff --git a/src/sdk.ts b/src/sdk.ts
index 18b2ceaa..ecb28f09 100644
--- a/src/sdk.ts
+++ b/src/sdk.ts
@@ -6,6 +6,7 @@ import {
 import { Hub, makeMain } from '@sentry/hub';
 import { RewriteFrames } from '@sentry/integrations';
 
+import { EventOrigin, SdkInfo } from './integrations';
 import { CapacitorOptions } from './options';
 import { CapacitorScope } from './scope';
 import { NativeTransport } from './transports/native';
@@ -61,6 +62,8 @@ export function init<O>(
         return frame;
       },
     }),
+    new SdkInfo(),
+    new EventOrigin(),
   ];
 
   if (typeof finalOptions.enableNative === 'undefined') {