Skip to content

Commit f319338

Browse files
Merge pull request #27 from kaleido-io/debug
add debug endpoint
2 parents 70b5dca + 5fdd2ec commit f319338

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

config.md

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ nav_order: 2
7373
|methods| CORS setting to control the allowed methods|`string`|`[GET POST PUT PATCH DELETE]`
7474
|origins|CORS setting to control the allowed origins|`string`|`[*]`
7575

76+
## debug
77+
78+
|Key|Description|Type|Default Value|
79+
|---|-----------|----|-------------|
80+
|port|An HTTP port on which to enable the go debugger|`int`|`-1`
81+
7682
## eventstreams
7783

7884
|Key|Description|Type|Default Value|

internal/tmconfig/tmconfig.go

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var (
5656
PersistenceLevelDBSyncWrites = ffc("persistence.leveldb.syncWrites")
5757
APIDefaultRequestTimeout = ffc("api.defaultRequestTimeout")
5858
APIMaxRequestTimeout = ffc("api.maxRequestTimeout")
59+
DebugPort = ffc("debug.port")
5960
)
6061

6162
var APIConfig config.Section
@@ -100,6 +101,7 @@ func setDefaults() {
100101
viper.SetDefault(string(EventStreamsRetryInitDelay), "250ms")
101102
viper.SetDefault(string(EventStreamsRetryMaxDelay), "30s")
102103
viper.SetDefault(string(EventStreamsRetryFactor), 2.0)
104+
viper.SetDefault(string(DebugPort), -1)
103105
}
104106

105107
func Reset() {

internal/tmmsgs/en_config_descriptions.go

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ var (
3636
ConfigAPIWriteTimeout = ffc("config.api.writeTimeout", "The maximum time to wait when writing to a HTTP connection", i18n.TimeDurationType)
3737
ConfigAPIShutdownTimeout = ffc("config.api.shutdownTimeout", "The maximum amount of time to wait for any open HTTP requests to finish before shutting down the HTTP server", i18n.TimeDurationType)
3838

39+
ConfigDebugPort = ffc("config.debug.port", "An HTTP port on which to enable the go debugger", i18n.IntType)
40+
3941
ConfigConfirmationsBlockCacheSize = ffc("config.confirmations.blockCacheSize", "The maximum number of block headers to keep in the cache", i18n.IntType)
4042
ConfigConfirmationsBlockQueueLength = ffc("config.confirmations.blockQueueLength", "Internal queue length for notifying the confirmations manager of new blocks", i18n.IntType)
4143
ConfigConfirmationsNotificationsQueueLength = ffc("config.confirmations.notificationQueueLength", "Internal queue length for notifying the confirmations manager of new transactions/events", i18n.IntType)

pkg/fftm/api.go

+28
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ package fftm
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"net/http"
23+
"net/http/pprof"
24+
"time"
2225

2326
"github.com/ghodss/yaml"
2427
"github.com/gorilla/mux"
2528
"github.com/hyperledger/firefly-common/pkg/config"
2629
"github.com/hyperledger/firefly-common/pkg/ffapi"
2730
"github.com/hyperledger/firefly-common/pkg/i18n"
31+
"github.com/hyperledger/firefly-common/pkg/log"
2832
"github.com/hyperledger/firefly-transaction-manager/internal/tmconfig"
2933
)
3034

@@ -77,3 +81,27 @@ func (m *manager) router() *mux.Router {
7781
func (m *manager) runAPIServer() {
7882
m.apiServer.ServeHTTP(m.ctx)
7983
}
84+
85+
func (m *manager) runDebugServer() {
86+
var debugServer *http.Server
87+
debugPort := config.GetInt(tmconfig.DebugPort)
88+
89+
defer func() {
90+
if debugServer != nil {
91+
_ = debugServer.Close()
92+
}
93+
close(m.debugServerDone)
94+
}()
95+
96+
if debugPort >= 0 {
97+
r := mux.NewRouter()
98+
r.PathPrefix("/debug/pprof/cmdline").HandlerFunc(pprof.Cmdline)
99+
r.PathPrefix("/debug/pprof/profile").HandlerFunc(pprof.Profile)
100+
r.PathPrefix("/debug/pprof/symbol").HandlerFunc(pprof.Symbol)
101+
r.PathPrefix("/debug/pprof/trace").HandlerFunc(pprof.Trace)
102+
r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
103+
debugServer = &http.Server{Addr: fmt.Sprintf("localhost:%d", debugPort), Handler: r, ReadHeaderTimeout: 30 * time.Second}
104+
log.L(m.ctx).Debugf("Debug HTTP endpoint listening on localhost:%d", debugPort)
105+
_ = debugServer.ListenAndServe()
106+
}
107+
}

pkg/fftm/manager.go

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type manager struct {
8787
blockListenerDone chan struct{}
8888
started bool
8989
apiServerDone chan error
90+
debugServerDone chan struct{}
9091

9192
policyLoopInterval time.Duration
9293
nonceStateTimeout time.Duration
@@ -182,6 +183,8 @@ func (m *manager) Start() error {
182183
return err
183184
}
184185

186+
m.debugServerDone = make(chan struct{})
187+
go m.runDebugServer()
185188
go m.runAPIServer()
186189
m.policyLoopDone = make(chan struct{})
187190
m.markInflightStale()
@@ -199,6 +202,7 @@ func (m *manager) Close() {
199202
<-m.apiServerDone
200203
<-m.policyLoopDone
201204
<-m.blockListenerDone
205+
<-m.debugServerDone
202206

203207
streams := []events.Stream{}
204208
m.mux.Lock()

0 commit comments

Comments
 (0)