Skip to content

fix: transaction sampling #41

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 2 commits into from
Mar 12, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Send events synchronously so they're not lost when the script exits ([#39](https://github.com/getsentry/sentry-powershell/pull/39))

### Fixes

- Transaction sampling ([#38](https://github.com/getsentry/sentry-powershell/pull/41))

## 0.0.2

### Various fixes & improvements
Expand Down
15 changes: 12 additions & 3 deletions modules/Sentry/public/Start-SentryTransaction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ function Start-SentryTransaction
[Parameter(Mandatory, ParameterSetName = 'Basic', Position = 0)]
[Parameter(Mandatory, ParameterSetName = 'BasicWithDescription', Position = 0)]
[string] $Name,

[Parameter(Mandatory, ParameterSetName = 'Basic', Position = 1)]
[Parameter(Mandatory, ParameterSetName = 'BasicWithDescription', Position = 1)]
[string] $Operation,

[Parameter(ParameterSetName = 'BasicWithDescription', Position = 2)]
[string] $Description = $null,

Expand All @@ -20,15 +22,22 @@ function Start-SentryTransaction
[Parameter(ParameterSetName = 'TransactionContext', Position = 1)]
[hashtable] $CustomSamplingContext,

[Parameter()]
[switch] $ForceSampled = $false
[Parameter(ParameterSetName = 'Basic')]
[Parameter(ParameterSetName = 'BasicWithDescription')]
[Parameter(ParameterSetName = 'TransactionContext')]
[switch] $ForceSampled
)

begin
{
if ($null -eq $TransactionContext)
{
$TransactionContext = [Sentry.TransactionContext]::new($Name, $Operation, $null, $null, $null, $Description, $null, $ForceSampled)
$IsSampled = $null
if ($ForceSampled)
{
$IsSampled = $true
}
$TransactionContext = [Sentry.TransactionContext]::new($Name, $Operation, $null, $null, $null, $Description, $null, $IsSampled)
}

}
Expand Down
20 changes: 18 additions & 2 deletions samples/locate-city.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ param([string]$City = '')
Import-Module $PSScriptRoot/../modules/Sentry/Sentry.psd1

# Start the Sentry client.
Start-Sentry 'https://[email protected]/5428537'
Start-Sentry -Debug {
$_.Dsn = 'https://[email protected]/5428537'
$_.TracesSampleRate = 1.0
}

# Transaction can be started by providing, at minimum, the name and the operation
$transaction = Start-SentryTransaction 'transaction-name' 'transaction-operation'

try
{
$span = $transaction.StartChild('wait for input')
if ($City -eq '' ) { $City = Read-Host 'Enter the city name' }
$span.Finish()

$span = $transaction.StartChild('read CSV')
Write-Progress 'Reading worldcities.csv...'
$Table = Import-Csv "$PSScriptRoot/../data/worldcities.csv"
$span.Finish()

$span = $transaction.StartChild('search')
$FoundOne = 0
foreach ($Row in $Table)
{
Expand All @@ -42,6 +53,7 @@ try
Write-Host "* $City ($Country, $Region, population $Population) is at $Lat°N, $Long°W"
}
}
$span.Finish()

if ($FoundOne)
{
Expand All @@ -54,5 +66,9 @@ catch
{
$_ | Out-Sentry
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
finally
{
# Mark the transaction as finished and send it to Sentry
$transaction.Finish()
}
39 changes: 39 additions & 0 deletions tests/traces.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,43 @@ Describe 'Start-SentryTransaction' {

$global:TraceSamplerExecuted | Should -Be $true
}

It 'sets IsSampled correctly based on ForceSampled, with tracing disabled' -ForEach @($true, $false) {
$ForceSampled = $_

Start-Sentry {
$_.Dsn = 'https://[email protected]/1'
}

if ($ForceSampled)
{
$transaction = Start-SentryTransaction 'foo' 'bar' -ForceSampled
$transaction.IsSampled | Should -Be $true
}
else
{
$transaction = Start-SentryTransaction 'foo' 'bar'
$transaction.IsSampled | Should -Be $false
}
}

It 'sets IsSampled correctly based on ForceSampled, with tracing enabled' -ForEach @($true, $false) {
$ForceSampled = $_

Start-Sentry {
$_.Dsn = 'https://[email protected]/1'
$_.TracesSampleRate = 1.0
}

if ($ForceSampled)
{
$transaction = Start-SentryTransaction 'foo' 'bar' -ForceSampled
$transaction.IsSampled | Should -Be $true
}
else
{
$transaction = Start-SentryTransaction 'foo' 'bar'
$transaction.IsSampled | Should -Be $true
}
}
}