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 2, 2024
1 parent 4737f6f commit 3bb4ceb
Show file tree
Hide file tree
Showing 4 changed files with 36 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
26 changes: 26 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,30 @@ 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()

Check warning on line 54 in handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers.go#L52-L54

Added lines #L52 - L54 were not covered by tests

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

Check warning on line 58 in handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers.go#L56-L58

Added lines #L56 - L58 were not covered by tests

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

Check failure on line 61 in handlers.go

View workflow job for this annotation

GitHub Actions / go-check / All

unnecessary use of fmt.Sprintf (S1039)
return

Check warning on line 62 in handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers.go#L60-L62

Added lines #L60 - L62 were not covered by tests
}

if err := log.SetLogLevel(subsystem, level); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return

Check warning on line 67 in handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers.go#L65-L67

Added lines #L65 - L67 were not covered by tests
}
})
mux.HandleFunc("/mgr/log/ls", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(strings.Join(log.GetSubsystems(), ",")))
return

Check failure on line 72 in handlers.go

View workflow job for this annotation

GitHub Actions / go-check / All

redundant return statement (S1023)
})

Check warning on line 73 in handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers.go#L70-L73

Added lines #L70 - L73 were not covered by tests
}

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)

Check warning on line 508 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L508

Added line #L508 was not covered by tests

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

0 comments on commit 3bb4ceb

Please sign in to comment.