hertzsentry

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

hertzsentry -> (This is a community driven project)

Introduction

Sentry is an open source real-time error monitoring project that supports many sides, including the Web front end, server side, mobile side, and game side.In order to introduce sentry to Hertz on the basis of Sentry-Go, we implement the middleware hertzsentry, which provides some unified interfaces to help users get sentry hub and report error messages.

This project refers to fibersentry.

Install
go get github.com/hertz-contrib/hertzsentry
import
import "github.com/hertz-contrib/hertzsentry"
Config

hertzsentry provides a struct named Option for meeting your requirements. For exampple, you can use WithRePanic function to configure whether you want to block the request before moving forward with the response.

// WithRePanic configures whether Sentry should repanic after recovery.
// Set to true, if Recover middleware is used.
func WithRePanic(rePanic bool) Option {
	return Option{F: func(o *options) {
		o.rePanic = rePanic
	}}
}
Usage

Hertzsentry attaches an instance of *sentry.Hub to the *app. RequestContext so that the hub can be used during the lifetime of the request. Concerning about concurrent scenarios, every request should maintain an unique instance of the *sentry.Hub for the security of hub. Therefore, We will clone a new instance of sentry.CurrentHub only when the request used it, so as to reduce the memory cost.

package main

import (
	"context"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/getsentry/sentry-go"
	"github.com/hertz-contrib/hertzsentry"
	"log"
)

var yourDsn = ""

func main()  {
	// set interval to 0 means using fs-watching mechanism.
	h := server.Default(server.WithAutoReloadRender(true, 0))

	// init sentry
	if err := sentry.Init(sentry.ClientOptions{
		// The DSN to use. If the DSN is not set, the client is effectively disabled.
		Dsn: yourDsn,
		// Before send callback.
		BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
			return event
		},
		// In debug mode, the debug information is printed to stdout to help you understand what
		// sentry is doing.
		Debug: true,
		// Configures whether SDK should generate and attach stacktraces to pure capture message calls.
		AttachStacktrace: true,
	}); err != nil {
		log.Fatal("sentry init failed")
	}

	// use sentry middleware and config with your requirements.
  // attention! you should use sentry handler after recovery.Recovery() 
	h.Use(hertzsentry.NewSentry(
		hertzsentry.WithSendRequest(true),
		hertzsentry.WithRePanic(true),
		))

	h.GET("/hello", func(c context.Context, ctx *app.RequestContext) {
		// use GetHubFromContext to get the hub
		if hub := hertzsentry.GetHubFromContext(ctx); hub != nil {
			hub.WithScope(func(scope *sentry.Scope) {
				scope.SetTag("hertz", "CloudWeGo Hertz")
				scope.SetLevel(sentry.LevelDebug)
				hub.CaptureMessage("Just for debug")
			})
		}
		ctx.SetStatusCode(0)
	})

	h.Spin()
}
Test

Send a request to the interface localhost:8888/hello, then you can see the event in your sentry UI.

curl localhost:8888/hello

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHubFromContext

func GetHubFromContext(ctx *app.RequestContext) *sentry.Hub

GetHubFromContext get the sentry hub, every RequestContext shares the same hub instance

func NewOptions

func NewOptions(opts ...Option) options

func NewSentry

func NewSentry(options ...Option) app.HandlerFunc

NewSentry the config of sentry and return the handler of sentry middleware

Types

type Option

type Option struct {
	F func(o *options)
}

Option set to config unique option

func WithRePanic

func WithRePanic(rePanic bool) Option

WithRePanic configures whether Sentry should repanic after recovery. Set to true, if Recover middleware is used.

func WithSendBody

func WithSendBody(sendBody bool) Option

WithSendBody configures whether you want to add current request body when capturing sentry events.

func WithSendRequest

func WithSendRequest(sendRequest bool) Option

WithSendRequest configures whether you want to add current request head when capturing sentry events.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout configs timeout for the event delivery requests.

func WithWaitForDelivery

func WithWaitForDelivery(waitForDelivery bool) Option

WithWaitForDelivery configures whether you want to block the request before moving forward with the response. If Recover middleware is used, it's safe to either skip this option or set it to false.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL