getsentry / sentry-powershell

Sentry for PowerShell
MIT License
4 stars 0 forks source link

Cannot add device name to context #48

Closed marvint24 closed 1 month ago

marvint24 commented 1 month ago

Environment

Version 0.1.0

PowerShell:

Windows PowerShell 5.1 + PowerShell 7.4.2

Steps to Reproduce

  1. Set your Sentry DNS in this script:
Import-Module Sentry

Start-Sentry 'https://sentry.url' -Debug
Edit-SentryScope {
    $_.user = @{
        username = 'marvint24'
    }
    $_.contexts['os'] = @{
        name    = 'Windows 11'
        version = '10.0.22631'
    }

    $_.contexts['device'] = @{
        name    = 'Dell XPS 13'
        family = 'XPS'
    }
}

try {
    blupp
}
catch {
    $_ | Out-Sentry
}
  1. Run

Expected Result

If I understood https://docs.sentry.io/platforms/powershell/enriching-events/context/ correctly you should be able to enhance the context. For "os" it is working, but for "device" it is not.

(Maybe it would also be useful if user.username is default: $env:username and device.name is $env:computername)

Actual Result

You get this error: [Sentry] An error occurred when capturing the event 5a38d6ae53214facb309aec8c3395c18. Expected a type of Sentry.Protocol.Device to exist for the key 'device'. Instead found a System.Collections.Hashtable. The likely cause of this is that the value for 'device' has been incorrectly set to an instance of a different type.

bruno-garcia commented 1 month ago

device is a reserved key, that expects type Sentry.Protocol.Device. It's a typed field you can access with context.Device.Name for example. You don't need to assign a new instance of a hashmap.

Same for OS.

You can define with a key as string any custom context, like ['my_device'] should work with that syntax

marvint24 commented 1 month ago

I see. This seams to work:

Edit-SentryScope {
    $_.Contexts.Device.Name = "My device"
}

Is this the right approach?

vaind commented 1 month ago

Is this the right approach?

yes, that looks correct.

Here's the link to the contexts available in .NET https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry/SentryContexts.cs

and specifically the device context: https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry/Protocol/Device.cs

marvint24 commented 1 month ago

okay, nice! Thank you 👍