@embrace-io/react-native-tracer-provider

中文文档
7.2.0发布于 2026/10/09Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 3.3万

A React Native for the Embrace SDK that conforms to the OpenTelemetry TracerProvider interface

原始使用文档
文档目录3 节
  1. Install
  2. Setup in your code
  3. Limitations

React Native Embrace - Native Tracer Provider

[!IMPORTANT]

This module requires the React Native Embrace SDK.

This package wraps Embrace's native SDKs with an OpenTelemetry Tracer Provider. Allowing custom instrumentations to be creating following OpenTelemetry's JS API as well as collecting traces from any JS Opentelemetry instrumentation library or native instrumentation included in your application.

Install

npm:

npm install @embrace-io/react-native-tracer-provider

yarn:

yarn add @embrace-io/react-native-tracer-provider

For iOS you will also need to install or update pods for the application:

cd ios && pod install --repo-update

Setup in your code

Initialize the Tracer Provider near the start of your app's cycle. The Embrace SDK will need to have started before you can use the tracer provider. To achieve this you can either start the SDK before the component that sets up the provider renders, or you can make use of the enabled parameter on the useEmbraceNativeTracerProvider hook to prevent it from triggering until the SDK is ready as in this example:

const {isStarted} = useEmbrace({
  ios: SDK_CONFIG,
});

const optionalConfig = {...}; // See EmbraceNativeTracerProviderConfig in ./src/types/ for possible options
const {tracerProvider: embraceTracerProvider, tracer: embraceTracer} = useEmbraceNativeTracerProvider(optionalConfig, isStarted);

You can then use the provided tracer to add custom instrumentations to your application:


const MyScreen = () => {
  useEffect(() => {
    span = embraceTracer.startSpan("my-span");

    someAsyncOperation().then(() => span.end());
  }, []);
  return <View />;
};

If you'd prefer not to have to pass around the tracer object you can instead leverage helpers from the @opentelemetry/api library. First register the Embrace tracer provider as the global one:

import { trace } from "@opentelemetry/api";
trace.setGlobalTracerProvider(embraceTracerProvider);

You can then create new tracers on the fly where needed and the getTracer method from the OTel API will know to use Embrace as the tracer provider:

import { trace } from "@opentelemetry/api";

const MyScreen = () => {
  useEffect(() => {
    const tracer = trace.getTracer("my-application");
    
    span = tracer.startSpan("my-span");

    someAsyncOperation().then(() => span.end());
  }, []);

  return <View />;
};

This method of registering the tracer provider globally also means that any OTel instrumentation libraries in your app to will now automatically be able to find Embrace's provider and use it for tracing.

Limitations

  • Adding links to spans is not currently supported, span.addLink(...) and span.addLinks(...) behave as noops.
  • Only string span attributes are currently supported, other types will be converted to their string representations
  • parentSpanId will not be set if the parent span was already ended in a previous session when the child span is started
  • Due to a limitation in the OTEL Swift API schemaUrl in calls to getTracer is ignored on iOS
  • Since communication with the native modules is asynchronous span.spanContext() will return a blank span context if executed immediately after a call to startSpan without yielding, for example:
  const mySpan = tracer.startSpan("my-span");
  const spanContext = mySpan.spanContext(); // can be configured to throw an error instead through EmbraceNativeTracerProviderConfig

  console.log(spanContext.traceId) // prints ""
  console.log(spanContext.spanId) // prints ""

To avoid this issue you can use the async version:

  const mySpan = tracer.startSpan("my-span");
  const spanContext = await (mySpan as EmbraceNativeSpan).spanContextAsync();

  console.log(spanContext.traceId) // prints "51e60a6917dfe46871d7f1d39f66d02c"
  console.log(spanContext.spanId) // prints "b2248eb58720064e"

版本与发布记录

@embrace-io/react-native-tracer-provider 共发布 20 个版本,首次发布于 2024/10/23;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.2.0latest2026/10/091 个月前
7.1.02026/09/012 个月前
7.0.02026/07/134 个月前
6.7.12026/07/074 个月前
6.7.02026/06/115 个月前
6.6.02026/03/128 个月前

关于 @embrace-io/react-native-tracer-provider 的常见问题

@embrace-io/react-native-tracer-provider 是什么?

A React Native for the Embrace SDK that conforms to the OpenTelemetry TracerProvider interface

如何安装 @embrace-io/react-native-tracer-provider?

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

@embrace-io/react-native-tracer-provider 支持 TypeScript 吗?

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

@embrace-io/react-native-tracer-provider 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@embrace-io/react-native-tracer-provider 可以商用吗?

@embrace-io/react-native-tracer-provider 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@embrace-io/react-native-tracer-provider 的下载量有多少?

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

@embrace-io/react-native-tracer-provider 还在维护吗?

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