DEV Community

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

Posted on

2

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/

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (1)

Collapse
 
febriramadani profile image
Febri Ramadani

like

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay