Skip to content

Commit e8b9c7c

Browse files
authored
feature: add sentry integration and update go version in go.mod (#510)
* feature: add sentry integration and update go version in go.mod * change configuration for sentry * add test case for sentry-dsn configuration * change error message
1 parent b5699ae commit e8b9c7c

File tree

8 files changed

+59
-4
lines changed

8 files changed

+59
-4
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ARG GOLANG_VERSION=1.23.3
1+
ARG GOLANG_VERSION=1.23.4
22
FROM golang:${GOLANG_VERSION}-bookworm as builder
33

44
ARG VIPS_VERSION=8.16.0

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,4 +814,7 @@ Usage of imagor:
814814
VIPS avif speed, the lowest is at 0 and the fastest is at 9 (Default 5).
815815
-vips-strip-metadata
816816
VIPS strips all metadata from the resulting image
817+
818+
-sentry-dsn
819+
include sentry dsn to integrate imagor with sentry
817820
```

config/config.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"crypto/sha512"
77
"flag"
88
"fmt"
9+
"github.com/TheZeroSlave/zapsentry"
10+
"github.com/getsentry/sentry-go"
11+
"go.uber.org/zap/zapcore"
912
"runtime"
1013
"strings"
1114
"time"
@@ -150,6 +153,8 @@ func CreateServer(args []string, funcs ...Option) (srv *server.Server) {
150153
"Enable strip query string redirection")
151154
serverAccessLog = fs.Bool("server-access-log", false,
152155
"Enable server access log")
156+
sentryDsn = fs.String("sentry-dsn", "",
157+
"Sentry DSN config")
153158

154159
prometheusBind = fs.String("prometheus-bind", "", "Specify address and port to enable Prometheus metrics, e.g. :5000, prom:7000")
155160
prometheusPath = fs.String("prometheus-path", "/", "Prometheus metrics path")
@@ -170,6 +175,28 @@ func CreateServer(args []string, funcs ...Option) (srv *server.Server) {
170175
} else {
171176
logger = zap.Must(zap.NewProduction())
172177
}
178+
179+
if len(*sentryDsn) > 0 {
180+
err = sentry.Init(sentry.ClientOptions{
181+
Dsn: *sentryDsn,
182+
})
183+
if err != nil {
184+
fmt.Printf("sentry.Init: %s", err)
185+
}
186+
defer sentry.Flush(2 * time.Second)
187+
188+
// Add Sentry integration to zap logger
189+
core, err := zapsentry.NewCore(zapsentry.Configuration{
190+
Level: zapcore.ErrorLevel, // only log errors or higher levels to Sentry
191+
EnableBreadcrumbs: true, // enable sending breadcrumbs to Sentry
192+
BreadcrumbLevel: zapcore.InfoLevel, // at what level should we sent breadcrumbs to sentry, this level can't be higher than `Level`
193+
}, zapsentry.NewSentryClientFromClient(sentry.CurrentHub().Client()))
194+
if err != nil {
195+
fmt.Printf("zapsentry integration error: %s", err)
196+
}
197+
logger = zapsentry.AttachCoreToLogger(core, logger)
198+
}
199+
173200
return logger, *debug
174201
}, funcs...)
175202

@@ -203,5 +230,6 @@ func CreateServer(args []string, funcs ...Option) (srv *server.Server) {
203230
server.WithLogger(logger),
204231
server.WithDebug(*debug),
205232
server.WithMetrics(pm),
233+
server.WithSentry(*sentryDsn),
206234
)
207235
}

config/config_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ func TestBind(t *testing.T) {
102102
assert.Equal(t, ":4567", srv.Addr)
103103
}
104104

105+
func TestSentry(t *testing.T) {
106+
srv := CreateServer([]string{
107+
"-sentry-dsn", "https://12345@sentry.com/123",
108+
})
109+
assert.Equal(t, "https://12345@sentry.com/123", srv.SentryDsn)
110+
}
111+
105112
func TestSignerAlgorithm(t *testing.T) {
106113
srv := CreateServer([]string{
107114
"-imagor-signer-type", "sha256",

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module github.com/cshum/imagor
22

3-
go 1.22.7
4-
5-
toolchain go1.23.1
3+
go 1.23.4
64

75
require (
86
cloud.google.com/go/storage v1.47.0
7+
github.com/TheZeroSlave/zapsentry v1.23.0
98
github.com/aws/aws-sdk-go v1.55.5
109
github.com/fsouza/fake-gcs-server v1.50.2
10+
github.com/getsentry/sentry-go v0.30.0
1111
github.com/johannesboyne/gofakes3 v0.0.0-20241026070602-0da3aa9c32ca
1212
github.com/peterbourgon/ff/v3 v3.4.0
1313
github.com/prometheus/client_golang v1.20.5

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0
3434
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0.49.0/go.mod h1:l2fIqmwB+FKSfvn3bAD/0i+AXAxhIZjTK2svT/mgUXs=
3535
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 h1:GYUJLfvd++4DMuMhCFLgLXvFwofIxh/qOwoGuS/LTew=
3636
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0/go.mod h1:wRbFgBQUVm1YXrvWKofAEmq9HNJTDphbAaJSSX01KUI=
37+
github.com/TheZeroSlave/zapsentry v1.23.0 h1:TKyzfEL7LRlRr+7AvkukVLZ+jZPC++ebCUv7ZJHl1AU=
38+
github.com/TheZeroSlave/zapsentry v1.23.0/go.mod h1:3DRFLu4gIpnCTD4V9HMCBSaqYP8gYU7mZickrs2/rIY=
3739
github.com/aws/aws-sdk-go v1.44.256/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
3840
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
3941
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
6769
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
6870
github.com/fsouza/fake-gcs-server v1.50.2 h1:ulrS1pavCOCbMZfN5ZPgBRMFWclON9xDsuLBniXtQoE=
6971
github.com/fsouza/fake-gcs-server v1.50.2/go.mod h1:VU6Zgei4647KuT4XER8WHv5Hcj2NIySndyG8gfvwckA=
72+
github.com/getsentry/sentry-go v0.30.0 h1:lWUwDnY7sKHaVIoZ9wYqRHJ5iEmoc0pqcRqFkosKzBo=
73+
github.com/getsentry/sentry-go v0.30.0/go.mod h1:WU9B9/1/sHDqeV8T+3VwwbjeR5MSXs/6aqG3mqZrezA=
74+
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
75+
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
7076
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
7177
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
7278
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
141147
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
142148
github.com/peterbourgon/ff/v3 v3.4.0 h1:QBvM/rizZM1cB0p0lGMdmR7HxZeI/ZrBWB4DqLkMUBc=
143149
github.com/peterbourgon/ff/v3 v3.4.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ=
150+
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
151+
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
152+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
144153
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
145154
github.com/pkg/xattr v0.4.10 h1:Qe0mtiNFHQZ296vRgUjRCoPHPqH7VdTOrZx3g0T+pGA=
146155
github.com/pkg/xattr v0.4.10/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6ktU=

server/option.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ func WithDebug(debug bool) Option {
7373
}
7474
}
7575

76+
// WithSentry with sentry option
77+
func WithSentry(dsn string) Option {
78+
return func(s *Server) {
79+
s.SentryDsn = dsn
80+
}
81+
}
82+
7683
// WithStartupTimeout with server startup timeout option
7784
func WithStartupTimeout(timeout time.Duration) Option {
7885
return func(s *Server) {

server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Server struct {
4545
CertFile string
4646
KeyFile string
4747
PathPrefix string
48+
SentryDsn string
4849
StartupTimeout time.Duration
4950
ShutdownTimeout time.Duration
5051
Logger *zap.Logger

0 commit comments

Comments
 (0)