@devfellowship/observability

中文文档
2.2.1发布于 2026/11/05MITTypeScriptES Module近 30 天下载 7924

Browser observability SDK — OpenTelemetry + Sentry for React apps

原始使用文档
文档目录15 节
  1. Install
  2. Peer dependencies
  3. Quick start
  4. Explicit configuration
  5. Props
  6. Utilities
  7. Track custom events
  8. Set user context
  9. Sentry helpers
  10. Error boundary
  11. What's included
  12. Privacy (Sentry)
  13. Development
  14. Publishing
  15. License

@devfellowship/observability

Browser observability SDK for React apps. Provides OpenTelemetry tracing, metrics, Sentry error tracking, Web Vitals, fetch instrumentation, error boundaries, and React Query error reporting — all via a single provider wrapper.

Install

npm install @devfellowship/observability

Peer dependencies

  • react >= 18
  • @tanstack/react-query >= 5 (optional — enables automatic query error reporting)

Quick start

Wrap your app with <ObservabilityProvider>:

import { ObservabilityProvider } from "@devfellowship/observability";

function App() {
  return (
    <ObservabilityProvider>
      <YourApp />
    </ObservabilityProvider>
  );
}

With zero props, the provider reads configuration from Vite environment variables:

Env varPurpose
VITE_APP_NAMEService name for traces/metrics
VITE_APP_VERSIONService version
VITE_OTEL_API_KEYAPI key sent as x-api-key header
VITE_SENTRY_DSNSentry DSN (enables error tracking. Session replay is OFF.)
VITE_GIT_SHAGit SHA of the build. Becomes the Sentry release. Falls back to VITE_COMMIT_SHA, then <app>@<version>.
VITE_OTEL_ENABLEDSet to "true" to enable (auto-enabled in production)

Explicit configuration

All settings can be passed as props, overriding env vars:

<ObservabilityProvider
  serviceName="my-app"
  serviceVersion="1.2.0"
  collectorUrl="https://otel.devfellowship.com"
  apiKey="your-api-key"
  sentryDsn="https://[email protected]/yyy"
  environment="production"
  enabled={true}
  enableWebVitals={true}
  enableQueryTracking={true}
  enableFetchTracing={true}
>
  <App />
</ObservabilityProvider>

Props

PropTypeDefaultDescription
serviceNamestringVITE_APP_NAME or "unknown"Service name for OTel resource
serviceVersionstringVITE_APP_VERSION or "0.0.0"Service version
collectorUrlstring"https://otel.devfellowship.com"OTel collector base URL
apiKeystringVITE_OTEL_API_KEYAPI key header value
sentryDsnstringVITE_SENTRY_DSNSentry DSN (omit to disable Sentry)
environmentstring"production" / "development"Environment label
enabledbooleantrue in productionMaster kill-switch
enableWebVitalsbooleantrueCapture LCP, CLS, INP, TTFB
enableQueryTrackingbooleantrueReport React Query errors as spans
enableFetchTracingbooleantrueAuto-trace all fetch() calls

Utilities

Track custom events

import { trackEvent } from "@devfellowship/observability";

trackEvent("button_click", { "button.id": "checkout", page: "/cart" });

Set user context

import { setUserId, setUserRole } from "@devfellowship/observability";

// After authentication
setUserId(user.id);
setUserRole(user.role);

Sentry helpers

import { captureError, setSentryUser } from "@devfellowship/observability";

captureError(new Error("something broke"), { component: "Checkout" });
setSentryUser(user.id, user.role);

Error boundary

import { ErrorBoundary } from "@devfellowship/observability";

<ErrorBoundary fallback={<p>Something went wrong</p>}>
  <RiskyComponent />
</ErrorBoundary>

What's included

  • OTel traces — BatchSpanProcessor → OTLP/HTTP exporter
  • OTel metrics — PeriodicExportingMetricReader → OTLP/HTTP exporter
  • Fetch instrumentation — auto-creates spans for all fetch() calls (collector URLs filtered)
  • Web Vitals — LCP, CLS, INP, TTFB reported as OTel histogram metrics
  • Sentry — error tracking and browser tracing, with scrubbing (see Privacy). No session replay.
  • Error boundary — catches React component errors, reports to both OTel and Sentry
  • React Query integration — subscribes to query cache, creates error spans on failures
  • Resource attributes — browser, OS, device type, session ID, viewport, connection type, route

Privacy (Sentry)

Since 2.2.0 the Sentry client is built to leak no personal data (plan 20261101-fleet-observability-error-loop, T1):

  • No session replay. No replay integration, and both replay sample rates are 0. Do not add it back without a new privacy decision.
  • Anonymous user. The Sentry user is a random id per browser (localStorage key dfl_obs_anon_id). setSentryUser(id, role) keeps the signature but ignores id: no account id, email or name is sent.
  • Scrubbing. beforeSend, beforeSendTransaction and beforeBreadcrumb remove emails, URL query strings and fragments, JWTs, bearer tokens, key=value secrets and IPv4 addresses from messages, request data, headers, breadcrumbs and span data. Cookies, Authorization and similar headers are dropped. See src/lib/scrub.ts.
  • Fail closed, never throw. The three hooks use scrubEventOrDrop and scrubBreadcrumbOrDrop. A getter-only property (seen 2026-11-04) is read and written safely. When a value needs scrubbing and its property cannot take the new value, the event or crumb is dropped, a console.warn names the key (never the value), and nothing is sent half scrubbed. A hook that threw was reported by the SDK as a new error and lost the transaction.
  • IP address. sendDefaultPii is false; with it the SDK sets infer_ip: "never". There is no scrubIPAddresses SDK option in @sentry/react 10.x. "Prevent Storing of IP Addresses" is a Sentry project setting and must be on in the project (dfl-react). The SDK never sends an IP, and a test checks it.
  • Release. Set VITE_GIT_SHA (or the release prop) at build time, so a release changes at each deploy.

Tests: npm test (unit, with fixtures). Real-browser proof of "no replay envelope": PLAYWRIGHT_PATH=<path to playwright/index.mjs> npm run proof:browser.

Development

npm install
npm run build    # builds with tsup (ESM + .d.ts)

Publishing

npm version patch  # or minor/major
npm publish

License

MIT

版本与发布记录

@devfellowship/observability 共发布 6 个版本,首次发布于 2026/04/03;过去 12 个月发布了 6 个版本。

版本发布日期距今
2.2.1latest2026/11/057 天前
2.2.02026/11/038 天前
2.1.02026/04/097 个月前
2.0.22026/04/037 个月前
2.0.12026/04/037 个月前
2.0.02026/04/037 个月前

关于 @devfellowship/observability 的常见问题

@devfellowship/observability 是什么?

Browser observability SDK — OpenTelemetry + Sentry for React apps

如何安装 @devfellowship/observability?

在项目目录运行 npm install @devfellowship/observability,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@devfellowship/observability 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@devfellowship/observability 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@devfellowship/observability 可以商用吗?

@devfellowship/observability 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@devfellowship/observability 的下载量有多少?

根据 npm 官方下载统计,@devfellowship/observability 在 2026/10/12 – 2026/11/10 这 30 天内被下载 7,924 次,日均约 264 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@devfellowship/observability 还在维护吗?

最新版本 2.2.1 发布于 2026/11/05(7 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/04/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。