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

improve monitor performance #10368

Merged
merged 12 commits into from
Jun 15, 2021
Merged
3 changes: 3 additions & 0 deletions .changelog/10368.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
monitoring: optimize the monitoring endpoint to avoid losing logs when under high load.
```
8 changes: 8 additions & 0 deletions agent/agent_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-memdb"
Expand Down Expand Up @@ -1202,6 +1203,9 @@ func (s *HTTPHandlers) AgentMonitor(resp http.ResponseWriter, req *http.Request)
// a gzip stream it will go ahead and write out the HTTP response header
resp.Write([]byte(""))
flusher.Flush()
const flushDelay = 200 * time.Millisecond
flushTicker := time.NewTicker(flushDelay)
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved
defer flushTicker.Stop()

// Stream logs until the connection is closed.
for {
Expand All @@ -1211,9 +1215,13 @@ func (s *HTTPHandlers) AgentMonitor(resp http.ResponseWriter, req *http.Request)
if droppedCount > 0 {
s.agent.logger.Warn("Dropped logs during monitor request", "dropped_count", droppedCount)
}
flusher.Flush()
return nil, nil

case log := <-logsCh:
fmt.Fprint(resp, string(log))

case <-flushTicker.C:
flusher.Flush()
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
4 changes: 1 addition & 3 deletions logging/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func (d *monitor) Stop() int {
// received log messages over the returned channel.
func (d *monitor) Start() <-chan []byte {
// register our sink with the logger
d.logger.RegisterSink(d.sink)
streamCh := make(chan []byte, d.bufSize)

// run a go routine that listens for streamed
// log messages and sends them to streamCh
go func() {
Expand All @@ -101,7 +99,7 @@ func (d *monitor) Start() <-chan []byte {
}
}
}()

d.logger.RegisterSink(d.sink)
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved
return streamCh
}

Expand Down