github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/scrape/metrics.go (about)

     1  package scrape
     2  
     3  import (
     4  	"github.com/prometheus/client_golang/prometheus"
     5  	"github.com/prometheus/client_golang/prometheus/promauto"
     6  )
     7  
     8  type metrics struct {
     9  	pools             prometheus.Counter
    10  	poolsFailed       prometheus.Counter
    11  	poolReloads       prometheus.Counter
    12  	poolReloadsFailed prometheus.Counter
    13  
    14  	// Once pool exits, these metrics should be also unregistered.
    15  	// Metrics specific to jobs (pools).
    16  	poolReloadIntervalLength *prometheus.SummaryVec
    17  	poolSyncIntervalLength   *prometheus.SummaryVec
    18  	poolSyncs                *prometheus.CounterVec
    19  	poolSyncFailed           *prometheus.CounterVec
    20  	poolTargetsAdded         *prometheus.GaugeVec
    21  	// Metrics shared by scrape loops.
    22  	scrapes              *prometheus.CounterVec
    23  	scrapesFailed        *prometheus.CounterVec
    24  	scrapeIntervalLength *prometheus.SummaryVec
    25  	// Metrics specific to targets.
    26  	profileSize    *prometheus.SummaryVec
    27  	profileSamples *prometheus.SummaryVec
    28  	scrapeDuration *prometheus.SummaryVec
    29  }
    30  
    31  type poolMetrics struct {
    32  	poolSyncs                prometheus.Counter
    33  	poolSyncFailed           prometheus.Counter
    34  	poolReloadIntervalLength prometheus.Observer
    35  	poolSyncIntervalLength   prometheus.Observer
    36  	poolTargetsAdded         prometheus.Gauge
    37  
    38  	scrapes              prometheus.Counter
    39  	scrapesFailed        prometheus.Counter
    40  	scrapeIntervalLength prometheus.Observer
    41  }
    42  
    43  type targetMetrics struct {
    44  	profileSize    prometheus.Observer
    45  	profileSamples prometheus.Observer
    46  	scrapeDuration prometheus.Observer
    47  }
    48  
    49  func newMetrics(r prometheus.Registerer) *metrics {
    50  	poolLabels := []string{"scrape_job"}
    51  	targetLabels := []string{"scrape_job", "profile_path"}
    52  	return &metrics{
    53  		pools: promauto.With(r).NewCounter(prometheus.CounterOpts{
    54  			Name: "pyroscope_scrape_target_pools_total",
    55  			Help: "Total number of scrape pool creation attempts.",
    56  		}),
    57  		poolsFailed: promauto.With(r).NewCounter(prometheus.CounterOpts{
    58  			Name: "pyroscope_scrape_target_pools_failed_total",
    59  			Help: "Total number of scrape pool creations that failed.",
    60  		}),
    61  		poolReloads: promauto.With(r).NewCounter(prometheus.CounterOpts{
    62  			Name: "pyroscope_scrape_target_pool_reloads_total",
    63  			Help: "Total number of scrape pool reloads.",
    64  		}),
    65  		poolReloadsFailed: promauto.With(r).NewCounter(prometheus.CounterOpts{
    66  			Name: "pyroscope_scrape_target_pool_reloads_failed_total",
    67  			Help: "Total number of failed scrape pool reloads.",
    68  		}),
    69  
    70  		poolSyncs: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
    71  			Name: "pyroscope_scrape_target_pool_sync_total",
    72  			Help: "Total number of syncs that were executed on a scrape pool.",
    73  		}, poolLabels),
    74  		poolSyncFailed: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
    75  			Name: "pyroscope_scrape_target_pool_sync_failed_total",
    76  			Help: "Total number of target sync failures.",
    77  		}, poolLabels),
    78  		poolSyncIntervalLength: promauto.With(r).NewSummaryVec(prometheus.SummaryOpts{
    79  			Name:       "pyroscope_scrape_target_pool_sync_length_seconds",
    80  			Help:       "Actual interval to sync the scrape pool.",
    81  			Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
    82  		}, poolLabels),
    83  		poolReloadIntervalLength: promauto.With(r).NewSummaryVec(prometheus.SummaryOpts{
    84  			Name:       "pyroscope_scrape_target_pool_reload_length_seconds",
    85  			Help:       "Actual interval to reload the scrape pool with a given configuration.",
    86  			Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
    87  		}, poolLabels),
    88  		poolTargetsAdded: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{
    89  			Name: "pyroscope_scrape_target_pool_targets",
    90  			Help: "Current number of targets in this scrape pool.",
    91  		}, poolLabels),
    92  
    93  		scrapes: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
    94  			Name: "pyroscope_scrape_target_pool_scrapes_total",
    95  			Help: "Total number of scrapes that were executed on a scrape pool.",
    96  		}, poolLabels),
    97  		scrapesFailed: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
    98  			Name: "pyroscope_scrape_target_pool_scrapes_failed_total",
    99  			Help: "Total number of scrapes failed.",
   100  		}, poolLabels),
   101  		scrapeIntervalLength: promauto.With(r).NewSummaryVec(prometheus.SummaryOpts{
   102  			Name:       "pyroscope_scrape_target_pool_scrape_interval_length_seconds",
   103  			Help:       "Actual intervals between scrapes.",
   104  			Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
   105  		}, poolLabels),
   106  
   107  		profileSize: promauto.With(r).NewSummaryVec(prometheus.SummaryOpts{
   108  			Name:       "pyroscope_scrape_target_profile_size_bytes",
   109  			Help:       "Size of scraped profiles.",
   110  			Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
   111  		}, targetLabels),
   112  		profileSamples: promauto.With(r).NewSummaryVec(prometheus.SummaryOpts{
   113  			Name:       "pyroscope_scrape_target_profile_samples",
   114  			Help:       "Number of samples per profile.",
   115  			Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
   116  		}, targetLabels),
   117  		scrapeDuration: promauto.With(r).NewSummaryVec(prometheus.SummaryOpts{
   118  			Name:       "pyroscope_scrape_target_scrape_duration_seconds",
   119  			Help:       "Actual duration of profile scraping.",
   120  			Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
   121  		}, targetLabels),
   122  	}
   123  }
   124  
   125  func (m *metrics) poolMetrics(jobName string) *poolMetrics {
   126  	return &poolMetrics{
   127  		poolSyncs:                m.poolSyncs.WithLabelValues(jobName),
   128  		poolSyncFailed:           m.poolSyncFailed.WithLabelValues(jobName),
   129  		poolSyncIntervalLength:   m.poolSyncIntervalLength.WithLabelValues(jobName),
   130  		poolReloadIntervalLength: m.poolReloadIntervalLength.WithLabelValues(jobName),
   131  		poolTargetsAdded:         m.poolTargetsAdded.WithLabelValues(jobName),
   132  
   133  		scrapes:              m.scrapes.WithLabelValues(jobName),
   134  		scrapesFailed:        m.scrapesFailed.WithLabelValues(jobName),
   135  		scrapeIntervalLength: m.scrapeIntervalLength.WithLabelValues(jobName),
   136  	}
   137  }
   138  
   139  func (m *metrics) targetMetrics(jobName, profilePath string) *targetMetrics {
   140  	return &targetMetrics{
   141  		profileSize:    m.profileSize.WithLabelValues(jobName, profilePath),
   142  		profileSamples: m.profileSamples.WithLabelValues(jobName, profilePath),
   143  		scrapeDuration: m.scrapeDuration.WithLabelValues(jobName, profilePath),
   144  	}
   145  }