Skip to content

Commit ba14c3c

Browse files
authored
fix: transaction sampling (#41)
* fix: transaction sampling * chore: update changelog
1 parent 7bab257 commit ba14c3c

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

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

9+
### Fixes
10+
11+
- Transaction sampling ([#38](https://github.com/getsentry/sentry-powershell/pull/41))
12+
913
## 0.0.2
1014

1115
### Various fixes & improvements

modules/Sentry/public/Start-SentryTransaction.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ function Start-SentryTransaction
66
[Parameter(Mandatory, ParameterSetName = 'Basic', Position = 0)]
77
[Parameter(Mandatory, ParameterSetName = 'BasicWithDescription', Position = 0)]
88
[string] $Name,
9+
910
[Parameter(Mandatory, ParameterSetName = 'Basic', Position = 1)]
1011
[Parameter(Mandatory, ParameterSetName = 'BasicWithDescription', Position = 1)]
1112
[string] $Operation,
13+
1214
[Parameter(ParameterSetName = 'BasicWithDescription', Position = 2)]
1315
[string] $Description = $null,
1416

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

23-
[Parameter()]
24-
[switch] $ForceSampled = $false
25+
[Parameter(ParameterSetName = 'Basic')]
26+
[Parameter(ParameterSetName = 'BasicWithDescription')]
27+
[Parameter(ParameterSetName = 'TransactionContext')]
28+
[switch] $ForceSampled
2529
)
2630

2731
begin
2832
{
2933
if ($null -eq $TransactionContext)
3034
{
31-
$TransactionContext = [Sentry.TransactionContext]::new($Name, $Operation, $null, $null, $null, $Description, $null, $ForceSampled)
35+
$IsSampled = $null
36+
if ($ForceSampled)
37+
{
38+
$IsSampled = $true
39+
}
40+
$TransactionContext = [Sentry.TransactionContext]::new($Name, $Operation, $null, $null, $null, $Description, $null, $IsSampled)
3241
}
3342

3443
}

samples/locate-city.ps1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@ param([string]$City = '')
1919
Import-Module $PSScriptRoot/../modules/Sentry/Sentry.psd1
2020

2121
# Start the Sentry client.
22-
Start-Sentry 'https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537'
22+
Start-Sentry -Debug {
23+
$_.Dsn = 'https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537'
24+
$_.TracesSampleRate = 1.0
25+
}
26+
27+
# Transaction can be started by providing, at minimum, the name and the operation
28+
$transaction = Start-SentryTransaction 'transaction-name' 'transaction-operation'
2329

2430
try
2531
{
32+
$span = $transaction.StartChild('wait for input')
2633
if ($City -eq '' ) { $City = Read-Host 'Enter the city name' }
34+
$span.Finish()
2735

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

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

4658
if ($FoundOne)
4759
{
@@ -54,5 +66,9 @@ catch
5466
{
5567
$_ | Out-Sentry
5668
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
57-
exit 1
69+
}
70+
finally
71+
{
72+
# Mark the transaction as finished and send it to Sentry
73+
$transaction.Finish()
5874
}

tests/traces.tests.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,43 @@ Describe 'Start-SentryTransaction' {
9797

9898
$global:TraceSamplerExecuted | Should -Be $true
9999
}
100+
101+
It 'sets IsSampled correctly based on ForceSampled, with tracing disabled' -ForEach @($true, $false) {
102+
$ForceSampled = $_
103+
104+
Start-Sentry {
105+
$_.Dsn = 'https://key@127.0.0.1/1'
106+
}
107+
108+
if ($ForceSampled)
109+
{
110+
$transaction = Start-SentryTransaction 'foo' 'bar' -ForceSampled
111+
$transaction.IsSampled | Should -Be $true
112+
}
113+
else
114+
{
115+
$transaction = Start-SentryTransaction 'foo' 'bar'
116+
$transaction.IsSampled | Should -Be $false
117+
}
118+
}
119+
120+
It 'sets IsSampled correctly based on ForceSampled, with tracing enabled' -ForEach @($true, $false) {
121+
$ForceSampled = $_
122+
123+
Start-Sentry {
124+
$_.Dsn = 'https://key@127.0.0.1/1'
125+
$_.TracesSampleRate = 1.0
126+
}
127+
128+
if ($ForceSampled)
129+
{
130+
$transaction = Start-SentryTransaction 'foo' 'bar' -ForceSampled
131+
$transaction.IsSampled | Should -Be $true
132+
}
133+
else
134+
{
135+
$transaction = Start-SentryTransaction 'foo' 'bar'
136+
$transaction.IsSampled | Should -Be $true
137+
}
138+
}
100139
}

0 commit comments

Comments
 (0)