diff --git a/cmd/serve.go b/cmd/serve.go index 1fbacc20b..f66cea077 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -2,16 +2,17 @@ package cmd import ( "github.com/formancehq/go-libs/httpserver" - "github.com/formancehq/go-libs/pprof" + "github.com/formancehq/go-libs/otlp" "github.com/formancehq/ledger/internal/storage/driver" "github.com/go-chi/chi/v5" "go.opentelemetry.io/otel/sdk/metric" + "net/http" + "net/http/pprof" "time" "github.com/formancehq/go-libs/auth" "github.com/formancehq/go-libs/aws/iam" "github.com/formancehq/go-libs/bun/bunconnect" - "github.com/formancehq/go-libs/logging" "github.com/formancehq/go-libs/otlp/otlpmetrics" "github.com/formancehq/go-libs/otlp/otlptraces" "github.com/formancehq/go-libs/publish" @@ -25,10 +26,6 @@ import ( "github.com/formancehq/go-libs/service" "github.com/spf13/cobra" "go.uber.org/fx" - - _ "github.com/grafana/pyroscope-go/godeltaprof/http/pprof" - //nolint:gosec - _ "net/http/pprof" ) const ( @@ -51,20 +48,14 @@ func NewServeCommand() *cobra.Command { } enablePProf, _ := cmd.Flags().GetBool(EnablePProfFlag) + otelMetricsExporter, _ := cmd.Flags().GetString(otlpmetrics.OtelMetricsExporterFlag) + options := []fx.Option{ fx.NopLogger, + otlp.FXModuleFromFlags(cmd), otlptraces.FXModuleFromFlags(cmd), - } - if enablePProf { - logging.FromContext(cmd.Context()).Info("Enabling pprof...") - options = append(options, pprof.NewFXModule()) - } - - otelMetricsExporter, _ := cmd.Flags().GetString(otlpmetrics.OtelMetricsExporterFlag) - - options = append(options, - publish.FXModuleFromFlags(cmd, service.IsDebug(cmd)), otlpmetrics.FXModuleFromFlags(cmd), + publish.FXModuleFromFlags(cmd, service.IsDebug(cmd)), auth.FXModuleFromFlags(cmd), bunconnect.Module(*connectionOptions, service.IsDebug(cmd)), storage.NewFXModule(serveConfiguration.autoUpgrade), @@ -86,15 +77,20 @@ func NewServeCommand() *cobra.Command { fx.Invoke(func(lc fx.Lifecycle, h chi.Router) { lc.Append(httpserver.NewHook(h, httpserver.WithAddress(serveConfiguration.bind))) }), - ) - if otelMetricsExporter == "memory" { + } + if otelMetricsExporter == "memory" || enablePProf { options = append(options, fx.Decorate(func( h chi.Router, meterProvider *metric.MeterProvider, exporter *otlpmetrics.InMemoryExporter, ) chi.Router { wrappedRouter := chi.NewRouter() - wrappedRouter.Handle("/_metrics", otlpmetrics.NewInMemoryExporterHandler(meterProvider, exporter)) + if otelMetricsExporter == "memory" { + wrappedRouter.Handle("/_metrics", otlpmetrics.NewInMemoryExporterHandler(meterProvider, exporter)) + } + if enablePProf { + wrappedRouter.Handle("/debug/pprof/*", http.HandlerFunc(pprof.Index)) + } wrappedRouter.Mount("/", h) return wrappedRouter diff --git a/go.mod b/go.mod index 608ff074f..79643d998 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/alitto/pond v1.9.2 github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 github.com/bluele/gcache v0.0.2 - github.com/formancehq/go-libs v1.7.2-0.20241012142206-fbfc2c797d2c + github.com/formancehq/go-libs v1.7.2-0.20241012190325-2eea0f3f4611 github.com/formancehq/stack/ledger/client v0.0.0-00010101000000-000000000000 github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/cors v1.2.1 diff --git a/go.sum b/go.sum index 7b2abd643..b854e438c 100644 --- a/go.sum +++ b/go.sum @@ -105,6 +105,10 @@ github.com/formancehq/go-libs v1.7.2-0.20241011191318-a18ddf18e4f9 h1:71fY4Pc9Zg github.com/formancehq/go-libs v1.7.2-0.20241011191318-a18ddf18e4f9/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4= github.com/formancehq/go-libs v1.7.2-0.20241012142206-fbfc2c797d2c h1:HE8yjpXyLkXJKDC7ig+INzdWvkT0SiuINt+gqC0/cBU= github.com/formancehq/go-libs v1.7.2-0.20241012142206-fbfc2c797d2c/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4= +github.com/formancehq/go-libs v1.7.2-0.20241012145821-406b86d0bedc h1:XbGJ6Vq7H749JyXVsWSLZhMuNWhIRtD5o4tBHhvrexs= +github.com/formancehq/go-libs v1.7.2-0.20241012145821-406b86d0bedc/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4= +github.com/formancehq/go-libs v1.7.2-0.20241012190325-2eea0f3f4611 h1:jDgRN/vZiCJ4gaMQyM1NCgMxPzxyHfgDyeDbzRU8M6k= +github.com/formancehq/go-libs v1.7.2-0.20241012190325-2eea0f3f4611/go.mod h1:B/KB2mPjFqybLDZufMcZQKpXcHg8Va6JaoUWda1KGU4= github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw= github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= diff --git a/internal/controller/ledger/controller_with_too_many_client_handling.go b/internal/controller/ledger/controller_with_too_many_client_handling.go index 3f676c9c1..17f3ca52c 100644 --- a/internal/controller/ledger/controller_with_too_many_client_handling.go +++ b/internal/controller/ledger/controller_with_too_many_client_handling.go @@ -23,7 +23,7 @@ func (fn DelayCalculatorFn) Next(iteration int) time.Duration { type ControllerWithTooManyClientHandling struct { Controller delayCalculator DelayCalculator - tracer trace.Tracer + tracer trace.Tracer } func NewControllerWithTooManyClientHandling( @@ -34,7 +34,7 @@ func NewControllerWithTooManyClientHandling( return &ControllerWithTooManyClientHandling{ Controller: underlying, delayCalculator: delayCalculator, - tracer: tracer, + tracer: tracer, } } diff --git a/internal/controller/ledger/log_process.go b/internal/controller/ledger/log_process.go index 3c69d4e45..dd2b0f4cf 100644 --- a/internal/controller/ledger/log_process.go +++ b/internal/controller/ledger/log_process.go @@ -10,6 +10,8 @@ import ( ledger "github.com/formancehq/ledger/internal" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" + "math/rand" + "time" ) func runTx[INPUT any, OUTPUT ledger.LogPayload](ctx context.Context, store Store, parameters Parameters[INPUT], fn func(ctx context.Context, sqlTX TX, input INPUT) (*OUTPUT, error)) (*OUTPUT, error) { @@ -57,6 +59,8 @@ func forgeLog[INPUT any, OUTPUT ledger.LogPayload](ctx context.Context, store St case errors.Is(err, postgres.ErrDeadlockDetected): trace.SpanFromContext(ctx).SetAttributes(attribute.Bool("deadlock", true)) logging.FromContext(ctx).Info("deadlock detected, retrying...") + // todo: keep ? / set configurable? + <-time.After(time.Duration(rand.Intn(100)) * time.Millisecond) continue // A log with the IK could have been inserted in the meantime, read again the database to retrieve it case errors.Is(err, ErrIdempotencyKeyConflict{}): diff --git a/pkg/testserver/server.go b/pkg/testserver/server.go index 77e9cf86c..e2f28375d 100644 --- a/pkg/testserver/server.go +++ b/pkg/testserver/server.go @@ -32,6 +32,7 @@ type T interface { } type OTLPConfig struct { + BaseConfig otlp.Config Metrics *otlpmetrics.ModuleConfig } @@ -134,9 +135,9 @@ func (s *Server) Start() { strings.Join(s.configuration.OTLPConfig.Metrics.ResourceAttributes, ","), ) } - if s.configuration.OTLPConfig.Metrics.ServiceName != "" { - args = append(args, "--"+otlp.OtelServiceNameFlag, s.configuration.OTLPConfig.Metrics.ServiceName) - } + } + if s.configuration.OTLPConfig.BaseConfig.ServiceName != "" { + args = append(args, "--"+otlp.OtelServiceNameFlag, s.configuration.OTLPConfig.BaseConfig.ServiceName) } } diff --git a/test/stress/stress_test.go b/test/stress/stress_test.go index 1914353b8..886d7b21e 100644 --- a/test/stress/stress_test.go +++ b/test/stress/stress_test.go @@ -39,7 +39,7 @@ var _ = Context("Ledger stress tests", func() { countLedgers = 6 countBuckets = 3 countTransactions = 500 - countAccounts = 20 + countAccounts = 80 ) When(fmt.Sprintf("creating %d ledgers dispatched on %d buckets", countLedgers, countLedgers/10), func() {