|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Retrieves all spans for a given trace ID from the Datadog API. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +Queries the Datadog Spans API to retrieve all spans belonging to a specific trace ID. |
| 7 | +Requires DD_API_KEY and DD_APPLICATION_KEY environment variables to be set. |
| 8 | +
|
| 9 | +.PARAMETER TraceId |
| 10 | +The 128-bit trace ID to query (hex string, e.g., "690507fc00000000b882bcd2bdac6b9e") |
| 11 | +
|
| 12 | +.PARAMETER TimeRange |
| 13 | +How far back to search for the trace. Defaults to "2h" (2 hours). |
| 14 | +Examples: "15m", "1h", "2h", "1d" |
| 15 | +
|
| 16 | +.PARAMETER OutputFormat |
| 17 | +Output format: "table" (default), "json", or "hierarchy" |
| 18 | +
|
| 19 | +.EXAMPLE |
| 20 | +.\Get-DatadogTrace.ps1 -TraceId "690507fc00000000b882bcd2bdac6b9e" |
| 21 | +
|
| 22 | +.EXAMPLE |
| 23 | +.\Get-DatadogTrace.ps1 -TraceId "690507fc00000000b882bcd2bdac6b9e" -OutputFormat hierarchy |
| 24 | +
|
| 25 | +.EXAMPLE |
| 26 | +.\Get-DatadogTrace.ps1 -TraceId "690507fc00000000b882bcd2bdac6b9e" -OutputFormat json | ConvertFrom-Json | ConvertTo-Json -Depth 10 |
| 27 | +#> |
| 28 | + |
| 29 | +[CmdletBinding()] |
| 30 | +param( |
| 31 | + [Parameter(Mandatory = $true, Position = 0)] |
| 32 | + [string]$TraceId, |
| 33 | + |
| 34 | + [Parameter(Mandatory = $false)] |
| 35 | + [string]$TimeRange = "2h", |
| 36 | + |
| 37 | + [Parameter(Mandatory = $false)] |
| 38 | + [ValidateSet("table", "json", "hierarchy")] |
| 39 | + [string]$OutputFormat = "table" |
| 40 | +) |
| 41 | + |
| 42 | +# Check for required environment variables |
| 43 | +$apiKey = $env:DD_API_KEY |
| 44 | +$appKey = $env:DD_APPLICATION_KEY |
| 45 | + |
| 46 | +if ([string]::IsNullOrEmpty($apiKey)) { |
| 47 | + Write-Error "DD_API_KEY environment variable is not set. Please set it before running this script." |
| 48 | + exit 1 |
| 49 | +} |
| 50 | + |
| 51 | +if ([string]::IsNullOrEmpty($appKey)) { |
| 52 | + Write-Error "DD_APPLICATION_KEY environment variable is not set. Please set it before running this script." |
| 53 | + exit 1 |
| 54 | +} |
| 55 | + |
| 56 | +# Build the request |
| 57 | +$uri = "https://api.datadoghq.com/api/v2/spans/events/search" |
| 58 | +$headers = @{ |
| 59 | + "DD-API-KEY" = $apiKey |
| 60 | + "DD-APPLICATION-KEY" = $appKey |
| 61 | + "Content-Type" = "application/json" |
| 62 | +} |
| 63 | + |
| 64 | +$body = @{ |
| 65 | + data = @{ |
| 66 | + attributes = @{ |
| 67 | + filter = @{ |
| 68 | + query = "trace_id:$TraceId" |
| 69 | + from = "now-$TimeRange" |
| 70 | + to = "now" |
| 71 | + } |
| 72 | + sort = "start_timestamp" |
| 73 | + page = @{ |
| 74 | + limit = 100 |
| 75 | + } |
| 76 | + } |
| 77 | + type = "search_request" |
| 78 | + } |
| 79 | +} | ConvertTo-Json -Depth 10 |
| 80 | + |
| 81 | +Write-Verbose "Querying Datadog API for trace ID: $TraceId" |
| 82 | +Write-Verbose "Time range: now-$TimeRange to now" |
| 83 | + |
| 84 | +try { |
| 85 | + $response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body -ErrorAction Stop |
| 86 | + |
| 87 | + if ($null -eq $response.data -or $response.data.Count -eq 0) { |
| 88 | + Write-Warning "No spans found for trace ID: $TraceId" |
| 89 | + Write-Warning "Make sure the trace ID is correct and the trace is within the time range (now-$TimeRange to now)" |
| 90 | + exit 0 |
| 91 | + } |
| 92 | + |
| 93 | + $spans = $response.data | ForEach-Object { |
| 94 | + $attrs = $_.attributes |
| 95 | + |
| 96 | + # Convert duration (nanoseconds to milliseconds) |
| 97 | + $durationMs = if ($attrs.custom.duration) { |
| 98 | + [math]::Round([double]$attrs.custom.duration / 1000000.0, 2) |
| 99 | + } else { |
| 100 | + 0 |
| 101 | + } |
| 102 | + |
| 103 | + [PSCustomObject]@{ |
| 104 | + SpanId = $attrs.span_id |
| 105 | + ParentId = $attrs.parent_id |
| 106 | + OperationName = $attrs.operation_name |
| 107 | + ResourceName = $attrs.resource_name |
| 108 | + Status = $attrs.status |
| 109 | + Duration = $durationMs |
| 110 | + Process = $attrs.custom.aas.function.process |
| 111 | + Service = $attrs.service |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + switch ($OutputFormat) { |
| 116 | + "table" { |
| 117 | + Write-Host "`nFound $($spans.Count) spans in trace:" -ForegroundColor Green |
| 118 | + Write-Host "Trace ID: $TraceId`n" -ForegroundColor Cyan |
| 119 | + |
| 120 | + $spans | Format-Table -Property @( |
| 121 | + @{Label = "Operation"; Expression = { $_.OperationName }; Width = 25 } |
| 122 | + @{Label = "Resource"; Expression = { $_.ResourceName }; Width = 30 } |
| 123 | + @{Label = "Span ID"; Expression = { $_.SpanId }; Width = 20 } |
| 124 | + @{Label = "Parent ID"; Expression = { $_.ParentId }; Width = 20 } |
| 125 | + @{Label = "Process"; Expression = { $_.Process }; Width = 8 } |
| 126 | + @{Label = "Duration (ms)"; Expression = { $_.Duration }; Width = 12 } |
| 127 | + ) -AutoSize |
| 128 | + } |
| 129 | + "json" { |
| 130 | + $spans | ConvertTo-Json -Depth 10 |
| 131 | + } |
| 132 | + "hierarchy" { |
| 133 | + Write-Host "`nFound $($spans.Count) spans in trace:" -ForegroundColor Green |
| 134 | + Write-Host "Trace ID: $TraceId`n" -ForegroundColor Cyan |
| 135 | + |
| 136 | + # Build hierarchy |
| 137 | + $spanMap = @{} |
| 138 | + foreach ($span in $spans) { |
| 139 | + $spanMap[$span.SpanId] = $span |
| 140 | + } |
| 141 | + |
| 142 | + function Show-SpanHierarchy { |
| 143 | + param( |
| 144 | + [PSCustomObject]$Span, |
| 145 | + [int]$Indent = 0, |
| 146 | + [hashtable]$SpanMap, |
| 147 | + [PSCustomObject[]]$AllSpans |
| 148 | + ) |
| 149 | + |
| 150 | + $prefix = " " * $Indent |
| 151 | + $arrow = if ($Indent -gt 0) { "└─ " } else { "" } |
| 152 | + |
| 153 | + $processTag = if ($Span.Process) { " [$($Span.Process)]" } else { "" } |
| 154 | + Write-Host "$prefix$arrow$($Span.OperationName)$processTag" -ForegroundColor Cyan |
| 155 | + Write-Host "$prefix Resource: $($Span.ResourceName)" -ForegroundColor Gray |
| 156 | + Write-Host "$prefix Span ID: $($Span.SpanId), Parent: $($Span.ParentId)" -ForegroundColor DarkGray |
| 157 | + Write-Host "$prefix Duration: $($Span.Duration) ms`n" -ForegroundColor DarkGray |
| 158 | + |
| 159 | + # Find children |
| 160 | + $children = $AllSpans | Where-Object { $_.ParentId -eq $Span.SpanId } |
| 161 | + foreach ($child in $children) { |
| 162 | + Show-SpanHierarchy -Span $child -Indent ($Indent + 1) -SpanMap $SpanMap -AllSpans $AllSpans |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + # Find root span(s) (parent_id = 0) |
| 167 | + $rootSpans = $spans | Where-Object { $_.ParentId -eq "0" } |
| 168 | + |
| 169 | + if ($rootSpans.Count -eq 0) { |
| 170 | + Write-Warning "No root span found (parent_id = 0). Showing all spans:" |
| 171 | + $spans | ForEach-Object { |
| 172 | + Show-SpanHierarchy -Span $_ -Indent 0 -SpanMap $spanMap -AllSpans $spans |
| 173 | + } |
| 174 | + } else { |
| 175 | + foreach ($root in $rootSpans) { |
| 176 | + Show-SpanHierarchy -Span $root -Indent 0 -SpanMap $spanMap -AllSpans $spans |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | +} catch { |
| 183 | + Write-Error "Failed to query Datadog API: $_" |
| 184 | + Write-Error $_.Exception.Message |
| 185 | + if ($_.ErrorDetails.Message) { |
| 186 | + Write-Error "API Response: $($_.ErrorDetails.Message)" |
| 187 | + } |
| 188 | + exit 1 |
| 189 | +} |
0 commit comments