Skip to content

Commit d6840c6

Browse files
authored
Merge pull request #138 from kolbe/debug-switches
add debug API endpoint and signal listener
2 parents d490ea0 + 9df0291 commit d6840c6

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

docs/content/api/blip.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@ Returns the Blip version (same as [`--version`]({{< ref "/config/blip#--version"
6060
"v1.0.75"
6161
```
6262

63-
Response encoding is text (string).
63+
## GET /debug
64+
65+
Toggles debug logging.
66+
67+
*Response*
68+
69+
```json
70+
{"debugging":true}
71+
```
72+
73+

docs/content/config/blip.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ In this case, Blip tries to auto-detect a local MySQL instance, which is useful
5555

5656
Print debug to STDERR.
5757

58+
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.
59+
5860
### `--help`
5961

6062
Print help and exit.

docs/content/config/logging.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Start `blip` with the [`--log`]({{< ref "blip#--log" >}}) option to print info e
77

88
<p class="note">
99
<a href="blip#--debug">Debug</a> info is printed to <code>STDERR</code>.
10+
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.
1011
</p>
1112

1213
This is _pseudo-logging_ because there is no traditional log printing, only events that are printed by default.

server/api.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func NewAPI(cfg blip.Config, ml *monitor.Loader) *API {
5050
mux.HandleFunc("/status", api.status)
5151
mux.HandleFunc("/status/monitors", api.statusMonitors)
5252

53+
mux.HandleFunc("/debug", api.debug)
54+
5355
api.httpServer = &http.Server{
5456
Addr: cfg.API.Bind,
5557
Handler: mux,
@@ -123,7 +125,13 @@ func (api *API) registered(w http.ResponseWriter, r *http.Request) {
123125

124126
func (api *API) version(w http.ResponseWriter, r *http.Request) {
125127
blip.Debug("%v", r)
126-
w.Write([]byte(blip.VERSION))
128+
json.NewEncoder(w).Encode(blip.VERSION)
129+
}
130+
131+
func (api *API) debug(w http.ResponseWriter, r *http.Request) {
132+
blip.Debug("%v", r)
133+
blip.Debugging = !blip.Debugging
134+
json.NewEncoder(w).Encode(map[string]bool{"debugging": blip.Debugging})
127135
}
128136

129137
// --------------------------------------------------------------------------

server/server.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,23 @@ func (s *Server) Run(stopChan, doneChan chan struct{}) error {
261261

262262
// Run until caller closes stopChan or blip process catches a signal
263263
status.Blip(status.SERVER, "running since %s", blip.FormatTime(time.Now()))
264-
signalChan := make(chan os.Signal)
265-
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
266-
select {
267-
case <-stopChan:
268-
stopReason = "server stopped"
269-
case <-signalChan:
270-
stopReason = "caught signal"
264+
signalChan := make(chan os.Signal, 1)
265+
defer close(signalChan)
266+
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
267+
for {
268+
select {
269+
case <-stopChan:
270+
stopReason = "server stopped"
271+
return nil
272+
case s := <-signalChan:
273+
switch s {
274+
case os.Interrupt, syscall.SIGTERM:
275+
stopReason = "caught signal"
276+
return nil
277+
case syscall.SIGUSR1:
278+
blip.Debugging = !blip.Debugging
279+
fmt.Fprintf(os.Stderr, "SIGUSR1 has set blip.Debugging to %t\n", blip.Debugging)
280+
}
281+
}
271282
}
272-
return nil
273283
}

0 commit comments

Comments
 (0)