1
0
Fork 0

Pilot metrics provider

Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com>
This commit is contained in:
Romain 2020-08-10 15:26:04 +02:00 committed by GitHub
parent 5b05c990b0
commit 0d5d14d41a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 712 additions and 27 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/cenkalti/backoff/v4"
"github.com/containous/traefik/v2/pkg/config/runtime"
"github.com/containous/traefik/v2/pkg/log"
"github.com/containous/traefik/v2/pkg/metrics"
"github.com/containous/traefik/v2/pkg/safe"
"github.com/containous/traefik/v2/pkg/version"
)
@ -44,10 +45,11 @@ type serviceInfoRepresentation struct {
type instanceInfo struct {
ID string `json:"id,omitempty"`
Configuration RunTimeRepresentation `json:"configuration,omitempty"`
Metrics []metrics.PilotMetric `json:"metrics,omitempty"`
}
// New creates a new Pilot.
func New(token string, pool *safe.Pool) *Pilot {
func New(token string, metricsRegistry *metrics.PilotRegistry, pool *safe.Pool) *Pilot {
return &Pilot{
rtConfChan: make(chan *runtime.Configuration),
client: &client{
@ -55,7 +57,8 @@ func New(token string, pool *safe.Pool) *Pilot {
httpClient: &http.Client{Timeout: 5 * time.Second},
baseURL: baseURL,
},
routinesPool: pool,
routinesPool: pool,
metricsRegistry: metricsRegistry,
}
}
@ -64,8 +67,9 @@ type Pilot struct {
routinesPool *safe.Pool
client *client
rtConf *runtime.Configuration
rtConfChan chan *runtime.Configuration
rtConf *runtime.Configuration
rtConfChan chan *runtime.Configuration
metricsRegistry *metrics.PilotRegistry
}
// SetRuntimeConfiguration stores the runtime configuration.
@ -99,8 +103,8 @@ func (p *Pilot) getRepresentation() RunTimeRepresentation {
return result
}
func (p *Pilot) sendData(ctx context.Context, conf RunTimeRepresentation) {
err := p.client.SendData(ctx, conf)
func (p *Pilot) sendData(ctx context.Context, conf RunTimeRepresentation, pilotMetrics []metrics.PilotMetric) {
err := p.client.SendData(ctx, conf, pilotMetrics)
if err != nil {
log.WithoutContext().Error(err)
}
@ -117,9 +121,10 @@ func (p *Pilot) Tick(ctx context.Context) {
}
conf := p.getRepresentation()
pilotMetrics := p.metricsRegistry.Data()
p.routinesPool.GoCtx(func(ctxRt context.Context) {
p.sendData(ctxRt, conf)
p.sendData(ctxRt, conf, pilotMetrics)
})
ticker := time.NewTicker(pilotTimer)
@ -129,9 +134,10 @@ func (p *Pilot) Tick(ctx context.Context) {
log.WithoutContext().Debugf("Send to pilot: %s", tick)
conf := p.getRepresentation()
pilotMetrics := p.metricsRegistry.Data()
p.routinesPool.GoCtx(func(ctxRt context.Context) {
p.sendData(ctxRt, conf)
p.sendData(ctxRt, conf, pilotMetrics)
})
case rtConf := <-p.rtConfChan:
p.rtConf = rtConf
@ -184,13 +190,13 @@ func (c *client) createUUID() (string, error) {
}
// SendData sends data to Pilot.
func (c *client) SendData(ctx context.Context, rtConf RunTimeRepresentation) error {
func (c *client) SendData(ctx context.Context, rtConf RunTimeRepresentation, pilotMetrics []metrics.PilotMetric) error {
exponentialBackOff := backoff.NewExponentialBackOff()
exponentialBackOff.MaxElapsedTime = maxElapsedTime
return backoff.RetryNotify(
func() error {
return c.sendData(rtConf)
return c.sendData(rtConf, pilotMetrics)
},
backoff.WithContext(exponentialBackOff, ctx),
func(err error, duration time.Duration) {
@ -198,7 +204,7 @@ func (c *client) SendData(ctx context.Context, rtConf RunTimeRepresentation) err
})
}
func (c *client) sendData(_ RunTimeRepresentation) error {
func (c *client) sendData(_ RunTimeRepresentation, pilotMetrics []metrics.PilotMetric) error {
if len(c.uuid) == 0 {
var err error
c.uuid, err = c.createUUID()
@ -210,7 +216,8 @@ func (c *client) sendData(_ RunTimeRepresentation) error {
}
info := instanceInfo{
ID: c.uuid,
ID: c.uuid,
Metrics: pilotMetrics,
}
b, err := json.Marshal(info)