This commit is contained in:
Jim Minter 2021-05-05 11:22:04 -05:00 коммит произвёл bennerv
Родитель fb0ff700c8
Коммит dc1d2efacd
2 изменённых файлов: 26 добавлений и 2 удалений

Просмотреть файл

@ -66,7 +66,7 @@ func gateway(ctx context.Context, log *logrus.Entry) error {
}
}
dbRefresher, err := pkgdbtoken.NewRefresher(log, _env, msiRefresherAuthorizer, insecureSkipVerify, dbc, "gateway")
dbRefresher, err := pkgdbtoken.NewRefresher(log, _env, msiRefresherAuthorizer, insecureSkipVerify, dbc, "gateway", m, "gateway")
if err != nil {
return err
}

Просмотреть файл

@ -15,6 +15,7 @@ import (
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/metrics"
utilrecover "github.com/Azure/ARO-RP/pkg/util/recover"
)
@ -31,9 +32,12 @@ type refresher struct {
permission string
lastRefresh atomic.Value //time.Time
m metrics.Interface
metricPrefix string
}
func NewRefresher(log *logrus.Entry, env env.Core, authorizer autorest.Authorizer, insecureSkipVerify bool, dbc cosmosdb.DatabaseClient, permission string) (Refresher, error) {
func NewRefresher(log *logrus.Entry, env env.Core, authorizer autorest.Authorizer, insecureSkipVerify bool, dbc cosmosdb.DatabaseClient, permission string, m metrics.Interface, metricPrefix string) (Refresher, error) {
c, err := NewClient(env, authorizer, insecureSkipVerify)
if err != nil {
return nil, err
@ -45,12 +49,17 @@ func NewRefresher(log *logrus.Entry, env env.Core, authorizer autorest.Authorize
dbc: dbc,
permission: permission,
m: m,
metricPrefix: metricPrefix,
}, nil
}
func (r *refresher) Run(ctx context.Context) error {
defer utilrecover.Panic(r.log)
go r.metrics()
t := time.NewTicker(10 * time.Second)
defer t.Stop()
@ -66,6 +75,21 @@ func (r *refresher) Run(ctx context.Context) error {
}
}
func (r *refresher) metrics() {
defer utilrecover.Panic(r.log)
t := time.NewTicker(time.Minute)
defer t.Stop()
for {
if lastRefresh, ok := r.lastRefresh.Load().(time.Time); ok {
r.m.EmitGauge(r.metricPrefix+".lastrefresh", lastRefresh.Unix(), nil)
}
<-t.C
}
}
func (r *refresher) runOnce(ctx context.Context) (err error) {
// extra hardening to prevent a panic under runOnce taking out the refresher
// goroutine