Skip to content

Commit 3bdb9ce

Browse files
committed
refactor: extract environment handling
1 parent b8d3348 commit 3bdb9ce

4 files changed

Lines changed: 61 additions & 41 deletions

File tree

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: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,18 @@ import * as errors from '@/errors'
44
import { sync } from '@/gateway'
55
import type { LayoutLoad } from './$types'
66
import { browser } from '$app/environment'
7-
8-
import { HoneycombWebSDK } from '@honeycombio/opentelemetry-web'
9-
import { getWebAutoInstrumentations } from '@opentelemetry/auto-instrumentations-web'
7+
import { initializeHoneycomb } from '@/honeycomb'
8+
import { forHost } from '@/environment'
109

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

1414
if (browser) {
15-
const configDefaults = {
16-
ignoreNetworkEvents: true,
17-
}
18-
19-
const sdk = new HoneycombWebSDK({
20-
debug: dev,
21-
apiKey: 'Sx608N12wwH9ZJ6qmHMVnA',
22-
serviceName: 'π-base web',
23-
instrumentations: [
24-
getWebAutoInstrumentations({
25-
'@opentelemetry/instrumentation-xml-http-request': configDefaults,
26-
'@opentelemetry/instrumentation-fetch': configDefaults,
27-
'@opentelemetry/instrumentation-document-load': configDefaults,
28-
}),
29-
],
30-
})
31-
sdk.start()
15+
initializeHoneycomb({ env })
3216
}
3317

34-
const errorHandler = dev
35-
? errors.log()
36-
: errors.sentry({ environment: errorEnv(host) })
18+
const errorHandler = dev ? errors.log() : errors.sentry({ env })
3719

3820
const context = initialize({
3921
showDev: dev,
@@ -48,15 +30,3 @@ export const load: LayoutLoad = async ({ fetch, url: { host } }) => {
4830

4931
return context
5032
}
51-
52-
function errorEnv(host: string): errors.Environment {
53-
if (['topology.pi-base.org', 'topology.pages.dev'].includes(host)) {
54-
return 'production'
55-
}
56-
57-
if (host.includes('pages.dev')) {
58-
return 'deploy-preview'
59-
}
60-
61-
return 'dev'
62-
}

0 commit comments

Comments
 (0)