Skip to content

Commit

Permalink
Adds support for HttpClientInstrumentationOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hunt committed Dec 24, 2024
1 parent d1bccfb commit 76c23cb
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ New-TracerProviderBuilder |
Add-ExporterConsole |
Start-Tracer
```

See the [sample](samples/) module for a walkthrough of how to instrument your own module.
1 change: 1 addition & 0 deletions docs/Add-HttpClientInstrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Adds Http Client Instrumentation
### Parameter Set 1

- `[TracerProviderBuilderBase]` **TracerProvider** _Instance of TracerProviderBuilderBase._ Mandatory, ValueFromPipeline
- `[Action[OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions]]` **Options** _Parameter help description_

## Examples

Expand Down
42 changes: 42 additions & 0 deletions docs/New-HttpClientTraceInstrumentationOption.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# New-HttpClientTraceInstrumentationOption

Initializes a new instance of the HttpClientTraceInstrumentationOptions class for HttpClient instrumentation.

## Parameters

### Parameter Set 1

- `[Func[Net.Http.HttpRequestMessage, bool]]` **RequestFilter** _A filter function that determines whether or not to collect telemetry on a per request basis._
- `[Switch]` **RecordException** _Indicating whether exception will be recorded ActivityEvent or not. This instrumentation automatically sets Activity Status to Error if the Http StatusCode is >= 400. Additionally, `RecordException` feature may be turned on, to store the exception to the Activity itself as ActivityEvent._

## Examples

### Example 1

Collect only web requests with a `Method` of `Get`.

```powershell
$options = New-HttpClientTraceInstrumentationOption -RequestFilter {param([Net.Http.HttpRequestMessage]$request) $request.Method -eq 'Get' }
New-TracerProviderBuilder | Add-HttpClientInstrumentation -Options $options
```

### Example 2

Collect only web requests send to the "google.com" domain.

```powershell
$options = New-HttpClientTraceInstrumentationOption -RequestFilter {param([Net.Http.HttpRequestMessage]$request) $request.RequestUri -like '*google.com*' }
New-TracerProviderBuilder | Add-HttpClientInstrumentation -Options $options
```

## Links

- [https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httprequestmessage?view=netstandard-2.1#properties](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httprequestmessage?view=netstandard-2.1#properties)

## Notes

Filter scriptblock must return a bool. `[Func[Net.Http.HttpRequestMessage, bool]]`

## Outputs

- `OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions`
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ PowerShell module for collecting and sending Open Telemetry
- [Enable-OtelDiagnosticLog](Enable-OtelDiagnosticLog.md) _Enable the internal logs generated by all OpenTelemetry components._
- [Get-OtelDiagnosticLog](Get-OtelDiagnosticLog.md) _Get the contents of the diagnostic log._
- [New-ActivitySource](New-ActivitySource.md) _Create an instance of ActivitySource._
- [New-HttpClientTraceInstrumentationOption](New-HttpClientTraceInstrumentationOption.md) _Options for HttpClient instrumentation._
- [New-TracerProviderBuilder](New-TracerProviderBuilder.md) _Creates new Tracer Provider Builder_
- [Set-ActivityStatus](Set-ActivityStatus.md) _Set the Activity Status._
- [Start-Activity](Start-Activity.md) _Start an Activity._
Expand Down
7 changes: 6 additions & 1 deletion samples/sample.psm1
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# The Activity Source binds Activities (Spans) to a Tracer.
$activitySource = New-ActivitySource -Name potel-sample

# Only collect HttpClient requests to the google.com domain
$options = New-HttpClientTraceInstrumentationOption -RequestFilter {
param([Net.Http.HttpRequestMessage]$request) $request.RequestUri -like '*google.com*'
}

# A Tracer provides the configuration and lifecycle of your instrumentation.
# The Tracer does nothing itself, but binds inputs and outputs.
New-TracerProviderBuilder |
Add-TracerSource -ActivitySource $activitySource |
Add-ResourceConfiguration -ServiceName potel-sample -Attribute @{"host.name" = $(hostname) } |
Add-HttpClientInstrumentation |
Add-HttpClientInstrumentation -Options $options |
Add-ExporterOtlpTrace -Endpoint http://localhost:4317 |
Add-ExporterConsole |
Start-Tracer
Expand Down
48 changes: 27 additions & 21 deletions src/public/Add-HttpClientInstrumentation.ps1
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
<#
.SYNOPSIS
Adds Http Client Instrumentation
.DESCRIPTION
Adds Http Client Instrumentation
.PARAMETER TracerProvider
Instance of TracerProviderBuilderBase.
.INPUTS
Instance of TracerProviderBuilderBase
.OUTPUTS
TracerProviderBuilderBase
.EXAMPLE
New-TracerProviderBuilder | Add-HttpClientInstrumentation
.LINK
New-TracerProviderBuilder
.LINK
New-HttpClientTraceInstrumentationOption
.LINK
https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.Http/README.md
#>
function Add-HttpClientInstrumentation {
<#
.SYNOPSIS
Adds Http Client Instrumentation
.DESCRIPTION
Adds Http Client Instrumentation
.PARAMETER TracerProvider
Instance of TracerProviderBuilderBase.
.INPUTS
Instance of TracerProviderBuilderBase
.OUTPUTS
TracerProviderBuilderBase
.EXAMPLE
New-TracerProviderBuilder | Add-HttpClientInstrumentation
.LINK
New-TracerProviderBuilder
#>
[CmdletBinding()]
param (
# Parameter help description
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[Parameter(Mandatory, Position = 1, ValueFromPipeline)]
[OpenTelemetry.Trace.TracerProviderBuilderBase]
$TracerProvider
$TracerProvider,

[Parameter(Position = 0)]
[Action[OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions]]
$Options
)

$type = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location -like "*potel*lib*OpenTelemetry.Instrumentation.Http.dll" | Select-Object -Last 1
[OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions]::AddHttpClientInstrumentation($TracerProvider, $null, $Options)

$type.GetType('OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions').GetMethod('AddHttpClientInstrumentation', ([System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Static), [OpenTelemetry.Trace.TracerProviderBuilder]).Invoke($null, @($TracerProvider))
}
45 changes: 45 additions & 0 deletions src/public/New-HttpClientTraceInstrumentationOption.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<#
.SYNOPSIS
Options for HttpClient instrumentation.
.DESCRIPTION
Initializes a new instance of the HttpClientTraceInstrumentationOptions class for HttpClient instrumentation.
.PARAMETER RequestFilter
A filter function that determines whether or not to collect telemetry on a per request basis.
.PARAMETER RecordException
Indicating whether exception will be recorded ActivityEvent or not. This instrumentation automatically sets Activity Status to Error if the Http StatusCode is >= 400. Additionally, `RecordException` feature may be turned on, to store the exception to the Activity itself as ActivityEvent.
.NOTES
Filter scriptblock must return a bool. `[Func[Net.Http.HttpRequestMessage, bool]]`
.LINK
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httprequestmessage?view=netstandard-2.1#properties
.EXAMPLE
$options = New-HttpClientTraceInstrumentationOption -RequestFilter {param([Net.Http.HttpRequestMessage]$request) $request.Method -eq 'Get' }
New-TracerProviderBuilder | Add-HttpClientInstrumentation -Options $options
Collect only web requests with a `Method` of `Get`.
.EXAMPLE
$options = New-HttpClientTraceInstrumentationOption -RequestFilter {param([Net.Http.HttpRequestMessage]$request) $request.RequestUri -like '*google.com*' }
New-TracerProviderBuilder | Add-HttpClientInstrumentation -Options $options
Collect only web requests send to the "google.com" domain.
#>
function New-HttpClientTraceInstrumentationOption {
[CmdletBinding()]
[OutputType('OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions')]
param (
[Parameter(Position = 0)]
[Func[Net.Http.HttpRequestMessage, bool]]
$RequestFilter,

[Parameter()]
[switch]
$RecordException
)

$options = {
param([OpenTelemetry.Instrumentation.Http.HttpClientTraceInstrumentationOptions]$o)
$o.FilterHttpRequestMessage = $RequestFilter
$o.RecordException = $RecordException
}

$options.GetNewClosure() | Write-Output
}

0 comments on commit 76c23cb

Please sign in to comment.