OpenTelemetry in PowerShell: Observability for Your Scripts
Bring modern observability to your PowerShell scripts and automation pipelines using the OtelCollector module — traces, spans, and metrics from …
Read more →A PowerShell module for sending OpenTelemetry traces and metrics — bring observability to your PowerShell scripts and automation pipelines.
OtelCollector (PSOtelCollector) is a PowerShell module that enables sending OpenTelemetry telemetry data — traces, spans, and metrics — directly from PowerShell scripts to any OpenTelemetry-compatible backend (Jaeger, Zipkin, Grafana Tempo, Azure Monitor, etc.).
Bring modern observability practices to your PowerShell automation, runbooks, and CI/CD pipelines.
Install from the PowerShell Gallery:
# Install for current user
Install-Module -Name OtelCollector -Scope CurrentUser
# Import
Import-Module OtelCollector
Note: Requires PowerShell 7.0 or later.
Import-Module OtelCollector
# Configure the OTLP exporter endpoint
Initialize-OtelTracer -ServiceName "MyAutomation" -Endpoint "http://localhost:4317"
# Start a root span
$span = Start-OtelSpan -Name "deploy-application"
try {
# Add attributes to the span
Set-OtelSpanAttribute -Span $span -Key "deployment.environment" -Value "production"
Set-OtelSpanAttribute -Span $span -Key "app.version" -Value "2.5.0"
# Child span for a sub-operation
$childSpan = Start-OtelSpan -Name "run-migration" -ParentSpan $span
try {
# ... perform database migration ...
Invoke-SqlMigration -Server "prod-db" -Database "AppDB"
} finally {
Stop-OtelSpan -Span $childSpan
}
Write-Host "Deployment complete"
} catch {
Set-OtelSpanStatus -Span $span -Status "Error" -Description $_.Exception.Message
throw
} finally {
Stop-OtelSpan -Span $span
}
| Command | Description |
|---|---|
Initialize-OtelTracer | Configure and initialize the OpenTelemetry tracer |
| Command | Description |
|---|---|
Start-OtelSpan | Begin a new span |
Stop-OtelSpan | End a span and export it |
Set-OtelSpanAttribute | Add a key-value attribute to a span |
Set-OtelSpanStatus | Set the status of a span (Ok / Error) |
Add-OtelSpanEvent | Add a timed event to a span |
Initialize-OtelTracer Parameters| Parameter | Type | Default | Description |
|---|---|---|---|
-ServiceName | string | "PowerShell" | Name of the service reported in traces |
-Endpoint | string | "http://localhost:4317" | OTLP gRPC endpoint URL |
-ServiceVersion | string | "1.0.0" | Service version attribute |
-Headers | hashtable | @{} | Additional headers (e.g., auth tokens) |
Import-Module OtelCollector
Initialize-OtelTracer -ServiceName "CI-Pipeline" -Endpoint "http://otel-collector:4317"
$pipeline = Start-OtelSpan -Name "ci-pipeline"
Set-OtelSpanAttribute -Span $pipeline -Key "git.branch" -Value "main"
Set-OtelSpanAttribute -Span $pipeline -Key "git.commit" -Value $env:GIT_SHA
# Run tests
$testSpan = Start-OtelSpan -Name "run-tests" -ParentSpan $pipeline
# ... run tests ...
Stop-OtelSpan -Span $testSpan
# Build artifact
$buildSpan = Start-OtelSpan -Name "build" -ParentSpan $pipeline
# ... build ...
Stop-OtelSpan -Span $buildSpan
Stop-OtelSpan -Span $pipeline
# Record a counter metric
Add-OtelMetric -Name "jobs.processed" -Value 1 -Type Counter `
-Attributes @{ queue = "email"; status = "success" }
# Record a gauge
Add-OtelMetric -Name "queue.depth" -Value 42 -Type Gauge `
-Attributes @{ queue = "email" }
OtelCollector is compatible with any OTLP-supporting backend:
See the GitHub repository for contribution guidelines.
OtelCollector is released under the MIT License.