反应原生拥抱 - OTLP
[尖叫声] 此套件被贬值,将在下个主要发行中被移除. 相反, 在本地代码中启动拥抱 财务报告和审定财务报表 配置本地 SDK 中的 OTLP 导出。 。 。 .
这个 打开遥测协议 (OTLP)是一种开放的标准,它能够将可观察性数据——例如痕迹和日志——从应用转移到各种监测和分析后端。 通过采用OTLP,开发者可以以一致的格式发送遥测数据,使多个后端的集成直截了当.
这一组件为通过HTTP向任何OTLP兼容后端输出跟踪和日志数据提供了一种简单的方式. 该组件还不断向Embrace发送遥测数据,确保Embrace平台的持续可观察性,同时允许用户将数据导出至其他可观察后端.
所需资源
使用此软件包必须事先安装 @embrace-io/react-native 和综合。 要了解更多情况 请访问我们 文档。 。 。 .
一体化
添加 JavaScript 库
无 :
npm install @embrace-io/react-native-otlp
线条 :
yarn add @embrace-io/react-native-otlp
对于iOS,您还需要安装或更新应用程序的吊舱:
cd ios && pod install --repo-update
初始化
举例来说,我们将使用Grafana Cloud, 用OTLP的端点, 将遥测数据重新定位。 详情请上网查询 文档。 。 。 .
import React, {useEffect, useMemo, useState} from "react";
import {useEmbrace} from "@embrace-io/react-native";
import {View, Text} from "react-native";
import {Stack} from "expo-router";
// for this particular case where we use Grafana Cloud this token should be passed with the format `instance:token` converted into a base64 string.
const GRAFANA_TOKEN = "base64(instance:token)";
const EXPORT_CONFIG = {
logExporter: {
endpoint: "https://otlp-gateway-prod-us-central-0.grafana.net/otlp/v1/logs",
headers: [
{
key: "Authorization",
token: `Basic ${GRAFANA_TOKEN}`,
},
],
},
traceExporter: {
endpoint:
"https://otlp-gateway-prod-us-central-0.grafana.net/otlp/v1/traces",
headers: [
{
key: "Authorization",
token: `Basic ${GRAFANA_TOKEN}`,
},
],
},
};
// this is the minimum of configuration needed at this point,
// for more information please refer to docs under [`@embrace-io/react-native`](../core).
// iOS is configurable through code, Android configuration happens at build time
const SDK_CONFIG = {appId: "abcde"};
function RootLayout() {
const {isPending, isStarted} = useEmbrace({
ios: SDK_CONFIG,
exporters: EXPORT_CONFIG
});
if (isPending) {
return (
<View>
<Text>Loading Embrace</Text>
</View>
);
} else {
if (!isStarted) {
console.log('An error occurred during Embrace initialization');
}
}
// regular content of the application
return (
<Stack>
<Stack.Screen name="(tabs)" options={{headerShown: false}} />
</Stack>
);
}
export default RootLayout;
在土著地层初始化
如果您已经将 Embrace React 原生 SDK 初始化, 或者您正在计划运行我们文件节中提到的安装脚本, 您仍然可以从 OTLP 自定义导出功能中获益 。 记住安装脚本正在添加初始化 Embrace 在原住民一侧所需的最小代码, 但并没有将导出遥测数据的配置整合到您选择的后端 。 为此,你需要手动调整Android/iOS两侧.
iOS系统
如果您已经运行了上面提到的安装脚本, 您就可以找到 EmbraceInitializer.swift 带有您可以更新的初始代码的文件 :
import Foundation
import EmbraceIO
import RNEmbraceOTLP // Do not forget to import `RNEmbraceOTLP` module which will make the proper classes available
let GRAFANA_AUTH_TOKEN = "Basic __YOUR TOKEN__"
let GRAFANA_TRACES_ENDPOINT = "https://otlp-gateway-prod-us-central-0.grafana.net/otlp/v1/traces"
let GRAFANA_LOGS_ENDPOINT = "https://otlp-gateway-prod-us-central-0.grafana.net/otlp/v1/logs"
@objcMembers class EmbraceInitializer: NSObject {
static func start() -> Void {
do {
// Preparing Span Exporter config with the minimum required
let traceExporter = OtlpHttpTraceExporter(endpoint: URL(string: GRAFANA_TRACES_ENDPOINT)!,
config: OtlpConfiguration(
headers: [("Authorization", GRAFANA_AUTH_TOKEN)]
)
)
// Preparing Log Exporter config with the minimum required
let logExporter = OtlpHttpLogExporter(endpoint: URL(string: GRAFANA_LOGS_ENDPOINT)!,
config: OtlpConfiguration(
headers: [("Authorization", GRAFANA_AUTH_TOKEN)]
)
)
try Embrace
.setup(
options: Embrace.Options(
appId: "__YOUR APP ID__",
platform: .reactNative,
export: OpenTelemetryExport(spanExporter: traceExporter, logExporter: logExporter) // passing the configuration into `export`
)
)
.start()
} catch let e {
print("Error starting Embrace \(e.localizedDescription)")
}
}
}
机器人
与 iOS 类似, 如果您已经运行了安装脚本, 您将会看到您已设置的以下行 MainApplication 文件 :
Embrace.start(this)
扭动 onCreate 使用以下片段以初始化出口商的方法,并采用所需的最低配置.
拥抱暴露出两种注册自定义出口商的方法,这取决于您出口商的OpenTeleometry API的构建方式:
Embrace.addSpanExporter编号 :Embrace.addLogRecordExporter接受 OpenTeleometry Kotlin 出口商.Embrace.addJavaSpanExporter编号 :Embrace.addJavaLogRecordExporter接受 OpenTeleomet Java 导出 .
下面的例子使用OpenTeleomet Java导出.
增加下列进口:
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogRecordExporter
然后在"OnCreate"方法中列出以下内容:
// Preparing Span Exporter config with the minimum required
val spanExporter = OtlpHttpSpanExporter.builder()
.setEndpoint("https://otlp-gateway-prod-us-central-0.grafana.net/otlp/v1/traces")
.addHeader("Authorization", "Basic __YOUR TOKEN__")
// Preparing Log Exporter config with the minimum required
val logExporter = OtlpHttpLogRecordExporter.builder()
.setEndpoint("https://otlp-gateway-prod-us-central-0.grafana.net/otlp/v1/logs")
.addHeader("Authorization", "Basic __YOUR TOKEN__")
Embrace.addJavaSpanExporter(spanExporter.build())
Embrace.addJavaLogRecordExporter(logExporter.build())
// This is the line already added by the install script
Embrace.start(this)
禁用对 OTLP 导出网络请求的跟踪
拥抱会自动为网络请求创建跨度, 但因为 OTLP 导出本身会提出网络请求 这样可以产生一个循环,使导出网络请求产生一个跨度,然后导出,然后创建另一个跨度 间距等.
为避免这种情况, 您可以配置您正在导出到的端点, 以忽略 Android 和 iOS .
机器人
编辑 android/app/src/main/embrace-config.json 在应用程序中添加 disabled_url_patterns 键。 在上述例子中
如果你出口到格拉法纳 你的配置看起来像:
{
"app_id": "xxxxx",
"api_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"sdk_config": {
"app_framework": "react_native",
"networking": {
"enable_network_span_forwarding": true,
"disabled_url_patterns": [
"grafana.net"
]
}
}
}
iOS系统
在联署材料中初始化时,您只需包括 disabledUrlPatterns 键,在上述示例中,如果您是
导出到 Grafana 您将在 SDK_CONFIG数字 :
"disabledUrlPatterns": [
"grafana.net"
]
在斯威夫特初始化时,在设置 Embrace 时需要更多的配置. 首先将以下导入添加到
EmbraceInitializer.swift数字 :
import EmbraceCrash
然后设置自定义 URLSessionCaptureService 忽略格拉法纳导出:
let servicesBuilder = CaptureServiceBuilder()
let urlSessionServiceOptions = URLSessionCaptureService.Options(
injectTracingHeader: true,
requestsDataSource: nil,
ignoredURLs: ["grafana.net"]
)
// manually adding the URLSessionCaptureService
servicesBuilder.add(.urlSession(options: urlSessionServiceOptions))
// adding defaults
servicesBuilder.addDefaults()
启动 SDK 时最后通过附加配置 :
try Embrace
.setup(
options: Embrace.Options(
appId: "__YOUR APP ID__",
platform: .reactNative,
captureServices: servicesBuilder.build(),
crashReporter: EmbraceCrashReporter(),
export: OpenTelemetryExport(spanExporter: traceExporter, logExporter: logExporter) // passing the configuration into `export`
)
)
.start()