DEV Community

Cover image for Adding Context to Sentry for Simplified Issue Analysis
Suharxxxx
Suharxxxx

Posted on

Adding Context to Sentry for Simplified Issue Analysis

Context in Sentry

Refers to additional information that can be added to an error message or message sent to Sentry. Context usually contains information that is useful for analyzing and resolving issues in your application.

Examples of information that can be added as context include:

  • User information: Information about the user experiencing the issue.

  • Extra data: Additional data that is useful for analyzing the issue.

Context can be added to an error message or message using methods such as SetExtra, and SetUser in Sentry.



package main

import (
    "log"
    "time"
    "errors"
    "github.com/getsentry/sentry-go"
)

func main() {
    err := sentry.Init(sentry.ClientOptions{
        Dsn: "YOUR DSN",
        Debug: true,
    })
    if err != nil {
        log.Fatalf("sentry.Init: %s", err)
    }
    defer sentry.Flush(2 * time.Second)

    sentry.WithScope(func(scope *sentry.Scope) {

        // set context character
        scope.SetContext("character", map[string]interface{}{
            "name":        "Mighty Fighter",
            "age":         19,
            "attack_type": "melee",
        })

        //add extra data additional
        scope.SetExtra("extra_key", "extra_value")
        //set user id
        scope.SetUser(sentry.User{ID: "user_id_example"})
        //exemple error, fake err
        err = errors.New("Example Error 4")
        //capture error and level error
        scope.SetLevel(sentry.LevelError)
        sentry.CaptureException(err)
    })  
}


Enter fullscreen mode Exit fullscreen mode

Some examples in implementation when sending to sentry.

Image description

  • Set context character information


// set context character
scope.SetContext("character", map[string]interface{}{
    "name":        "Mighty Fighter",
    "age":         19,
    "attack_type": "melee",
})


Enter fullscreen mode Exit fullscreen mode

Image description

  • Set extra data additional


//add extra data additional
scope.SetExtra("extra_key", "extra_value")


Enter fullscreen mode Exit fullscreen mode

Image description

  • Set user id


scope.SetUser(sentry.User{ID: "user_id_example"})


Enter fullscreen mode Exit fullscreen mode

Image description

Learn more about context Sentry.
https://docs.sentry.io/platforms/go/enriching-events/context/
Learn more about conventions for common contexts in the contexts interface developer documentation.
https://develop.sentry.dev/sdk/event-payloads/contexts/

Top comments (1)

Collapse
 
febriramadani profile image
Febri Ramadani

like