You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何在Application Insights中添加并查看API的Post请求体参数?

嘿,我之前也碰到过一模一样的问题!要在Application Insights里看到POST请求的体参数,其实通过API Management的策略就能搞定,下面是具体的操作步骤和注意事项:

如何在Application Insights中添加POST请求体数据

核心思路是通过API Management的自定义策略,把请求体注入到跟踪日志或自定义属性中,让Application Insights能捕获到这些数据。

步骤1:在APIM中配置请求体捕获策略

你可以在API的操作级别(针对特定接口)或全局级别添加策略,先读取请求体再把它附加到监控数据里。直接上可用的策略代码:

<inbound>
    <!-- 读取请求体,这里一定要加preserveContent: true,不然读完后后续流程就拿不到请求数据了! -->
    <set-variable name="requestBody" value="@(context.Request.Body.As<string>(preserveContent: true))" />
    
    <!-- 方式1:把请求体作为自定义元数据加入APIM跟踪日志,会自动同步到Application Insights -->
    <trace source="APIM-Request-Body" severity="Information">
        <message>POST Request Body: @((string)context.Variables["requestBody"])</message>
        <metadata name="RequestBody" value="@((string)context.Variables["requestBody"])" />
    </trace>

    <!-- 方式2:添加自定义请求头,后续在Application Insights中配置捕获这个头 -->
    <add-custom-header name="X-APIM-Request-Body" value="@((string)context.Variables["requestBody"])" />
</inbound>

步骤2:在Application Insights中查看数据

  • 如果用了trace策略:直接打开Application Insights的跟踪面板,筛选source = APIM-Request-Body,就能看到包含请求体的日志条目,点击详情还能看到RequestBody这个自定义元数据字段。
  • 如果用了自定义请求头:需要在Application Insights的配置里,开启捕获自定义请求头X-APIM-Request-Body,之后就能在请求遥测的“自定义属性”里找到它。

必看注意事项

  • 敏感数据脱敏:如果请求体里有密码、令牌、手机号这类敏感信息,一定要先处理再记录!比如用正则替换敏感字段:
    <set-variable name="sanitizedRequestBody" value="@(Regex.Replace((string)context.Variables["requestBody"], "\"password\":\"[^\"]*\"", "\"password\":\"***\""))" />
    
  • 性能影响:如果是超大请求体(比如几百KB以上),频繁捕获会拖慢APIM和Application Insights的性能,建议只在关键接口启用,或者设置采样率。
  • 非文本请求体:如果是文件上传这类二进制请求,完全没必要捕获,只针对JSON/XML这类文本格式的POST请求处理就行。

备选方案:后端代码直接捕获

如果你的API后端是.NET/Java这类语言,也可以用Application Insights SDK在代码里直接捕获请求体,比如ASP.NET Core里写个中间件:

using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Http;

public class RequestBodyTrackingMiddleware
{
    private readonly RequestDelegate _next;
    private readonly TelemetryClient _telemetryClient;

    public RequestBodyTrackingMiddleware(RequestDelegate next, TelemetryClient telemetryClient)
    {
        _next = next;
        _telemetryClient = telemetryClient;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        if (context.Request.Method == HttpMethods.Post && context.Request.ContentType?.Contains("application/json") == true)
        {
            context.Request.EnableBuffering();
            // 读取请求体后要重置流位置,不然后续中间件读不到
            using var reader = new StreamReader(context.Request.Body, leaveOpen: true);
            var requestBody = await reader.ReadToEndAsync();
            context.Request.Body.Position = 0;

            // 把请求体添加到请求遥测的自定义属性
            var requestTelemetry = context.Features.Get<RequestTelemetry>();
            requestTelemetry?.Properties.Add("RequestBody", requestBody);
        }

        await _next(context);
    }
}

这种方法适合需要在后端做额外处理的场景,但APIM策略的方式更无侵入,不需要改后端代码。


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

火山引擎 最新活动