From 8d78b4c1e3a5de5bccc69245dd015665246e02c6 Mon Sep 17 00:00:00 2001 From: Aslam Hadi Harsono Date: Mon, 16 Dec 2024 10:26:35 +0700 Subject: [PATCH 1/4] feature: add sentry integration and update go version in go.mod --- Dockerfile | 2 +- README.md | 3 +++ config/config.go | 24 ++++++++++++++++++++++++ go.mod | 6 +++--- go.sum | 9 +++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index a2f9a3f66..c24b87e0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -ARG GOLANG_VERSION=1.23.3 +ARG GOLANG_VERSION=1.23.4 FROM golang:${GOLANG_VERSION}-bookworm as builder ARG VIPS_VERSION=8.16.0 diff --git a/README.md b/README.md index 80b5c5faf..6578cdb21 100644 --- a/README.md +++ b/README.md @@ -814,4 +814,7 @@ Usage of imagor: VIPS avif speed, the lowest is at 0 and the fastest is at 9 (Default 5). -vips-strip-metadata VIPS strips all metadata from the resulting image + + -sentry-dsn + include sentry dsn to integrate imagor with sentry ``` diff --git a/config/config.go b/config/config.go index a8c23e056..6612e38c9 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,8 @@ import ( "crypto/sha512" "flag" "fmt" + "github.com/TheZeroSlave/zapsentry" + "github.com/getsentry/sentry-go" "runtime" "strings" "time" @@ -150,6 +152,8 @@ func CreateServer(args []string, funcs ...Option) (srv *server.Server) { "Enable strip query string redirection") serverAccessLog = fs.Bool("server-access-log", false, "Enable server access log") + sentryDsn = fs.String("sentry-dsn", "", + "Sentry DSN config") prometheusBind = fs.String("prometheus-bind", "", "Specify address and port to enable Prometheus metrics, e.g. :5000, prom:7000") prometheusPath = fs.String("prometheus-path", "/", "Prometheus metrics path") @@ -170,6 +174,26 @@ func CreateServer(args []string, funcs ...Option) (srv *server.Server) { } else { logger = zap.Must(zap.NewProduction()) } + + if len(*sentryDsn) > 0 { + err = sentry.Init(sentry.ClientOptions{ + Dsn: *sentryDsn, + }) + if err != nil { + fmt.Printf("sentry.Init: %s", err) + } + defer sentry.Flush(2 * time.Second) + + // Add Sentry integration to zap logger + core, err := zapsentry.NewCore(zapsentry.Configuration{ + Level: zap.ErrorLevel, // Only log errors or higher levels to Sentry + }, zapsentry.NewSentryClientFromClient(sentry.CurrentHub().Client())) + if err != nil { + panic(err) + } + logger = zapsentry.AttachCoreToLogger(core, logger) + } + return logger, *debug }, funcs...) diff --git a/go.mod b/go.mod index a5788566b..f84a17647 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,13 @@ module github.com/cshum/imagor -go 1.22.7 - -toolchain go1.23.1 +go 1.23.4 require ( cloud.google.com/go/storage v1.47.0 + github.com/TheZeroSlave/zapsentry v1.23.0 github.com/aws/aws-sdk-go v1.55.5 github.com/fsouza/fake-gcs-server v1.50.2 + github.com/getsentry/sentry-go v0.30.0 github.com/johannesboyne/gofakes3 v0.0.0-20241026070602-0da3aa9c32ca github.com/peterbourgon/ff/v3 v3.4.0 github.com/prometheus/client_golang v1.20.5 diff --git a/go.sum b/go.sum index f97a65b68..4324e93cf 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0 github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0.49.0/go.mod h1:l2fIqmwB+FKSfvn3bAD/0i+AXAxhIZjTK2svT/mgUXs= github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 h1:GYUJLfvd++4DMuMhCFLgLXvFwofIxh/qOwoGuS/LTew= github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0/go.mod h1:wRbFgBQUVm1YXrvWKofAEmq9HNJTDphbAaJSSX01KUI= +github.com/TheZeroSlave/zapsentry v1.23.0 h1:TKyzfEL7LRlRr+7AvkukVLZ+jZPC++ebCUv7ZJHl1AU= +github.com/TheZeroSlave/zapsentry v1.23.0/go.mod h1:3DRFLu4gIpnCTD4V9HMCBSaqYP8gYU7mZickrs2/rIY= github.com/aws/aws-sdk-go v1.44.256/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= @@ -67,6 +69,10 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2 github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fsouza/fake-gcs-server v1.50.2 h1:ulrS1pavCOCbMZfN5ZPgBRMFWclON9xDsuLBniXtQoE= github.com/fsouza/fake-gcs-server v1.50.2/go.mod h1:VU6Zgei4647KuT4XER8WHv5Hcj2NIySndyG8gfvwckA= +github.com/getsentry/sentry-go v0.30.0 h1:lWUwDnY7sKHaVIoZ9wYqRHJ5iEmoc0pqcRqFkosKzBo= +github.com/getsentry/sentry-go v0.30.0/go.mod h1:WU9B9/1/sHDqeV8T+3VwwbjeR5MSXs/6aqG3mqZrezA= +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/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -141,6 +147,9 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/peterbourgon/ff/v3 v3.4.0 h1:QBvM/rizZM1cB0p0lGMdmR7HxZeI/ZrBWB4DqLkMUBc= github.com/peterbourgon/ff/v3 v3.4.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ= +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/pkg/xattr v0.4.10 h1:Qe0mtiNFHQZ296vRgUjRCoPHPqH7VdTOrZx3g0T+pGA= github.com/pkg/xattr v0.4.10/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6ktU= From c7ed8d0097fd47b39bd32c06d1f1e7e72ed1ad4f Mon Sep 17 00:00:00 2001 From: Aslam Hadi Harsono Date: Mon, 16 Dec 2024 13:29:19 +0700 Subject: [PATCH 2/4] change configuration for sentry --- config/config.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 6612e38c9..db053dcd2 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ import ( "fmt" "github.com/TheZeroSlave/zapsentry" "github.com/getsentry/sentry-go" + "go.uber.org/zap/zapcore" "runtime" "strings" "time" @@ -186,7 +187,9 @@ func CreateServer(args []string, funcs ...Option) (srv *server.Server) { // Add Sentry integration to zap logger core, err := zapsentry.NewCore(zapsentry.Configuration{ - Level: zap.ErrorLevel, // Only log errors or higher levels to Sentry + Level: zapcore.ErrorLevel, // only log errors or higher levels to Sentry + EnableBreadcrumbs: true, // enable sending breadcrumbs to Sentry + BreadcrumbLevel: zapcore.InfoLevel, // at what level should we sent breadcrumbs to sentry, this level can't be higher than `Level` }, zapsentry.NewSentryClientFromClient(sentry.CurrentHub().Client())) if err != nil { panic(err) From 414bd1367b98791876f2735502542fd001c8605f Mon Sep 17 00:00:00 2001 From: Aslam Hadi Harsono Date: Wed, 18 Dec 2024 15:51:07 +0700 Subject: [PATCH 3/4] add test case for sentry-dsn configuration --- config/config.go | 1 + config/config_test.go | 7 +++++++ server/option.go | 7 +++++++ server/server.go | 1 + 4 files changed, 16 insertions(+) diff --git a/config/config.go b/config/config.go index db053dcd2..c1374db4f 100644 --- a/config/config.go +++ b/config/config.go @@ -230,5 +230,6 @@ func CreateServer(args []string, funcs ...Option) (srv *server.Server) { server.WithLogger(logger), server.WithDebug(*debug), server.WithMetrics(pm), + server.WithSentry(*sentryDsn), ) } diff --git a/config/config_test.go b/config/config_test.go index 221ddea14..35d6f0970 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -102,6 +102,13 @@ func TestBind(t *testing.T) { assert.Equal(t, ":4567", srv.Addr) } +func TestSentry(t *testing.T) { + srv := CreateServer([]string{ + "-sentry-dsn", "https://12345@sentry.com/123", + }) + assert.Equal(t, "https://12345@sentry.com/123", srv.SentryDsn) +} + func TestSignerAlgorithm(t *testing.T) { srv := CreateServer([]string{ "-imagor-signer-type", "sha256", diff --git a/server/option.go b/server/option.go index 1d01b6028..fa93c9073 100644 --- a/server/option.go +++ b/server/option.go @@ -73,6 +73,13 @@ func WithDebug(debug bool) Option { } } +// WithSentry with sentry option +func WithSentry(dsn string) Option { + return func(s *Server) { + s.SentryDsn = dsn + } +} + // WithStartupTimeout with server startup timeout option func WithStartupTimeout(timeout time.Duration) Option { return func(s *Server) { diff --git a/server/server.go b/server/server.go index 3b4842503..cc7f0a729 100644 --- a/server/server.go +++ b/server/server.go @@ -45,6 +45,7 @@ type Server struct { CertFile string KeyFile string PathPrefix string + SentryDsn string StartupTimeout time.Duration ShutdownTimeout time.Duration Logger *zap.Logger From 0e4c5c0262ffb4477e95a0683b30151c0a9e4c3b Mon Sep 17 00:00:00 2001 From: Aslam Hadi Harsono Date: Wed, 18 Dec 2024 15:55:13 +0700 Subject: [PATCH 4/4] change error message --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index c1374db4f..75dd474a2 100644 --- a/config/config.go +++ b/config/config.go @@ -192,7 +192,7 @@ func CreateServer(args []string, funcs ...Option) (srv *server.Server) { BreadcrumbLevel: zapcore.InfoLevel, // at what level should we sent breadcrumbs to sentry, this level can't be higher than `Level` }, zapsentry.NewSentryClientFromClient(sentry.CurrentHub().Client())) if err != nil { - panic(err) + fmt.Printf("zapsentry integration error: %s", err) } logger = zapsentry.AttachCoreToLogger(core, logger) }