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

如何让TraceTelemetry显示在Application Insights时间线中

解决方案:用自定义操作型遥测实现时间线展示

这个问题我之前也遇到过——TrackTrace确实能记录耗时,但因为它是日志型遥测,只有单一时间戳、没有持续时间范围,所以没法出现在Application Insights的时间线操作条目里。不想用TrackDependency的话,其实有两个更合适的方案,都是利用Application Insights的操作型遥测(带明确开始/结束时间的类型)来实现,既不会被误分类为外部依赖,又能在时间线中展示你的性能分析数据:

方案1:使用自定义RequestTelemetry

虽然RequestTelemetry通常用来记录HTTP请求,但你可以把它用作自定义性能分析操作,通过添加属性标记区分开真实请求。这样它会被Application Insights识别为一个有时间范围的操作,自动出现在时间线中。

修改后的性能分析类代码:

public class PerformanceAnalyzer : IDisposable
{
    private readonly TelemetryClient _telemetryClient;
    private readonly IOperationHolder<RequestTelemetry> _operation;
    private readonly Stopwatch _stopwatch;

    public PerformanceAnalyzer(TelemetryClient telemetryClient, string operationName)
    {
        _telemetryClient = telemetryClient;
        // 启动自定义性能分析操作,命名前缀方便识别
        _operation = _telemetryClient.StartOperation<RequestTelemetry>($"PerfCheck: {operationName}");
        // 添加自定义属性,用于后续筛选区分
        _operation.Telemetry.Properties["OperationCategory"] = "PerformanceAnalysis";
        _stopwatch = Stopwatch.StartNew();
    }

    public void Dispose()
    {
        _stopwatch.Stop();
        // 补充耗时属性(StopOperation会自动计算Duration,这里是额外记录)
        _operation.Telemetry.Properties["ElapsedMs"] = _stopwatch.ElapsedMilliseconds.ToString();
        // 停止操作,自动完成时间范围记录
        _telemetryClient.StopOperation(_operation);
    }
}

方案2:使用标记为Internal的DependencyTelemetry

如果你觉得用RequestTelemetry不够贴切,也可以用DependencyTelemetry,但把它的DependencyKind设置为Internal——这样Application Insights会把它识别为内部操作,而不是外部服务依赖,同样会出现在时间线中。

修改后的性能分析类代码:

public class PerformanceAnalyzer : IDisposable
{
    private readonly TelemetryClient _telemetryClient;
    private readonly IOperationHolder<DependencyTelemetry> _operation;
    private readonly Stopwatch _stopwatch;

    public PerformanceAnalyzer(TelemetryClient telemetryClient, string operationName)
    {
        _telemetryClient = telemetryClient;
        var dependencyTelemetry = new DependencyTelemetry
        {
            Name = $"PerfCheck: {operationName}",
            // 标记为内部操作,避免被当成外部依赖
            DependencyKind = DependencyKind.Internal,
            // 自定义类型,方便后续筛选
            Type = "PerformanceAnalysis",
            Target = "InternalOperation"
        };
        _operation = _telemetryClient.StartOperation(dependencyTelemetry);
        _stopwatch = Stopwatch.StartNew();
    }

    public void Dispose()
    {
        _stopwatch.Stop();
        _operation.Telemetry.Properties["ElapsedMs"] = _stopwatch.ElapsedMilliseconds.ToString();
        _telemetryClient.StopOperation(_operation);
    }
}

效果说明

用这两种方案后,你的性能分析操作会在Azure门户的Application Insights中这样展示:

  • 在Transaction Search页面:你可以通过自定义属性(比如OperationCategory:PerformanceAnalysis)筛选出这些操作,它们会以带时间范围的条目出现在时间线中,和其他请求/内部依赖一起展示。
  • 在Performance面板:你可以创建自定义筛选器,查看这些性能分析操作的耗时分布、平均时长等指标,甚至添加到仪表板。

为什么TrackTrace不行?

TrackTrace属于日志型遥测,它只记录一个时间点的信息,没有开始和结束时间的范围。Application Insights的时间线是基于有持续时间的操作型遥测(Request、Dependency)来渲染的,所以日志条目不会被纳入时间线的操作展示中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:23:42