@glimt.dev/otel-browser 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,51 +1,166 @@
1
1
  # @glimt.dev/otel-browser
2
2
 
3
- Minimal browser-only OpenTelemetry bootstrap for Glimt.dev.
3
+ OpenTelemetry for the browser. Works out of the box, highly configurable.
4
4
 
5
- - WebTracerProvider + ZoneContextManager
6
- - W3C tracecontext + baggage propagators
7
- - Instrumentations: document-load, user-interaction, XHR, fetch, browser-errors (logs)
8
- - OTLP HTTP export for traces and logs (protobuf default; JSON optional)
9
- - Browser resource attributes; user_agent is opt-in
10
-
11
- ## Install
5
+ ## Quick Start (3 lines)
12
6
 
13
7
  ```bash
14
- pnpm add @glimt.dev/otel-browser
8
+ npm install @glimt.dev/otel-browser
15
9
  ```
16
10
 
17
- ## Usage
11
+ ```ts
12
+ import { registerOTelBrowser } from '@glimt.dev/otel-browser'
18
13
 
19
- ```html
20
- <meta name="traceparent" content="00-ab42124a3c573678d4d8b21ba52df3bf-d21f7bc17caa5aba-01" />
21
- <script type="module">
22
- import { registerOTelBrowser } from '/dist/index.js'
23
- registerOTelBrowser({
24
- serviceName: 'my-web-app',
25
- includeUserAgent: false,
26
- // Traces
27
- exporterUrl: 'http://localhost:4318/v1/traces',
14
+ registerOTelBrowser({ serviceName: 'my-web-app' })
15
+ ```
16
+
17
+ Done. Traces go to `http://localhost:4318/v1/traces`, logs to `/v1/logs`.
18
+
19
+ ---
20
+
21
+ ## Full Example (all options)
22
+
23
+ ```ts
24
+ import { registerOTelBrowser } from '@glimt.dev/otel-browser'
25
+
26
+ const sdk = registerOTelBrowser({
27
+ // Identity
28
+ serviceName: 'my-web-app',
29
+
30
+ // Glimt auth
31
+ organisationId: 'org_xxx',
32
+ publishableKey: 'pk_xxx',
33
+
34
+ // Export
35
+ exporterUrl: 'https://ingest.glimt.dev/v1/traces',
36
+ exporter: 'http/protobuf', // or 'http/json'
37
+ exporterHeaders: { 'x-tenant-id': 'acme' },
38
+ credentials: 'include', // for CORS
39
+
40
+ // Logs (auto-derived to /v1/logs)
41
+ logs: {
28
42
  exporter: 'http/protobuf',
29
- exporterHeaders: { 'x-tenant-id': 'acme' },
30
- credentials: 'include',
31
- // Logs (default on). URL auto-derives to /v1/logs unless overridden
32
- logs: {
33
- exporter: 'http/protobuf', // or 'http/json'
34
- // exporterUrl: 'http://localhost:4318/v1/logs',
35
- },
36
- // Console instrumentation (optional)
37
- console: { enabled: true },
38
- })
39
- </script>
40
- ```
41
-
42
- ## CORS
43
- - Access-Control-Allow-Origin: * (or your origin)
44
- - Access-Control-Allow-Credentials: true (if using credentials)
45
- - Access-Control-Allow-Headers: content-type, traceparent, baggage, accept, authorization, x-tenant-id
46
- - Content-Type: application/x-protobuf (protobuf) or application/json (json)
47
-
48
- ## References
49
- - https://opentelemetry.io/docs/languages/js/getting-started/browser/
50
- - https://opentelemetry.io/docs/specs/semconv/resource/browser/
51
- - https://last9.io/blog/getting-started-with-opentelemetry-for-browser-monitoring/
43
+ },
44
+
45
+ // Instrumentations
46
+ instrumentations: ['auto'], // document-load, user-interaction, xhr, fetch
47
+ fetch: {
48
+ ignoreUrls: [/\/healthz?$/, /analytics\.js$/],
49
+ propagateContextUrls: [/^https:\/\/api\.myapp\.com/],
50
+ },
51
+
52
+ // Capture
53
+ errors: { captureUnhandled: true },
54
+ console: { enabled: true }, // capture console.* as logs
55
+ includeUserAgent: false, // privacy default
56
+
57
+ // Initial user context
58
+ user: { userId: 'user_123', role: 'admin' },
59
+
60
+ // Debug
61
+ logLevel: 'DEBUG',
62
+ })
63
+
64
+ // Update user on login/logout
65
+ sdk.setUserContext({ userId: 'user_456' })
66
+ sdk.setUserContext(null)
67
+ ```
68
+
69
+ ---
70
+
71
+ ## React / Next.js Integration
72
+
73
+ ```tsx
74
+ 'use client'
75
+ import { useEffect, useRef } from 'react'
76
+ import { registerOTelBrowser, type BrowserSDK } from '@glimt.dev/otel-browser'
77
+
78
+ export function TelemetryProvider({ userId }: { userId?: string }) {
79
+ const sdk = useRef<BrowserSDK | null>(null)
80
+
81
+ useEffect(() => {
82
+ sdk.current = registerOTelBrowser({
83
+ serviceName: process.env.NEXT_PUBLIC_OTEL_SERVICE_NAME!,
84
+ exporterUrl: process.env.NEXT_PUBLIC_OTEL_EXPORTER_OTLP_ENDPOINT,
85
+ user: userId ? { userId } : undefined,
86
+ })
87
+ }, [])
88
+
89
+ useEffect(() => {
90
+ sdk.current?.setUserContext(userId ? { userId } : null)
91
+ }, [userId])
92
+
93
+ return null
94
+ }
95
+ ```
96
+
97
+ ```tsx
98
+ // app/layout.tsx
99
+ export default async function RootLayout({ children }) {
100
+ const user = await getCurrentUser()
101
+ return (
102
+ <html>
103
+ <body>
104
+ <TelemetryProvider userId={user?.id} />
105
+ {children}
106
+ </body>
107
+ </html>
108
+ )
109
+ }
110
+ ```
111
+
112
+ ---
113
+
114
+ ## Server-side traceparent
115
+
116
+ Inject in your HTML to correlate browser ↔ server:
117
+
118
+ ```html
119
+ <meta name="traceparent" content="00-abc123...-def456...-01" />
120
+ ```
121
+
122
+ The SDK picks this up automatically as the parent span.
123
+
124
+ ---
125
+
126
+ ## CORS Requirements
127
+
128
+ Your collector must allow:
129
+
130
+ ```
131
+ Access-Control-Allow-Origin: * (or your origin)
132
+ Access-Control-Allow-Headers: content-type, traceparent, baggage, authorization
133
+ Access-Control-Allow-Credentials: true (if using credentials)
134
+ ```
135
+
136
+ ---
137
+
138
+ ## Defaults
139
+
140
+ | Feature | Default |
141
+ |---------|---------|
142
+ | Trace exporter | `http/protobuf` → `localhost:4318/v1/traces` |
143
+ | Log exporter | `http/protobuf` → auto-derived `/v1/logs` |
144
+ | Instrumentations | document-load, user-interaction, xhr, fetch |
145
+ | Error capture | `window.onerror`, `unhandledrejection` |
146
+ | User agent | NOT included (privacy) |
147
+ | Console capture | OFF |
148
+
149
+ ---
150
+
151
+ ## User Context Attributes
152
+
153
+ When you call `sdk.setUserContext()`, these attributes are added to all spans:
154
+
155
+ | Attribute | Description |
156
+ |-----------|-------------|
157
+ | `enduser.id` | Primary user identifier |
158
+ | `enduser.pseudo.id` | Privacy-preserving hash |
159
+ | `enduser.email` | PII, use with caution |
160
+ | `enduser.role` | Permission scope |
161
+
162
+ ---
163
+
164
+ ## License
165
+
166
+ MIT