Created
January 14, 2025 17:01
-
-
Save cdhunt/d3055f6a097a0c01fa72d958e2eeeb51 to your computer and use it in GitHub Desktop.
A fork of https://gist.github.com/StartAutomating/fb229d462f2b8bbeda67220c2b5740d5 with Open Telemetry Instrumentation using https://github.com/cdhunt/Potel
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
$port = Get-Random -Min 4200 -Max 42000 | |
$JobName = "http://localhost:$($port)/" | |
$activitySource = New-ActivitySource -Name "littleweb" | |
New-TracerProviderBuilder | | |
Add-TracerSource -ActivitySource $activitySource | | |
Add-ResourceConfiguration -ServiceName $ExecutionContext.Host.Name -Attribute @{"host.name" = $(hostname); "host.port" = $port } | | |
Add-ExporterOtlpTrace -Endpoint http://localhost:4317 | | |
Add-ExporterConsole | | |
Start-Tracer | |
$httpListener = [Net.HttpListener]::new() | |
$httpListener.Prefixes.Add($JobName) | |
$httpListener.Start() | |
Start-ThreadJob -ScriptBlock { | |
param($MainRunspace, $httpListener, $SourceIdentifier = 'http', $activitySource) | |
while ($httpListener.IsListening) { | |
$activity = $activitySource | Start-Activity -Name "Get Context" -Kind Server | |
$contextAsync = $httpListener.GetContextAsync() | |
while (-not ($contextAsync.IsCompleted -or $contextAsync.IsFaulted -or $contextAsync.IsCanceled)) {} | |
if ($contextAsync.IsFaulted) { | |
Write-Error -Exception $contextAsync.Exception -Category ProtocolError | |
continue | |
} | |
$context = $(try { $contextAsync.Result } catch { $_ }) | |
$url = $context.Request.Url | |
if ($url -match '/favicon.ico$') { | |
$context.Response.StatusCode = 404 | |
$context.Response.Close() | |
continue | |
} | |
$activity | Add-ActivityEvent -Message ("Generate Event. Url = {0}" -f $context.Request.Url) | |
$MainRunspace.Events.GenerateEvent( | |
$SourceIdentifier, $httpListener, @($context, $context.Request, $context.Response), | |
[Ordered]@{Url = $context.Request.Url; Context = $context; Request = $context.Request; Response = $context.Response } | |
) | |
$activity.Stop() | |
} | |
} -Name $JobName -ArgumentList ([Runspace]::DefaultRunspace, $httpListener, 'http', $activitySource) -ThrottleLimit 50 -InitializationScript { Import-Module potel } | | |
Add-Member -NotePropertyMembers ([Ordered]@{HttpListener = $httpListener }) -PassThru | |
Write-Host "Now Serving $jobName" -ForegroundColor Green | |
$rng = [Random]::new() | |
while ($httpListener.IsListening) { | |
foreach ($event in @(Get-Event HTTP*)) { | |
$activity = $activitySource | Start-Activity -Name "Process Event" -Kind Server | |
$context, $request, $response = $event.SourceArgs | |
if (-not $response.OutputStream) { continue } | |
$response.Close($outputEncoding.GetBytes("$($rng.Next())"), $false) | |
$activity | Add-ActivityEvent -Message "Responded to $($request.Url) in $([DateTime]::Now - $event.TimeGenerated)" | |
$event | Remove-Event | |
$activity.Stop() | |
} | |
} | |
Stop-Tracer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment