1
0
mirror of https://github.com/amir20/dozzle.git synced 2026-01-03 03:27:29 +01:00

chore: uses zerolog instead of logrus (#3203)

This commit is contained in:
Amir Raminfar
2024-08-14 10:04:18 -07:00
committed by GitHub
parent 37f0249065
commit 077eeb7917
38 changed files with 278 additions and 265 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"path/filepath"
"sort"
"strconv"
"strings"
@@ -21,7 +20,7 @@ import (
"github.com/docker/docker/api/types/system"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)
type StdType int
@@ -83,7 +82,7 @@ type httpClient struct {
func NewClient(cli DockerCLI, filters filters.Args, host Host) Client {
info, err := cli.Info(context.Background())
if err != nil {
log.Errorf("unable to get docker info: %v", err)
log.Error().Err(err).Msg("Failed to get docker info")
}
host.NCPU = info.NCPU
@@ -107,7 +106,7 @@ func NewLocalClient(f map[string][]string, hostname string) (Client, error) {
}
}
log.Debugf("filterArgs = %v", filterArgs)
log.Debug().Interface("filterArgs", filterArgs).Msg("Creating local client")
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
@@ -149,10 +148,10 @@ func NewRemoteClient(f map[string][]string, host Host) (Client, error) {
}
}
log.Debugf("filterArgs = %v", filterArgs)
log.Debug().Interface("filterArgs", filterArgs).Msg("Creating remote client")
if host.URL.Scheme != "tcp" {
log.Fatal("Only tcp scheme is supported")
return nil, fmt.Errorf("invalid scheme: %s", host.URL.Scheme)
}
opts := []client.Opt{
@@ -160,10 +159,10 @@ func NewRemoteClient(f map[string][]string, host Host) (Client, error) {
}
if host.ValidCerts {
log.Debugf("Using TLS client config with certs at: %s", filepath.Dir(host.CertPath))
log.Debug().Str("caCertPath", host.CACertPath).Str("certPath", host.CertPath).Str("keyPath", host.KeyPath).Msg("Using TLS for remote client")
opts = append(opts, client.WithTLSClientConfig(host.CACertPath, host.CertPath, host.KeyPath))
} else {
log.Debugf("No valid certs found, using plain TCP")
log.Debug().Msg("Not using TLS for remote client")
}
opts = append(opts, client.WithAPIVersionNegotiation())
@@ -181,7 +180,7 @@ func NewRemoteClient(f map[string][]string, host Host) (Client, error) {
// Finds a container by id, skipping the filters
func (d *httpClient) FindContainer(id string) (Container, error) {
log.Debugf("finding container with id: %s", id)
log.Debug().Str("id", id).Msg("Finding container")
if json, err := d.cli.ContainerInspect(context.Background(), id); err == nil {
return newContainerFromJSON(json, d.host.ID), nil
} else {
@@ -204,7 +203,7 @@ func (d *httpClient) ContainerActions(action ContainerAction, containerID string
}
func (d *httpClient) ListContainers() ([]Container, error) {
log.Debugf("listing containers with filters: %v", d.filters)
log.Debug().Interface("filter", d.filters).Str("host", d.host.Name).Msg("Listing containers")
containerListOptions := container.ListOptions{
Filters: d.filters,
All: true,
@@ -277,7 +276,7 @@ func (d *httpClient) ContainerStats(ctx context.Context, id string, stats chan<-
}
func (d *httpClient) ContainerLogs(ctx context.Context, id string, since time.Time, stdType StdType) (io.ReadCloser, error) {
log.WithField("id", id).WithField("since", since).WithField("stdType", stdType).Debug("streaming logs for container")
log.Debug().Str("id", id).Time("since", since).Stringer("stdType", stdType).Str("host", d.host.Name).Msg("Streaming logs for container")
sinceQuery := since.Add(-50 * time.Millisecond).Format(time.RFC3339Nano)
options := container.LogsOptions{
@@ -320,6 +319,7 @@ func (d *httpClient) ContainerEvents(ctx context.Context, messages chan<- Contai
}
func (d *httpClient) ContainerLogsBetweenDates(ctx context.Context, id string, from time.Time, to time.Time, stdType StdType) (io.ReadCloser, error) {
log.Debug().Str("id", id).Time("from", from).Time("to", to).Stringer("stdType", stdType).Str("host", d.host.Name).Msg("Fetching logs between dates for container")
options := container.LogsOptions{
ShowStdout: stdType&STDOUT != 0,
ShowStderr: stdType&STDERR != 0,
@@ -328,8 +328,6 @@ func (d *httpClient) ContainerLogsBetweenDates(ctx context.Context, id string, f
Until: to.Format(time.RFC3339Nano),
}
log.Debugf("fetching logs from Docker with option: %+v", options)
reader, err := d.cli.ContainerLogs(ctx, id, options)
if err != nil {
return nil, err

View File

@@ -7,8 +7,8 @@ import (
"sync/atomic"
"github.com/puzpuzpuz/xsync/v3"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
)
@@ -51,10 +51,10 @@ var (
func (s *ContainerStore) checkConnectivity() error {
if s.connected.CompareAndSwap(false, true) {
go func() {
log.Debugf("subscribing to docker events from container store %s", s.client.Host())
log.Debug().Str("host", s.client.Host().Name).Msg("docker store subscribing docker events")
err := s.client.ContainerEvents(s.ctx, s.events)
if !errors.Is(err, context.Canceled) {
log.Errorf("docker store unexpectedly disconnected from docker events from %s with %v", s.client.Host(), err)
log.Error().Err(err).Str("host", s.client.Host().Name).Msg("docker store unexpectedly disconnected from docker events")
}
s.connected.Store(false)
}()
@@ -76,7 +76,7 @@ func (s *ContainerStore) checkConnectivity() error {
for i, c := range running {
if err := sem.Acquire(s.ctx, 1); err != nil {
log.Errorf("failed to acquire semaphore: %v", err)
log.Error().Err(err).Msg("failed to acquire semaphore")
break
}
go func(c Container, i int) {
@@ -88,10 +88,10 @@ func (s *ContainerStore) checkConnectivity() error {
}
if err := sem.Acquire(s.ctx, maxFetchParallelism); err != nil {
log.Errorf("failed to acquire semaphore: %v", err)
log.Error().Err(err).Msg("failed to acquire semaphore")
}
log.Debugf("finished initializing container store with %d containers", len(containers))
log.Debug().Int("containers", len(containers)).Msg("finished initializing container store")
}
}
@@ -120,7 +120,7 @@ func (s *ContainerStore) FindContainer(id string) (Container, error) {
if ok {
return *container, nil
} else {
log.Warnf("container %s not found in store", id)
log.Warn().Str("id", id).Msg("container not found")
return Container{}, ErrContainerNotFound
}
}
@@ -132,7 +132,6 @@ func (s *ContainerStore) Client() Client {
func (s *ContainerStore) SubscribeEvents(ctx context.Context, events chan<- ContainerEvent) {
go func() {
if s.statsCollector.Start(s.ctx) {
log.Debug("clearing container stats as stats collector has been stopped")
s.containers.Range(func(_ string, c *Container) bool {
c.Stats.Clear()
return true
@@ -171,7 +170,7 @@ func (s *ContainerStore) init() {
for {
select {
case event := <-s.events:
log.Tracef("received event: %+v", event)
log.Trace().Str("event", event.Name).Str("id", event.ActorID).Msg("received container event")
switch event.Name {
case "start":
if container, err := s.client.FindContainer(event.ActorID); err == nil {
@@ -183,7 +182,7 @@ func (s *ContainerStore) init() {
})
if valid {
log.Debugf("container %s started", container.ID)
log.Debug().Str("id", container.ID).Msg("container started")
s.containers.Store(container.ID, &container)
s.newContainerSubscribers.Range(func(c context.Context, containers chan<- Container) bool {
select {
@@ -195,13 +194,13 @@ func (s *ContainerStore) init() {
}
}
case "destroy":
log.Debugf("container %s destroyed", event.ActorID)
log.Debug().Str("id", event.ActorID).Msg("container destroyed")
s.containers.Delete(event.ActorID)
case "die":
s.containers.Compute(event.ActorID, func(c *Container, loaded bool) (*Container, bool) {
if loaded {
log.Debugf("container %s died", c.ID)
log.Debug().Str("id", c.ID).Msg("container died")
c.State = "exited"
return c, false
} else {
@@ -216,7 +215,7 @@ func (s *ContainerStore) init() {
s.containers.Compute(event.ActorID, func(c *Container, loaded bool) (*Container, bool) {
if loaded {
log.Debugf("health status for container %s is %s", c.ID, healthy)
log.Debug().Str("id", c.ID).Str("health", healthy).Msg("container health status changed")
c.Health = healthy
return c, false
} else {

View File

@@ -17,7 +17,7 @@ import (
orderedmap "github.com/wk8/go-ordered-map/v2"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)
type EventGenerator struct {
@@ -100,7 +100,6 @@ func (g *EventGenerator) consumeReader() {
if readerError != nil {
if readerError != ErrBadHeader {
log.Tracef("reader error: %v", readerError)
g.Errors <- readerError
close(g.buffer)
break
@@ -141,7 +140,7 @@ func readEvent(reader *bufio.Reader, tty bool) (string, StdType, error) {
return "", streamType, err
}
if n != 8 {
log.Warnf("unable to read header: %v", header)
log.Warn().Bytes("header", header).Msg("short read")
message, _ := reader.ReadString('\n')
return message, streamType, ErrBadHeader
}
@@ -152,7 +151,7 @@ func readEvent(reader *bufio.Reader, tty bool) (string, StdType, error) {
case 2:
streamType = STDERR
default:
log.Warnf("unknown stream type: %v", header[0])
log.Warn().Bytes("header", header).Msg("unknown stream type")
}
count := binary.BigEndian.Uint32(header[4:])
@@ -183,7 +182,7 @@ func createEvent(message string, streamType StdType) *LogEvent {
var jsonErr *json.UnmarshalTypeError
if errors.As(err, &jsonErr) {
if jsonErr.Value == "string" {
log.Warnf("unable to parse json logs - error was \"%v\" while trying unmarshal \"%v\"", err.Error(), message)
log.Warn().Err(err).Str("value", jsonErr.Value).Msg("failed to unmarshal json")
}
}
} else {

View File

@@ -7,7 +7,7 @@ import (
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)
type Host struct {
@@ -49,14 +49,14 @@ func ParseConnection(connection string) (Host, error) {
basePath, err := filepath.Abs("./certs")
if err != nil {
log.Fatalf("error converting certs path to absolute: %s", err)
return Host{}, err
}
host := remoteUrl.Hostname()
if _, err := os.Stat(filepath.Join(basePath, host)); !os.IsNotExist(err) {
basePath = filepath.Join(basePath, host)
} else {
log.Debugf("Remote host certificate path does not exist %s, falling back to default: %s", filepath.Join(basePath, host), basePath)
log.Debug().Msgf("Remote host certificate path does not exist %s, falling back to default: %s", filepath.Join(basePath, host), basePath)
}
cacertPath := filepath.Join(basePath, "ca.pem")

View File

@@ -4,7 +4,7 @@ import (
"regexp"
"strings"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
@@ -74,7 +74,7 @@ func guessLogLevel(logEvent *LogEvent) string {
}
default:
log.Debugf("unknown type to guess level: %T", value)
log.Debug().Type("type", value).Msg("unknown logEvent type")
}
return ""

View File

@@ -9,7 +9,7 @@ import (
"time"
"github.com/puzpuzpuz/xsync/v3"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)
type StatsCollector struct {
@@ -48,7 +48,7 @@ func (c *StatsCollector) forceStop() {
if c.stopper != nil {
c.stopper()
c.stopper = nil
log.Debug("stopping container stats collector")
log.Debug().Str("host", c.client.Host().ID).Msg("stopped container stats collector")
}
}
@@ -56,7 +56,6 @@ func (c *StatsCollector) Stop() {
c.mu.Lock()
defer c.mu.Unlock()
if c.totalStarted.Add(-1) == 0 {
log.Tracef("scheduled to stop container stats collector %s", c.client.Host())
c.timer = time.AfterFunc(timeToStop, func() {
c.forceStop()
})
@@ -66,7 +65,6 @@ func (c *StatsCollector) Stop() {
func (c *StatsCollector) reset() {
c.mu.Lock()
defer c.mu.Unlock()
log.Tracef("resetting timer for container stats collector %s", c.client.Host())
if c.timer != nil {
c.timer.Stop()
}
@@ -76,11 +74,11 @@ func (c *StatsCollector) reset() {
func streamStats(parent context.Context, sc *StatsCollector, id string) {
ctx, cancel := context.WithCancel(parent)
sc.cancelers.Store(id, cancel)
log.Debugf("starting to stream stats for: %s", id)
log.Debug().Str("container", id).Str("host", sc.client.Host().Name).Msg("starting to stream stats")
if err := sc.client.ContainerStats(ctx, id, sc.stream); err != nil {
log.Debugf("stopping to stream stats for: %s", id)
log.Debug().Str("container", id).Str("host", sc.client.Host().Name).Err(err).Msg("stopping to stream stats")
if !errors.Is(err, context.Canceled) && !errors.Is(err, io.EOF) {
log.Errorf("unexpected error when streaming container stats: %v", err)
log.Error().Str("container", id).Str("host", sc.client.Host().Name).Err(err).Msg("unexpected error while streaming stats")
}
}
}
@@ -106,16 +104,16 @@ func (sc *StatsCollector) Start(parentCtx context.Context) bool {
}
}
} else {
log.Errorf("error while listing containers: %v", err)
log.Error().Str("host", sc.client.Host().Name).Err(err).Msg("failed to list containers")
}
events := make(chan ContainerEvent)
go func() {
log.Debugf("subscribing to docker events from stats collector %s", sc.client.Host())
log.Debug().Str("host", sc.client.Host().Name).Msg("starting to listen to docker events")
err := sc.client.ContainerEvents(context.Background(), events)
if !errors.Is(err, context.Canceled) {
log.Errorf("stats collector unexpectedly disconnected from docker events from %s with %v", sc.client.Host(), err)
log.Error().Str("host", sc.client.Host().Name).Err(err).Msg("unexpected error while listening to docker events")
}
sc.forceStop()
}()
@@ -137,7 +135,7 @@ func (sc *StatsCollector) Start(parentCtx context.Context) bool {
for {
select {
case <-ctx.Done():
log.Info("stopped collecting container stats")
log.Info().Str("host", sc.client.Host().Name).Msg("stopped container stats collector")
return true
case stat := <-sc.stream:
sc.subscribers.Range(func(c context.Context, stats chan<- ContainerStat) bool {