Skip to content
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ PowerShell Sentry SDK is a thin wrapper on top of [Sentry .NET SDK](https://gith

The PowerShell Sentry SDK is tested to work with:

- Windows PowerShell 5.1
- PowerShell 7.4+ on Windows, macOS, and Linux
* Windows PowerShell 5.1
* PowerShell 7.4+ on Windows, macOS, and Linux

## Documentation

Sentry has extensive documentation for its SDKs on docs.sentry.io:

- [PowerShell SDK](https://docs.sentry.io/platforms/powershell/)
- [.NET SDK](https://docs.sentry.io/platforms/dotnet/)
* [PowerShell SDK](https://docs.sentry.io/platforms/powershell/)
* [.NET SDK](https://docs.sentry.io/platforms/dotnet/)

## Resources

- [![Discord Chat](https://img.shields.io/discord/621778831602221064?logo=discord&logoColor=ffffff&color=7389D8)](https://discord.gg/PXa5Apfe7K)
- [![Stack Overflow](https://img.shields.io/badge/stack%20overflow-sentry-green.svg)](http://stackoverflow.com/questions/tagged/sentry)
- [![Twitter Follow](https://img.shields.io/twitter/follow/getsentry?label=getsentry&style=social)](https://twitter.com/intent/follow?screen_name=getsentry)
* [![Discord Chat](https://img.shields.io/discord/621778831602221064?logo=discord&logoColor=ffffff&color=7389D8)](https://discord.gg/PXa5Apfe7K)
* [![Stack Overflow](https://img.shields.io/badge/stack%20overflow-sentry-green.svg)](http://stackoverflow.com/questions/tagged/sentry)
* [![Twitter Follow](https://img.shields.io/twitter/follow/getsentry?label=getsentry&style=social)](https://twitter.com/intent/follow?screen_name=getsentry)
62 changes: 19 additions & 43 deletions dependencies/download.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@ $libDir = "$moduleDir/lib"

New-Item $libDir -ItemType Directory -Force | Out-Null

function CheckAssemblyVersion([string] $libFile, [string] $assemblyVersion)
{
function CheckAssemblyVersion([string] $libFile, [string] $assemblyVersion) {
$assembly = [Reflection.Assembly]::LoadFile($libFile)
if ($assembly.GetName().Version.ToString() -ne $assemblyVersion)
{
if ($assembly.GetName().Version.ToString() -ne $assemblyVersion) {
throw "Dependency $libFile has different assembly version ($($assembly.GetName().Version)) than expected ($assemblyVersion)"
}
}

function Download([string] $dependency, [string] $TFM, [string] $targetTFM = $null, [string] $assemblyVersion = $null)
{
function Download([string] $dependency, [string] $TFM, [string] $targetTFM = $null, [string] $assemblyVersion = $null) {
$targetTFM = "$targetTFM" -eq '' ? $TFM : $targetTFM
New-Item "$libDir/$targetTFM" -ItemType Directory -Force | Out-Null

$props = (Get-Content "$propsDir/$dependency.properties" -Raw | ConvertFrom-StringData)

if ("$assemblyVersion" -eq '')
{
if ("$assemblyVersion" -eq '') {
$assemblyVersion = $props.ContainsKey('assemblyVersion') ? $props.assemblyVersion : "$($props.version).0"
}

Expand All @@ -32,76 +28,56 @@ function Download([string] $dependency, [string] $TFM, [string] $targetTFM = $nu
$targetVersionFile = "$libDir/$targetTFM/$dependency.version"
$targetLicenseFile = "$libDir/$targetTFM/$dependency.license"

if ((Test-Path $targetLibFile) -and ((Get-Content $targetVersionFile -Raw -ErrorAction SilentlyContinue) -eq $assemblyVersion))
{
try
{
if ((Test-Path $targetLibFile) -and ((Get-Content $targetVersionFile -Raw -ErrorAction SilentlyContinue) -eq $assemblyVersion)) {
try {
CheckAssemblyVersion $targetLibFile $assemblyVersion
Write-Debug "Dependency $targetLibFile already exists and has the expected assembly version ($assemblyVersion), skipping."
return
}
catch
{
} catch {
Write-Warning "$_, downloading again"
}
}

if (Test-Path $targetLibFile)
{
if (Test-Path $targetLibFile) {
Remove-Item $targetLibFile -Force
}
if (Test-Path $targetVersionFile)
{
if (Test-Path $targetVersionFile) {
Remove-Item $targetVersionFile -Force
}
if (Test-Path $targetLicenseFile)
{
if (Test-Path $targetLicenseFile) {
Remove-Item $targetLicenseFile -Force
}

$archiveName = "$($package.ToLower()).$($props.version).nupkg"
$archiveFile = "$downloadDir/$archiveName"

if (Test-Path $archiveFile)
{
if (Test-Path $archiveFile) {
Write-Debug "Archive $archiveFile already exists, skipping download"
}
else
{
} else {
$sourceUrl = "https://globalcdn.nuget.org/packages/$archiveName"
Write-Output "Downloading $sourceUrl"
Invoke-WebRequest $sourceUrl -OutFile $archiveFile
}

$archive = [IO.Compression.ZipFile]::OpenRead($archiveFile)

function extract([string] $fileToExtract, [string] $targetFile)
{
if ($file = $archive.Entries.Where(({ $_.FullName -eq $fileToExtract })))
{
function extract([string] $fileToExtract, [string] $targetFile) {
if ($file = $archive.Entries.Where(({ $_.FullName -eq $fileToExtract }))) {
Write-Output "Extracting $fileToExtract to $targetFile"
[IO.Compression.ZipFileExtensions]::ExtractToFile($file[0], $targetFile)
}
else
{
} else {
throw "File not found in ZIP: $fileToExtract"
}
}

try
{
try {
extract "lib/$TFM/$package.dll" $targetLibFile
if ($props.ContainsKey('licenseFile'))
{
if ($props.ContainsKey('licenseFile')) {
extract $props.licenseFile $targetLicenseFile
}
else
{
} else {
$props.license | Out-File -NoNewline $targetLicenseFile
}
}
finally
{
} finally {
$archive.Dispose()
}

Expand Down
14 changes: 4 additions & 10 deletions modules/Sentry/assemblies-loader.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@ $dir = Get-SentryAssembliesDirectory

# Check if the assembly is already loaded.
$type = 'Sentry.SentrySdk' -as [type]
if ($type)
{
if ($type) {
$loadedAsssembly = $type.Assembly
$expectedAssembly = [Reflection.Assembly]::LoadFile((Join-Path $dir 'Sentry.dll'))

if ($loadedAsssembly.ToString() -ne $expectedAssembly.ToString())
{
if ($loadedAsssembly.ToString() -ne $expectedAssembly.ToString()) {
throw "Sentry assembly is already loaded but it's not the expected version.
Found: ($loadedAsssembly), location: $($loadedAsssembly.Location)
Expected: ($expectedAssembly), location: $($expectedAssembly.Location)"
}
else
{
} else {
Write-Debug "Sentry assembly is already loaded and at the expected version ($($expectedAssembly.GetName().Version)"
}
}
else
{
} else {
Write-Debug "Loading assemblies from $($dir):"
Get-ChildItem -Path $dir -Filter '*.dll' | ForEach-Object {
Write-Debug "Loading assembly: $($_.Name)"
Expand Down
38 changes: 13 additions & 25 deletions modules/Sentry/private/DiagnosticLogger.ps1
Original file line number Diff line number Diff line change
@@ -1,61 +1,49 @@
class DiagnosticLogger : Sentry.Extensibility.IDiagnosticLogger
{
class DiagnosticLogger : Sentry.Extensibility.IDiagnosticLogger {
hidden [Sentry.SentryLevel] $minimalLevel

DiagnosticLogger([Sentry.SentryLevel] $minimalLevel)
{
DiagnosticLogger([Sentry.SentryLevel] $minimalLevel) {
$this.minimalLevel = $minimalLevel
}

[bool] IsEnabled([Sentry.SentryLevel] $level)
{
[bool] IsEnabled([Sentry.SentryLevel] $level) {
return $level -ge $this.minimalLevel
}

Log([Sentry.SentryLevel] $level, [string] $message, [Exception] $exception = $null, [object[]] $params)
{
Log([Sentry.SentryLevel] $level, [string] $message, [Exception] $exception = $null, [object[]] $params) {
# Important: Only format the string if there are args passed.
# Otherwise, a pre-formatted string that contains braces can cause a FormatException.
if ($params.Count -gt 0)
{
if ($params.Count -gt 0) {
$message = $message -f $params
}

# Note, linefeed and newline chars are removed to guard against log injection attacks.
$message = $message -replace '[\r\n]+', ' '

$message = "[Sentry] $message"
if ($null -ne $exception)
{
if ($null -ne $exception) {
$message += [Environment]::NewLine
$message += $exception | Out-String
}

switch ($level)
{
([Sentry.SentryLevel]::Info)
{
switch ($level) {
([Sentry.SentryLevel]::Info) {
Write-Verbose $message
}
([Sentry.SentryLevel]::Warning)
{
([Sentry.SentryLevel]::Warning) {
Write-Warning $message
}
([Sentry.SentryLevel]::Error)
{
([Sentry.SentryLevel]::Error) {
Write-Error $message
}
([Sentry.SentryLevel]::Fatal)
{
([Sentry.SentryLevel]::Fatal) {
Write-Error $message
}
default
{
default {
# Workaround for Windows Powershell issue of halting and asking for user confirmation.
# see https://github.com/PowerShell/PowerShell/issues/5148
if ($global:PSVersionTable.PSEdition -eq 'Desktop') {
$pref = Get-Variable DebugPreference
if (($null -ne $pref) -and ($pref.value -eq "Inquire")) {
if (($null -ne $pref) -and ($pref.value -eq 'Inquire')) {
$DebugPreference = 'Continue'
}
}
Expand Down
9 changes: 3 additions & 6 deletions modules/Sentry/private/EventUpdater.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
class EventUpdater : SentryEventProcessor
{
[Sentry.SentryEvent]DoProcess([Sentry.SentryEvent] $event_)
{
class EventUpdater : SentryEventProcessor {

Check notice

Code scanning / PSScriptAnalyzer

Ignoring 'TypeNotFound' parse error on type 'SentryEventProcessor'. Check if the specified type is correct. This can also be due the type not being known at parse time due to types imported by 'using' statements. Note

Ignoring 'TypeNotFound' parse error on type 'SentryEventProcessor'. Check if the specified type is correct. This can also be due the type not being known at parse time due to types imported by 'using' statements.
[Sentry.SentryEvent]DoProcess([Sentry.SentryEvent] $event_) {
$event_.Platform = 'powershell'

# Clear useless release set by the .NET SDK (referring to the PowerShell assembly version)
# "pwsh@7.4.1 SHA: 6a98b28414948626f1b29a5e8b062e73b7ff165a+6a98b28414948626f1b29a5e8b062e73b7ff165a"
if ($event_.Release -match "pwsh@$($global:PSVersionTable.PSVersion) .*")
{
if ($event_.Release -match "pwsh@$($global:PSVersionTable.PSVersion) .*") {
$event_.Release = $null
}

Expand Down
6 changes: 2 additions & 4 deletions modules/Sentry/private/Get-CurrentOptions.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
function Get-CurrentOptions
{
function Get-CurrentOptions {

Check warning

Code scanning / PSScriptAnalyzer

The cmdlet 'Get-CurrentOptions' uses a plural noun. A singular noun should be used instead. Warning

The cmdlet 'Get-CurrentOptions' uses a plural noun. A singular noun should be used instead.
$bindingFlags = [System.Reflection.BindingFlags]::Static + [System.Reflection.BindingFlags]::NonPublic + [System.Reflection.BindingFlags]::Public
$currentOptionsProperty = [Sentry.SentrySdk].GetProperty('CurrentOptions', $bindingFlags)
if ($null -eq $currentOptionsProperty)
{
if ($null -eq $currentOptionsProperty) {
return $null
}

Expand Down
17 changes: 5 additions & 12 deletions modules/Sentry/private/Get-SentryAssembliesDirectory.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function GetTFM
{
function GetTFM {
# Source https://learn.microsoft.com/en-us/powershell/scripting/install/powershell-support-lifecycle?view=powershell-7.4#powershell-end-of-support-dates
# PowerShell 7.5 - Built on .NET 9.0
# PowerShell 7.4 (LTS) - Built on .NET 8.0
Expand All @@ -10,22 +9,16 @@ function GetTFM
# PowerShell 6.2 - Built on .NET Core 2.1
# PowerShell 6.1 - Built on .NET Core 2.1
# PowerShell 6.0 - Built on .NET Core 2.0
if ($PSVersionTable.PSVersion -ge '7.5')
{
if ($PSVersionTable.PSVersion -ge '7.5') {
return 'net9.0'
}
elseif ($PSVersionTable.PSVersion -ge '7.4')
{
} elseif ($PSVersionTable.PSVersion -ge '7.4') {
return 'net8.0'
}
else
{
} else {
return 'net462'
}
}

function Get-SentryAssembliesDirectory
{
function Get-SentryAssembliesDirectory {
$dir = Split-Path -Parent $PSScriptRoot
$dir = Join-Path $dir 'lib'
$dir = Join-Path $dir (GetTFM)
Expand Down
3 changes: 1 addition & 2 deletions modules/Sentry/private/New-HttpTransport.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Wrapper to expose Sentry.Internal.SdkComposer::CreateHttpTransport()
function New-HttpTransport
{
function New-HttpTransport {

Check warning

Code scanning / PSScriptAnalyzer

Function 'New-HttpTransport' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'New-HttpTransport' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
[OutputType([Sentry.Extensibility.ITransport])]
[CmdletBinding()]
param(
Expand Down
13 changes: 4 additions & 9 deletions modules/Sentry/private/ScopeIntegration.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
class ScopeIntegration : Sentry.Integrations.ISdkIntegration
{
Register([Sentry.IHub] $hub, [Sentry.SentryOptions] $options)
{
class ScopeIntegration : Sentry.Integrations.ISdkIntegration {
Register([Sentry.IHub] $hub, [Sentry.SentryOptions] $options) {
$hub.ConfigureScope([System.Action[Sentry.Scope]] {
param([Sentry.Scope]$scope)

$scope.Sdk.Name = 'sentry.dotnet.powershell'
$scope.Sdk.Version = $moduleInfo.ModuleVersion
$scope.Sdk.AddPackage("ps:$($scope.Sdk.Name)", $scope.Sdk.Version)

if ($PSVersionTable.PSEdition -eq 'Core')
{
if ($PSVersionTable.PSEdition -eq 'Core') {
$scope.Contexts.Runtime.Name = 'PowerShell'
}
else
{
} else {
$scope.Contexts.Runtime.Name = 'Windows PowerShell'
}
$scope.Contexts.Runtime.Version = $PSVersionTable.PSVersion.ToString()
Expand Down
16 changes: 5 additions & 11 deletions modules/Sentry/private/SentryEventProcessor.ps1
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
class SentryEventProcessor : SentryEventProcessor_
{
[Sentry.SentryEvent]DoProcess([Sentry.SentryEvent] $event_)
{
class SentryEventProcessor : SentryEventProcessor_ {
[Sentry.SentryEvent]DoProcess([Sentry.SentryEvent] $event_) {
throw [NotImplementedException]::new('You must override SentryEventProcessor::DoProcess()')
}

[Sentry.SentryEvent]Process_([Sentry.SentryEvent] $event_)
{
try
{
[Sentry.SentryEvent]Process_([Sentry.SentryEvent] $event_) {
try {
return $this.DoProcess($event_)
}
catch
{
} catch {
$ErrorRecord = $_
"$($this.GetType()) failed to process event $($event_.EventId):" | Write-Warning
$ErrorRecord | Format-List * -Force | Out-String | Write-Warning
Expand Down
Loading