@@ -16,15 +16,15 @@ package internal
16
16
17
17
import (
18
18
"context"
19
+ "errors"
19
20
"fmt"
20
21
"net/http"
22
+ "net/http/pprof"
21
23
"os"
22
24
"os/signal"
23
25
"syscall"
24
26
"time"
25
27
26
- "net/http/pprof"
27
-
28
28
"planet-exporter/collector"
29
29
taskdarkstat "planet-exporter/collector/task/darkstat"
30
30
taskebpf "planet-exporter/collector/task/ebpf"
@@ -38,8 +38,8 @@ import (
38
38
log "github.com/sirupsen/logrus"
39
39
)
40
40
41
- // Config contains main service config options
42
- type Config struct {
41
+ // Config contains main service config options.
42
+ type Config struct { // nolint:maligned
43
43
// Main config
44
44
ListenAddress string
45
45
LogLevel string
@@ -63,23 +63,23 @@ type Config struct {
63
63
TaskSocketstatEnabled bool
64
64
}
65
65
66
- // Service contains main service dependency
66
+ // Service contains main service dependency.
67
67
type Service struct {
68
68
Config Config
69
69
70
70
// Collector is prometheus collector that is registered
71
71
Collector * collector.PlanetCollector
72
72
}
73
73
74
- // New service
74
+ // New service.
75
75
func New (config Config , collector * collector.PlanetCollector ) Service {
76
76
return Service {
77
77
Config : config ,
78
78
Collector : collector ,
79
79
}
80
80
}
81
81
82
- // Run main service
82
+ // Run main service.
83
83
func (s Service ) Run (ctx context.Context ) error {
84
84
ctx , cancel := context .WithCancel (ctx )
85
85
defer cancel ()
@@ -88,14 +88,14 @@ func (s Service) Run(ctx context.Context) error {
88
88
log .Infof ("Set task ticker duration to %v" , s .Config .TaskInterval )
89
89
interval , err := time .ParseDuration (s .Config .TaskInterval )
90
90
if err != nil {
91
- return err
91
+ return fmt . Errorf ( "error parsing interval duration: %w" , err )
92
92
}
93
93
go s .collect (ctx , interval )
94
94
95
- r := prometheus .NewRegistry ()
96
- r .MustRegister (version .NewCollector ("planet_exporter" ))
97
- if err := r .Register (s .Collector ); err != nil {
98
- return fmt .Errorf ("Failed to register planet collector: %v " , err )
95
+ promRegistry := prometheus .NewRegistry ()
96
+ promRegistry .MustRegister (version .NewCollector ("planet_exporter" ))
97
+ if err := promRegistry .Register (s .Collector ); err != nil {
98
+ return fmt .Errorf ("failed to register planet collector: %w " , err )
99
99
}
100
100
101
101
handler := http .NewServeMux ()
@@ -113,8 +113,8 @@ func (s Service) Run(ctx context.Context) error {
113
113
}
114
114
})
115
115
handler .Handle ("/metrics" , promhttp .HandlerFor (
116
- prometheus.Gatherers {r },
117
- promhttp.HandlerOpts {
116
+ prometheus.Gatherers {promRegistry },
117
+ promhttp.HandlerOpts { // nolint:exhaustivestruct
118
118
ErrorHandling : promhttp .ContinueOnError ,
119
119
},
120
120
))
@@ -139,18 +139,20 @@ func (s Service) Run(ctx context.Context) error {
139
139
}()
140
140
141
141
log .Infof ("Start HTTP server on %v" , s .Config .ListenAddress )
142
- if err := httpServer .Serve (s .Config .ListenAddress ); err != http .ErrServerClosed {
143
- return err
142
+ if err := httpServer .Serve (s .Config .ListenAddress ); ! errors . Is ( err , http .ErrServerClosed ) {
143
+ return fmt . Errorf ( "error on HTTP server: %w" , err )
144
144
}
145
145
146
146
<- stopChan
147
147
148
148
return nil
149
149
}
150
150
151
- // collect periodically runs all collector tasks that are expensive to compute on-the-fly
151
+ // collect periodically runs all collector tasks that are expensive to compute on-the-fly.
152
152
func (s Service ) collect (ctx context.Context , interval time.Duration ) {
153
- inventoryTicker := time .NewTicker (interval * 25 )
153
+ const inventoryTickerIntervalSeconds = 25
154
+
155
+ inventoryTicker := time .NewTicker (interval * inventoryTickerIntervalSeconds )
154
156
defaultTicker := time .NewTicker (interval )
155
157
defer inventoryTicker .Stop ()
156
158
defer defaultTicker .Stop ()
0 commit comments