forked from mtusi2026/mtusi2026lab6
150 lines
6.2 KiB
PowerShell
150 lines
6.2 KiB
PowerShell
param(
|
|
[string]$BaseUrl = "http://localhost:8080",
|
|
[string]$EnvFile = ".env",
|
|
[int]$LoadSeconds = 75,
|
|
[int]$Concurrency = 4
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
$project = Split-Path -Parent $PSScriptRoot
|
|
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
|
|
$evidenceDir = Join-Path $project "evidence\lab6-$stamp-telemetry-backend"
|
|
New-Item -ItemType Directory -Force -Path $evidenceDir | Out-Null
|
|
|
|
function Query-Scalar([string]$expression) {
|
|
try {
|
|
$encoded = [uri]::EscapeDataString($expression)
|
|
$response = Invoke-RestMethod "http://localhost:9090/api/v1/query?query=$encoded"
|
|
if (@($response.data.result).Count -eq 0) { return 0.0 }
|
|
return [double]$response.data.result[0].value[1]
|
|
}
|
|
catch { return 0.0 }
|
|
}
|
|
|
|
function Convert-MemoryToBytes([string]$value) {
|
|
if ($value -notmatch '^([0-9.]+)([KMG]iB)$') { return 0 }
|
|
$number = [double]$Matches[1]
|
|
$factor = switch ($Matches[2]) {
|
|
'KiB' { 1KB }
|
|
'MiB' { 1MB }
|
|
'GiB' { 1GB }
|
|
}
|
|
return [long]($number * $factor)
|
|
}
|
|
|
|
function Capture-Sample {
|
|
$stats = docker stats --no-stream --format '{{json .}}' $collector | ConvertFrom-Json
|
|
$used = ($stats.MemUsage -split '/')[0].Trim()
|
|
return [ordered]@{
|
|
captured_at = [DateTimeOffset]::UtcNow.ToString("o")
|
|
collector_memory = $used
|
|
collector_memory_bytes = Convert-MemoryToBytes $used
|
|
collector_memory_percent = $stats.MemPerc
|
|
exporter_queue_size = Query-Scalar 'max(otelcol_exporter_queue_size)'
|
|
exporter_queue_capacity = Query-Scalar 'max(otelcol_exporter_queue_capacity)'
|
|
failed_spans_total = Query-Scalar 'sum(otelcol_exporter_send_failed_spans_total) or vector(0)'
|
|
failed_logs_total = Query-Scalar 'sum(otelcol_exporter_send_failed_log_records_total) or vector(0)'
|
|
}
|
|
}
|
|
|
|
function Ensure-Running([string]$container) {
|
|
if ($container -and (docker inspect --format '{{.State.Status}}' $container) -ne 'running') {
|
|
docker start $container | Out-Null
|
|
}
|
|
}
|
|
|
|
$samples = @()
|
|
$loadJob = $null
|
|
Push-Location $project
|
|
try {
|
|
$collector = docker compose --env-file $EnvFile ps -q otel-collector
|
|
$loki = docker compose --env-file $EnvFile ps -q loki
|
|
$tempo = docker compose --env-file $EnvFile ps -q tempo
|
|
if (-not $collector -or -not $loki -or -not $tempo) {
|
|
throw "collector/loki/tempo containers do not exist; start the stack first"
|
|
}
|
|
|
|
$samples += Capture-Sample
|
|
docker stop $loki $tempo | Out-Null
|
|
|
|
$makeOutput = & make test BASE_URL=$BaseUrl PYTHON=python 2>&1
|
|
$makeExit = $LASTEXITCODE
|
|
$makeOutput | Set-Content -Encoding utf8 (Join-Path $evidenceDir "make-test.txt")
|
|
|
|
$loadOutput = Join-Path $evidenceDir "message-load.json"
|
|
$loadJob = Start-Job -ArgumentList $project,$BaseUrl,$LoadSeconds,$Concurrency,$loadOutput -ScriptBlock {
|
|
param($projectPath,$url,$duration,$workers,$outputPath)
|
|
Set-Location $projectPath
|
|
$env:PYTHONUTF8 = "1"
|
|
& python tests/load.py --base-url $url --duration $duration `
|
|
--concurrency $workers --output $outputPath
|
|
[pscustomobject]@{ exit_code = $LASTEXITCODE }
|
|
}
|
|
|
|
while ($loadJob.State -in @('NotStarted','Running')) {
|
|
$samples += Capture-Sample
|
|
Start-Sleep -Seconds 5
|
|
}
|
|
$loadJobResult = Receive-Job $loadJob
|
|
$loadExit = @($loadJobResult | Where-Object { $_.PSObject.Properties.Name -contains 'exit_code' })[-1].exit_code
|
|
$samples += Capture-Sample
|
|
|
|
$maxMemory = (($samples | ForEach-Object { [long]$_['collector_memory_bytes'] }) |
|
|
Measure-Object -Maximum).Maximum
|
|
$maxQueue = (($samples | ForEach-Object { [double]$_['exporter_queue_size'] }) |
|
|
Measure-Object -Maximum).Maximum
|
|
$maxFailedSpans = (($samples | ForEach-Object { [double]$_['failed_spans_total'] }) |
|
|
Measure-Object -Maximum).Maximum
|
|
$maxFailedLogs = (($samples | ForEach-Object { [double]$_['failed_logs_total'] }) |
|
|
Measure-Object -Maximum).Maximum
|
|
$loadReport = Get-Content $loadOutput -Raw | ConvertFrom-Json
|
|
|
|
$samples | ConvertTo-Json -Depth 10 | Set-Content -Encoding utf8 `
|
|
(Join-Path $evidenceDir "collector-samples.json")
|
|
[ordered]@{
|
|
captured_at = [DateTimeOffset]::UtcNow.ToString("o")
|
|
make_test_passed = ($makeExit -eq 0 -and ($makeOutput -join "`n") -match 'PASS:')
|
|
message_load_exit_code = $loadExit
|
|
successful_message_requests = $loadReport.successful_requests
|
|
failed_message_requests = $loadReport.failed_requests
|
|
collector_memory_limit_bytes = 536870912
|
|
collector_memory_limiter_bytes = 402653184
|
|
max_collector_memory_bytes = $maxMemory
|
|
max_exporter_queue_size = $maxQueue
|
|
exporter_queue_capacity = 2048
|
|
failed_spans_total = $maxFailedSpans
|
|
failed_log_records_total = $maxFailedLogs
|
|
bounded = ($maxMemory -lt 536870912 -and $maxQueue -le 2048)
|
|
} | ConvertTo-Json -Depth 10 | Set-Content -Encoding utf8 `
|
|
(Join-Path $evidenceDir "failure-observation.json")
|
|
|
|
if ($makeExit -ne 0 -or $loadExit -ne 0 -or $loadReport.successful_requests -lt 1) {
|
|
throw "business traffic failed while telemetry backends were unavailable"
|
|
}
|
|
}
|
|
finally {
|
|
if ($loadJob) { Remove-Job $loadJob -Force -ErrorAction SilentlyContinue }
|
|
if ($loki) { Ensure-Running $loki }
|
|
if ($tempo) { Ensure-Running $tempo }
|
|
Pop-Location
|
|
}
|
|
|
|
$recoveryDeadline = (Get-Date).AddMinutes(2)
|
|
$recovery = [ordered]@{
|
|
captured_at = [DateTimeOffset]::UtcNow.ToString("o")
|
|
loki_ready = $false
|
|
tempo_ready = $false
|
|
exporter_queue_size = Query-Scalar 'max(otelcol_exporter_queue_size)'
|
|
}
|
|
do {
|
|
Start-Sleep -Seconds 5
|
|
try { $recovery.loki_ready = ((Invoke-WebRequest http://localhost:3100/ready -TimeoutSec 5).StatusCode -eq 200) } catch {}
|
|
try { $recovery.tempo_ready = ((Invoke-WebRequest http://localhost:3200/ready -TimeoutSec 5).StatusCode -eq 200) } catch {}
|
|
} while ((-not $recovery.loki_ready -or -not $recovery.tempo_ready) -and
|
|
(Get-Date) -lt $recoveryDeadline)
|
|
$recovery.exporter_queue_size = Query-Scalar 'max(otelcol_exporter_queue_size)'
|
|
$recovery | ConvertTo-Json -Depth 10 | Set-Content -Encoding utf8 `
|
|
(Join-Path $evidenceDir "recovery-observation.json")
|
|
|
|
Write-Host "Evidence saved to $evidenceDir"
|