@obtrace/browser 2.1.0 → 2.2.0

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.
@@ -29,6 +29,7 @@ export function setupOtelWeb(config) {
29
29
  ...(config.projectId ? { "obtrace.project_id": config.projectId } : {}),
30
30
  ...(config.appId ? { "obtrace.app_id": config.appId } : {}),
31
31
  ...(config.env ? { "obtrace.env": config.env } : {}),
32
+ ...(config.sessionId ? { "session.id": config.sessionId } : {}),
32
33
  "runtime.name": "browser",
33
34
  });
34
35
  const traceExporter = new OTLPTraceExporter({
@@ -1,2 +1,2 @@
1
- import type { Tracer } from "@opentelemetry/api";
1
+ import { type Tracer } from "@opentelemetry/api";
2
2
  export declare function installConsoleCapture(tracer: Tracer, sessionId: string): () => void;
@@ -6,4 +6,6 @@ export interface OtelHandles {
6
6
  shutdown: () => Promise<void>;
7
7
  forceFlush: () => Promise<void>;
8
8
  }
9
- export declare function setupOtelWeb(config: ObtraceSDKConfig): OtelHandles;
9
+ export declare function setupOtelWeb(config: ObtraceSDKConfig & {
10
+ sessionId?: string;
11
+ }): OtelHandles;
@@ -5,4 +5,6 @@ export declare function getObtrace(): BrowserSDK | null;
5
5
  export declare function obtraceLog(level: "debug" | "info" | "warn" | "error" | "fatal", message: string, context?: SDKContext): void;
6
6
  export declare function obtraceMetric(name: string, value: number, unit?: string, context?: SDKContext): void;
7
7
  export declare function obtraceError(error: unknown, context?: SDKContext): void;
8
+ export declare function createObtraceErrorBoundary(React: any): any;
9
+ export declare function getObtraceErrorBoundary(): any;
8
10
  export type { BrowserSDK, ObtraceSDKConfig, SDKContext };
@@ -18,3 +18,60 @@ export function obtraceMetric(name, value, unit, context) {
18
18
  export function obtraceError(error, context) {
19
19
  _sdk?.captureException(error, context);
20
20
  }
21
+ let _React = null;
22
+ function getReact() {
23
+ if (_React)
24
+ return _React;
25
+ try {
26
+ _React = require("react");
27
+ }
28
+ catch {
29
+ try {
30
+ _React = globalThis.__obtrace_react;
31
+ }
32
+ catch { }
33
+ }
34
+ return _React;
35
+ }
36
+ export function createObtraceErrorBoundary(React) {
37
+ const Component = React.Component;
38
+ class ObtraceErrorBoundary extends Component {
39
+ constructor(props) {
40
+ super(props);
41
+ this.state = { hasError: false, error: null };
42
+ }
43
+ static getDerivedStateFromError(error) {
44
+ return { hasError: true, error };
45
+ }
46
+ componentDidCatch(error, errorInfo) {
47
+ const sdk = getObtrace();
48
+ if (sdk) {
49
+ const stack = errorInfo?.componentStack || "";
50
+ sdk.captureException(error, {
51
+ attrs: {
52
+ "error.type": "react.render",
53
+ "error.component_stack": typeof stack === "string" ? stack.slice(0, 4096) : "",
54
+ },
55
+ });
56
+ }
57
+ this.props.onError?.(error, errorInfo);
58
+ }
59
+ render() {
60
+ if (this.state.hasError) {
61
+ return this.props.fallback ?? null;
62
+ }
63
+ return this.props.children;
64
+ }
65
+ }
66
+ return ObtraceErrorBoundary;
67
+ }
68
+ let _ObtraceErrorBoundary = null;
69
+ export function getObtraceErrorBoundary() {
70
+ if (_ObtraceErrorBoundary)
71
+ return _ObtraceErrorBoundary;
72
+ const React = getReact();
73
+ if (!React)
74
+ throw new Error("React not found. Use createObtraceErrorBoundary(React) instead.");
75
+ _ObtraceErrorBoundary = createObtraceErrorBoundary(React);
76
+ return _ObtraceErrorBoundary;
77
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@obtrace/browser",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Obtrace Browser SDK with frontend wrappers",
5
5
  "type": "module",
6
6
  "license": "MIT",