Skip to content

Commit

Permalink
Improve Sentry Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
f0o committed Sep 7, 2024
1 parent d8f351d commit 0c6d712
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 29 deletions.
4 changes: 3 additions & 1 deletion example.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ web: # WebU
url: "https://example.com/privacy" # Example link URL
sentry: # Sentry error reporting settings (optional)
enabled: false # Enable or disable Sentry error reporting
dsn: "https://xxx.sentry.io/yyy" # Sentry DSN (optional, URL is not validated for correctness)
dsn: "https://xxx.sentry.io/yyy" # DSN (optional, URL is not validated for correctness)
environment: "production" # Environment (optional, defaults to nothing which is interpreted by sentry as 'production')
sample_rate: 1.0 # Trace sample rate (optional, defaults to 0.0 which disables trace sampling but not error reporting)

security.txt: # See https://www.rfc-editor.org/rfc/rfc9116 for field descriptions
enabled: true # Enable or disable security.txt generation
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
connectrpc.com/connect v1.16.2
connectrpc.com/grpchealth v1.3.0
github.com/getsentry/sentry-go v0.28.1
github.com/redis/go-redis/v9 v9.6.1
github.com/rs/cors v1.11.1
golang.org/x/crypto v0.27.0
Expand Down
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,28 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k=
github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
Expand All @@ -32,3 +46,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
27 changes: 26 additions & 1 deletion pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/AS203038/looking-glass/pkg/http/grpc"
"github.com/AS203038/looking-glass/pkg/http/webui"
"github.com/AS203038/looking-glass/pkg/utils"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/redis/go-redis/v9"
"github.com/rs/cors"
"golang.org/x/net/http2"
Expand Down Expand Up @@ -190,7 +192,7 @@ func ListenAndServe(ctx context.Context, cfg *utils.Config, rts utils.RouterMap,
// Create a Redis client
opts, err := redis.ParseURL(cfg.Redis.URI)
if err != nil {
log.Println("ERROR: Failed to parse Redis URL:", err)
log.Println("ERROR: Failed to parse Redis URL:", err, "disabling Redis cache")
} else {
log.Println("NOTICE: Connecting to Redis at", cfg.Redis.URI)
redisClient = redis.NewClient(opts)
Expand All @@ -200,6 +202,29 @@ func ListenAndServe(ctx context.Context, cfg *utils.Config, rts utils.RouterMap,
}

handler = loggingHandler(corsHandler.Handler(handler))

if cfg.Web.Sentry.Enabled {
err := sentry.Init(sentry.ClientOptions{
Dsn: cfg.Web.Sentry.DSN,
EnableTracing: true,
TracesSampleRate: cfg.Web.Sentry.SampleRate,
ProfilesSampleRate: 1.0,
Release: utils.Version(),
Environment: cfg.Web.Sentry.Environment,
})
if err != nil {
log.Println("WARNING: Failed to initialize Sentry:", err, "disabling Sentry middleware")
} else {
log.Println("NOTICE: Sentry initialized")
handler = sentryhttp.New(
sentryhttp.Options{
Repanic: true,
WaitForDelivery: true,
},
).Handle(handler)
}
}

srv := &http.Server{
Addr: cfg.Grpc.Listen,
ErrorLog: log.Default(),
Expand Down
52 changes: 28 additions & 24 deletions pkg/http/webui/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,38 @@ import (
)

type EnvJS struct {
Theme string `json:"PUBLIC_THEME"`
PageTitle string `json:"PUBLIC_PAGE_TITLE"`
HeaderText string `json:"PUBLIC_HEADER_TEXT"`
HeaderLinks string `json:"PUBLIC_HEADER_LINKS"`
HeaderLogo string `json:"PUBLIC_HEADER_LOGO"`
FooterText string `json:"PUBLIC_FOOTER_TEXT"`
FooterLinks string `json:"PUBLIC_FOOTER_LINKS"`
FooterLogo string `json:"PUBLIC_FOOTER_LOGO"`
GrpcURL string `json:"PUBLIC_GRPC_URL"`
LGVersion string `json:"PUBLIC_LG_VERSION"`
SentryDSN string `json:"PUBLIC_SENTRY_DSN"`
RtListMax int `json:"PUBLIC_RT_LIST_MAX"`
Theme string `json:"PUBLIC_THEME"`
PageTitle string `json:"PUBLIC_PAGE_TITLE"`
HeaderText string `json:"PUBLIC_HEADER_TEXT"`
HeaderLinks string `json:"PUBLIC_HEADER_LINKS"`
HeaderLogo string `json:"PUBLIC_HEADER_LOGO"`
FooterText string `json:"PUBLIC_FOOTER_TEXT"`
FooterLinks string `json:"PUBLIC_FOOTER_LINKS"`
FooterLogo string `json:"PUBLIC_FOOTER_LOGO"`
GrpcURL string `json:"PUBLIC_GRPC_URL"`
LGVersion string `json:"PUBLIC_LG_VERSION"`
SentryDSN string `json:"PUBLIC_SENTRY_DSN"`
SentryEnv string `json:"PUBLIC_SENTRY_ENV"`
SentrySampleRate float64 `json:"PUBLIC_SENTRY_SAMPLE_RATE"`
RtListMax int `json:"PUBLIC_RT_LIST_MAX"`
}

func ConfigInjector(cfg utils.WebConfig) http.Handler {
envjson, err := json.Marshal(EnvJS{
Theme: cfg.Theme,
PageTitle: cfg.Title,
HeaderText: cfg.Header.Text,
HeaderLinks: cfg.Header.LinksString(),
HeaderLogo: cfg.Header.Logo,
FooterText: cfg.Footer.Text,
FooterLinks: cfg.Footer.LinksString(),
FooterLogo: cfg.Footer.Logo,
GrpcURL: cfg.GrpcURL,
LGVersion: utils.Version(),
SentryDSN: cfg.Sentry.DSN,
RtListMax: cfg.RtListMax,
Theme: cfg.Theme,
PageTitle: cfg.Title,
HeaderText: cfg.Header.Text,
HeaderLinks: cfg.Header.LinksString(),
HeaderLogo: cfg.Header.Logo,
FooterText: cfg.Footer.Text,
FooterLinks: cfg.Footer.LinksString(),
FooterLogo: cfg.Footer.Logo,
GrpcURL: cfg.GrpcURL,
LGVersion: utils.Version(),
SentryDSN: cfg.Sentry.DSN,
SentryEnv: cfg.Sentry.Environment,
SentrySampleRate: cfg.Sentry.SampleRate,
RtListMax: cfg.RtListMax,
})
if err != nil {
panic(err)
Expand Down
6 changes: 4 additions & 2 deletions pkg/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ type WebConfig struct {
}

type SentryConfig struct {
Enabled bool `yaml:"enabled"`
DSN string `yaml:"dsn"`
Enabled bool `yaml:"enabled"`
DSN string `yaml:"dsn"`
Environment string `yaml:"environment"`
SampleRate float64 `yaml:"sample_rate"`
}

type HFBlock struct {
Expand Down
4 changes: 3 additions & 1 deletion webui/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@
({ init, browserTracingIntegration, replayIntegration }) => {
init({
dsn: env.PUBLIC_SENTRY_DSN,
environment: env.PUBLIC_SENTRY_ENV,
release: env.PUBLIC_LG_VERSION,
integrations: [browserTracingIntegration(), replayIntegration()],
// tracePropagationTargets: [env.PUBLIC_GRPC_URL],
tracesSampleRate: 1.0,
tracesSampleRate: parseFloat(env.PUBLIC_SENTRY_SAMPLE_RATE || "0.0"),
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
});
Expand Down

0 comments on commit 0c6d712

Please sign in to comment.