You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Application Insights中查看应用的挂起请求?

查看Azure Application Insights中处于等待状态的长请求

默认情况下,Azure Application Insights的Request遥测是在请求完成后才会上传到门户,所以长等待的请求不会实时出现在探查器中。要查看处于等待状态的请求,可以通过以下几种方式实现:

1. 启用实时指标流(Live Metrics Stream)

这是最直接的实时监控方式,无需额外代码配置:

  • 登录Azure门户,打开你的Application Insights资源
  • 左侧菜单选择「实时指标流」,进入后就能看到当前活跃的请求列表,包括正在等待处理的长请求
  • 可以查看请求的持续时间、请求路径、当前状态等关键信息,数据延迟通常在1-2秒内

2. 自定义上报请求启动事件

在后端请求处理的开头主动发送遥测事件,即使请求未完成,也能在Application Insights中看到该请求的启动记录:

  • 在控制器或自定义中间件中注入TelemetryClient,在请求进入时调用TrackEvent:
    private readonly TelemetryClient _telemetryClient;
    
    public YourController(TelemetryClient telemetryClient)
    {
        _telemetryClient = telemetryClient;
    }
    
    [HttpGet("long-running")]
    public async Task<IActionResult> LongRunningRequest()
    {
        // 上报请求启动事件
        _telemetryClient.TrackEvent("LongRunningRequestInitiated", new Dictionary<string, string>
        {
            { "RequestId", HttpContext.TraceIdentifier },
            { "RequestPath", HttpContext.Request.Path },
            { "StartTimeUtc", DateTime.UtcNow.ToString("o") }
        });
    
        // 模拟10分钟等待
        await Task.Delay(TimeSpan.FromMinutes(10));
        return Ok();
    }
    
  • 之后可以在Application Insights的「事件」或「日志」中,通过RequestId关联后续的完成事件,实时追踪未完成的请求

3. 调整遥测发送策略减少延迟

默认的遥测通道会批量缓存数据,可能导致自定义事件延迟上报。可以修改配置让遥测更及时发送:

  • 在Program.cs中调整Application Insights配置:
    services.AddApplicationInsightsTelemetry(options =>
    {
        // 减少缓存容量,优先即时发送
        options.TelemetryChannel.MaxTelemetryBufferCapacity = 1;
        // 设置每5秒刷新一次缓存
        options.TelemetryChannel.FlushInterval = TimeSpan.FromSeconds(5);
    });
    

4. 客户端侧跟踪请求状态

在Angular中通过HTTP拦截器,在请求发起和完成时分别上报事件,配合后端数据完整追踪请求生命周期:

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { AzureMonitoringService } from './azure-monitoring.service';
import { v4 as uuidv4 } from 'uuid';

@Injectable()
export class RequestTrackingInterceptor implements HttpInterceptor {
  constructor(private monitoringService: AzureMonitoringService) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const requestId = uuidv4();
    // 上报客户端请求启动事件
    this.monitoringService.trackEvent('ClientRequestStarted', {
      requestId,
      url: request.url,
      startTime: new Date().toISOString()
    });

    return next.handle(request).pipe(
      finalize(() => {
        // 上报客户端请求完成事件
        this.monitoringService.trackEvent('ClientRequestCompleted', {
          requestId,
          url: request.url,
          endTime: new Date().toISOString()
        });
      })
    );
  }
}

日志查询筛选未完成请求

结合自定义上报的启动事件,用Kusto查询筛选出已启动但未完成的请求:

// 获取所有已启动的请求
let startedRequests = customEvents
| where name == "LongRunningRequestInitiated"
| project RequestId = customDimensions.RequestId, StartTime = todatetime(customDimensions.StartTimeUtc);

// 获取所有已完成的请求
let completedRequests = requests
| project RequestId = operation_Id, EndTime = timestamp;

// 筛选出只启动未完成的请求
startedRequests
| join kind=leftanti completedRequests on RequestId
| project RequestId, StartTime, Duration = now() - StartTime

内容的提问来源于stack exchange,提问作者Sora Teichman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:48:36