@glimt.dev/otel-browser 0.1.1 → 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 +157 -42
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,51 +1,166 @@
|
|
|
1
1
|
# @glimt.dev/otel-browser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
OpenTelemetry for the browser. Works out of the box, highly configurable.
|
|
4
4
|
|
|
5
|
-
|
|
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
|
-
|
|
8
|
+
npm install @glimt.dev/otel-browser
|
|
15
9
|
```
|
|
16
10
|
|
|
17
|
-
|
|
11
|
+
```ts
|
|
12
|
+
import { registerOTelBrowser } from '@glimt.dev/otel-browser'
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glimt.dev/otel-browser",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
"protobufjs": "^7.5.4"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@types/node": "
|
|
47
|
+
"@types/node": "25.0.1",
|
|
48
48
|
"esbuild": "^0.27.1",
|
|
49
|
-
"jsdom": "^27.
|
|
49
|
+
"jsdom": "^27.3.0",
|
|
50
50
|
"typescript": "^5.9.3",
|
|
51
51
|
"vitest": "4.0.15",
|
|
52
52
|
"typescript-config": "1.0.0"
|