-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add SynchronousTransport implementation, enabled similarly to SynchronousWorker * Switch to using directive for loading .NET assembly Add CHANGELOG.md entry for new SynchronousTransport feature * Require System.Net.Http assembly * Update CHANGELOG.md * move SynchronousTransport reflection to constructor * use content-type from response headers * disable progress indicator * fix typo * respect request.method * chore: update changelog * cleanup * fixup progress preference setting * use local ProgressPreference * fix the typo fix --------- Co-authored-by: Will Horne <will.horne@cloudrunner.com>
- Loading branch information
Showing
5 changed files
with
90 additions
and
1 deletion.
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,69 @@ | ||
# Take Sentry's SerializableHttpContent, convert it to a string, and send via PowerShell's Invoke-WebRequest, | ||
# then translate the response back to a .NET HttpResponseMessage. | ||
# There are limited options to perform synchronous operations in Windows PowerShell 5.1 on .NET 4.6, so this is a workaround. | ||
class SynchronousTransport : Sentry.Http.HttpTransportBase, Sentry.Extensibility.ITransport | ||
{ | ||
hidden [Sentry.Extensibility.IDiagnosticLogger] $logger | ||
hidden [System.Reflection.MethodInfo] $ProcessEnvelope | ||
hidden [System.Reflection.MethodInfo] $CreateRequest | ||
hidden [System.Reflection.MethodInfo] $SerializeToStream | ||
|
||
SynchronousTransport([Sentry.SentryOptions] $options) : base($options) | ||
{ | ||
$this.logger = $options.DiagnosticLogger | ||
|
||
# These are internal methods, so we need to use reflection to access them. | ||
$instanceMethod = [System.Reflection.BindingFlags]::Instance + [System.Reflection.BindingFlags]::NonPublic + [System.Reflection.BindingFlags]::Public; | ||
$this.ProcessEnvelope = [Sentry.Http.HttpTransportBase].GetMethod('ProcessEnvelope', $instanceMethod) | ||
$this.CreateRequest = [Sentry.Http.HttpTransportBase].GetMethod('CreateRequest', $instanceMethod) | ||
$EnvelopeHttpContentType = [Sentry.SentrySdk].Assembly.GetType('Sentry.Internal.Http.EnvelopeHttpContent') | ||
$this.SerializeToStream = $EnvelopeHttpContentType.GetMethod('SerializeToStream', $instanceMethod) | ||
} | ||
|
||
[System.Threading.Tasks.Task] SendEnvelopeAsync([Sentry.Protocol.Envelopes.Envelope] $envelope, [System.Threading.CancellationToken]$cancellationToken = [System.Threading.CancellationToken]::None) | ||
{ | ||
$processedEnvelope = $this.ProcessEnvelope.Invoke($this, @($envelope)) | ||
if ($processedEnvelope.Items.count -gt 0) | ||
{ | ||
$request = $this.CreateRequest.Invoke($this, @($processedEnvelope)) | ||
|
||
$headers = @{} | ||
foreach ($header in $request.Headers) | ||
{ | ||
$Key = $header.Key | ||
$Value = $header.Value.Trim() -join ', ' | ||
$headers[$Key] = $Value | ||
} | ||
|
||
$memoryStream = [System.IO.MemoryStream]::new() | ||
$this.SerializeToStream.Invoke($request.Content, @($memoryStream, $null, $cancellationToken)) | ||
$memoryStream.Position = 0 | ||
|
||
$reader = New-Object System.IO.StreamReader($memoryStream) | ||
$content = $reader.ReadToEnd() | ||
$reader.Close() | ||
|
||
$this.logger.Log([Sentry.SentryLevel]::Debug, 'Sending content synchronously, Content-Length: {0}', $null, $content.Length) | ||
|
||
$ProgressPreference = 'SilentlyContinue' | ||
$psResponse = Invoke-WebRequest -Uri $request.RequestUri -Method $request.Method.Method -Headers $headers -Body $content -UseBasicParsing | ||
|
||
$response = [System.Net.Http.HttpResponseMessage]::new($psResponse.StatusCode) | ||
$contentType = $psResponse.Headers['Content-Type'] | ||
if ($null -eq $contentType) | ||
{ | ||
$contentType = 'application/json' | ||
} | ||
$response.Content = [System.Net.Http.StringContent]::new($psResponse.Content, [System.Text.Encoding]::UTF8, $contentType) | ||
|
||
foreach ($header in $psResponse.Headers.GetEnumerator()) | ||
{ | ||
$response.Headers.TryAddWithoutValidation($header.Key, $header.Value) | ||
} | ||
|
||
$this.HandleResponse($response, $processedEnvelope) | ||
} | ||
|
||
return [System.Threading.Tasks.Task]::CompletedTask | ||
} | ||
} |
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