
Product
Socket MCP Adds Org Alerts, Threat Feed Review, and Package Inspection
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.
@atrim/instrument-web
Advanced tools
OpenTelemetry instrumentation for browsers with centralized YAML configuration
OpenTelemetry instrumentation for browsers with centralized YAML configuration.
instrumentation.yamlnpm install @atrim/instrument-web
import { initializeInstrumentation } from '@atrim/instrument-web'
// Initialize once at application startup
await initializeInstrumentation({
serviceName: 'my-app',
otlpEndpoint: 'http://localhost:4318/v1/traces'
})
// That's it! Auto-instrumentation is now active.
The following are automatically instrumented when you initialize:
Captures page load timing metrics (LCP, FCP, TTFB).
All fetch() calls are automatically traced with:
Legacy AJAX requests are traced automatically.
Click events and form submissions are captured.
You can also create custom spans:
import { trace } from '@opentelemetry/api'
const tracer = trace.getTracer('my-app')
tracer.startActiveSpan('my-operation', (span) => {
try {
// Your code here
span.setAttribute('user.id', '123')
span.setAttribute('operation.type', 'checkout')
// ... do work ...
span.setStatus({ code: 1 }) // OK
} catch (error) {
span.recordException(error)
span.setStatus({ code: 2 }) // ERROR
throw error
} finally {
span.end()
}
})
We provide convenient helpers for common annotations:
import {
annotateHttpRequest,
annotateCacheOperation,
annotateUserInteraction,
annotateNavigation
} from '@atrim/instrument-web'
// HTTP request
annotateHttpRequest(span, {
method: 'GET',
url: 'https://api.example.com/users',
statusCode: 200,
responseTime: 150
})
// Cache operation
annotateCacheOperation(span, {
operation: 'get',
key: 'user:123',
hit: true
})
// User interaction
annotateUserInteraction(span, {
type: 'click',
target: 'button#submit',
page: '/checkout'
})
// Navigation
annotateNavigation(span, {
from: '/home',
to: '/profile',
type: 'client-side'
})
await initializeInstrumentation({
serviceName: 'my-app', // Required
serviceVersion: '1.0.0', // Optional
otlpEndpoint: 'http://localhost:4318/v1/traces', // Optional
otlpHeaders: { // Optional
'Authorization': 'Bearer token'
}
})
Create an instrumentation.yaml file:
version: "1.0"
instrumentation:
enabled: true
# Only instrument these patterns
instrument_patterns:
- pattern: "^documentLoad"
description: "Page load metrics"
- pattern: "^HTTP (GET|POST)"
description: "API requests"
- pattern: "^click"
description: "User clicks"
# Ignore these patterns
ignore_patterns:
- pattern: "^HTTP GET /health"
description: "Health check endpoints"
- pattern: "^HTTP.*analytics"
description: "Analytics requests"
Then load it:
await initializeInstrumentation({
serviceName: 'my-app',
configPath: '/instrumentation.yaml' // Path relative to your app
})
Load configuration from a remote URL:
await initializeInstrumentation({
serviceName: 'my-app',
configUrl: 'https://config.company.com/instrumentation.yaml'
})
await initializeInstrumentation({
serviceName: 'my-app',
enableDocumentLoad: true, // Page load metrics
enableUserInteraction: false, // Disable click tracking
enableFetch: true, // HTTP requests via fetch()
enableXhr: false // Disable XMLHttpRequest
})
Configure via Vite/Webpack environment variables:
# .env
VITE_OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/v1/traces
Or set at runtime via window object:
window.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://custom:4318/v1/traces'
Your OpenTelemetry collector must have CORS enabled:
# otel-collector-config.yaml
receivers:
otlp:
protocols:
http:
cors:
allowed_origins:
- "http://localhost:3000"
- "https://your-app.com"
allowed_headers:
- "*"
Add your OTLP endpoint to CSP:
<meta http-equiv="Content-Security-Policy"
content="connect-src 'self' http://localhost:4318">
The package is optimized for web with:
Estimated sizes:
See the examples/web-vanilla directory for a complete working example.
initializeInstrumentation(options)Initialize OpenTelemetry for the browser.
Options:
serviceName (string, required) - Service name for telemetryserviceVersion (string, optional) - Service versionotlpEndpoint (string, optional) - OTLP HTTP endpoint (default: http://localhost:4318/v1/traces)otlpHeaders (object, optional) - Custom HTTP headers for OTLP exportconfigPath (string, optional) - Path to instrumentation.yaml fileconfigUrl (string, optional) - URL to remote instrumentation.yamlconfig (object, optional) - Inline configuration objectenableDocumentLoad (boolean, optional) - Enable document load instrumentation (default: true)enableUserInteraction (boolean, optional) - Enable user interaction instrumentation (default: true)enableFetch (boolean, optional) - Enable fetch instrumentation (default: true)enableXhr (boolean, optional) - Enable XMLHttpRequest instrumentation (default: true)Returns: Promise<WebTracerProvider>
getSdkInstance()Get the current SDK instance.
Returns: WebTracerProvider | null
shutdownSdk()Shutdown the SDK gracefully.
Returns: Promise<void>
| Feature | @atrim/instrument-web | @atrim/instrument-node |
|---|---|---|
| Platform | Browser | Node.js, Bun, Deno |
| Export Protocol | HTTP only | HTTP + gRPC |
| Auto-instrumentations | Fetch, XHR, DOM events | HTTP, gRPC, DB, etc. |
| Effect-TS | ❌ No | ✅ Yes |
| Bundle Size | <50KB | N/A |
| Service Detection | Manual | Automatic |
Check collector is running:
docker run -p 4318:4318 otel/opentelemetry-collector
Check CORS configuration on your collector
Check browser console for errors
Verify endpoint:
import { getOtlpEndpoint } from '@atrim/instrument-web'
console.log('OTLP endpoint:', getOtlpEndpoint())
Disable unused instrumentations:
await initializeInstrumentation({
serviceName: 'my-app',
enableUserInteraction: false, // Reduce overhead
enableDocumentLoad: false
})
Use pattern filtering to reduce span volume
Sample traces at the collector level
See CONTRIBUTING.md for development setup.
MIT License - see LICENSE for details.
FAQs
OpenTelemetry instrumentation for browsers with centralized YAML configuration
We found that @atrim/instrument-web demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.

Research
More than 140 Mastra npm packages were compromised in a supply chain attack that used a typosquatted dependency to deliver a cross-platform infostealer during installation.