Skip to content

Commit 75b3607

Browse files
authored
try out honeycomb frontend monitoring (#217)
* try out honeycomb frontend monitoring * refactor: extract environment handling
1 parent 6fa08d0 commit 75b3607

File tree

6 files changed

+637
-31
lines changed

6 files changed

+637
-31
lines changed

packages/viewer/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"validate": "svelte-check"
1818
},
1919
"dependencies": {
20+
"@honeycombio/opentelemetry-web": "^0.15.0",
21+
"@opentelemetry/auto-instrumentations-web": "^0.46.0",
2022
"@pi-base/core": "workspace:*",
2123
"@sentry/browser": "^7.93.0",
2224
"bootstrap": "^4.6.2",

packages/viewer/src/environment.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export type Environment = 'production' | 'deploy-preview' | 'dev'
2+
3+
export function forHost(host: string): Environment {
4+
if (['topology.pi-base.org', 'topology.pages.dev'].includes(host)) {
5+
return 'production'
6+
}
7+
8+
if (host.includes('pages.dev')) {
9+
return 'deploy-preview'
10+
}
11+
12+
return 'dev'
13+
}

packages/viewer/src/errors.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import * as Sentry from '@sentry/browser'
22
import { build, sentryIngest } from '@/constants'
3+
import type { Environment } from './environment'
34

45
type Meta = Record<string, unknown>
56

67
export type Handler = (error: Error, meta?: Meta) => void
78

8-
export type Environment = 'production' | 'deploy-preview' | 'dev'
9-
109
export function log(): Handler {
1110
return function handle(error: Error, meta: Meta = {}) {
1211
console.error(error, meta)
@@ -15,14 +14,14 @@ export function log(): Handler {
1514

1615
export function sentry({
1716
dsn = sentryIngest,
18-
environment,
17+
env,
1918
}: {
2019
dsn?: string
21-
environment: Environment
20+
env: Environment
2221
}): Handler {
2322
const release = `pi-base@${build.version}`
2423

25-
Sentry.init({ dsn, release, environment })
24+
Sentry.init({ dsn, release, environment: env })
2625

2726
return function handle(error: Error, meta: Meta = {}) {
2827
Sentry.withScope(scope => {

packages/viewer/src/honeycomb.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { HoneycombWebSDK } from '@honeycombio/opentelemetry-web'
2+
import { getWebAutoInstrumentations } from '@opentelemetry/auto-instrumentations-web'
3+
import type { Environment } from '@/environment'
4+
5+
// These are frontend-embedded ingest-only keys that are safe to expose
6+
const apiKeys: Record<Environment, string | null> = {
7+
'deploy-preview':
8+
'hcaik_01jqmng6xbp8wnh1fg2srx4cbpmbyk4kb7kq8ngm8stx5ynv4t1c4x0qm9',
9+
production:
10+
'hcaik_01jqmnfmeqprbx9x3vyr0rgj5sads64hnctxqkqcbp4bfhjf56s56dsszs',
11+
dev: null,
12+
}
13+
14+
const configDefaults = {
15+
ignoreNetworkEvents: true,
16+
}
17+
18+
export function initializeHoneycomb({ env }: { env: Environment }) {
19+
const apiKey = apiKeys[env]
20+
if (!apiKey) {
21+
return
22+
}
23+
24+
const sdk = new HoneycombWebSDK({
25+
serviceName: 'π-base web',
26+
apiKey,
27+
debug: env !== 'production',
28+
instrumentations: [
29+
getWebAutoInstrumentations({
30+
'@opentelemetry/instrumentation-xml-http-request': configDefaults,
31+
'@opentelemetry/instrumentation-fetch': configDefaults,
32+
'@opentelemetry/instrumentation-document-load': configDefaults,
33+
}),
34+
],
35+
})
36+
37+
sdk.start()
38+
}

packages/viewer/src/routes/+layout.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ import { defaultHost } from '@/constants'
33
import * as errors from '@/errors'
44
import { sync } from '@/gateway'
55
import type { LayoutLoad } from './$types'
6+
import { browser } from '$app/environment'
7+
import { initializeHoneycomb } from '@/honeycomb'
8+
import { forHost } from '@/environment'
69

710
export const load: LayoutLoad = async ({ fetch, url: { host } }) => {
8-
const dev = host.match(/(dev(elopment)?[.-]|localhost)/) !== null
11+
const env = forHost(host)
12+
const dev = env === 'dev'
913

10-
const errorHandler = dev
11-
? errors.log()
12-
: errors.sentry({ environment: errorEnv(host) })
14+
if (browser) {
15+
initializeHoneycomb({ env })
16+
}
17+
18+
const errorHandler = dev ? errors.log() : errors.sentry({ env })
1319

1420
const context = initialize({
1521
showDev: dev,
@@ -24,15 +30,3 @@ export const load: LayoutLoad = async ({ fetch, url: { host } }) => {
2430

2531
return context
2632
}
27-
28-
function errorEnv(host: string): errors.Environment {
29-
if (['topology.pi-base.org', 'topology.pages.dev'].includes(host)) {
30-
return 'production'
31-
}
32-
33-
if (host.includes('pages.dev')) {
34-
return 'deploy-preview'
35-
}
36-
37-
return 'dev'
38-
}

0 commit comments

Comments
 (0)