gsscoder
gsscoder

Reputation: 3392

App Insights REST call using KQL query returns empty

Following Microsoft documentation I'd like to query an App Insights resource for trace events. If I directly access the trace using the specific endpoint:

https://api.applicationinsights.io/v1/apps/$aiAppId/events/traces

it perfectly works. But I'm unable to use a KQL query:

$aiAppId = 'my_app_insights_app_id'
$aiApiKey = 'my_app_insights_api_key'

$content = (Invoke-WebRequest -Uri "https://api.applicationinsights.io/v1/apps/$aiAppId/query" `
    -Method Post -UseBasicParsing `
    -Headers @{
        'x-api-key' = $aiApiKey
     } `
    -Body @{
        'timespan' = 'PT60M'
        'query' = 'traces | where timestamp >= ago(30m)'
     }
).Content

$content | ConvertFrom-Json

It yields an empty result:

tables
------
{}

Executing the KQL query from Azure portal I get correct results.

Am I missing something? Any help really appreciated.

Best regards.

Giacomo S. S.

Upvotes: 1

Views: 769

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29995

The type of $content | ConvertFrom-Json is PSCustomObject. So it shows the result as you mentioned:

enter image description here

You can use code like $s = $content | ConvertFrom-Json, then use $s.tables.rows or $s.tables.columns to display the details.

Or just use $content, then you can see the correct output:

enter image description here

And please consider writing your own logic to parse the result instead of directly use ConvertFrom-Json.

Upvotes: 1

Related Questions