Hi, I'm using Sentry for a presentation for school, exploring telemetry software. I was messing around with PowerShell scripts in PowerShell 7.5.0 when I noticed trace events set up with "Start-SentryTransaction" don't seem to register in the scope. I've been reading as much docs as I could before posting here. I'll post more info below. Thanks in advance
#PowerShell transactions/spans seems to not be working .NET 9
32 messages · Page 1 of 1 (latest)
^I halted execution on line 114 to inspect variables. At line 104 a span is created
In the local scope explorer, the span appears instanciated and still ongoing
Getting the current span gets nothing, which is odd
This is the current scope, which I got into a global var from Edit-SentryScope
Start-Sentry config
I'll get breadcrumbs and everything else in the issue created on sentry.io (web), but the trace is completely empty:
Versions:
- Win 10
- Sentry SDK 2.0.1 for PWSH, with .NET 9 Sentry.dll added from the C# install to work with PWSH 7.5.0
- Sentry.io (not a self-hosted instance)
Thanks again for anyone that has insight on this and for your time.
Hey, thanks for reaching our @proud atlas!
Off the top of my head, [Sentry.SentrySdk]::GetSpan gets the current ongoing span that's associated on the scope. If you did not set the transaction then there's nothing to retrieve.
I imagine it being something like
Import-Module Sentry
Start-Sentry {
$_.Dsn = $dsn
$_.TracesSampleRate = 1.0
}
$transaction = [Sentry.SentrySdk]::StartTransaction(
"my-transaction-name",
"my-operation-name"
)
[Sentry.SentrySdk]::ConfigureScope({
param($scope)
$scope.Transaction = $transaction
$scope.SetTag("some", "tag")
})
$transaction.StartChild("some-span")
# fake doing some work
Start-Sleep -Seconds 1
$retrieved_span = [Sentry.SentrySdk]::GetSpan()
$retrieved_span.Finish()
$transaction.Finish()
should work.
@runic dagger, thank you for your reply. I've replaced "Start-SentryTransaction" with your code, unfortunately that didn't seem to do the trick.
Edit: Please see on the next message.
Upon digging for a few hours, I noticed that the manual traces do get sent to Sentry.io, however they are not linked with the event. Therefore, the event will still show this trace preview starting with "e9". This was for the username called unhandled, which simulated an unhandled app error such as int.parse of a string.
After a lot of testing, I think I figured it out and it may have been my fault for not understanding how Sentry works.
Sending a local exception to Out-Sentry while still in the scope of an open span (a local try/catch block) behaves as expected:
Sending a global exception, with spans still open or all spans closed with GetSpan().Finish(), always seems to yield no trace at all (a "catch-all" try/catch condition). Perhaps this is because we fell out of scope in the actual code?
I would have another question for PowerShell and that would be my last to have everything prepared for my class demonstration.
There was one issue I was having with an image attachment type, which I was trying to demontrate.
Adding the following code, given a file "myimage.png" exists in the current workdir, and calling Out-Sentry to send the event, will always result in an error.
$imageBytes = [System.IO.File]::ReadAllBytes("myimage.png")
Edit-SentryScope {
$_.AddAttachment($imageBytes, "this_is_the_image.png", [Sentry.AttachmentType]::Default, "image/png")
}# byte[], filename on Sentry, attachment type, content type
Written hash is 00000000000000000000000000000000
I could only salvage this much of the error digging in with the pwsh debugger. Not a big deal if I can't demonstrate that though. I want to thank you for your help so far!
@exotic heath did you mess around with the powershell side of the SDK? Any idea why it might now like attachments?
you can try to enable debug mode to see what the error is
for example, like this: https://github.com/getsentry/sentry-powershell/blob/main/samples/locate-city.ps1#L22C1-L22C20
or by setting the Debug option to true
GitHub
Sentry for PowerShell. Contribute to getsentry/sentry-powershell development by creating an account on GitHub.
This might not be specific to the PowerShell SDK, since in the background it's all .NET objects being manipulated. I only got that method by decompiling the Sentry.dll lib for .NET 9. To add more information, the object is added properly to the scope "Attachments", but errors on Out-Sentry. I've had debug mode enabled already, but I'll give it one more shot in abour half an hour when I'm more available. Thanks for the help so far
missing newline after header or payload
That's very weird. The envelope composition is handled by the .net SDK and I don't see why it would produce an invalid envelope
I can confirm attachments are broken
I suspect it's related to the custom transport.
I've tried attachments a while back and they worked before
you can comment out https://github.com/getsentry/sentry-powershell/blob/2cf122537ce6f961d7d4d514e90e6c44021b9fed/modules/Sentry/public/Start-Sentry.ps1#L60 to workaround the issue for now
Sorry I only could get back to you now. This was with the switch -Debug and $Debug = true in the ctor.
Here's my source for this test
& "$PSScriptRoot/sentry_loader.ps1" -Import
$config = Import-PowerShellDataFile -Path "$PSScriptRoot/config.ps1"
Start-Sentry -Debug {
$_.Dsn = $config.DSN
$_.TracesSampleRate = 1.0
$_.Debug = $true
}
$firstImage = Get-ChildItem "$PSScriptRoot/demos/demo2/pizzas" | Select-Object -First 1
$imageBytes = [System.IO.File]::ReadAllBytes($firstImage.FullName)
Edit-SentryScope {
$_.AddAttachment($imageBytes, "a_pizza_image.png", [Sentry.AttachmentType]::Default, "image/png")
}
"test-image" | Out-Sentry
To note that plaintext attachments (existing log files on the disk) work as expected and are forwarded with the event. Only the image (which I was attaching as byte[]) didn't work.
At the path Sentry/{version}/private/Get-SentryAssembliesDirectory.ps1 you probably want to include this code as well for PWSH 7.5. Currently the 0.2.1 PowerShell SDK for Sentry is broken since it's trying to load a .NET 8 assembly on version > 7.4. 7.5 requires .NET 9, so a crash happens. I'm guessing this can be included with the future fix for the attachment to fix the release.
Sorry I couldn't make a ticket on GitHub now. Kinda short on time with school, but I can add diagnostic information on Friday on GitHub issues if you'd prefer so. I'd be happy to do so since I'm presenting Sentry for class and I've received very quick help from you guys!
Currently the 0.2.1 PowerShell SDK for Sentry is broken since it's trying to load
I'm aware and this is fixxed on the main branch for quite a while, just the release approval slipped through the cracks so I'll trigger a new one
btw I've fixed the attachments issue on the main as well
Thank you very much for your swift work and help