Skip to content

Commit

Permalink
Fix: memory leak in sentry traces (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Dec 17, 2024
1 parent 979eaa4 commit c2dbe11
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 31 deletions.
17 changes: 3 additions & 14 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ import (
"github.com/celenium-io/celestia-indexer/pkg/node/rpc"
"github.com/dipdup-net/go-lib/config"
"github.com/getsentry/sentry-go"
sentryotel "github.com/getsentry/sentry-go/otel"
"github.com/grafana/pyroscope-go"
"github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"golang.org/x/time/rate"

"github.com/MarceloPetrucio/go-scalar-api-reference"
Expand Down Expand Up @@ -155,7 +152,7 @@ func observableCacheSkipper(c echo.Context) bool {
return false
}

func initEcho(cfg ApiConfig, db postgres.Storage, env string) *echo.Echo {
func initEcho(cfg ApiConfig, env string) *echo.Echo {
e := echo.New()
e.Validator = handler.NewCelestiaApiValidator()

Expand Down Expand Up @@ -238,7 +235,7 @@ func initEcho(cfg ApiConfig, db postgres.Storage, env string) *echo.Echo {

}

if err := initSentry(e, db, cfg.SentryDsn, env); err != nil {
if err := initSentry(e, cfg.SentryDsn, env); err != nil {
log.Err(err).Msg("sentry")
}
e.Server.IdleTimeout = time.Second * 30
Expand Down Expand Up @@ -522,7 +519,7 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
}
}

func initSentry(e *echo.Echo, db postgres.Storage, dsn, environment string) error {
func initSentry(e *echo.Echo, dsn, environment string) error {
if dsn == "" {
return nil
}
Expand All @@ -541,14 +538,6 @@ func initSentry(e *echo.Echo, db postgres.Storage, dsn, environment string) erro
return errors.Wrap(err, "initialization")
}

tp := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(sentryotel.NewSentrySpanProcessor()),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(sentryotel.NewSentryPropagator())

db.SetTracer(tp)

e.Use(SentryMiddleware())

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

db := initDatabase(cfg.Database, cfg.Indexer.ScriptsDir)
e := initEcho(cfg.ApiConfig, db, cfg.Environment)
e := initEcho(cfg.ApiConfig, cfg.Environment)
initDispatcher(ctx, db)
initGasTracker(ctx, db)
initHandlers(ctx, e, *cfg, db)
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestRoutes(t *testing.T) {
BlobReceiver: "dal_node",
}

e := initEcho(apiCfg, db, "development")
e := initEcho(apiCfg, "development")
defer func() {
err := e.Close()
require.NoError(t, err)
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/fatih/structs v1.1.0
github.com/gabriel-vasile/mimetype v1.4.3
github.com/getsentry/sentry-go v0.30.0
github.com/getsentry/sentry-go/otel v0.27.0
github.com/go-playground/validator/v10 v10.18.0
github.com/go-testfixtures/testfixtures/v3 v3.9.0
github.com/goccy/go-json v0.10.3
Expand All @@ -48,7 +47,6 @@ require (
github.com/uptrace/bun/driver/pgdriver v1.1.17
github.com/vmihailenco/msgpack/v5 v5.4.1
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/sdk v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
go.uber.org/mock v0.2.0
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,6 @@ github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqG
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
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/getsentry/sentry-go/otel v0.27.0 h1:p06qcOWuwA5eZkIoJuWjQVBQGFAPrHibShoFKakDV2g=
github.com/getsentry/sentry-go/otel v0.27.0/go.mod h1:ulQ2Luf7K0eIiESCpcPaUZeQx6T0BS08hSPJx+xIZNg=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
Expand Down
11 changes: 0 additions & 11 deletions internal/storage/postgres/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/pkg/errors"
"github.com/uptrace/bun"
"github.com/uptrace/bun/migrate"
"go.opentelemetry.io/otel/trace"
)

// Storage -
Expand Down Expand Up @@ -226,13 +225,3 @@ func (s Storage) Close() error {
}
return nil
}

func (s Storage) SetTracer(tp trace.TracerProvider) {
s.Connection().DB().AddQueryHook(
NewSentryHook(
s.cfg.Database,
tp.Tracer("db"),
true,
),
)
}

0 comments on commit c2dbe11

Please sign in to comment.