2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-05-21 23:07:50 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-03-24 20:58:03 +03:00
|
|
|
"net/http"
|
2020-05-21 23:07:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Monitor returns a channel that outputs strings containing the log messages
|
|
|
|
// coming from the server.
|
2022-05-24 20:10:53 +03:00
|
|
|
func (c *Sys) Monitor(ctx context.Context, logLevel string, logFormat string) (chan string, error) {
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodGet, "/v1/sys/monitor")
|
2020-05-21 23:07:50 +03:00
|
|
|
|
|
|
|
if logLevel == "" {
|
|
|
|
r.Params.Add("log_level", "info")
|
|
|
|
} else {
|
|
|
|
r.Params.Add("log_level", logLevel)
|
|
|
|
}
|
|
|
|
|
2023-02-06 17:41:56 +03:00
|
|
|
if logFormat == "" {
|
2022-05-24 20:10:53 +03:00
|
|
|
r.Params.Add("log_format", "standard")
|
|
|
|
} else {
|
|
|
|
r.Params.Add("log_format", logFormat)
|
|
|
|
}
|
|
|
|
|
2020-05-21 23:07:50 +03:00
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logCh := make(chan string, 64)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
droppedCount := 0
|
|
|
|
|
|
|
|
defer close(logCh)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !scanner.Scan() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logMessage := scanner.Text()
|
|
|
|
|
|
|
|
if droppedCount > 0 {
|
|
|
|
select {
|
|
|
|
case logCh <- fmt.Sprintf("Monitor dropped %d logs during monitor request\n", droppedCount):
|
|
|
|
droppedCount = 0
|
|
|
|
default:
|
|
|
|
droppedCount++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case logCh <- logMessage:
|
|
|
|
default:
|
|
|
|
droppedCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return logCh, nil
|
|
|
|
}
|