【Application Insights】使用CURL命令向Application Insgihts发送测试数据

问题描述

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

 

 

问题解答

可以使用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

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

  • time: 修改为发送请求的当前时间点,如2024-01-08T12:57:25.0000000Z
  • iKey :  在Application Insights的Overview页面中,复制 Instrumentation Key 的值
  • Application Insights的终结点,https://dc.applicationinsights.azure.com/v2/track 为global azure的终结点,如果使用的是中国区的Application Insights,需要从连接字符串中提取Ingress Endpoint,完成的值为:
    • 中国东部2区:https://chinaeast2-0.in.applicationinsights.azure.cn/v2/track 
    • 中国北部3区:https://chinanorth3-0.in.applicationinsights.azure.cn/v2/track

 

下图为在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":[]}

 

 

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

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

 

 

参考资料

 

posted @ 2024-01-08 21:19  路边两盏灯  阅读(9)  评论(0编辑  收藏  举报