Skip to content

Commit

Permalink
feat: add more sentry tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
fritterhoff committed Nov 16, 2024
1 parent d0f4df4 commit 32cb9c3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 19 deletions.
6 changes: 5 additions & 1 deletion backend/common/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ func SubFromRequest(token interface{}) (string, error) {
func UserFromRequest(c echo.Context) (string, error) {
if token, ok := c.Get("user").(*jwt.Token); ok {
if user, ok := token.Claims.(jwt.MapClaims); ok {
return user["email"].(string), nil
if token, ok := user["email"]; !ok {
return "", errors.New("email not found in token")
} else {

Check failure on line 31 in backend/common/auth/oauth2.go

View workflow job for this annotation

GitHub Actions / lint (backend/common)

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
return token.(string), nil
}
}
}

Expand Down
55 changes: 37 additions & 18 deletions backend/domain-rest-interface/pkg/api/domains/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,9 @@ import (
func (h *Handler) ListDomains(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "list")
hub := sentry.GetHubFromContext(ctx)
if hub == nil {
// Check the concurrency guide for more details: https://docs.sentry.io/platforms/go/concurrency/
hub = sentry.CurrentHub().Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
}

options := []sentry.SpanOption{
// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(c.Request()),
sentry.WithTransactionSource(sentry.SourceURL),
}

transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", c.Request().Method, c.Request().URL.Path),
options...,
)
// Check the concurrency guide for more details: https://docs.sentry.io/platforms/go/concurrency/
// Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()

Expand All @@ -72,6 +57,28 @@ func (h *Handler) ListDomains(c echo.Context) error {
return c.JSON(http.StatusOK, domains)
}

func sentryTrace(ctx context.Context, c echo.Context) (context.Context, *sentry.Span) {
hub := sentry.GetHubFromContext(ctx)
if hub == nil {

hub = sentry.CurrentHub().Clone()
ctx = sentry.SetHubOnContext(ctx, hub)
}

options := []sentry.SpanOption{

sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(c.Request()),
sentry.WithTransactionSource(sentry.SourceURL),
}

transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", c.Request().Method, c.Request().URL.Path),
options...,
)
return ctx, transaction
}

func (h *Handler) enumerateDomains(ctx context.Context, user string, logger *zap.Logger) ([]*model.Domain, error) {

ctx, span := h.tracer.Start(ctx, "enumerating")
Expand Down Expand Up @@ -177,6 +184,8 @@ func (h *Handler) enumerateDomains(ctx context.Context, user string, logger *zap
func (h *Handler) CreateDomain(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "create")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()

user, err := auth.UserFromRequest(c)
Expand Down Expand Up @@ -244,6 +253,8 @@ func (h *Handler) CreateDomain(c echo.Context) error {
func (h *Handler) DeleteDomain(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "delete")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()

item, err := h.evaluatePermission(ctx, c, logger, func(d *model.Domain) bool { return d.Permissions.CanDelete })
Expand Down Expand Up @@ -308,6 +319,8 @@ func (h *Handler) evaluatePermission(ctx context.Context, c echo.Context, logger
func (h *Handler) ApproveDomain(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "approve")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()

item, err := h.evaluatePermission(ctx, c, logger, func(d *model.Domain) bool { return d.Permissions.CanApprove })
Expand Down Expand Up @@ -340,6 +353,8 @@ func (h *Handler) ApproveDomain(c echo.Context) error {
func (h *Handler) TransferDomain(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "transfer")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()

req := &model.TransferRequest{}
Expand Down Expand Up @@ -377,6 +392,8 @@ func (h *Handler) TransferDomain(c echo.Context) error {
func (h *Handler) DeleteDelegation(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "deleteDelegation")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()

item, err := h.evaluatePermission(ctx, c, logger, func(d *model.Domain) bool { return d.Permissions.CanDelegate })
Expand Down Expand Up @@ -420,6 +437,8 @@ func (h *Handler) DeleteDelegation(c echo.Context) error {
func (h *Handler) AddDelegation(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "addDelegation")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()

req := &model.DelegationRequest{}
Expand Down
17 changes: 17 additions & 0 deletions backend/pki-rest-interface/pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"time"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getsentry/sentry-go"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/lestrrat-go/jwx/jwk"
echoSwagger "github.com/swaggo/echo-swagger"

Expand Down Expand Up @@ -91,6 +94,20 @@ func (api *Server) wireRoutesAndMiddleware() {
api.app.Use(logging.ZapLogger(api.logger))
api.app.Use(middleware.Recover())

if api.config.SentryDSN != "" {
if err := sentry.Init(sentry.ClientOptions{
Dsn: api.config.SentryDSN,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
EnableTracing: true,
}); err != nil {
log.Warnf("Sentry initialization failed: %v\n", err)
} else {
api.app.Use(sentryecho.New(sentryecho.Options{}))
}
}
if len(api.config.CorsAllowedOrigins) != 0 {
api.app.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: api.config.CorsAllowedOrigins,
Expand Down
32 changes: 32 additions & 0 deletions backend/pki-rest-interface/pkg/api/ssl/certificate.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ssl

import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"net/http"
"strings"

"github.com/getsentry/sentry-go"
"github.com/hm-edu/pki-rest-interface/pkg/model"
"github.com/hm-edu/portal-common/auth"
"github.com/hm-edu/portal-common/helper"
Expand All @@ -20,6 +22,28 @@ import (
"go.uber.org/zap"
)

func sentryTrace(ctx context.Context, c echo.Context) (context.Context, *sentry.Span) {

Check failure on line 25 in backend/pki-rest-interface/pkg/api/ssl/certificate.go

View workflow job for this annotation

GitHub Actions / lint (backend/pki-rest-interface)

undefined: sentry (typecheck)
hub := sentry.GetHubFromContext(ctx)

Check failure on line 26 in backend/pki-rest-interface/pkg/api/ssl/certificate.go

View workflow job for this annotation

GitHub Actions / lint (backend/pki-rest-interface)

undefined: sentry (typecheck)
if hub == nil {

hub = sentry.CurrentHub().Clone()

Check failure on line 29 in backend/pki-rest-interface/pkg/api/ssl/certificate.go

View workflow job for this annotation

GitHub Actions / lint (backend/pki-rest-interface)

undefined: sentry (typecheck)
ctx = sentry.SetHubOnContext(ctx, hub)
}

options := []sentry.SpanOption{

sentry.WithOpName("http.server"),
sentry.ContinueFromRequest(c.Request()),
sentry.WithTransactionSource(sentry.SourceURL),
}

transaction := sentry.StartTransaction(ctx,
fmt.Sprintf("%s %s", c.Request().Method, c.Request().URL.Path),
options...,
)
return ctx, transaction
}

// Active godoc
// @Summary SSL List active certificates Endpoint
// @Tags SSL
Expand All @@ -34,6 +58,8 @@ func (h *Handler) Active(c echo.Context) error {

logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "list active ssl certificates for given domain")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()

user, err := auth.UserFromRequest(c)
Expand Down Expand Up @@ -91,6 +117,8 @@ func (h *Handler) Active(c echo.Context) error {
func (h *Handler) List(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "list ssl certificates")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()
user, err := auth.UserFromRequest(c)
if err != nil {
Expand Down Expand Up @@ -128,6 +156,8 @@ func (h *Handler) List(c echo.Context) error {
func (h *Handler) Revoke(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "revoke")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()
user, err := auth.UserFromRequest(c)
if err != nil {
Expand Down Expand Up @@ -196,6 +226,8 @@ func (h *Handler) Revoke(c echo.Context) error {
func (h *Handler) HandleCsr(c echo.Context) error {
logger := c.Request().Context().Value(logging.LoggingContextKey).(*zap.Logger)
ctx, span := h.tracer.Start(c.Request().Context(), "issue new certificate")
ctx, transaction := sentryTrace(ctx, c)
defer transaction.Finish()
defer span.End()
user, err := auth.UserFromRequest(c)
if err != nil {
Expand Down

0 comments on commit 32cb9c3

Please sign in to comment.