Skip to content

Commit

Permalink
feat: add sentry to reverse-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Grubenmann committed Jun 2, 2023
1 parent db1a128 commit 13fccbf
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
dsn=os.environ.get("SENTRY_DSN"),
integrations=[FlaskIntegration()],
environment=os.environ.get("SENTRY_ENVIRONMENT"),
sample_rate=float(os.environ.get("SENTRY_SAMPLE_RATE", 0.2)),
)
except Exception:
app.logger.warning("Error while trying to initialize Sentry", exc_info=True)
Expand All @@ -90,7 +91,6 @@ def setup_redis_client():
"""Set up a redis connection to the master by querying sentinel."""

if "pytest" not in sys.modules:

if current_app.config["REDIS_IS_SENTINEL"]:
sentinel = Sentinel(
[(current_app.config["REDIS_HOST"], current_app.config["REDIS_PORT"])],
Expand Down
1 change: 0 additions & 1 deletion app/auth/oauth_provider_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def __init__(
# TODO: Use marshmallow for (de)serialization
# TODO: https://github.com/SwissDataScienceCenter/renku-gateway/issues/231
def to_json(self):

serializer_attributes = [
"kind",
"base_url",
Expand Down
1 change: 0 additions & 1 deletion app/auth/renku_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

class RenkuCoreAuthHeaders:
def process(self, request, headers):

m = re.search(
r"bearer (?P<token>.+)", headers.get("Authorization", ""), re.IGNORECASE
)
Expand Down
1 change: 0 additions & 1 deletion app/tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def client():

@responses.activate
def test_simple(client):

test_url = app.config["GITLAB_URL"] + "/dummy"
responses.add(responses.GET, test_url, json={"error": "not found"}, status=404)

Expand Down
8 changes: 8 additions & 0 deletions cmd/revproxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ type rateLimits struct {
Burst int `mapstructure:"rate_limits_burst"`
}

type sentryConfig struct {
Enabled bool `mapstructure:"sentry_enabled"`
Dsn string `mapstructure:"sentry_dsn"`
Environment string `mapstructure:"sentry_environment"`
SampleRate float64 `mapstructure:"sentry_sample_rate"`
}

type revProxyConfig struct {
RenkuBaseURL *url.URL `mapstructure:"renku_base_url"`
AllowOrigin []string `mapstructure:"allow_origin"`
Expand All @@ -39,6 +46,7 @@ type revProxyConfig struct {
Metrics metricsConfig `mapstructure:",squash"`
RateLimits rateLimits `mapstructure:",squash"`
Port int
Sentry sentryConfig `mapstructure:",squash"`
}

func parseStringAsURL() mapstructure.DecodeHookFuncType {
Expand Down
28 changes: 23 additions & 5 deletions cmd/revproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"

"github.com/getsentry/sentry-go"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"golang.org/x/time/rate"
Expand Down Expand Up @@ -48,11 +50,11 @@ func setupServer(config revProxyConfig) *echo.Echo {
e.Use(middleware.RateLimiter(
middleware.NewRateLimiterMemoryStoreWithConfig(
middleware.RateLimiterMemoryStoreConfig{
Rate: rate.Limit(config.RateLimits.Rate),
Burst: config.RateLimits.Burst,
Rate: rate.Limit(config.RateLimits.Rate),
Burst: config.RateLimits.Burst,
ExpiresIn: 3 * time.Minute,
}),
),
),
)
}

Expand Down Expand Up @@ -97,6 +99,19 @@ func setupServer(config revProxyConfig) *echo.Echo {

func main() {
config := getConfig()

// setup sentry
if config.Sentry.Enabled {
err := sentry.Init(sentry.ClientOptions{
Dsn: config.Sentry.Dsn,
Environment: config.Sentry.Environment,
SampleRate: config.Sentry.SampleRate,
})
if err != nil {
log.Printf("sentry.Init: %s", err)
}
}

e := setupServer(config)
// Start API server
e.Logger.Printf("Starting server with config: %+v", config)
Expand All @@ -107,7 +122,7 @@ func main() {
}()
// Start metrics server if enabled
var metricsServer *echo.Echo
if (config.Metrics.Enabled) {
if config.Metrics.Enabled {
metricsServer = getMetricsServer(e, config.Metrics.Port)
go func() {
if err := metricsServer.Start(fmt.Sprintf(":%d", config.Metrics.Port)); err != nil && err != http.ErrServerClosed {
Expand All @@ -117,13 +132,16 @@ func main() {
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit // Wait for interrupt signal from OS
<-quit // Wait for interrupt signal from OS
// Start shutting down servers
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
if config.Sentry.Enabled {
defer sentry.Flush(2 * time.Second)
}
if config.Metrics.Enabled {
if err := metricsServer.Shutdown(ctx); err != nil {
metricsServer.Logger.Fatal(err)
Expand Down
17 changes: 9 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/labstack/echo/v4 v4.10.1
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.2
golang.org/x/time v0.3.0
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
Expand All @@ -20,6 +20,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/sentry-go v0.21.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
Expand All @@ -36,7 +37,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
Expand All @@ -56,14 +57,14 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.29.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/getsentry/sentry-go v0.21.0 h1:c9l5F1nPF30JIppulk4veau90PK6Smu3abgVtVQWon4=
github.com/getsentry/sentry-go v0.21.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -186,6 +188,7 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand Down Expand Up @@ -248,6 +251,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand All @@ -274,6 +278,8 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -340,6 +346,8 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -402,9 +410,12 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -413,6 +424,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -565,6 +578,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62UoM=
google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
10 changes: 9 additions & 1 deletion helm-chart/renku-gateway/templates/deployment-revproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
{{- if not .Values.reverseProxy.autoscaling.enabled }}
replicas: {{ .Values.reverseProxy.replicaCount }}
{{- end }}
strategy:
strategy:
{{- toYaml .Values.reverseProxy.updateStrategy | nindent 4 }}
selector:
matchLabels:
Expand Down Expand Up @@ -71,6 +71,14 @@ spec:
value: {{ .Values.rateLimits.general.average | quote }}
- name: REVPROXY_RATE_LIMITS_BURST
value: {{ .Values.rateLimits.general.burst | quote }}
- name: REVPROXY_SENTRY_ENABLED
value: {{ .Values.sentry.enabled | quote }}
- name: REVPROXY_SENTRY_DSN
value: {{ .Values.sentry.dsn }}
- name: REVPROXY_SENTRY_ENVIRONMENT
value: {{ .Values.sentry.environment }}
- name: REVPROXY_SENTRY_SAMPLE_RATE
value: {{ .Values.sentry.sampleRate | quote }}
volumeMounts:
{{- include "certificates.volumeMounts.system" . | nindent 12 }}
livenessProbe:
Expand Down
2 changes: 2 additions & 0 deletions helm-chart/renku-gateway/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ spec:
value: {{ .Values.sentry.dsn }}
- name: SENTRY_ENVIRONMENT
value: {{ .Values.sentry.environment }}
- name: SENTRY_SAMPLE_RATE
value: {{ .Values.sentry.sampleRate | quote }}
- name: DEBUG
value: {{ .Values.global.debug | quote }}
{{- include "certificates.env.python" . | nindent 12 }}
Expand Down
3 changes: 2 additions & 1 deletion helm-chart/renku-gateway/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ image:
repository: renku/renku-gateway
tag: "latest"
pullPolicy: IfNotPresent


service:
type: ClusterIP
Expand Down Expand Up @@ -196,6 +196,7 @@ sentry:
enabled: false
dsn:
environment:
sampleRate: 0.1

# An ingress could be enabled here. Usually the gateway runs
# without its own ingress.
Expand Down

0 comments on commit 13fccbf

Please sign in to comment.