-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for HttpClientInstrumentationOptions
- Loading branch information
Chris Hunt
committed
Dec 24, 2024
1 parent
d1bccfb
commit 76c23cb
Showing
7 changed files
with
124 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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` |
This file contains 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
This file contains 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
This file contains 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
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)) | ||
} |
This file contains 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
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 | ||
} |