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

add debug API endpoint and signal listener #138

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion docs/content/api/blip.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ Returns the Blip version (same as [`--version`]({{< ref "/config/blip#--version"
"v1.0.75"
```

Response encoding is text (string).
## GET /debug

Toggles debug logging.

*Response*

```json
{"debugging":true}
```


2 changes: 2 additions & 0 deletions docs/content/config/blip.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ In this case, Blip tries to auto-detect a local MySQL instance, which is useful

Print debug to STDERR.

You can also toggle debug logging by sending a request to the `/debug` [API endpoint]({{< ref "/api" >}}) or by sending the `SIGUSR1` signal to the Blip process.

### `--help`

Print help and exit.
Expand Down
1 change: 1 addition & 0 deletions docs/content/config/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Start `blip` with the [`--log`]({{< ref "blip#--log" >}}) option to print info e

<p class="note">
<a href="blip#--debug">Debug</a> info is printed to <code>STDERR</code>.
You can also toggle debug logging by sending a request to the `/debug` [API endpoint]({{< ref "/api" >}}) or by sending the `SIGUSR1` signal to the Blip process.
</p>

This is _pseudo-logging_ because there is no traditional log printing, only events that are printed by default.
Expand Down
10 changes: 9 additions & 1 deletion server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func NewAPI(cfg blip.Config, ml *monitor.Loader) *API {
mux.HandleFunc("/status", api.status)
mux.HandleFunc("/status/monitors", api.statusMonitors)

mux.HandleFunc("/debug", api.debug)

api.httpServer = &http.Server{
Addr: cfg.API.Bind,
Handler: mux,
Expand Down Expand Up @@ -123,7 +125,13 @@ func (api *API) registered(w http.ResponseWriter, r *http.Request) {

func (api *API) version(w http.ResponseWriter, r *http.Request) {
blip.Debug("%v", r)
w.Write([]byte(blip.VERSION))
json.NewEncoder(w).Encode(blip.VERSION)
}

func (api *API) debug(w http.ResponseWriter, r *http.Request) {
blip.Debug("%v", r)
blip.Debugging = !blip.Debugging
json.NewEncoder(w).Encode(map[string]bool{"debugging": blip.Debugging})
}

// --------------------------------------------------------------------------
Expand Down
26 changes: 18 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,23 @@ func (s *Server) Run(stopChan, doneChan chan struct{}) error {

// Run until caller closes stopChan or blip process catches a signal
status.Blip(status.SERVER, "running since %s", blip.FormatTime(time.Now()))
signalChan := make(chan os.Signal)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
select {
case <-stopChan:
stopReason = "server stopped"
case <-signalChan:
stopReason = "caught signal"
signalChan := make(chan os.Signal, 1)
defer close(signalChan)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
for {
select {
case <-stopChan:
stopReason = "server stopped"
return nil
case s := <-signalChan:
switch s {
case os.Interrupt, syscall.SIGTERM:
stopReason = "caught signal"
return nil
case syscall.SIGUSR1:
blip.Debugging = !blip.Debugging
fmt.Fprintf(os.Stderr, "SIGUSR1 has set blip.Debugging to %t\n", blip.Debugging)
}
}
}
return nil
}
Loading