Created
April 14, 2026 07:30
-
-
Save trietsch/61f1d8469e80d2f822ee25c6069b1c01 to your computer and use it in GitHub Desktop.
azure_log_analytics_traces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| azure_log_analytics_traces() { | |
| subscription=$1 | |
| resource_group=$2 | |
| workspace_name=$3 | |
| service=$4 | |
| timespan=${5:-"10m"} | |
| level_filter=${6:-""} | |
| if [[ -z "$subscription" || -z "$resource_group" || -z "$workspace_name" || -z "$service" ]]; then | |
| echo "Usage: azure_log_analytics_traces <subscription> <resource_group> <workspace_name> <service> [timespan] [level]" | |
| echo "Or use aliases: logsprd, logsacc, logsdev" | |
| return 1 | |
| fi | |
| # Get workspace ID | |
| workspace_id=$(az monitor log-analytics workspace show --workspace-name "$workspace_name" --resource-group "$resource_group" --subscription "$subscription" --query customerId -o tsv) | |
| if [[ -z "$workspace_id" ]]; then | |
| echo "Failed to get workspace ID" | |
| return 1 | |
| fi | |
| # Build the query | |
| role_name="mcplatform.$service" | |
| query="AppTraces | where AppRoleName == '$role_name' | where TimeGenerated >= ago($timespan)" | |
| # Add level filter if specified | |
| if [[ -n "$level_filter" ]]; then | |
| case "$level_filter" in | |
| DEBUG|debug) query="$query | where SeverityLevel == '0'" ;; | |
| INFO|info) query="$query | where SeverityLevel == '1'" ;; | |
| WARN|warn) query="$query | where SeverityLevel == '2'" ;; | |
| ERROR|error) query="$query | where SeverityLevel == '3'" ;; | |
| *) echo "Unknown level: $level_filter (use DEBUG, INFO, WARN, ERROR)"; return 1 ;; | |
| esac | |
| fi | |
| query="$query | order by TimeGenerated asc" | |
| # Execute query and format output | |
| az monitor log-analytics query \ | |
| --workspace "$workspace_id" \ | |
| --subscription "$subscription" \ | |
| --analytics-query "$query" \ | |
| --output json | \ | |
| jq -r '.[] | "\(.TimeGenerated) [\(.SeverityLevel | if . == "1" then "INFO" elif . == "2" then "WARN" elif . == "3" then "ERROR" else "DEBUG" end)] \(.Message)"' | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put this function in your
.bashrc/.zshrcto be able to use it.