我正在尝试查询Application Insights" trace"通过使用API快速入门页面(https://dev.applicationinsights.io/quickstart)上给出的C#示例通过API获取数据,我认为我在理解调用工作的路径时遇到了问题。
以下内容摘自快速入门页面......
public class QueryAppInsights
{
private const string URL = "https://api.applicationinsights.io/v1/apps/{0}/{1}/{2}?{3}";
public static string GetTelemetry(string appid, string apikey, string queryType, string queryPath, string parameterString)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", apikey);
var req = string.Format(URL, appid, queryType, queryPath, parameterString);
HttpResponseMessage response = client.GetAsync(req).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.WriteLine(response.StatusCode);
return response.Content.ReadAsStringAsync().Result;
}
else
{
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.WriteLine(response.StatusCode);
return response.ReasonPhrase;
}
}
}
当我使用以下参数调用它时,出现以下错误。
{"error":{"message":"The requested path does not exist","code":"PathNotFoundError"}}
NotFound
public class TestSuite
{
public void CallTest()
{
QueryAppInsights.GetTelemetry("My Application ID", "My API Key", "query", "traces", "timespan=P7D&query=traces%7C%20where%20message%20contains%20%1111a11aa1-1111-11aa-1a1a-11aa11a1a11a%22");
}
}
当我称之为替换"查询" param with" events"我在顶部
返回了超过500行,其中包含以下内容 {"@odata.context":"https://api.applicationinsights.io/v1/apps/GUID PLACE HOLDER/events/$metadata#traces","@ai.messages":[{"code":"AddedLimitToQuery","message":"The query was limited to 500 rows"}
public class TestSuite
{
public void CallTest()
{
QueryAppInsights.GetTelemetry("My Application ID", "My API Key", "events", "traces", "timespan=P7D&query=traces%7C%20where%20message%20contains%20%1111a11aa1-1111-11aa-1a1a-11aa11a1a11a%22");
}
}
当我打电话给它取代"事件" param with" metrics"我收到以下错误:
{"error":{"message":"The requested item was not found","code":"ItemNotFoundError","innererror":{"code":"MetricNotFoundError","message":"Metric traces does not exist"}}}
NotFound
public class TestSuite
{
public void CallTest()
{
QueryAppInsights.GetTelemetry("My Application ID", "My API Key", "metrics", "traces", "timespan=P7D&query=traces%7C%20where%20message%20contains%20%1111a11aa1-1111-11aa-1a1a-11aa11a1a11a%22");
}
}
所以我不知道我传递查询的方式是不正确的,还是我正在尝试一些不可能的事情。该查询来自"查询"中的API资源管理器页面(https://dev.applicationinsights.io/apiexplorer/query)。 > " GET / query"部分,它按预期工作返回正确的行:
迹线 |其中消息包含" 1111a11aa1-1111-11aa-1a1a-11aa11a1a11a" (我已经用组成的GUID替换了真正的GUID)
答案 0 :(得分:0)
万一有人遇到这个问题,我想分享一下我是如何成功的。基本上,我使用了快速启动(https://dev.applicationinsights.io/quickstart)页面上的示例提供的错误URL常量。我必须修改它才能查询Traces:
快速入门的给定示例:
private const string URL = "https://api.applicationinsights.io/v1/apps/{0}/{1}/{2}?{3}";
我的实施:
private const string URL = "https://api.applicationinsights.io/v1/apps/{0}/{1}?{2}{3}";
基本上移动查询字符串params以匹配GET /查询API资源管理器(https://dev.applicationinsights.io/apiexplorer/query)在发送查询时所执行的操作。