mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add StreamLogs() to collector interface
This commit is contained in:
parent
2d284d9277
commit
240345d527
@ -1,6 +1,9 @@
|
|||||||
package collector
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/bcicen/ctop/models"
|
"github.com/bcicen/ctop/models"
|
||||||
api "github.com/fsouza/go-dockerclient"
|
api "github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
@ -65,6 +68,31 @@ func (c *Docker) Stream() chan models.Metrics {
|
|||||||
return c.stream
|
return c.stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Docker) StreamLogs() (chan string, error) {
|
||||||
|
r, w := io.Pipe()
|
||||||
|
logCh := make(chan string)
|
||||||
|
|
||||||
|
opts := api.LogsOptions{
|
||||||
|
Container: c.id,
|
||||||
|
OutputStream: w,
|
||||||
|
ErrorStream: w,
|
||||||
|
Stdout: true,
|
||||||
|
Stderr: true,
|
||||||
|
Tail: "10",
|
||||||
|
Follow: true,
|
||||||
|
Timestamps: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
go tailLogs(r, logCh)
|
||||||
|
go func() {
|
||||||
|
err := c.client.Logs(opts)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("error reading container logs: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return logCh, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Stop collector
|
// Stop collector
|
||||||
func (c *Docker) Stop() {
|
func (c *Docker) Stop() {
|
||||||
c.done <- true
|
c.done <- true
|
||||||
@ -111,3 +139,10 @@ func (c *Docker) ReadIO(stats *api.Stats) {
|
|||||||
}
|
}
|
||||||
c.IOBytesRead, c.IOBytesWrite = read, write
|
c.IOBytesRead, c.IOBytesWrite = read, write
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tailLogs(reader io.Reader, ch chan string) {
|
||||||
|
scanner := bufio.NewScanner(reader)
|
||||||
|
for scanner.Scan() {
|
||||||
|
ch <- scanner.Text()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,10 +4,19 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/bcicen/ctop/logging"
|
"github.com/bcicen/ctop/logging"
|
||||||
|
"github.com/bcicen/ctop/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = logging.Init()
|
var log = logging.Init()
|
||||||
|
|
||||||
|
type Collector interface {
|
||||||
|
Stream() chan models.Metrics
|
||||||
|
StreamLogs() (chan string, error)
|
||||||
|
Running() bool
|
||||||
|
Start()
|
||||||
|
Stop()
|
||||||
|
}
|
||||||
|
|
||||||
func round(num float64) int {
|
func round(num float64) int {
|
||||||
return int(num + math.Copysign(0.5, num))
|
return int(num + math.Copysign(0.5, num))
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,8 @@ import (
|
|||||||
"github.com/bcicen/ctop/models"
|
"github.com/bcicen/ctop/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const mockLog = "Cura ob pro qui tibi inveni dum qua fit donec amare illic mea, regem falli contexo pro peregrinorum heremo absconditi araneae meminerim deliciosas actionibus facere modico dura sonuerunt psalmi contra rerum, tempus mala anima volebant dura quae o modis."
|
||||||
|
|
||||||
// Mock collector
|
// Mock collector
|
||||||
type Mock struct {
|
type Mock struct {
|
||||||
models.Metrics
|
models.Metrics
|
||||||
@ -45,6 +47,17 @@ func (c *Mock) Stream() chan models.Metrics {
|
|||||||
return c.stream
|
return c.stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Mock) StreamLogs() (chan string, error) {
|
||||||
|
logCh := make(chan string)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
logCh <- mockLog
|
||||||
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return logCh, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Mock) run() {
|
func (c *Mock) run() {
|
||||||
c.running = true
|
c.running = true
|
||||||
rand.Seed(int64(time.Now().Nanosecond()))
|
rand.Seed(int64(time.Now().Nanosecond()))
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
package collector
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bcicen/ctop/models"
|
"github.com/bcicen/ctop/models"
|
||||||
@ -51,6 +52,10 @@ func (c *Runc) Stream() chan models.Metrics {
|
|||||||
return c.stream
|
return c.stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Runc) StreamLogs() (chan string, error) {
|
||||||
|
return nil, fmt.Errorf("log streaming unavailable for runc collector")
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Runc) run() {
|
func (c *Runc) run() {
|
||||||
c.running = true
|
c.running = true
|
||||||
defer close(c.stream)
|
defer close(c.stream)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/bcicen/ctop/connector/collector"
|
||||||
"github.com/bcicen/ctop/cwidgets"
|
"github.com/bcicen/ctop/cwidgets"
|
||||||
"github.com/bcicen/ctop/cwidgets/compact"
|
"github.com/bcicen/ctop/cwidgets/compact"
|
||||||
"github.com/bcicen/ctop/logging"
|
"github.com/bcicen/ctop/logging"
|
||||||
@ -19,10 +20,10 @@ type Container struct {
|
|||||||
Widgets *compact.Compact
|
Widgets *compact.Compact
|
||||||
Display bool // display this container in compact view
|
Display bool // display this container in compact view
|
||||||
updater cwidgets.WidgetUpdater
|
updater cwidgets.WidgetUpdater
|
||||||
collector models.Collector
|
collector collector.Collector
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(id string, collector models.Collector) *Container {
|
func New(id string, collector collector.Collector) *Container {
|
||||||
widgets := compact.NewCompact(id)
|
widgets := compact.NewCompact(id)
|
||||||
return &Container{
|
return &Container{
|
||||||
Metrics: models.NewMetrics(),
|
Metrics: models.NewMetrics(),
|
||||||
|
@ -24,10 +24,3 @@ func NewMetrics() Metrics {
|
|||||||
Pids: -1,
|
Pids: -1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Collector interface {
|
|
||||||
Stream() chan Metrics
|
|
||||||
Running() bool
|
|
||||||
Start()
|
|
||||||
Stop()
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user