From c5855e7e18d065db35992ec65a7ffb5f4308a3d3 Mon Sep 17 00:00:00 2001 From: Christian Fritz Date: Thu, 17 Aug 2023 22:47:43 +0200 Subject: [PATCH] Introduces fields into logging to provide more structured log information. --- pkg/knx/exporter.go | 8 ++++++-- pkg/knx/listener.go | 30 +++++++++++++++++++++++------- pkg/knx/poller.go | 17 +++++++++++------ pkg/knx/snapshot.go | 5 ++++- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/pkg/knx/exporter.go b/pkg/knx/exporter.go index 2b28cd4..b9904a4 100644 --- a/pkg/knx/exporter.go +++ b/pkg/knx/exporter.go @@ -100,7 +100,9 @@ func (e *metricsExporter) IsAlive() error { func (e *metricsExporter) createClient() error { switch e.config.Connection.Type { case Tunnel: - logrus.Infof("Connect to %s using tunneling", e.config.Connection.Endpoint) + logrus.WithField("endpoint", e.config.Connection.Endpoint). + WithField("connection_type", "tunnel"). + Infof("Connect to %s using tunneling", e.config.Connection.Endpoint) tunnel, err := knx.NewGroupTunnel(e.config.Connection.Endpoint, knx.DefaultTunnelConfig) if err != nil { return err @@ -108,7 +110,9 @@ func (e *metricsExporter) createClient() error { e.client = &tunnel return nil case Router: - logrus.Infof("Connect to %s using multicast routing", e.config.Connection.Endpoint) + logrus.WithField("endpoint", e.config.Connection.Endpoint). + WithField("connection_type", "routing"). + Infof("Connect to %s using multicast routing", e.config.Connection.Endpoint) router, err := knx.NewGroupRouter(e.config.Connection.Endpoint, knx.DefaultRouterConfig) if err != nil { return err diff --git a/pkg/knx/listener.go b/pkg/knx/listener.go index 7b8858e..e75add4 100644 --- a/pkg/knx/listener.go +++ b/pkg/knx/listener.go @@ -70,26 +70,42 @@ func (l *listener) handleEvent(event knx.GroupEvent) { addr, ok := l.config.AddressConfigs[destination] if !ok { - logrus.Tracef("Got ignored %s telegram from %s for %s.", - event.Command.String(), - event.Source.String(), - event.Destination.String()) + logrus. + WithFields(logrus.Fields{ + "command": event.Command.String(), + "source": event.Source.String(), + "destination": event.Destination.String(), + }). + Tracef("Got ignored %s telegram from %s for %s.", + event.Command.String(), + event.Source.String(), + event.Destination.String()) return } value, err := unpackEvent(event, addr) + messageLogFields := logrus.Fields{ + "dpt": addr.DPT, + "command": event.Command.String(), + "source": event.Source.String(), + "destination": event.Destination.String(), + } if err != nil { - logrus.Warn(err) + logrus.WithFields(messageLogFields). + Warn(err) return } floatValue, err := extractAsFloat64(value) if err != nil { - logrus.Warn(err) + logrus.WithFields(messageLogFields). + Warn(err) return } metricName := l.config.NameFor(addr) - logrus.Tracef("Processed value %s for %s on group address %s", value.String(), metricName, destination) + logrus.WithFields(messageLogFields). + WithField("metricName", metricName). + Tracef("Processed value %s for %s on group address %s", value.String(), metricName, destination) l.metricsChan <- &Snapshot{ name: metricName, value: floatValue, diff --git a/pkg/knx/poller.go b/pkg/knx/poller.go index 2410373..393098d 100644 --- a/pkg/knx/poller.go +++ b/pkg/knx/poller.go @@ -80,7 +80,8 @@ func (p *poller) pollAddresses(t time.Time) { for address, config := range p.metricsToPoll { s := p.snapshotHandler.FindYoungestSnapshot(config.Name) if s == nil { - logrus.Tracef("Initial poll of %s", address.String()) + logrus.WithField("address", address). + Tracef("Initial poll of %s", address.String()) p.sendReadMessage(address) continue } @@ -88,10 +89,13 @@ func (p *poller) pollAddresses(t time.Time) { diff := t.Sub(s.timestamp).Round(time.Second) maxAge := time.Duration(config.MaxAge) if diff >= maxAge { - logrus.Tracef("Poll %s for new value as it is already %s old but max age is %s only", - address.String(), - diff.String(), - maxAge.String()) + logrus.WithField("address", address). + WithField("diff", diff). + WithField("maxAge", maxAge). + Tracef("Poll %s for new value as it is already %s old but max age is %s only", + address.String(), + diff.String(), + maxAge.String()) p.sendReadMessage(address) } } @@ -105,7 +109,8 @@ func (p *poller) sendReadMessage(address GroupAddress) { } if e := p.client.Send(event); e != nil { - logrus.Infof("can not send read request for %s: %s", address.String(), e) + logrus.WithField("address", address.String()). + Infof("can not send read request for %s: %s", address.String(), e) } p.messageCounter.WithLabelValues("sent", "true").Inc() } diff --git a/pkg/knx/snapshot.go b/pkg/knx/snapshot.go index faef434..e25235d 100644 --- a/pkg/knx/snapshot.go +++ b/pkg/knx/snapshot.go @@ -108,7 +108,10 @@ func (m *metricSnapshots) AddSnapshot(s *Snapshot) { meta = snapshot{snapshot: s, metric: metric} err = m.registerer.Register(meta.metric) if err != nil && !errors.Is(err, prometheus.AlreadyRegisteredError{}) { - logrus.Warnf("Can not register new metric %s from %s: %s", s.name, s.source.String(), err) + logrus. + WithField("metricName", s.name). + WithField("source", s.source). + Warnf("Can not register new metric %s from %s: %s", s.name, s.source.String(), err) } } m.snapshots[key] = meta