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

如何阻止AppInsights接收HTTP POST请求时自动扁平化字符串数组

问题原因

Azure Application Insights 的 v2/track 数据摄取接口原生设计中,customDimensions 仅支持字符串类型的键值对,所有非字符串的复杂类型(数组、嵌套对象等)都会被默认扁平化处理:数组会被自动拼接为逗号分隔的字符串,嵌套对象会被序列化为JSON字符串,该行为是接口默认逻辑,无直接上报参数可关闭。

可行解决方案

方案1:预序列化+查询还原(兼容性最高,改造成本最低)

这是官方推荐的标准处理方式:

  • 上报前将数组先序列化为JSON格式的字符串,再填入properties对应字段,你给出的C++代码修改示例如下:
int main(void)
{
    CURL *curl;

    curl_global_init(CURL_GLOBAL_ALL);

    curl = curl_easy_init();
    if (curl) {
        // 数组提前序列化为转义后的JSON字符串
        const char *line = 
            "{"
                "\"data\": {"
                    "\"baseData\": {"
                        "\"name\": \"UWMLicensing\","
                        "\"properties\": {"
                        "\"myValues\": \"[\\\"1\\\", \\\"2\\\"]\""
                        "},"
                        "\"ver\": \"2\""
                    "},"
                    "\"baseType\": \"EventData\""
                "},"
                "\"iKey\": \"<YOUR_INSTRUMENTATION_KEY>\","
                "\"name\": \"Event\","
                "\"time\": \"%s\""
            "}";

        char json[255];
        sprintf_s(json, 255, line, getTime().c_str());

        struct curl_slist *list = NULL;
        list = curl_slist_append(list, "Content-Type: application/json; charset=UTF-8");
        curl_easy_setopt(curl, CURLOPT_URL, "https://dc.services.visualstudio.com/v2/track");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);

        curl_easy_perform(curl);
        curl_slist_free_all(list);

        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();

    return 0;
}
  • 在App Insights查询数据时,使用Kusto查询语言的parse_json方法即可将字符串还原为原始数组结构,可直接进行数组相关操作:
customEvents
| extend myValues = parse_json(customDimensions.myValues)
// 示例:筛选数组包含元素"1"的事件
| where array_contains(myValues, "1")

方案2:使用复杂类型扩展规则(仅支持较新的Azure Monitor工作区)

如果你的App Insights资源已迁移到Azure Monitor工作区,可以通过上报扩展格式直接保留数组结构:

  • 在properties节点下新增固定扩展标记字段:"$schema": "https://azure.microsoft.com/schemas/2020/09/extensibility"
  • 数组字段直接按原始结构放在properties下即可,接口会自动识别并保留数组结构。

注意事项

customDimensions 单个属性值的长度上限为8192字符,若数组序列化后超过该长度,建议拆分存储或者将数组写入自定义日志表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 19:54:02