diff --git a/CHANGELOG.md b/CHANGELOG.md index 8db234b..d09ee3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/modules/Sentry/public/Start-SentryTransaction.ps1 b/modules/Sentry/public/Start-SentryTransaction.ps1 index 7f11381..59a5b61 100644 --- a/modules/Sentry/public/Start-SentryTransaction.ps1 +++ b/modules/Sentry/public/Start-SentryTransaction.ps1 @@ -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, @@ -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) } } diff --git a/samples/locate-city.ps1 b/samples/locate-city.ps1 index 55d6c15..70cc4b0 100644 --- a/samples/locate-city.ps1 +++ b/samples/locate-city.ps1 @@ -19,15 +19,26 @@ param([string]$City = '') Import-Module $PSScriptRoot/../modules/Sentry/Sentry.psd1 # Start the Sentry client. -Start-Sentry 'https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537' +Start-Sentry -Debug { + $_.Dsn = 'https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/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) { @@ -42,6 +53,7 @@ try Write-Host "* $City ($Country, $Region, population $Population) is at $Lat°N, $Long°W" } } + $span.Finish() if ($FoundOne) { @@ -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() } diff --git a/tests/traces.tests.ps1 b/tests/traces.tests.ps1 index 24fa03d..bf0fc0a 100644 --- a/tests/traces.tests.ps1 +++ b/tests/traces.tests.ps1 @@ -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://key@127.0.0.1/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://key@127.0.0.1/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 + } + } }