Skip to content

Commit

Permalink
Introduces fields into logging to provide more structured log informa…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
chr-fritz committed Aug 17, 2023
1 parent eeba1af commit c5855e7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
8 changes: 6 additions & 2 deletions pkg/knx/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ 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
}
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
Expand Down
30 changes: 23 additions & 7 deletions pkg/knx/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions pkg/knx/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,22 @@ 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
}

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)
}
}
Expand All @@ -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()
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/knx/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c5855e7

Please sign in to comment.