Skip to content

Commit

Permalink
feat: add logging http api
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Aug 5, 2024
1 parent 4737f6f commit 2b45b4e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The following emojis are used to highlight certain changes:
### Added

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

### Changed

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ Example cURL commmand to run GC:

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

## Logging

While the logging can be controlled via [environment variable](./docs/environment-variables.md#logging) it is also
possible to dynamically modify the logging at runtime.

- `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
- `http://$RAINBOW_CTL_LISTEN_ADDRESS/mgr/log/ls` will return a comma separated list of available logging subsystems

## Deployment

Suggested method for self-hosting is to run a [prebuilt Docker image](#docker).
Expand Down
25 changes: 25 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"os"
"runtime"
"strconv"
"strings"

"github.com/ipfs/boxo/blockstore"
leveldb "github.com/ipfs/go-ds-leveldb"
"github.com/ipfs/go-log/v2"

_ "embed"
_ "net/http/pprof"
Expand Down Expand Up @@ -47,6 +49,29 @@ func makeMetricsAndDebuggingHandler() *http.ServeMux {
return mux
}

func addLogHandlers(mux *http.ServeMux) {
mux.HandleFunc("/mgr/log/level", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

q := r.URL.Query()
subsystem := q.Get("subsystem")
level := q.Get("level")

if subsystem == "" || level == "" {
http.Error(w, "both subsystem and level must be passed", http.StatusBadRequest)
return
}

if err := log.SetLogLevel(subsystem, level); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
mux.HandleFunc("/mgr/log/ls", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(strings.Join(log.GetSubsystems(), ",")))
})
}

func GCHandler(gnd *Node) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ share the same seed as long as the indexes are different.

apiMux := makeMetricsAndDebuggingHandler()
apiMux.HandleFunc("/mgr/gc", GCHandler(gnd))
addLogHandlers(apiMux)

apiSrv := &http.Server{
Addr: ctlListen,
Expand Down

0 comments on commit 2b45b4e

Please sign in to comment.