我正在尝试使用API快速入门页面(https://dev.applicationinsights.io/quickstart)上给出的C#示例通过API查询Application Insights "traces“数据,我认为我在理解调用工作的路径时遇到了问题。
以下内容摘自快速入门页面...
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");
}
}当我调用它将"query“参数替换为"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");
}
}当我调用它将"events“参数替换为"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 Explorer页面(https://dev.applicationinsights.io/apiexplorer/query)中的" query“> "GET /query”部分,它确实可以正常工作,并返回正确的行:
traces |其中消息包含"1111a11aa1-1111-11aa-1a1a-11aa11a1a11a“(我已经用虚构的GUID替换了真实的GUID)
发布于 2018-03-25 06:59:27
为了防止任何人遇到这个问题,我想分享一下我是如何成功做到的。基本上,我使用了快速入门(https://dev.applicationinsights.io/quickstart)页面上的示例提供的错误的URL常量。我不得不修改它以查询跟踪:
快速入门上的给定示例:
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}";实质上是移动查询字符串参数以匹配GET/query API Explorer (https://dev.applicationinsights.io/apiexplorer/query)在发送查询时所做的操作。
https://stackoverflow.com/questions/48725196
复制相似问题