typescript 将Instana误差跟踪集成到Angular 2应用程序中

inkz8wg9  于 2023-03-19  发布在  TypeScript
关注(0)|答案(1)|浏览(105)

我正在尝试将Instana集成到一个应用程序中。更具体地说,我正在尝试将错误从我的Angular应用程序发送到Instana。我的代码正在“工作”,所以这是一个关于最佳实践的问题。
Instana的BackendCorrelation文档根据我的理解定义了“window”中的函数,我在index.html中设置了类似的内容。

<script>
  (function(i,s,o,g,r,a,m){i['InstanaEumObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//eum.instana.io/eum.min.js','ineum');

  ineum('apiKey', 'someKey');
  ineum('traceId', 'backendTraceId'); //replace 'backendTraceId with the appropriate expression for retrieval
</script>

我遇到的问题是,当我尝试按照Instana的Angular 2+积分指南进行错误跟踪时,他们调用了我可以从窗口访问的方法之一。指南直接调用了函数ineum(...)本身。当我尝试这样做时,我的项目无法编译。

class CustomErrorHandler implements ErrorHandler {
  handleError(error) {
    ineum('reportError', error);

    // Continue to log caught errors to the console
    console.error(error);
  }
}

我当前的修复是:**(<any>window).ineum('reportError', errorContext);**但是我在看another stack overflow question,他们在javascript中访问窗口的方式不同。
1.将窗口类型转换为“any”是一种不好的做法吗?
1.是尝试window['ineum'](...)更好,还是这只是一个语法偏好?
1.尝试在另一个文件中定义函数(比如各种各样的Instana服务),然后在index.html脚本和CustomErrorHandler中使用该服务,或者将其保留在窗口中,这是更好的做法吗?(尽管这对我来说可能有点难以理解)
抱歉我的困惑,因为这可能不是一个实际的问题,因为我的代码是'工作',但我只是想澄清这一点。我不知道如果我只是没有按照Instana的指南正确,但这是我能找到的最好的。我试图通过他们的联系页面,但我还没有收到回应。

mkshixfv

mkshixfv1#

更新答案2023年3月14日

您可以通过npm install --save @types/ineum安装类型定义,这些定义由Instana团队维护,您不需要进一步的TypeScript配置。

原始答案

Instana有一个demo application,它显示要执行此操作。
总结一下您需要的部件:
1.确保在TypeScript配置中配置了一个本地目录,允许定义自定义类型:

// common file name: tsconfig.app.json
{
  // other configuration…
  "typeRoots": [
    // other configuration…
    "./custom-typings"
  ]
}

1.在此目录下的文件中声明全局ineum函数:

// common file name globals.d.ts
declare function ineum(s: string, ...parameters: any[]): any;

这两个步骤的组合将使TypeScript知道该函数。现在您可以像使用其他全局函数一样使用ineum

相关问题