@@ -29,7 +29,6 @@ import (
29
29
30
30
kingpin "github.com/alecthomas/kingpin/v2"
31
31
"github.com/coreos/go-systemd/v22/dbus"
32
- "github.com/pkg/errors"
33
32
"github.com/prometheus/client_golang/prometheus"
34
33
)
35
34
48
47
var unitStatesName = []string {"active" , "activating" , "deactivating" , "inactive" , "failed" }
49
48
50
49
var (
51
- errGetPropertyMsg = "couldn't get unit's %s property"
50
+ errGetPropertyMsg = "couldn't get unit's %s property: %w "
52
51
errConvertUint64PropertyMsg = "couldn't convert unit's %s property %v to uint64"
53
52
errConvertUint32PropertyMsg = "couldn't convert unit's %s property %v to uint32"
54
53
errConvertStringPropertyMsg = "couldn't convert unit's %s property %v to string"
@@ -301,7 +300,7 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error {
301
300
begin := time .Now ()
302
301
conn , err := c .newDbus ()
303
302
if err != nil {
304
- return errors . Wrapf ( err , "couldn't get dbus connection" )
303
+ return fmt . Errorf ( "couldn't get dbus connection: %w" , err )
305
304
}
306
305
defer conn .Close ()
307
306
@@ -317,7 +316,7 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error {
317
316
318
317
allUnits , err := conn .ListUnitsContext (c .ctx )
319
318
if err != nil {
320
- return errors . Wrap ( err , "could not get list of systemd units from dbus" )
319
+ return fmt . Errorf ( "could not get list of systemd units from dbus: %w" , err )
321
320
}
322
321
323
322
c .logger .Debug ("systemd ListUnits took" , "seconds" , time .Since (begin ).Seconds ())
@@ -493,11 +492,11 @@ func (c *Collector) collectUnitTimeMetrics(conn *dbus.Conn, ch chan<- prometheus
493
492
func (c * Collector ) collectUnitTimeMetric (conn * dbus.Conn , ch chan <- prometheus.Metric , unit dbus.UnitStatus , desc * prometheus.Desc , propertyName string ) error {
494
493
timestampValue , err := conn .GetUnitPropertyContext (c .ctx , unit .Name , propertyName )
495
494
if err != nil {
496
- return errors . Wrapf ( err , errGetPropertyMsg , propertyName )
495
+ return fmt . Errorf ( errGetPropertyMsg , propertyName , err )
497
496
}
498
497
startTimeUsec , ok := timestampValue .Value .Value ().(uint64 )
499
498
if ! ok {
500
- return errors .Errorf (errConvertUint64PropertyMsg , propertyName , timestampValue .Value .Value ())
499
+ return fmt .Errorf (errConvertUint64PropertyMsg , propertyName , timestampValue .Value .Value ())
501
500
}
502
501
503
502
ch <- prometheus .MustNewConstMetric (desc , prometheus .GaugeValue , float64 (startTimeUsec )/ 1e6 , unit .Name , parseUnitType (unit ))
@@ -510,12 +509,12 @@ func (c *Collector) collectMountMetainfo(conn *dbus.Conn, ch chan<- prometheus.M
510
509
// TODO: wrap GetUnitTypePropertyString(
511
510
serviceTypeProperty , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Mount" , "Type" )
512
511
if err != nil {
513
- return errors . Wrapf ( err , errGetPropertyMsg , "Type" )
512
+ return fmt . Errorf ( errGetPropertyMsg , "Type" , err )
514
513
}
515
514
516
515
serviceType , ok := serviceTypeProperty .Value .Value ().(string )
517
516
if ! ok {
518
- return errors .Errorf (errConvertStringPropertyMsg , "Type" , serviceTypeProperty .Value .Value ())
517
+ return fmt .Errorf (errConvertStringPropertyMsg , "Type" , serviceTypeProperty .Value .Value ())
519
518
}
520
519
521
520
ch <- prometheus .MustNewConstMetric (
@@ -529,11 +528,11 @@ func (c *Collector) collectMountMetainfo(conn *dbus.Conn, ch chan<- prometheus.M
529
528
func (c * Collector ) collectServiceMetainfo (conn * dbus.Conn , ch chan <- prometheus.Metric , unit dbus.UnitStatus ) error {
530
529
serviceTypeProperty , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Service" , "Type" )
531
530
if err != nil {
532
- return errors . Wrapf ( err , errGetPropertyMsg , "Type" )
531
+ return fmt . Errorf ( errGetPropertyMsg , "Type" , err )
533
532
}
534
533
serviceType , ok := serviceTypeProperty .Value .Value ().(string )
535
534
if ! ok {
536
- return errors .Errorf (errConvertStringPropertyMsg , "Type" , serviceTypeProperty .Value .Value ())
535
+ return fmt .Errorf (errConvertStringPropertyMsg , "Type" , serviceTypeProperty .Value .Value ())
537
536
}
538
537
539
538
ch <- prometheus .MustNewConstMetric (
@@ -545,11 +544,11 @@ func (c *Collector) collectServiceMetainfo(conn *dbus.Conn, ch chan<- prometheus
545
544
func (c * Collector ) collectServiceRestartCount (conn * dbus.Conn , ch chan <- prometheus.Metric , unit dbus.UnitStatus ) error {
546
545
restartsCount , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Service" , "NRestarts" )
547
546
if err != nil {
548
- return errors . Wrapf ( err , errGetPropertyMsg , "NRestarts" )
547
+ return fmt . Errorf ( errGetPropertyMsg , "NRestarts" , err )
549
548
}
550
549
val , ok := restartsCount .Value .Value ().(uint32 )
551
550
if ! ok {
552
- return errors .Errorf (errConvertUint32PropertyMsg , "NRestarts" , restartsCount .Value .Value ())
551
+ return fmt .Errorf (errConvertUint32PropertyMsg , "NRestarts" , restartsCount .Value .Value ())
553
552
}
554
553
ch <- prometheus .MustNewConstMetric (
555
554
c .nRestartsDesc , prometheus .CounterValue ,
@@ -565,11 +564,11 @@ func (c *Collector) collectServiceStartTimeMetrics(conn *dbus.Conn, ch chan<- pr
565
564
case "active" :
566
565
timestampValue , err := conn .GetUnitPropertyContext (c .ctx , unit .Name , "ActiveEnterTimestamp" )
567
566
if err != nil {
568
- return errors . Wrapf ( err , errGetPropertyMsg , "ActiveEnterTimestamp" )
567
+ return fmt . Errorf ( errGetPropertyMsg , "ActiveEnterTimestamp" , err )
569
568
}
570
569
startTime , ok := timestampValue .Value .Value ().(uint64 )
571
570
if ! ok {
572
- return errors .Errorf (errConvertUint64PropertyMsg , "ActiveEnterTimestamp" , timestampValue .Value .Value ())
571
+ return fmt .Errorf (errConvertUint64PropertyMsg , "ActiveEnterTimestamp" , timestampValue .Value .Value ())
573
572
}
574
573
startTimeUsec = startTime
575
574
@@ -587,7 +586,7 @@ func (c *Collector) collectServiceStartTimeMetrics(conn *dbus.Conn, ch chan<- pr
587
586
func (c * Collector ) collectSocketConnMetrics (conn * dbus.Conn , ch chan <- prometheus.Metric , unit dbus.UnitStatus ) error {
588
587
acceptedConnectionCount , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Socket" , "NAccepted" )
589
588
if err != nil {
590
- return errors . Wrapf ( err , errGetPropertyMsg , "NAccepted" )
589
+ return fmt . Errorf ( errGetPropertyMsg , "NAccepted" , err )
591
590
}
592
591
593
592
ch <- prometheus .MustNewConstMetric (
@@ -596,7 +595,7 @@ func (c *Collector) collectSocketConnMetrics(conn *dbus.Conn, ch chan<- promethe
596
595
597
596
currentConnectionCount , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Socket" , "NConnections" )
598
597
if err != nil {
599
- return errors . Wrapf ( err , errGetPropertyMsg , "NConnections" )
598
+ return fmt . Errorf ( errGetPropertyMsg , "NConnections" , err )
600
599
}
601
600
ch <- prometheus .MustNewConstMetric (
602
601
c .socketCurrentConnectionsDesc , prometheus .GaugeValue ,
@@ -605,7 +604,7 @@ func (c *Collector) collectSocketConnMetrics(conn *dbus.Conn, ch chan<- promethe
605
604
// NRefused wasn't added until systemd 239.
606
605
refusedConnectionCount , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Socket" , "NRefused" )
607
606
if err != nil {
608
- return errors . Wrapf ( err , errGetPropertyMsg , "NRefused" )
607
+ return fmt . Errorf ( errGetPropertyMsg , "NRefused" , err )
609
608
}
610
609
ch <- prometheus .MustNewConstMetric (
611
610
c .socketRefusedConnectionsDesc , prometheus .GaugeValue ,
@@ -625,12 +624,12 @@ func (c *Collector) collectIPAccountingMetrics(conn *dbus.Conn, ch chan<- promet
625
624
for propertyName , desc := range unitPropertyToPromDesc {
626
625
property , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Service" , propertyName )
627
626
if err != nil {
628
- return errors . Wrapf ( err , errGetPropertyMsg , propertyName )
627
+ return fmt . Errorf ( errGetPropertyMsg , propertyName , err )
629
628
}
630
629
631
630
counter , ok := property .Value .Value ().(uint64 )
632
631
if ! ok {
633
- return errors .Errorf (errConvertUint64PropertyMsg , propertyName , property .Value .Value ())
632
+ return fmt .Errorf (errConvertUint64PropertyMsg , propertyName , property .Value .Value ())
634
633
}
635
634
636
635
ch <- prometheus .MustNewConstMetric (desc , prometheus .CounterValue ,
@@ -645,12 +644,12 @@ func (c *Collector) collectIPAccountingMetrics(conn *dbus.Conn, ch chan<- promet
645
644
func (c * Collector ) collectServiceTasksMetrics (conn * dbus.Conn , ch chan <- prometheus.Metric , unit dbus.UnitStatus ) error {
646
645
tasksCurrentCount , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Service" , "TasksCurrent" )
647
646
if err != nil {
648
- return errors . Wrapf ( err , errGetPropertyMsg , "TasksCurrent" )
647
+ return fmt . Errorf ( errGetPropertyMsg , "TasksCurrent" , err )
649
648
}
650
649
651
650
currentCount , ok := tasksCurrentCount .Value .Value ().(uint64 )
652
651
if ! ok {
653
- return errors .Errorf (errConvertUint64PropertyMsg , "TasksCurrent" , tasksCurrentCount .Value .Value ())
652
+ return fmt .Errorf (errConvertUint64PropertyMsg , "TasksCurrent" , tasksCurrentCount .Value .Value ())
654
653
}
655
654
656
655
// Don't set if tasksCurrent if dbus reports MaxUint64.
@@ -662,12 +661,12 @@ func (c *Collector) collectServiceTasksMetrics(conn *dbus.Conn, ch chan<- promet
662
661
663
662
tasksMaxCount , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Service" , "TasksMax" )
664
663
if err != nil {
665
- return errors . Wrapf ( err , errGetPropertyMsg , "TasksMax" )
664
+ return fmt . Errorf ( errGetPropertyMsg , "TasksMax" , err )
666
665
}
667
666
668
667
maxCount , ok := tasksMaxCount .Value .Value ().(uint64 )
669
668
if ! ok {
670
- return errors .Errorf (errConvertUint64PropertyMsg , "TasksMax" , tasksMaxCount .Value .Value ())
669
+ return fmt .Errorf (errConvertUint64PropertyMsg , "TasksMax" , tasksMaxCount .Value .Value ())
671
670
}
672
671
// Don't set if tasksMax if dbus reports MaxUint64.
673
672
if maxCount != math .MaxUint64 {
@@ -682,11 +681,11 @@ func (c *Collector) collectServiceTasksMetrics(conn *dbus.Conn, ch chan<- promet
682
681
func (c * Collector ) collectTimerTriggerTime (conn * dbus.Conn , ch chan <- prometheus.Metric , unit dbus.UnitStatus ) error {
683
682
lastTriggerValue , err := conn .GetUnitTypePropertyContext (c .ctx , unit .Name , "Timer" , "LastTriggerUSec" )
684
683
if err != nil {
685
- return errors . Wrapf ( err , errGetPropertyMsg , "LastTriggerUSec" )
684
+ return fmt . Errorf ( errGetPropertyMsg , "LastTriggerUSec" , err )
686
685
}
687
686
val , ok := lastTriggerValue .Value .Value ().(uint64 )
688
687
if ! ok {
689
- return errors .Errorf (errConvertUint64PropertyMsg , "LastTriggerUSec" , lastTriggerValue .Value .Value ())
688
+ return fmt .Errorf (errConvertUint64PropertyMsg , "LastTriggerUSec" , lastTriggerValue .Value .Value ())
690
689
}
691
690
ch <- prometheus .MustNewConstMetric (
692
691
c .timerLastTriggerDesc , prometheus .GaugeValue ,
0 commit comments