2019-10-15 19:14:25 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
2019-10-30 13:55:41 +00:00
|
|
|
"fmt"
|
2019-10-15 19:14:25 +00:00
|
|
|
"sync"
|
2019-10-30 13:55:41 +00:00
|
|
|
"time"
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
)
|
|
|
|
|
2019-10-30 13:55:41 +00:00
|
|
|
// Monitor provides a mechanism to stream logs using go-hclog
|
|
|
|
// InterceptLogger and SinkAdapter. It allows streaming of logs
|
|
|
|
// at a different log level than what is set on the logger.
|
2019-11-05 14:16:51 +00:00
|
|
|
type Monitor interface {
|
|
|
|
// Start returns a channel of log messages which are sent
|
|
|
|
// ever time a log message occurs
|
|
|
|
Start() <-chan []byte
|
|
|
|
|
|
|
|
// Stop de-registers the sink from the InterceptLogger
|
|
|
|
// and closes the log channels
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// monitor implements the Monitor interface
|
|
|
|
type monitor struct {
|
|
|
|
// protects droppedCount and logCh
|
2019-10-15 19:14:25 +00:00
|
|
|
sync.Mutex
|
2019-11-04 19:32:53 +00:00
|
|
|
|
|
|
|
sink log.SinkAdapter
|
|
|
|
|
|
|
|
// logger is the logger we will be monitoring
|
|
|
|
logger log.InterceptLogger
|
2019-11-05 14:16:51 +00:00
|
|
|
|
|
|
|
// logCh is a buffered chan where we send logs when streaming
|
|
|
|
logCh chan []byte
|
|
|
|
|
|
|
|
// doneCh coordinates the shutdown of logCh
|
|
|
|
doneCh chan struct{}
|
|
|
|
|
2019-11-04 19:32:53 +00:00
|
|
|
// droppedCount is the current count of messages
|
|
|
|
// that were dropped from the logCh buffer.
|
|
|
|
// only access under lock
|
|
|
|
droppedCount int
|
|
|
|
bufSize int
|
|
|
|
// droppedDuration is the amount of time we should
|
|
|
|
// wait to check for dropped messages. Defaults
|
|
|
|
// to 3 seconds
|
2019-10-30 13:55:41 +00:00
|
|
|
droppedDuration time.Duration
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:55:41 +00:00
|
|
|
// New creates a new Monitor. Start must be called in order to actually start
|
|
|
|
// streaming logs
|
2019-11-05 14:16:51 +00:00
|
|
|
func New(buf int, logger log.InterceptLogger, opts *log.LoggerOptions) Monitor {
|
|
|
|
return new(buf, logger, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func new(buf int, logger log.InterceptLogger, opts *log.LoggerOptions) *monitor {
|
|
|
|
sw := &monitor{
|
2019-10-30 13:55:41 +00:00
|
|
|
logger: logger,
|
|
|
|
logCh: make(chan []byte, buf),
|
2019-11-05 14:16:51 +00:00
|
|
|
doneCh: make(chan struct{}, 1),
|
2019-10-30 13:55:41 +00:00
|
|
|
bufSize: buf,
|
|
|
|
droppedDuration: 3 * time.Second,
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts.Output = sw
|
|
|
|
sink := log.NewSinkAdapter(opts)
|
|
|
|
sw.sink = sink
|
|
|
|
|
|
|
|
return sw
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:16:51 +00:00
|
|
|
// Stop stops the monitoring process
|
|
|
|
func (d *monitor) Stop() {
|
|
|
|
close(d.doneCh)
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:32:53 +00:00
|
|
|
// Start registers a sink on the monitor's logger and starts sending
|
2019-11-05 14:16:51 +00:00
|
|
|
// received log messages over the returned channel.
|
|
|
|
func (d *monitor) Start() <-chan []byte {
|
2019-10-15 19:14:25 +00:00
|
|
|
d.logger.RegisterSink(d.sink)
|
|
|
|
|
2019-10-30 13:55:41 +00:00
|
|
|
streamCh := make(chan []byte, d.bufSize)
|
2019-10-15 19:14:25 +00:00
|
|
|
go func() {
|
2019-10-30 13:55:41 +00:00
|
|
|
defer close(streamCh)
|
2019-10-15 19:14:25 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case log := <-d.logCh:
|
2019-10-30 13:55:41 +00:00
|
|
|
select {
|
2019-11-05 14:16:51 +00:00
|
|
|
case <-d.doneCh:
|
2019-10-30 13:55:41 +00:00
|
|
|
d.logger.DeregisterSink(d.sink)
|
|
|
|
close(d.logCh)
|
|
|
|
return
|
|
|
|
case streamCh <- log:
|
|
|
|
}
|
2019-11-05 14:16:51 +00:00
|
|
|
case <-d.doneCh:
|
2019-10-24 16:47:46 +00:00
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
d.logger.DeregisterSink(d.sink)
|
|
|
|
close(d.logCh)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-10-30 13:55:41 +00:00
|
|
|
go func() {
|
|
|
|
// loop and check for dropped messages
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
2019-11-05 14:16:51 +00:00
|
|
|
case <-d.doneCh:
|
2019-10-30 13:55:41 +00:00
|
|
|
break LOOP
|
|
|
|
case <-time.After(d.droppedDuration):
|
|
|
|
d.Lock()
|
|
|
|
|
2019-11-04 19:32:53 +00:00
|
|
|
// Check if there have been any dropped messages.
|
2019-10-30 13:55:41 +00:00
|
|
|
if d.droppedCount > 0 {
|
|
|
|
dropped := fmt.Sprintf("[WARN] Monitor dropped %d logs during monitor request\n", d.droppedCount)
|
|
|
|
select {
|
2019-11-05 14:16:51 +00:00
|
|
|
case <-d.doneCh:
|
|
|
|
break LOOP
|
2019-11-04 19:32:53 +00:00
|
|
|
// Try sending dropped message count to logCh in case
|
|
|
|
// there is room in the buffer now.
|
2019-10-30 13:55:41 +00:00
|
|
|
case d.logCh <- []byte(dropped):
|
|
|
|
default:
|
2019-11-04 19:32:53 +00:00
|
|
|
// Drop a log message to make room for "Monitor dropped.." message
|
2019-10-30 13:55:41 +00:00
|
|
|
select {
|
|
|
|
case <-d.logCh:
|
|
|
|
d.droppedCount++
|
|
|
|
dropped = fmt.Sprintf("[WARN] Monitor dropped %d logs during monitor request\n", d.droppedCount)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
d.logCh <- []byte(dropped)
|
|
|
|
}
|
|
|
|
d.droppedCount = 0
|
|
|
|
}
|
2019-11-05 14:16:51 +00:00
|
|
|
// unlock after handling dropped message
|
2019-11-04 19:32:53 +00:00
|
|
|
d.Unlock()
|
2019-10-30 13:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return streamCh
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 18:25:19 +00:00
|
|
|
// Write attempts to send latest log to logCh
|
2019-10-15 19:14:25 +00:00
|
|
|
// it drops the log if channel is unavailable to receive
|
2019-11-05 14:16:51 +00:00
|
|
|
func (d *monitor) Write(p []byte) (n int, err error) {
|
2019-11-04 19:32:53 +00:00
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
2019-11-05 14:16:51 +00:00
|
|
|
// ensure logCh is still open
|
|
|
|
select {
|
|
|
|
case <-d.doneCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
bytes := make([]byte, len(p))
|
|
|
|
copy(bytes, p)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case d.logCh <- bytes:
|
|
|
|
default:
|
|
|
|
d.droppedCount++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|