helpers-go

module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: MIT

README

Flahmingo Golang Helpers

Sentry gRPC integration

Setup sentry with the following variables, they are optional, but recommended:

Option Type Purpose
DSN: string Sentry key for service. Found in Sentry dashboard for the specific project (i.e backend)
Environment: string As defined in the config file will tell Sentry how to segregate logs
AttachStacktrace: bool Release: branch and sha passed in from build
Release: string The dist to be sent with events
Debug: bool Configures whether SDK should generate and attach stacktraces to pure capture message calls.

More options found in sentry documentation

Implementation Code:

There are two vital pieces to implement and start the Sentry service:

  1. This code is to live in the app start or main file
// These vars to be overriden by the compiler
var (
  sha    = "unknown"
  branch = "unknown"
  // swap for service name
  serviceName = "name_your_service"
)

// Setup sentry
err = sentry.Init(sentry.ClientOptions{
  Dsn:              config.Sentry.DSN,
  Environment:      config.Env,
  AttachStacktrace: true,
  Release:          fmt.Sprintf("%s@%s", branch, sha),
  Debug:            config.Debug,
})

if err != nil {
  // please note requires flog package or in its abscence another package
  flog.Fatalf("sentry.Init: %v", err)
}

// Sets the tag so that the specific service can be traced in the dashboard 
sentry.ConfigureScope(func(scope *sentry.Scope) {
  scope.SetTag("service_name", serviceName)
})

// defer flush on shutdown by two seconds as per documentation (lint no magic number)
sentryFlushTimeout := 2 * time.Second

// We have to wait before quitting so, sentry push all the events.
defer sentry.Flush(sentryFlushTimeout)
  1. Implementation with Unary or Stream Server attach to gRPC middlewares
// gRPC middlewares
grpcOpts := []grpc.ServerOption{
  grpc.StreamInterceptor(
    middleware.ChainStreamServer(
      grpczap.StreamServerInterceptor(logger, zapOpts...),
      // your interceptors
      sentrygrpc.SentryStreamServerInterceptor(),
    ),
  ),
  grpc.UnaryInterceptor(
    middleware.ChainUnaryServer(
      tags.UnaryServerInterceptor(),
      // your interceptors
      grpczap.UnaryServerInterceptor(logger, zapOpts...),
      sentrygrpc.SentryUnaryServerInterceptor(unarySentryOptions...),
    ),
  ),
}

grpcServer := grpc.NewServer(grpcOpts...)

Directories

Path Synopsis
Package fconfig provides support to load config files and expand secrets.
Package fconfig provides support to load config files and expand secrets.
Package ferrors provides simple error handling primitives.
Package ferrors provides simple error handling primitives.
Package gcp provides GCP helpers.
Package gcp provides GCP helpers.
gloria
http
middleware
Package httpmw contains http middlewares.
Package httpmw contains http middlewares.
Package sentrygrpc provides interceptor for gRPC server.
Package sentrygrpc provides interceptor for gRPC server.

Jump to

Keyboard shortcuts

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