首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于反应和香草JS项目的开放遥测

用于反应和香草JS项目的开放遥测
EN

Stack Overflow用户
提问于 2022-04-06 22:13:54
回答 1查看 90关注 0票数 1

有人能帮我理解一下,是否有一种方法可以在客户端为react和普通的JS项目配置开放遥测,我所要做的就是安慰正在从浏览器进行的获取调用的跟踪。

我看到的大多数文档只针对nodejs。如果有的话,请找出一份文件。

EN

回答 1

Stack Overflow用户

发布于 2022-11-25 09:19:32

这些文档为Javascript提供了一个通用指南。您为您所做的反应将与您为Node.js甚至简单的JS脚本所做的一样。

跟着文档就好了。创建并导出跟踪器:

代码语言:javascript
运行
复制
import { ZoneContextManager } from '@opentelemetry/context-zone';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load';
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
import { UserInteractionInstrumentation } from '@opentelemetry/instrumentation-user-interaction';
import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request';
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';

const setupTracer = () => {
    const provider = new WebTracerProvider();

    provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
    
    provider.register({
      // Changing default contextManager to use ZoneContextManager - supports asynchronous operations - optional
      contextManager: new ZoneContextManager(),
    });
    
    // Registering instrumentations
    registerInstrumentations({
      instrumentations: [
        new DocumentLoadInstrumentation(),
        new UserInteractionInstrumentation(),
        new XMLHttpRequestInstrumentation(),
        new FetchInstrumentation()
      ],
    });
    
}

export default setupTracer;

在应用程序的入口点(通常是index.js)中导入这样的跟踪器:

代码语言:javascript
运行
复制
setupTracer();
ReactDOM.render(<App />, document.getElementById('root'));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71774194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档