0
votes

I am attempting to query the Application Insights "traces" data via the API by using the C# example given on the API Quickstart page (https://dev.applicationinsights.io/quickstart) and I think I am having an issue understanding the path to make the call work.

The following was taken from the Quickstart page...

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;
        }
    }
}

When I call it using the following parameters I get the following error.

{"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");
    }
}

When I call it replacing the "query" param with "events" I get over 500 rows returned with the following at the top

 {"@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");
    }
}

When I call it replacing the "events" param with "metrics" I get the following error:

{"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");
    }
}

So I don't know if the way I am passing the query is incorrect or if I am trying something that is not possible. The query was taken from the API Explorer page (https://dev.applicationinsights.io/apiexplorer/query) in the "Query" > "GET /query" section and it does work as expected returning the correct row:

traces | where message contains "1111a11aa1-1111-11aa-1a1a-11aa11a1a11a" (I've replaced the real GUID's with made up ones)

1

1 Answers

0
votes

Just in case anyone ever comes across this I wanted to share how I did it successfully. Basically, I was using the wrong URL constant provided by the example on the quickstart (https://dev.applicationinsights.io/quickstart) page. I had to modify it in order to query Traces:

The given example on the quickstart:

private const string URL = "https://api.applicationinsights.io/v1/apps/{0}/{1}/{2}?{3}";

My implementation:

private const string URL = "https://api.applicationinsights.io/v1/apps/{0}/{1}?{2}{3}";

essentially moving the query string params to match what the GET/query API Explorer (https://dev.applicationinsights.io/apiexplorer/query) does when sending a query.