Sentry-java: Context on Sentry for Android

Created on 23 Oct 2020  ·  9Comments  ·  Source: getsentry/sentry-java

Hello,

I want to use context (instead of extras, because 'Extra is deprecated') but I don't understand how it works.

Example :

I initialize my Sentry with
SentryAndroid.init(this, { options -> options.setDsn("https://[email protected]/123") })

Then, following your documentation, I try to add global context with :

Sentry.configureScope { scope: Scope ->
            scope.setContexts("name", "Mighty Fighter")
            scope.setContexts("age", 19)
            scope.setContexts("attack_type", "melee")
        } 

When i check my event, I can see :

value    melee

NAME
value   Mighty Fighter

AGE
value   19

Why this type of display?

I expected to see something like :

ATTACK_TYPE   melee
NAME              Mighty Fighter
AGE                   19

Also, if I try to add a context to an event likeevent.contexts["name"] = "myname"
Then, when I check the event on Sentry I can see an error saying :

contexts.name: Discarded invalid value
Reason 
   expected an object
Value   
   myname

If I set an hashMap like thisevent.contexts["name"] = "name" to "myname"
I've no error on Sentry but the context is displayed like this

first     name
second  myname

I come from an old version on Sentry, using Extras, and never encounter this type of problem.
It's not possible to set a simple key/value like extra?
The value cannot be a simple String?

Thanks

invalid

All 9 comments

hey @alexandresoler thanks for raising this.

extra is not deprecated afaik https://develop.sentry.dev/sdk/event-payloads/
you can achieve that by doing:

Sentry.configureScope { scope: Scope ->
            scope.setExtra("name", "Mighty Fighter")
            scope.setExtra("age", 19)
            scope.setExtra("attack_type", "melee")
} 

Contexts require an object as stated here:

https://develop.sentry.dev/sdk/event-payloads/contexts/

It accepts an object of key/value pairs. The key is the “alias” of the context and can be freely chosen

so if you call scope.setContexts("name", "Mighty Fighter") we just wrap up in a value field to make it as an object and be compatible with the Contexts type (accepting objects, not primitive types).

eg:

{
  "name": {
    "value": "Mighty Fighter"
  }
}

If you want key/value for primitive types, just use extras or tags for example, I hope that helps.

@alexandresoler event.contexts["name"] = "name" to "myname" is just the wrong syntax for Kotlin, this makes a Pair<String, String>, if you want to achieve a Map of values using to, do: mapOf("name" to "myname")

@marandaneto I can see this in the documentation: "Extra is deprecated in favor of context within most SDKs."
https://docs.sentry.io/platforms/android/enriching-events/context/#extra-data-additional-data

Tags is used for filtering, not the goal of what I want to store in my events.

Another question, about extras.
I want to modify my event (with beforeSend) and the list of extra is private so I can't clean extras before sending the event.
Any solution to get all keys ​​or extra Map to apply some processing on extras?

@marandaneto I can see this in the documentation: "Extra is deprecated in favor of context within most SDKs."
https://docs.sentry.io/platforms/android/enriching-events/context/#extra-data-additional-data

Tags is used for filtering, not the goal of what I want to store in my events.

good point, I'll check this internally and let you know

Another question, about extras.
I want to modify my event (with beforeSend) and the list of extra is private so I can't clean extras before sending the event.
Any solution to get all keys ​​or extra Map to apply some processing on extras?

mm, getExtra(key) and removeExtra (key) would do the job, but you must know the key.
It'd be nice to expose an immutable list though, I'll also check this and let you know.

Hello @marandaneto , any news for me please? 😃

I'm all in for that but @bruno-garcia had concerns about this iirc.
@bruno-garcia what's about making the getters public but returning an immutable list/map? so people can filter based on the keys, right now people need to getExtra and check if its non-null to removeExtra.
https://www.javatpoint.com/java-collections-unmodifiablelist-method

Hello @marandaneto and @bruno-garcia, let me know if it's possible to make the extras keys list public, I'm still interested :-)

Was this page helpful?
0 / 5 - 0 ratings