首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试通过REST API在Application Insights中查询"traces“

尝试通过REST API在Application Insights中查询"traces“
EN

Stack Overflow用户
提问于 2018-02-11 04:21:11
回答 1查看 994关注 0票数 0

我正在尝试使用API快速入门页面(https://dev.applicationinsights.io/quickstart)上给出的C#示例通过API查询Application Insights "traces“数据,我认为我在理解调用工作的路径时遇到了问题。

以下内容摘自快速入门页面...

代码语言:javascript
复制
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;
        }
    }
}

当我使用以下参数调用它时,我得到以下错误。

代码语言:javascript
复制
{"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行的返回值,并在顶部显示以下内容

代码语言:javascript
复制
 {"@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”时,我得到了以下错误:

代码语言:javascript
复制
{"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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-25 06:59:27

为了防止任何人遇到这个问题,我想分享一下我是如何成功做到的。基本上,我使用了快速入门(https://dev.applicationinsights.io/quickstart)页面上的示例提供的错误的URL常量。我不得不修改它以查询跟踪:

快速入门上的给定示例:

代码语言:javascript
复制
private const string URL = "https://api.applicationinsights.io/v1/apps/{0}/{1}/{2}?{3}";

我的实现:

代码语言:javascript
复制
private const string URL = "https://api.applicationinsights.io/v1/apps/{0}/{1}?{2}{3}";

实质上是移动查询字符串参数以匹配GET/query API Explorer (https://dev.applicationinsights.io/apiexplorer/query)在发送查询时所做的操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48725196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档