Skip to content

Commit

Permalink
Merge pull request #138 from kolbe/debug-switches
Browse files Browse the repository at this point in the history
add debug API endpoint and signal listener
  • Loading branch information
kolbe authored Dec 5, 2024
2 parents d490ea0 + 9df0291 commit d6840c6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
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
}

0 comments on commit d6840c6

Please sign in to comment.