Skip to content

Commit ada3dae

Browse files
Allow the router to trigger metrics capture right before reload
Even on short reload intervals we ensure we capture metrics before restart. A more sophisticated implementation could use a restart to detect when counters are reset.
1 parent bd1fa50 commit ada3dae

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

pkg/cmd/infra/router/template.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ func (o *TemplateRouterOptions) Validate() error {
201201

202202
// Run launches a template router using the provided options. It never exits.
203203
func (o *TemplateRouterOptions) Run() error {
204+
var reloadCallbacks []func()
204205
switch {
205206
case o.MetricsType == "haproxy" && len(o.ListenAddr) > 0:
206207
var timeout time.Duration
@@ -229,13 +230,17 @@ func (o *TemplateRouterOptions) Run() error {
229230
exported = append(exported, i)
230231
}
231232
}
232-
haproxy.NewPrometheusCollector(haproxy.PrometheusOptions{
233+
e, err := haproxy.NewPrometheusCollector(haproxy.PrometheusOptions{
233234
ScrapeURI: util.Env("ROUTER_METRICS_HAPROXY_SCRAPE_URI", ""),
234235
PidFile: util.Env("ROUTER_METRICS_HAPROXY_PID_FILE", ""),
235236
ServerThreshold: serverThreshold,
236237
BaseScrapeInterval: baseScrapeInterval,
237238
ExportedMetrics: exported,
238239
})
240+
if err != nil {
241+
return err
242+
}
243+
reloadCallbacks = append(reloadCallbacks, e.CollectNow)
239244
}
240245
if len(o.ListenAddr) > 0 {
241246
metrics.Listen(o.ListenAddr, o.StatsUsername, o.StatsPassword)
@@ -246,6 +251,7 @@ func (o *TemplateRouterOptions) Run() error {
246251
TemplatePath: o.TemplateFile,
247252
ReloadScriptPath: o.ReloadScript,
248253
ReloadInterval: o.ReloadInterval,
254+
ReloadCallbacks: reloadCallbacks,
249255
DefaultCertificate: o.DefaultCertificate,
250256
DefaultCertificatePath: o.DefaultCertificatePath,
251257
DefaultCertificateDir: o.DefaultCertificateDir,

pkg/router/template/plugin.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type TemplatePluginConfig struct {
4141
TemplatePath string
4242
ReloadScriptPath string
4343
ReloadInterval time.Duration
44+
ReloadCallbacks []func()
4445
DefaultCertificate string
4546
DefaultCertificatePath string
4647
DefaultCertificateDir string
@@ -130,6 +131,7 @@ func NewTemplatePlugin(cfg TemplatePluginConfig, lookupSvc ServiceLookup) (*Temp
130131
templates: templates,
131132
reloadScriptPath: cfg.ReloadScriptPath,
132133
reloadInterval: cfg.ReloadInterval,
134+
reloadCallbacks: cfg.ReloadCallbacks,
133135
defaultCertificate: cfg.DefaultCertificate,
134136
defaultCertificatePath: cfg.DefaultCertificatePath,
135137
defaultCertificateDir: cfg.DefaultCertificateDir,

pkg/router/template/router.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type templateRouter struct {
5151
templates map[string]*template.Template
5252
reloadScriptPath string
5353
reloadInterval time.Duration
54+
reloadCallbacks []func()
5455
state map[string]ServiceAliasConfig
5556
serviceUnits map[string]ServiceUnit
5657
certManager certificateManager
@@ -99,6 +100,7 @@ type templateRouterCfg struct {
99100
templates map[string]*template.Template
100101
reloadScriptPath string
101102
reloadInterval time.Duration
103+
reloadCallbacks []func()
102104
defaultCertificate string
103105
defaultCertificatePath string
104106
defaultCertificateDir string
@@ -158,6 +160,7 @@ func newTemplateRouter(cfg templateRouterCfg) (*templateRouter, error) {
158160
templates: cfg.templates,
159161
reloadScriptPath: cfg.reloadScriptPath,
160162
reloadInterval: cfg.reloadInterval,
163+
reloadCallbacks: cfg.reloadCallbacks,
161164
state: make(map[string]ServiceAliasConfig),
162165
serviceUnits: make(map[string]ServiceUnit),
163166
certManager: certManager,
@@ -384,17 +387,25 @@ func (r *templateRouter) Commit() {
384387

385388
// commitAndReload refreshes the backend and persists the router state.
386389
func (r *templateRouter) commitAndReload() error {
387-
r.lock.Lock()
388-
defer r.lock.Unlock()
390+
// only state changes must be done under the lock
391+
if err := func() error {
392+
r.lock.Lock()
393+
defer r.lock.Unlock()
394+
395+
glog.V(4).Infof("Writing the router state")
396+
if err := r.writeState(); err != nil {
397+
return err
398+
}
389399

390-
glog.V(4).Infof("Writing the router state")
391-
if err := r.writeState(); err != nil {
400+
glog.V(4).Infof("Writing the router config")
401+
return r.writeConfig()
402+
}(); err != nil {
392403
return err
393404
}
394405

395-
glog.V(4).Infof("Writing the router config")
396-
if err := r.writeConfig(); err != nil {
397-
return err
406+
for i, fn := range r.reloadCallbacks {
407+
glog.V(4).Infof("Calling reload function %d", i)
408+
fn()
398409
}
399410

400411
glog.V(4).Infof("Reloading the router")

0 commit comments

Comments
 (0)