Skip to content

Commit

Permalink
feat: add pprof at common location
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent f286b84 commit a9781da
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
34 changes: 15 additions & 19 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand All @@ -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),
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -34,7 +34,7 @@ func NewControllerWithTooManyClientHandling(
return &ControllerWithTooManyClientHandling{
Controller: underlying,
delayCalculator: delayCalculator,
tracer: tracer,
tracer: tracer,
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/controller/ledger/log_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{}):
Expand Down
7 changes: 4 additions & 3 deletions pkg/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type T interface {
}

type OTLPConfig struct {
BaseConfig otlp.Config
Metrics *otlpmetrics.ModuleConfig
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/stress/stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit a9781da

Please sign in to comment.