github.com/thanos-io/thanos@v0.32.5/pkg/prober/intrumentation.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package prober
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/go-kit/log"
    10  	"github.com/go-kit/log/level"
    11  	"github.com/prometheus/client_golang/prometheus"
    12  	"github.com/prometheus/client_golang/prometheus/promauto"
    13  
    14  	"github.com/thanos-io/thanos/pkg/component"
    15  )
    16  
    17  const (
    18  	ready    = "ready"
    19  	notReady = "not-ready"
    20  	healthy  = "healthy"
    21  )
    22  
    23  // InstrumentationProbe stores instrumentation state of Probe.
    24  // This is created with an intention to combine with other Probe's using prober.Combine.
    25  type InstrumentationProbe struct {
    26  	component component.Component
    27  	logger    log.Logger
    28  
    29  	statusMetric *prometheus.GaugeVec
    30  	mu           sync.Mutex
    31  	statusString string
    32  }
    33  
    34  // NewInstrumentation returns InstrumentationProbe records readiness and healthiness for given component.
    35  func NewInstrumentation(component component.Component, logger log.Logger, reg prometheus.Registerer) *InstrumentationProbe {
    36  	p := InstrumentationProbe{
    37  		component: component,
    38  		logger:    logger,
    39  		statusMetric: promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{
    40  			Name:        "status",
    41  			Help:        "Represents status (0 indicates failure, 1 indicates success) of the component.",
    42  			ConstLabels: map[string]string{"component": component.String()},
    43  		},
    44  			[]string{"check"},
    45  		),
    46  	}
    47  	return &p
    48  }
    49  
    50  // Ready records the component status when Ready is called, if combined with other Probes.
    51  func (p *InstrumentationProbe) Ready() {
    52  	p.statusMetric.WithLabelValues(ready).Set(1)
    53  	p.mu.Lock()
    54  	defer p.mu.Unlock()
    55  	if p.statusString != ready {
    56  		level.Info(p.logger).Log("msg", "changing probe status", "status", ready)
    57  		p.statusString = ready
    58  	}
    59  }
    60  
    61  // NotReady records the component status when NotReady is called, if combined with other Probes.
    62  func (p *InstrumentationProbe) NotReady(err error) {
    63  	p.statusMetric.WithLabelValues(ready).Set(0)
    64  	p.mu.Lock()
    65  	defer p.mu.Unlock()
    66  	if p.statusString != notReady {
    67  		level.Warn(p.logger).Log("msg", "changing probe status", "status", notReady, "reason", err)
    68  		p.statusString = notReady
    69  	}
    70  }
    71  
    72  // Healthy records the component status when Healthy is called, if combined with other Probes.
    73  func (p *InstrumentationProbe) Healthy() {
    74  	p.statusMetric.WithLabelValues(healthy).Set(1)
    75  	level.Info(p.logger).Log("msg", "changing probe status", "status", "healthy")
    76  }
    77  
    78  // NotHealthy records the component status when NotHealthy is called, if combined with other Probes.
    79  func (p *InstrumentationProbe) NotHealthy(err error) {
    80  	p.statusMetric.WithLabelValues(healthy).Set(0)
    81  	level.Info(p.logger).Log("msg", "changing probe status", "status", "not-healthy", "reason", err)
    82  }