Skip to content

Commit 2b45b4e

Browse files
committed
feat: add logging http api
1 parent 4737f6f commit 2b45b4e

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The following emojis are used to highlight certain changes:
1616
### Added
1717

1818
- Simple end-to-end test to check that trustless-gateway-domains are set correctly.
19+
- HTTP API to dynamically list logging subsystems and modify logging levels for subsystems.
1920

2021
### Changed
2122

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ Example cURL commmand to run GC:
134134

135135
curl -v --data '{"BytesToFree": 1099511627776}' http://127.0.0.1:8091/mgr/gc
136136

137+
## Logging
138+
139+
While the logging can be controlled via [environment variable](./docs/environment-variables.md#logging) it is also
140+
possible to dynamically modify the logging at runtime.
141+
142+
- `http://$RAINBOW_CTL_LISTEN_ADDRESS/mgr/log/level?subsystem=<system name or * for all system>&level=<level>` will set the logging level for a subsystem
143+
- `http://$RAINBOW_CTL_LISTEN_ADDRESS/mgr/log/ls` will return a comma separated list of available logging subsystems
144+
137145
## Deployment
138146

139147
Suggested method for self-hosting is to run a [prebuilt Docker image](#docker).

handlers.go

+25
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"os"
99
"runtime"
1010
"strconv"
11+
"strings"
1112

1213
"github.com/ipfs/boxo/blockstore"
1314
leveldb "github.com/ipfs/go-ds-leveldb"
15+
"github.com/ipfs/go-log/v2"
1416

1517
_ "embed"
1618
_ "net/http/pprof"
@@ -47,6 +49,29 @@ func makeMetricsAndDebuggingHandler() *http.ServeMux {
4749
return mux
4850
}
4951

52+
func addLogHandlers(mux *http.ServeMux) {
53+
mux.HandleFunc("/mgr/log/level", func(w http.ResponseWriter, r *http.Request) {
54+
defer r.Body.Close()
55+
56+
q := r.URL.Query()
57+
subsystem := q.Get("subsystem")
58+
level := q.Get("level")
59+
60+
if subsystem == "" || level == "" {
61+
http.Error(w, "both subsystem and level must be passed", http.StatusBadRequest)
62+
return
63+
}
64+
65+
if err := log.SetLogLevel(subsystem, level); err != nil {
66+
http.Error(w, err.Error(), http.StatusInternalServerError)
67+
return
68+
}
69+
})
70+
mux.HandleFunc("/mgr/log/ls", func(w http.ResponseWriter, r *http.Request) {
71+
_, _ = w.Write([]byte(strings.Join(log.GetSubsystems(), ",")))
72+
})
73+
}
74+
5075
func GCHandler(gnd *Node) func(w http.ResponseWriter, r *http.Request) {
5176
return func(w http.ResponseWriter, r *http.Request) {
5277
defer r.Body.Close()

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ share the same seed as long as the indexes are different.
505505

506506
apiMux := makeMetricsAndDebuggingHandler()
507507
apiMux.HandleFunc("/mgr/gc", GCHandler(gnd))
508+
addLogHandlers(apiMux)
508509

509510
apiSrv := &http.Server{
510511
Addr: ctlListen,

0 commit comments

Comments
 (0)