通过


操作方法:为代码应用设置 Azure App Insights

Azure Application Insights 是一种遥测和监视服务,可帮助你从应用程序收集和分析详细的遥测数据。 将它与Power Apps代码应用集成时,可以捕获会话负载性能和网络请求摘要等指标。 此集成可让你更深入地了解应用在现实世界中的行为方式。 有关详细信息,请参阅 Application Insights OpenTelemetry 可观测性概述

注释

本文演示了如何初始化和配置应用的遥测数据的 一个示例 。 可以遵循相同的模式来集成任何监视工具,而不仅仅是 Application Insights。

Azure Application Insights 通过提供精细日志和自定义事件来补充 Power Platform Monitor。 但是,它仅在应用成功加载后捕获遥测数据。 启动失败(包括受阻止的文件或初始化失败引起的问题)不会在此处显示,并且只会显示在 Monitor 中。

先决条件

  • Azure订阅。
  • 在Azure portal中创建的 Application Insights 资源。
  • 来自 App Insights 资源的连接字符串或监控密钥。

Steps

使用以下步骤预配 Application Insights、安装适用于代码应用的 Power Apps 客户端库、连接 Power Apps 代码应用中的遥测数据以及验证Azure中的数据。 本简短指南将引导你完成端到端设置,以便快速开始捕获性能和网络指标。

  1. 创建 Application Insights 资源

    • 登录到 Azure portal
    • 转到 Application Insights 并创建新资源。
    • 从资源概述复制 Connection StringInstrumentation Key
  2. 安装 Application Insights SDK

    将 npm 包添加到代码应用项目中:

    npm install @microsoft/applicationinsights-web 
    
  3. 初始化 Application Insights

    添加用于初始化 Application Insights 的逻辑:

    
    import { ApplicationInsights } from '@microsoft/applicationinsights-web'; 
    
    const initializeAppInsights = () => { 
    const appInsights = new ApplicationInsights({ 
     config: { 
      connectionString: 'InstrumentationKey=<YOUR_KEY>;IngestionEndpoint=<YOUR_ENDPOINT>' 
     } 
    }); 
    appInsights.loadAppInsights(); 
    appInsights.trackPageView(); // Optional: Tracks page view 
    return appInsights; 
    }; 
    
    

    注释

    代码应用尚不支持环境变量。 若要管理每个环境的检测密钥,可以将值存储在 Dataverse(例如设置表)中,或使用 getContext() 检测环境并从应用程序常量中选择适当的连接字符串。

  4. 配置记录器

    提供记录器,以便平台可以将会话和网络指标转发到监视系统。 平台调用logMetric函数,并使用sessionLoadSummarynetworkLoadSummary这样的指标。 应只调用此函数一次。

    import { setConfig } from '@microsoft/power-apps/app' 
    import type { IConfig } from '@microsoft/power-apps/app' 
    
    setConfig({ 
    logger: {   
       logMetric: (value: Metric) => {   
          appInsights.trackEvent(   
          {   
             name: value.type, 
          }, 
             value.data   
          );   
       }   
    } 
    }); 
    

    其他指南

    • logMetric 类型:为清晰起见,将其定义为 (value: Metric) => void

    • 记录器接口

      interface ILogger { 
      logMetric?: (value: Metric) => void; 
      } 
      

    可以从:ILogger 导入

    import type { ILogger } from '@microsoft/power-apps/telemetry' 
    

    注释

    这只是一个示例。 必须实现 logMetric ,以便将指标发送到所选的监视工具,不一定是 App Insights。 平台仅仅传送指标数据负载。 您的实现决定如何去处理它们。

  5. 配置内容安全策略 (CSP)

    如果环境中启用了内容安全策略(CSP),可能会阻止将遥测事件发送到 Application Insights。 您需要将 App Insights 终结点添加到环境的 CSP 设置中。

    标识所需的源

    1. 打开已配置 Application Insights 的应用程序。

    2. 通过以下任一方式打开浏览器 DevTools:

      • F12
      • Ctrl+Shift+I
      • 右键单击应用并选择“ 检查”
    3. 转到 “控制台 ”选项卡。

    4. 清除控制台内容,从下拉菜单选择仅显示错误。 此步骤不是必需的,但可以更轻松地识别相关错误。

      如何将控制台设置为仅显示错误

    5. 查找包含以下消息的错误:Connecting to 'https:...' violates the following Content Security Policy directive

      查看预期 CSP 违规项

    6. 请注意这些错误消息中显示的 URL。 这些 URL 是您需要添加到 CSP 配置中的来源。

    将源添加到 CSP 设置

    1. 转到 Power Platform 管理中心

    2. 按照 “配置内容安全策略 ”中的说明将前面步骤中标识的源添加到允许的列表 connect-src。 请参阅以下部分中的示例。

      CSP 源示例

    3. 等待应用设置。 CSP 更改可能需要几分钟才能传播。

    小窍门

    更新 CSP 设置后,刷新应用并再次检查控制台,验证是否已解决 CSP 冲突并成功发送遥测数据。

  6. 在Azure portal中查看日志

    • 打开 Application Insights 资源。
    • 转到 监视 > 日志
    • 查询customEvents表以查看会话摘要和网络请求。

    适用于代码应用的Power Apps客户端库目前提供两种内置指标类型:

    type SessionLoadSummaryMetricData = { 
    successfulAppLaunch: boolean; 
    appLoadResult: 'optimal' | 'other'; 
    appLoadNonOptimalReason: 'interactionRequired' | 'throttled' | 'screenNavigatedAway' | 'other'; 
    timeToAppInteractive: number; 
    } 
    
    type NetworkRequestMetricData = { 
    url: string; 
    method:  string; 
    duration: number; 
    statusCode: number; 
    responseSize: number; 
    } 
    
  7. (可选)记录自定义事件

    在应用中所需的点调用监控工具自己的 API,记录超出默认指标的更多遥测数据。 有关更高级的方案,请参阅官方 Application Insights 文档以获取指导。 避免记录敏感数据或不必要的数据,并始终遵循组织的合规性准则。

示例查询

为了帮助你开始分析遥测数据,以下示例查询使用 Azure Application Insights Analytics(Kusto 查询语言)。

应用启动性能

此查询按日展示应用启动性能指标。 使用此查询来评估一段时间内的性能趋势,或者在进行更改后评估性能趋势。 使用 timeToAppInteractive 字段的第 75 百分位数可避免异常值造成的干扰。

customEvents 
| where name == "sessionLoadSummary" 
| extend cd = parse_json(customDimensions) 
| extend cm = parse_json(customMeasurements)  
| extend timeToAppInteractive = todouble(cm["timeToAppInteractive"]) 
| extend successfulAppLaunch = tobool(cd.successfulAppLaunch) 
| where successfulAppLaunch == true 
| summarize percentile(timeToAppInteractive, 75)  
by bin(timestamp, 1d)  
| render timechart 

按 URL 提供的网络请求性能

此查询分析 Application Insights 中的自定义 networkRequest 事件。 它从遥测中提取 URL 和持续时间。 然后,它汇总了每个 URL 的每日计数和第 75 百分位响应时间。 它将结果可视化为时序图表。

customEvents 
| where name == "networkRequest" 
| extend cd = parse_json(customDimensions) 
| extend url = tostring(cd.url) 
| extend cm = parse_json(customMeasurements) 
| extend duration = todouble(cm.duration) 
| summarize  
count(), percentile(duration, 75) by url, bin(timestamp, 1d) 
| render timechart