【Application Insights】使用CURL命令向Application Insgihts发送测试数据 - LuBu0505/My-Code GitHub Wiki

问题描述

在使用App Service或者Kubernetes等服务时,需要收集一些日志数据并且发送到Application Insights中,当使用SDK或者是服务自带的Application Insights配置时遇见问题,有没有快速的调试方式能验证App Service是否与Application Insights之间的连通性呢? image.png

问题解答

可以使用CURL发送请求到Application Insigths的Ingress Endpoint来验证当前环境是否可以成功发送请求!

根据参考文档(用于发送可用性测试结果的 Curl 命令)中介绍,使用 curl 发送 REST 请求,引入终结点主机名、 iKey 值和 time 值。 Application Insights 引入终结点不接受任何超过 48 小时的记录。

###适用于 Linux/MacOS 的 Curl 命令: curl -H "Content-Type: application/json" -X POST -d '{"data":{"baseData":{"ver":2,"id":"SampleRunId","name":"MicrosoftSupportSampleWebtestResultUsingCurl","duration":"00.00:00:10","success":true,"runLocation":"RegionName","message":"SampleWebtestResult","properties":{"SampleProperty":"SampleValue"}},"baseType":"AvailabilityData"},"ver":1,"name":"Microsoft.ApplicationInsights.Metric","time":"2024-01-08T12:57:25.0000000Z","sampleRate":100,"iKey":"########-####-####-####-############","flags":0}'  https://chinanorth3-0.in.applicationinsights.azure.cn/v2/track   -v

### 适用于 Windows 的 Curl 命令: curl -H "Content-Type: application/json" -X POST -d {"data":{"baseData":{"ver":2,"id":"SampleRunId","name":"MicrosoftSupportSampleWebtestResultUsingCurl","duration":"00.00:00:10","success":true,"runLocation":"RegionName","message":"SampleWebtestResult","properties":{"SampleProperty":"SampleValue"}},"baseType":"AvailabilityData"},"ver":1,"name":"Microsoft.ApplicationInsights.Metric","time":"2024-01-08T12:57:25.0000000Z","sampleRate":100,"iKey":"########-####-####-####-############","flags":0} https://chinanorth3-0.in.applicationinsights.azure.cn/v2/track   -v

在以上内容中,需要修改的内容有

下图为在App Service Kudu中的测试和验证过程:

第一步:准备 curl 命令并进入App Service Kudu站点:

C:\home>curl -H "Content-Type: application/json" -X POST -d {\"data\":{\"baseData\":{\"ver\":2,\"id\":\"SampleRunId\",\"name\":\"MicrosoftSupportSampleWebtestResultUsingCurl\",\"duration\":\"00.00:00:10\",\"success\":true,\"runLocation\":\"RegionName\",\"message\":\"SampleWebtestResult\",\"properties\":{\"SampleProperty\":\"SampleValue\"}},\"baseType\":\"AvailabilityData\"},\"ver\":1,\"name\":\"Microsoft.ApplicationInsights.Metric\",\"time\":\"2024-01-08T13:09:00.0000000Z\",\"sampleRate\":100,\"iKey\":\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\",\"flags\":0} https://chinanorth3-0.in.applicationinsights.azure.cn/v2/track

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed 100   493  100    49  100   444    436   3956 --:--:-- --:--:-- --:--:--  5358 {"itemsReceived":1,"itemsAccepted":1,"errors":[]}

curl application insights.gif

第二步:在Application Insights中验证数据是否发送成功

进入Application Insights的Logs页面,直接查看 availabilityResults 表中最近1小时的数据: image.png

参考资料

用于发送可用性测试结果的 Curl 命令 : https://learn.microsoft.com/zh-cn/troubleshoot/azure/azure-monitor/app-insights/investigate-missing-telemetry#curl-command-send-availability-test-result

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!