-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add Add-SentryAttachment cmdlet with extension-based content-type defaults #134
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c55fe55
Add Add-SentryAttachment cmdlet with extension-based content-type def…
jamescrosswell b435593
Fix PR number in changelog
jamescrosswell 111d359
Merge branch 'main' into mime-type-defaults
jamescrosswell 0b56cc6
Tweaked comments
jamescrosswell 9dba662
Add send-attachment.ps1 sample
jamescrosswell 3668568
Resolve relative attachment paths against PowerShell $PWD
jamescrosswell 3dfebcc
Tolerate Windows file-lock race in relative-path test cleanup
jamescrosswell 879466e
Document and test piping a file path into Add-SentryAttachment
jamescrosswell 2721fd1
Support piping byte[] into Add-SentryAttachment via comma operator
jamescrosswell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Maps common file extensions that aren't guaranteed to be in OS MIME databases | ||
| # to a content type that Sentry will recognize for attachment preview to work. | ||
| $script:SentryAttachmentContentTypes = @{ | ||
| # PowerShell | ||
| '.ps1' = 'text/plain' | ||
| '.psm1' = 'text/plain' | ||
| '.psd1' = 'text/plain' | ||
| '.ps1xml' = 'application/xml' | ||
| '.pssc' = 'text/plain' | ||
| '.psrc' = 'text/plain' | ||
|
|
||
| # Plain text / logs / config | ||
| '.txt' = 'text/plain' | ||
| '.log' = 'text/plain' | ||
| '.md' = 'text/plain' | ||
| '.ini' = 'text/plain' | ||
| '.cfg' = 'text/plain' | ||
| '.conf' = 'text/plain' | ||
| '.yaml' = 'text/plain' | ||
| '.yml' = 'text/plain' | ||
| '.toml' = 'text/plain' | ||
|
|
||
| # Structured | ||
| '.json' = 'application/json' | ||
| '.xml' = 'application/xml' | ||
| '.csv' = 'text/csv' | ||
| '.html' = 'text/html' | ||
| '.htm' = 'text/html' | ||
| '.css' = 'text/css' | ||
| '.js' = 'text/javascript' | ||
| '.sql' = 'text/plain' | ||
| } | ||
|
|
||
| function Get-AttachmentContentType { | ||
| param([string] $FileName) | ||
|
|
||
| if ([string]::IsNullOrEmpty($FileName)) { | ||
| return $null | ||
| } | ||
| $ext = [System.IO.Path]::GetExtension($FileName) | ||
| if ([string]::IsNullOrEmpty($ext)) { | ||
| return $null | ||
| } | ||
| return $script:SentryAttachmentContentTypes[$ext.ToLowerInvariant()] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| function Add-SentryAttachment { | ||
| <# | ||
| .SYNOPSIS | ||
| Adds a file or byte attachment to the current Sentry scope. | ||
| .DESCRIPTION | ||
| Wraps Scope.AddAttachment and, when -ContentType is not specified, | ||
| infers a sensible content type from the file extension so that the | ||
| Sentry UI can preview common text formats (including PowerShell | ||
| scripts). | ||
| .PARAMETER Path | ||
| Path to a file to attach. | ||
| .PARAMETER Bytes | ||
| Raw bytes to attach. Must be combined with -FileName. Accepts pipeline | ||
| input, but note the pipeline unrolls arrays: to pipe a byte[] as a single | ||
| attachment, wrap it with the unary comma operator, e.g. | ||
| `,$bytes | Add-SentryAttachment -FileName 'data.json'`. | ||
| .PARAMETER FileName | ||
| File name to associate with byte data. | ||
| .PARAMETER ContentType | ||
| Optional MIME type. If omitted, a default is chosen based on the | ||
| file extension; if no default is known, content-type is left unset. | ||
| .PARAMETER Type | ||
| Sentry attachment type. Defaults to Default. | ||
| .EXAMPLE | ||
| PS> Add-SentryAttachment -Path $PSCommandPath | ||
| .EXAMPLE | ||
| PS> $PSCommandPath | Add-SentryAttachment | ||
| .EXAMPLE | ||
| PS> Add-SentryAttachment -Bytes $bytes -FileName 'data.json' | ||
| #> | ||
| [CmdletBinding(DefaultParameterSetName = 'Path')] | ||
| param( | ||
| [Parameter(Mandatory, Position = 0, ParameterSetName = 'Path', ValueFromPipeline = $true)] | ||
| [string] $Path, | ||
|
|
||
| [Parameter(Mandatory, ParameterSetName = 'Bytes', ValueFromPipeline = $true)] | ||
| [byte[]] $Bytes, | ||
|
|
||
| [Parameter(Mandatory, ParameterSetName = 'Bytes')] | ||
| [string] $FileName, | ||
|
|
||
| [string] $ContentType, | ||
|
|
||
| [Sentry.AttachmentType] $Type = [Sentry.AttachmentType]::Default | ||
| ) | ||
|
|
||
| process { | ||
| if ($PSCmdlet.ParameterSetName -eq 'Path') { | ||
| # Resolve relative paths against PowerShell's $PWD. The Sentry SDK reads | ||
| # the file lazily via .NET I/O, which resolves against [Environment]::CurrentDirectory | ||
| # — that can diverge from $PWD after Set-Location, so resolve eagerly here. | ||
| $Path = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path) | ||
| $resolvedFileName = [System.IO.Path]::GetFileName($Path) | ||
| $resolvedContentType = if ($PSBoundParameters.ContainsKey('ContentType')) { $ContentType } else { Get-AttachmentContentType $resolvedFileName } | ||
| Edit-SentryScope { $_.AddAttachment($Path, $Type, $resolvedContentType) }.GetNewClosure() | ||
| } else { | ||
| $resolvedContentType = if ($PSBoundParameters.ContainsKey('ContentType')) { $ContentType } else { Get-AttachmentContentType $FileName } | ||
| Edit-SentryScope { $_.AddAttachment($Bytes, $FileName, $Type, $resolvedContentType) }.GetNewClosure() | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Demonstrates attaching files and byte data to a Sentry event. | ||
| .DESCRIPTION | ||
| Shows how to use Add-SentryAttachment to upload supporting files alongside | ||
| an event. When -ContentType is not specified, the cmdlet infers one from | ||
| the file extension so the Sentry UI can preview common text formats | ||
| (including PowerShell scripts) instead of falling back to | ||
| application/octet-stream. | ||
| .EXAMPLE | ||
| PS> ./send-attachment.ps1 | ||
| .LINK | ||
| https://docs.sentry.io/platforms/powershell/enriching-events/attachments/ | ||
| #> | ||
|
|
||
| # Import the Sentry module. In your code, you would just use `Import-Module Sentry`. | ||
| Import-Module $PSScriptRoot/../modules/Sentry/Sentry.psd1 | ||
|
|
||
| Start-Sentry { | ||
| $_.Dsn = 'https://997874440feaba4ecc65c1e25df7912b@o447951.ingest.us.sentry.io/4508073336176640' | ||
| $_.Debug = $true | ||
| } | ||
|
|
||
| try { | ||
| # 1) Attach a file by path. The extension (.ps1) is recognized as text, | ||
| # so Sentry will render it inline in the event's Attachments tab. | ||
| # A path can also be supplied from the pipeline: | ||
| # $PSCommandPath | Add-SentryAttachment | ||
| Add-SentryAttachment -Path $PSCommandPath | ||
|
|
||
| # 2) Attach raw bytes with a filename hint. .json maps to application/json | ||
| # so the UI shows a JSON preview. | ||
| $payload = @{ host = [System.Net.Dns]::GetHostName(); psVersion = $PSVersionTable.PSVersion.ToString() } | | ||
| ConvertTo-Json | ||
| $bytes = [System.Text.Encoding]::UTF8.GetBytes($payload) | ||
| Add-SentryAttachment -Bytes $bytes -FileName 'context.json' | ||
|
|
||
| # 3) Explicit -ContentType always wins over the inferred default. | ||
| Add-SentryAttachment -Path $PSCommandPath -ContentType 'text/x-powershell' | ||
|
|
||
|
vaind marked this conversation as resolved.
|
||
| # Capture an event so the attachments have something to ride along with. | ||
| # All three attachments above are attached to this event via the current scope. | ||
| 'Sample event with attachments' | Out-Sentry | ||
| } finally { | ||
| Stop-Sentry | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.