Skip to content

fix: capture doesn't work if debug=false #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Capture doesn't work if `-Debug` wasn't set ([#74](https://github.com/getsentry/sentry-powershell/pull/74))
- Don't log debug messages if `-Debug` wasn't set ([#75](https://github.com/getsentry/sentry-powershell/pull/75))

### Dependencies
Expand Down
2 changes: 1 addition & 1 deletion modules/Sentry/private/DiagnosticLogger.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DiagnosticLogger : Sentry.Extensibility.IDiagnosticLogger
# see https://github.com/PowerShell/PowerShell/issues/5148
if ($global:PSVersionTable.PSEdition -eq 'Desktop') {
$pref = Get-Variable DebugPreference
if ($pref.value -eq "Inquire") {
if (($null -ne $pref) -and ($pref.value -eq "Inquire")) {
$DebugPreference = 'Continue'
}
}
Expand Down
16 changes: 14 additions & 2 deletions modules/Sentry/private/StackTraceProcessor.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ class StackTraceProcessor : SentryEventProcessor
[System.Management.Automation.InvocationInfo]$InvocationInfo
[System.Management.Automation.CallStackFrame[]]$StackTraceFrames
[string[]]$StackTraceString
hidden [Sentry.Extensibility.IDiagnosticLogger] $logger
hidden [string[]] $modulePaths
hidden [hashtable] $pwshModules = @{}

StackTraceProcessor()
StackTraceProcessor([Sentry.SentryOptions] $options)
{
$this.logger = $options.DiagnosticLogger
if ($null -eq $this.logger) {
$this.logger = Get-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ValueOnly -ErrorAction SilentlyContinue
}

if ($env:PSModulePath.Contains(';'))
{
# Windows
Expand Down Expand Up @@ -369,7 +375,13 @@ class StackTraceProcessor : SentryEventProcessor
{
if ($lines.Count -lt $sentryFrame.LineNumber)
{
Write-Debug "Couldn't set frame context because the line number ($($sentryFrame.LineNumber)) is lower than the available number of source code lines ($($lines.Count))."
if ($null -ne $this.logger) {
$this.logger.Log(
[Sentry.SentryLevel]::Debug,
"Couldn't set frame context because the line number ($($sentryFrame.LineNumber)) " +
"is lower than the available number of source code lines ($($lines.Count))."
)
}
return
}

Expand Down
7 changes: 6 additions & 1 deletion modules/Sentry/private/SynchronousTransport.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class SynchronousTransport : Sentry.Http.HttpTransportBase, Sentry.Extensibility
SynchronousTransport([Sentry.SentryOptions] $options) : base($options)
{
$this.logger = $options.DiagnosticLogger
if ($null -eq $this.logger) {
$this.logger = Get-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ValueOnly -ErrorAction SilentlyContinue
}

# 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;
Expand Down Expand Up @@ -43,7 +46,9 @@ class SynchronousTransport : Sentry.Http.HttpTransportBase, Sentry.Extensibility
$content = $reader.ReadToEnd()
$reader.Close()

$this.logger.Log([Sentry.SentryLevel]::Debug, 'Sending content synchronously, Content-Length: {0}', $null, $content.Length)
if ($null -ne $this.logger) {
$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
Expand Down
8 changes: 7 additions & 1 deletion modules/Sentry/public/Out-Sentry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@
{
if (-not [Sentry.SentrySdk]::IsEnabled)
{
# Workaround for:
# NullReferenceException: Object reference not set to an instance of an object.
# at Out-Sentry<Process>, D:\a\sentry-powershell\sentry-powershell\modules\Sentry\public\Out-Sentry.ps1:32
try {
Write-Debug "Sentry is not started: Out-Sentry invocation ignored."
} catch {}
return
}

$options = Get-CurrentOptions
[Sentry.SentryEvent]$event_ = $null
$processor = [StackTraceProcessor]::new()
$processor = [StackTraceProcessor]::new($options)

if ($ErrorRecord -ne $null)
{
Expand Down
4 changes: 4 additions & 0 deletions modules/Sentry/public/Start-Sentry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ function Start-Sentry
}

$logger = [DiagnosticLogger]::new($options.DiagnosticLevel)

# Note: this is currently a no-op if options.debug == false; see https://github.com/getsentry/sentry-dotnet/issues/3212
# As a workaround, we set the logger as a global variable so that we can reach it in other scripts.
$options.DiagnosticLogger = $logger
$script:SentryPowerShellDiagnosticLogger = $logger

if ($null -eq $options.Transport)
{
Expand Down
1 change: 1 addition & 0 deletions modules/Sentry/public/Stop-Sentry.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Stop-Sentry
{
[Sentry.SentrySdk]::Close()
Remove-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ErrorAction SilentlyContinue
}
2 changes: 1 addition & 1 deletion tests/stacktrace-processor.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Describe 'StackTraceProcessor' {
$event_.Message = 'Test'
$event_.Level = [Sentry.SentryLevel]::Info

$sut = [StackTraceProcessor]::new()
$sut = [StackTraceProcessor]::new([Sentry.SentryOptions]::new())
$sut.StackTraceString = 'at funcB, C:\dev\sentry-powershell\tests\throwing.ps1: line 17
at <ScriptBlock>, <No file>: line 1
at <ScriptBlock>, : line 3' -split "[`r`n]+"
Expand Down
Loading