Categorygithub.com/Flahmingo-Investments/helpers-go

# README

Flahmingo Golang Helpers

Sentry gRPC integration

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

OptionTypePurpose
DSN:stringSentry key for service. Found in Sentry dashboard for the specific project (i.e backend)
Environment:stringAs defined in the config file will tell Sentry how to segregate logs
AttachStacktrace:boolRelease: branch and sha passed in from build
Release:stringThe dist to be sent with events
Debug:boolConfigures 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...)

# Packages

Package fconfig provides support to load config files and expand secrets.
Package ferrors provides simple error handling primitives.
No description provided by the author
Package gcp provides GCP helpers.
No description provided by the author
No description provided by the author
Package sentrygrpc provides interceptor for gRPC server.