📜  python 应用洞察 azure - TypeScript (1)

📅  最后修改于: 2023-12-03 15:19:09.602000             🧑  作者: Mango

Python 应用洞察 Azure - TypeScript

Python 作为一门广泛使用的编程语言,与大量的应用程序集成使用。Azure 提供了丰富的工具集来监视和诊断由 Python 编写的应用程序。通过 Azure 应用洞察,可以获得丰富的应用程序性能和用户行为分析数据,从而提高应用程序的可靠性和性能。

什么是 Azure 应用洞察?

Azure 应用洞察是一种云服务,可以帮助开发人员诊断生产应用程序中的问题。Azure 应用洞察提供丰富的应用程序分析功能,能够帮助你了解:

  • 应用程序的性能
  • 应用程序的用户行为
  • 应用程序的异常和故障
  • 应用程序的日志
  • ……

Azure 应用洞察支持多种编程语言和框架。无论是使用 Python,还是使用其他语言,Azure 应用洞察都可以帮助你找到应用程序的性能问题和异常。

如何在 Python 应用程序中添加应用洞察?

在 Python 应用程序中添加 Azure 应用洞察,通常需要执行下列步骤:

  1. 在 Azure 门户中创建应用洞察资源
  2. 安装 Azure 应用洞察 SDK
  3. 在应用程序中添加应用洞察 SDK 的引用
  4. 在应用程序中启用应用洞察 SDK
  5. 在应用程序中记录自定义跟踪和指标
在 Azure 门户中创建应用洞察资源

要使用 Azure 应用洞察,需要在 Azure 门户中创建应用洞察资源。创建资源时,需要提供资源的名称、订阅、资源组等信息。创建完毕后,可以在资源内部找到应用程序 ID 和密钥,这是应用程序连接应用洞察,收集数据的凭据。

安装 Azure 应用洞察 SDK

Azure 应用洞察提供了 Python SDK,可以轻松地在 Python 应用程序中添加应用洞察功能。可以通过 pip 命令安装 SDK:

pip install applicationinsights
在应用程序中添加应用洞察 SDK 的引用

在 Python 应用程序中添加引用时,只需要导入 ApplicationInsights 包。在应用程序中添加以下代码:

import os
from applicationinsights import TelemetryClient

instrumentation_key = os.environ.get('APPINSIGHTS_INSTRUMENTATIONKEY')

client = TelemetryClient(instrumentation_key)

上述代码会导入 telemetry client,并且创建了一个 client 对象。在实际应用中,client 对象可以全局使用,以便在任何时刻发送遥测数据。

在应用程序中启用应用洞察 SDK

要在 Python 应用程序中启用应用洞察,只需要在启动代码之前添加以下代码行:

import os

os.environ['APPINSIGHTS_INSTRUMENTATIONKEY'] = 'my-instrumentation-key'

上述代码行会设置环境变量 APP_INSIGHTS_INSTRUMENTATION_KEY,从而使应用程序连接应用洞察。

在应用程序中记录自定义跟踪和指标

使用应用洞察可以记录应用程序的自定义跟踪和指标。可以调用 TrackTrace 方法并提供跟踪消息来记录自定义跟踪。例如:

import os
from applicationinsights import TelemetryClient

instrumentation_key = os.environ.get('APPINSIGHTS_INSTRUMENTATIONKEY')

client = TelemetryClient(instrumentation_key)

client.track_trace('a custom trace message')
client.flush()

TrackMetric 方法允许你记录自定义指标。例如:

import os
from applicationinsights import TelemetryClient

instrumentation_key = os.environ.get('APPINSIGHTS_INSTRUMENTATIONKEY')

client = TelemetryClient(instrumentation_key)

client.track_metric('MyMetric', 5)
client.flush()
如何在 TypeScript 应用程序中添加应用洞察?

在 TypeScript 应用程序中添加应用洞察的过程与 Python 应用程序类似。需要执行以下步骤:

  1. 在 Azure 门户中创建应用洞察资源
  2. 安装 Azure 应用洞察 SDK
  3. 在应用程序中添加应用洞察 SDK 的引用
  4. 在应用程序中启用应用洞察 SDK
  5. 在应用程序中记录自定义跟踪和指标
在 Azure 门户中创建应用洞察资源

与 Python 一样, TypeScript 应用程序需要在 Azure 门户中创建应用洞察资源。

安装 Azure 应用洞察 SDK

在 TypeScript 应用程序中,可以使用 npm 包管理器安装应用洞察 SDK:

npm install applicationinsights
在应用程序中添加应用洞察 SDK 的引用

在 TypeScript 应用程序中添加引用需要使用 import 语法。在应用程序中添加以下代码:

import * as applicationinsights from 'applicationinsights';

applicationinsights.setup('my-instrumentation-key')
  .setAutoDependencyCorrelation(true)
  .setSendLiveMetrics(true)
  .setDistributedTracingMode(applicationinsights.DistributedTracingModes.AI)
  .start();

上述代码会引用应用洞察 SDK,并设置仪器键(instrumentation key)。可以配置各种选项来设置应用洞察 SDK 的行为。

在应用程序中启用应用洞察 SDK

像 Python 一样,要在 TypeScript 应用程序中启用应用洞察,只需要设置环境变量 APPINSIGHTS_INSTRUMENTATION_KEY 即可。例如:

process.env['APPINSIGHTS_INSTRUMENTATIONKEY'] = 'my-instrumentation-key';
在应用程序中记录自定义跟踪和指标

在 TypeScript 应用程序中也可以使用 Application Insights SDK 记录自定义跟踪和指标。例如:

import * as applicationinsights from 'applicationinsights';

applicationinsights.setup('my-instrumentation-key')
  .setAutoDependencyCorrelation(true)
  .setSendLiveMetrics(true)
  .setDistributedTracingMode(applicationinsights.DistributedTracingModes.AI)
  .start();

const client = applicationinsights.defaultClient;
client.trackTrace({
  message: 'a custom trace message'
});

TrackMetric 方法同样允许你记录自定义指标。例如:

client.trackMetric({name: 'MyMetric', value: 5});