Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logging http api #156

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
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, "both subsystem and level must be passed", http.StatusBadRequest)
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(), ",")))
})

Check warning on line 72 in handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers.go#L70-L72

Added lines #L70 - L72 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 @@

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
Loading