From 560491d4102db645baa5fa7d4255d5bb7fef515c Mon Sep 17 00:00:00 2001
From: Bucky Schwarz <d.w.schwarz@gmail.com>
Date: Thu, 18 Jun 2020 08:05:35 -0700
Subject: [PATCH] feat: add error metric writing method. call it during
 honeybadger errors

---
 ui/src/shared/utils/errors.ts | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/ui/src/shared/utils/errors.ts b/ui/src/shared/utils/errors.ts
index 02d39d92b8..7ec7582c6d 100644
--- a/ui/src/shared/utils/errors.ts
+++ b/ui/src/shared/utils/errors.ts
@@ -3,6 +3,7 @@ import HoneyBadger from 'honeybadger-js'
 import {CLOUD, GIT_SHA} from 'src/shared/constants'
 
 import {getUserFlags} from 'src/shared/utils/featureFlag'
+import {reportPoints, PointTags, PointFields} from 'src/cloud/apis/reporting'
 
 if (CLOUD) {
   HoneyBadger.configure({
@@ -12,6 +13,19 @@ if (CLOUD) {
   })
 }
 
+export const reportSingleErrorMetric = (
+  tags: PointTags = {},
+  fields: PointFields = {},
+  measurement = 'ui_error'
+) => {
+  if (CLOUD) {
+    const points = {
+      points: [{measurement, tags, fields: {errorCount: 1, ...fields}}],
+    }
+    reportPoints(points)
+  }
+}
+
 // See https://docs.honeybadger.io/lib/javascript/guides/reporting-errors.html#additional-options
 interface HoneyBadgerAdditionalOptions {
   component?: string
@@ -44,6 +58,15 @@ export const reportError = (
 
   if (CLOUD) {
     HoneyBadger.notify(error, {context, ...options})
+
+    let errorType = 'generic (untagged) error'
+    if (options.name) {
+      errorType = options.name
+    } else if (options.component) {
+      errorType = options.component
+    }
+
+    reportSingleErrorMetric({errorType})
   } else {
     const honeyBadgerContext = (HoneyBadger as any).context
     /* eslint-disable no-console */