Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

refactor(pprof): extract infrastructure provider (#10673)

Pandey807211b8d81f
diff --git a/conf/example.yaml b/conf/example.yaml
index 4bce8dc3e6..6a9c0eab85 100644
--- a/conf/example.yaml
+++ b/conf/example.yaml
@@ -39,6 +39,13 @@ instrumentation:
host: "0.0.0.0"
port: 9090
+##################### PProf #####################
+pprof:
+ # Whether to enable the pprof server.
+ enabled: true
+ # The address on which the pprof server listens.
+ address: 0.0.0.0:6060
+
##################### Web #####################
web:
# Whether to enable the web frontend
diff --git a/ee/query-service/app/server.go b/ee/query-service/app/server.go
index 3b5f1ab37c..ddb8e16707 100644
--- a/ee/query-service/app/server.go
+++ b/ee/query-service/app/server.go
@@ -5,7 +5,6 @@ import (
"fmt"
"net"
"net/http"
- _ "net/http/pprof" // http profiler
"slices"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
@@ -313,15 +312,6 @@ func (s *Server) Start(ctx context.Context) error {
s.unavailableChannel <- healthcheck.Unavailable
}()
- go func() {
- slog.Info("Starting pprof server", "addr", baseconst.DebugHttpPort)
-
- err = http.ListenAndServe(baseconst.DebugHttpPort, nil)
- if err != nil {
- slog.Error("Could not start pprof server", errors.Attr(err))
- }
- }()
-
go func() {
slog.Info("Starting OpAmp Websocket server", "addr", baseconst.OpAmpWsEndpoint)
err := s.opampServer.Start(baseconst.OpAmpWsEndpoint)
diff --git a/pkg/pprof/config.go b/pkg/pprof/config.go
new file mode 100644
index 0000000000..6dcc508883
--- /dev/null
+++ b/pkg/pprof/config.go
@@ -0,0 +1,32 @@
+package pprof
+
+import "github.com/SigNoz/signoz/pkg/factory"
+
+// Config holds the configuration for the pprof server.
+type Config struct {
+ Enabled bool `mapstructure:"enabled"`
+ Address string `mapstructure:"address"`
+}
+
+func NewConfigFactory() factory.ConfigFactory {
+ return factory.NewConfigFactory(factory.MustNewName("pprof"), newConfig)
+}
+
+func newConfig() factory.Config {
+ return Config{
+ Enabled: true,
+ Address: "0.0.0.0:6060",
+ }
+}
+
+func (c Config) Validate() error {
+ return nil
+}
+
+func (c Config) Provider() string {
+ if c.Enabled {
+ return "http"
+ }
+
+ return "noop"
+}
diff --git a/pkg/pprof/config_test.go b/pkg/pprof/config_test.go
new file mode 100644
index 0000000000..3106556ede
--- /dev/null
+++ b/pkg/pprof/config_test.go
@@ -0,0 +1,42 @@
+package pprof
+
+import (
+ "context"
+ "testing"
+
+ "github.com/SigNoz/signoz/pkg/config"
+ "github.com/SigNoz/signoz/pkg/config/envprovider"
+ "github.com/SigNoz/signoz/pkg/factory"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestNewWithEnvProvider(t *testing.T) {
+ t.Setenv("SIGNOZ_PPROF_ENABLED", "false")
+ t.Setenv("SIGNOZ_PPROF_ADDRESS", "127.0.0.1:6061")
+
+ conf, err := config.New(
+ context.Background(),
+ config.ResolverConfig{
+ Uris: []string{"env:"},
+ ProviderFactories: []config.ProviderFactory{
+ envprovider.NewFactory(),
+ },
+ },
+ []factory.ConfigFactory{
+ NewConfigFactory(),
+ },
+ )
+ require.NoError(t, err)
+
+ actual := Config{}
+ err = conf.Unmarshal("pprof", &actual)
+ require.NoError(t, err)
+
+ expected := Config{
+ Enabled: false,
+ Address: "127.0.0.1:6061",
+ }
+
+ assert.Equal(t, expected, actual)
+}
diff --git a/pkg/pprof/httppprof/provider.go b/pkg/pprof/httppprof/provider.go
new file mode 100644
index 0000000000..fa96a75bb6
--- /dev/null
+++ b/pkg/pprof/httppprof/provider.go
@@ -0,0 +1,59 @@
+package httppprof
+
+import (
+ "context"
+ "log/slog"
+ "net/http"
+ nethttppprof "net/http/pprof"
+ runtimepprof "runtime/pprof"
+
+ "github.com/SigNoz/signoz/pkg/factory"
+ httpserver "github.com/SigNoz/signoz/pkg/http/server"
+ "github.com/SigNoz/signoz/pkg/pprof"
+)
+
+type provider struct {
+ server *httpserver.Server
+}
+
+func NewFactory() factory.ProviderFactory[pprof.PProf, pprof.Config] {
+ return factory.NewProviderFactory(factory.MustNewName("http"), New)
+}
+
+func New(_ context.Context, settings factory.ProviderSettings, config pprof.Config) (pprof.PProf, error) {
+ server, err := httpserver.New(
+ settings.Logger.With(slog.String("pkg", "github.com/SigNoz/signoz/pkg/pprof/httppprof")),
+ httpserver.Config{Address: config.Address},
+ newHandler(),
+ )
+ if err != nil {
+ return nil, err
+ }
+
+ return &provider{server: server}, nil
+}
+
+func (provider *provider) Start(ctx context.Context) error {
+ return provider.server.Start(ctx)
+}
+
+func (provider *provider) Stop(ctx context.Context) error {
+ return provider.server.Stop(ctx)
+}
+
+func newHandler() http.Handler {
+ mux := http.NewServeMux()
+ // Register the endpoints from net/http/pprof.
+ mux.HandleFunc("/debug/pprof/", nethttppprof.Index)
+ mux.HandleFunc("/debug/pprof/cmdline", nethttppprof.Cmdline)
+ mux.HandleFunc("/debug/pprof/profile", nethttppprof.Profile)
+ mux.HandleFunc("/debug/pprof/symbol", nethttppprof.Symbol)
+ mux.HandleFunc("/debug/pprof/trace", nethttppprof.Trace)
+
+ // Register the runtime profiles in the same order returned by runtime/pprof.Profiles().
+ for _, profile := range runtimepprof.Profiles() {
+ mux.Handle("/debug/pprof/"+profile.Name(), nethttppprof.Handler(profile.Name()))
+ }
+
+ return mux
+}
diff --git a/pkg/pprof/nooppprof/provider.go b/pkg/pprof/nooppprof/provider.go
new file mode 100644
index 0000000000..a89e7407df
--- /dev/null
+++ b/pkg/pprof/nooppprof/provider.go
@@ -0,0 +1,35 @@
+package nooppprof
+
+import (
+ "context"
+ "sync"
+
+ "github.com/SigNoz/signoz/pkg/factory"
+ "github.com/SigNoz/signoz/pkg/pprof"
+)
+
+type provider struct {
+ stopC chan struct{}
+ stopOnce sync.Once
+}
+
+func NewFactory() factory.ProviderFactory[pprof.PProf, pprof.Config] {
+ return factory.NewProviderFactory(factory.MustNewName("noop"), New)
+}
+
+func New(_ context.Context, _ factory.ProviderSettings, _ pprof.Config) (pprof.PProf, error) {
+ return &provider{stopC: make(chan struct{})}, nil
+}
+
+func (provider *provider) Start(context.Context) error {
+ <-provider.stopC
+ return nil
+}
+
+func (provider *provider) Stop(context.Context) error {
+ provider.stopOnce.Do(func() {
+ close(provider.stopC)
+ })
+
+ return nil
+}
diff --git a/pkg/pprof/pprof.go b/pkg/pprof/pprof.go
new file mode 100644
index 0000000000..af1a8d7875
--- /dev/null
+++ b/pkg/pprof/pprof.go
@@ -0,0 +1,8 @@
+package pprof
+
+import "github.com/SigNoz/signoz/pkg/factory"
+
+// PProf is the interface that wraps the pprof service lifecycle.
+type PProf interface {
+ factory.Service
+}
diff --git a/pkg/query-service/app/server.go b/pkg/query-service/app/server.go
index eeb9c0212c..854be97e92 100644
--- a/pkg/query-service/app/server.go
+++ b/pkg/query-service/app/server.go
@@ -5,7 +5,6 @@ import (
"fmt"
"net"
"net/http"
- _ "net/http/pprof" // http profiler
"slices"
"github.com/SigNoz/signoz/pkg/cache/memorycache"
@@ -294,15 +293,6 @@ func (s *Server) Start(ctx context.Context) error {
s.unavailableChannel <- healthcheck.Unavailable
}()
- go func() {
- slog.Info("Starting pprof server", "addr", constants.DebugHttpPort)
-
- err = http.ListenAndServe(constants.DebugHttpPort, nil)
- if err != nil {
- slog.Error("Could not start pprof server", errors.Attr(err))
- }
- }()
-
go func() {
slog.Info("Starting OpAmp Websocket server", "addr", constants.OpAmpWsEndpoint)
err := s.opampServer.Start(constants.OpAmpWsEndpoint)
diff --git a/pkg/query-service/constants/constants.go b/pkg/query-service/constants/constants.go
index e19e70f1a1..bf2345f808 100644
--- a/pkg/query-service/constants/constants.go
+++ b/pkg/query-service/constants/constants.go
@@ -14,7 +14,6 @@ import (
const (
HTTPHostPort = "0.0.0.0:8080" // Address to serve http (query service)
PrivateHostPort = "0.0.0.0:8085" // Address to server internal services like alert manager
- DebugHttpPort = "0.0.0.0:6060" // Address to serve http (pprof)
OpAmpWsEndpoint = "0.0.0.0:4320" // address for opamp websocket
)
diff --git a/pkg/signoz/config.go b/pkg/signoz/config.go
index c175bb0499..ee23d0d919 100644
--- a/pkg/signoz/config.go
+++ b/pkg/signoz/config.go
@@ -23,6 +23,7 @@ import (
"github.com/SigNoz/signoz/pkg/instrumentation"
"github.com/SigNoz/signoz/pkg/modules/metricsexplorer"
"github.com/SigNoz/signoz/pkg/modules/user"
+ "github.com/SigNoz/signoz/pkg/pprof"
"github.com/SigNoz/signoz/pkg/prometheus"
"github.com/SigNoz/signoz/pkg/querier"
"github.com/SigNoz/signoz/pkg/ruler"
@@ -50,6 +51,9 @@ type Config struct {
// Instrumentation config
Instrumentation instrumentation.Config `mapstructure:"instrumentation"`
+ // PProf config
+ PProf pprof.Config `mapstructure:"pprof"`
+
// Analytics config
Analytics analytics.Config `mapstructure:"analytics"`
@@ -122,6 +126,7 @@ func NewConfig(ctx context.Context, logger *slog.Logger, resolverConfig config.R
global.NewConfigFactory(),
version.NewConfigFactory(),
instrumentation.NewConfigFactory(),
+ pprof.NewConfigFactory(),
analytics.NewConfigFactory(),
web.NewConfigFactory(),
cache.NewConfigFactory(),
diff --git a/pkg/signoz/provider.go b/pkg/signoz/provider.go
index 0f9066fdf0..542acbc856 100644
--- a/pkg/signoz/provider.go
+++ b/pkg/signoz/provider.go
@@ -34,6 +34,9 @@ import (
"github.com/SigNoz/signoz/pkg/modules/session/implsession"
"github.com/SigNoz/signoz/pkg/modules/user"
"github.com/SigNoz/signoz/pkg/modules/user/impluser"
+ "github.com/SigNoz/signoz/pkg/pprof"
+ "github.com/SigNoz/signoz/pkg/pprof/httppprof"
+ "github.com/SigNoz/signoz/pkg/pprof/nooppprof"
"github.com/SigNoz/signoz/pkg/prometheus"
"github.com/SigNoz/signoz/pkg/prometheus/clickhouseprometheus"
"github.com/SigNoz/signoz/pkg/querier"
@@ -89,6 +92,13 @@ func NewWebProviderFactories() factory.NamedMap[factory.ProviderFactory[web.Web,
)
}
+func NewPProfProviderFactories() factory.NamedMap[factory.ProviderFactory[pprof.PProf, pprof.Config]] {
+ return factory.MustNewNamedMap(
+ httppprof.NewFactory(),
+ nooppprof.NewFactory(),
+ )
+}
+
func NewSQLStoreProviderFactories() factory.NamedMap[factory.ProviderFactory[sqlstore.SQLStore, sqlstore.Config]] {
return factory.MustNewNamedMap(
sqlitesqlstore.NewFactory(sqlstorehook.NewLoggingFactory(), sqlstorehook.NewInstrumentationFactory()),
diff --git a/pkg/signoz/signoz.go b/pkg/signoz/signoz.go
index cf51b108e9..0b18814d50 100644
--- a/pkg/signoz/signoz.go
+++ b/pkg/signoz/signoz.go
@@ -107,6 +107,17 @@ func New(
// Get the provider settings from instrumentation
providerSettings := instrumentation.ToProviderSettings()
+ pprofService, err := factory.NewProviderFromNamedMap(
+ ctx,
+ providerSettings,
+ config.PProf,
+ NewPProfProviderFactories(),
+ config.PProf.Provider(),
+ )
+ if err != nil {
+ return nil, err
+ }
+
// Initialize analytics just after instrumentation, as providers might require it
analytics, err := factory.NewProviderFromNamedMap(
ctx,
@@ -448,6 +459,7 @@ func New(
registry, err := factory.NewRegistry(
instrumentation.Logger(),
factory.NewNamedService(factory.MustNewName("instrumentation"), instrumentation),
+ factory.NewNamedService(factory.MustNewName("pprof"), pprofService),
factory.NewNamedService(factory.MustNewName("analytics"), analytics),
factory.NewNamedService(factory.MustNewName("alertmanager"), alertmanager),
factory.NewNamedService(factory.MustNewName("licensing"), licensing),