Search code examples
azureazure-application-insightsazure-log-analytics

How to access AppInsights request data via the Log Analytics API?


I am trying to access request data (custom dimensions field) from my AppInsights instance via the Log Analytics API. I am able to retrieve the data I want via the Log Analytics UI in the browser by selecting my AppInsights instance as the scope and filtering by "requests". However, when I try to filter by requests via the API I get the message that requests is an invalid table name.

Full error message is:

Failed to resolve table or column expression named 'requests'

I have configured my authentication request to successfully retrieve the token as described in the client credentials flow section here.

My authentication request looks like this:

POST https://login.microsoftonline.com/<my-tenant-id>/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
&client_id=<app-client-id>
&resource=https://api.loganalytics.io
&client_secret=<app-client-secret>

My query request looks like this:

POST https://api.loganalytics.io/v1/workspaces/<my-workspace-id>/query?timespan=P1D
Content-Type: application/json
Authorization: Bearer <token>

Body:
{
    "query": "requests | where success == false "
}

Which returns:

{
    "error": {
        "message": "The request had some invalid properties",
        "code": "BadArgumentError",
        "correlationId": "",
        "innererror": {
            "code": "SemanticError",
            "message": "A semantic error occurred.",
            "innererror": {
                "code": "SEM0100",
                "message": "'where' operator: Failed to resolve table or column expression named 'requests'"
            }
        }
    }
}

The above query works as expected from the Log Analytics UI, once the App Insights instance has been selected as the scope of the query.

Is there a way to specify the scope/resource group of the query in the Log Analytics API request? I have tried to find details here and here but there are no references to scope. Any help is appreciated.


Solution

  • I figured it out, I was using the incorrect syntax on the query.

    The below syntax gives me back what I'm expecting without having to specify a scope:

    {
        "query": "AppRequests | where Name has '<request-name>' | project Name, Properties"
    }