github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/metrics.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package db
    13  
    14  import (
    15  	"time"
    16  
    17  	"github.com/prometheus/client_golang/prometheus"
    18  	"github.com/sirupsen/logrus"
    19  	"github.com/weaviate/weaviate/usecases/monitoring"
    20  )
    21  
    22  type Metrics struct {
    23  	logger                logrus.FieldLogger
    24  	monitoring            bool
    25  	batchTime             prometheus.ObserverVec
    26  	batchDeleteTime       prometheus.ObserverVec
    27  	objectTime            prometheus.ObserverVec
    28  	startupDurations      prometheus.ObserverVec
    29  	filteredVectorFilter  prometheus.Observer
    30  	filteredVectorVector  prometheus.Observer
    31  	filteredVectorObjects prometheus.Observer
    32  	filteredVectorSort    prometheus.Observer
    33  	grouped               bool
    34  	baseMetrics           *monitoring.PrometheusMetrics
    35  }
    36  
    37  func NewMetrics(
    38  	logger logrus.FieldLogger, prom *monitoring.PrometheusMetrics,
    39  	className, shardName string,
    40  ) *Metrics {
    41  	m := &Metrics{
    42  		logger: logger,
    43  	}
    44  
    45  	if prom == nil {
    46  		return m
    47  	}
    48  
    49  	m.baseMetrics = prom
    50  
    51  	if prom.Group {
    52  		className = "n/a"
    53  		shardName = "n/a"
    54  		m.grouped = true
    55  	}
    56  
    57  	m.monitoring = true
    58  	m.batchTime = prom.BatchTime.MustCurryWith(prometheus.Labels{
    59  		"class_name": className,
    60  		"shard_name": shardName,
    61  	})
    62  	m.batchDeleteTime = prom.BatchDeleteTime.MustCurryWith(prometheus.Labels{
    63  		"class_name": className,
    64  		"shard_name": shardName,
    65  	})
    66  	m.objectTime = prom.ObjectsTime.MustCurryWith(prometheus.Labels{
    67  		"class_name": className,
    68  		"shard_name": shardName,
    69  	})
    70  	m.startupDurations = prom.StartupDurations.MustCurryWith(prometheus.Labels{
    71  		"class_name": className,
    72  		"shard_name": shardName,
    73  	})
    74  
    75  	m.filteredVectorFilter = prom.QueriesFilteredVectorDurations.With(prometheus.Labels{
    76  		"class_name": className,
    77  		"shard_name": shardName,
    78  		"operation":  "filter",
    79  	})
    80  
    81  	m.filteredVectorVector = prom.QueriesFilteredVectorDurations.With(prometheus.Labels{
    82  		"class_name": className,
    83  		"shard_name": shardName,
    84  		"operation":  "vector",
    85  	})
    86  
    87  	m.filteredVectorObjects = prom.QueriesFilteredVectorDurations.With(prometheus.Labels{
    88  		"class_name": className,
    89  		"shard_name": shardName,
    90  		"operation":  "objects",
    91  	})
    92  
    93  	m.filteredVectorSort = prom.QueriesFilteredVectorDurations.With(prometheus.Labels{
    94  		"class_name": className,
    95  		"shard_name": shardName,
    96  		"operation":  "sort",
    97  	})
    98  
    99  	return m
   100  }
   101  
   102  func (m *Metrics) DeleteShardLabels(class, shard string) {
   103  	if m.grouped {
   104  		// never delete the shared label, only individual ones
   105  		return
   106  	}
   107  
   108  	m.baseMetrics.DeleteShard(class, shard)
   109  }
   110  
   111  func (m *Metrics) BatchObject(start time.Time, size int) {
   112  	took := time.Since(start)
   113  	m.logger.WithField("action", "batch_objects").
   114  		WithField("batch_size", size).
   115  		WithField("took", took).
   116  		Tracef("object batch took %s", took)
   117  }
   118  
   119  func (m *Metrics) ObjectStore(start time.Time) {
   120  	took := time.Since(start)
   121  	m.logger.WithField("action", "store_object_store").
   122  		WithField("took", took).
   123  		Tracef("storing objects in KV/inverted store took %s", took)
   124  
   125  	if !m.monitoring {
   126  		return
   127  	}
   128  
   129  	m.batchTime.With(prometheus.Labels{"operation": "object_storage"}).
   130  		Observe(float64(took / time.Millisecond))
   131  }
   132  
   133  func (m *Metrics) VectorIndex(start time.Time) {
   134  	took := time.Since(start)
   135  	m.logger.WithField("action", "store_vector_index").
   136  		WithField("took", took).
   137  		Tracef("storing objects vector index took %s", took)
   138  
   139  	if !m.monitoring {
   140  		return
   141  	}
   142  
   143  	m.batchTime.With(prometheus.Labels{"operation": "vector_storage"}).
   144  		Observe(float64(took / time.Millisecond))
   145  }
   146  
   147  func (m *Metrics) PutObject(start time.Time) {
   148  	took := time.Since(start)
   149  	m.logger.WithField("action", "store_object_store_single_object_in_tx").
   150  		WithField("took", took).
   151  		Tracef("storing single object (complete) in KV/inverted took %s", took)
   152  
   153  	if !m.monitoring {
   154  		return
   155  	}
   156  
   157  	m.objectTime.With(prometheus.Labels{
   158  		"operation": "put",
   159  		"step":      "total",
   160  	}).Observe(float64(took) / float64(time.Millisecond))
   161  }
   162  
   163  func (m *Metrics) PutObjectDetermineStatus(start time.Time) {
   164  	took := time.Since(start)
   165  	m.logger.WithField("action", "store_object_store_determine_status").
   166  		WithField("took", took).
   167  		Tracef("retrieving previous and determining status in KV took %s", took)
   168  
   169  	if !m.monitoring {
   170  		return
   171  	}
   172  
   173  	m.objectTime.With(prometheus.Labels{
   174  		"operation": "put",
   175  		"step":      "retrieve_previous_determine_status",
   176  	}).Observe(float64(took) / float64(time.Millisecond))
   177  }
   178  
   179  func (m *Metrics) PutObjectUpsertObject(start time.Time) {
   180  	took := time.Since(start)
   181  	m.logger.WithField("action", "store_object_store_upsert_object_data").
   182  		WithField("took", took).
   183  		Tracef("storing object data in KV took %s", took)
   184  
   185  	if !m.monitoring {
   186  		return
   187  	}
   188  
   189  	m.objectTime.With(prometheus.Labels{
   190  		"operation": "put",
   191  		"step":      "upsert_object_store",
   192  	}).Observe(float64(took) / float64(time.Millisecond))
   193  }
   194  
   195  func (m *Metrics) PutObjectUpdateInverted(start time.Time) {
   196  	took := time.Since(start)
   197  	m.logger.WithField("action", "store_object_store_update_inverted").
   198  		WithField("took", took).
   199  		Tracef("updating inverted index for single object took %s", took)
   200  
   201  	if !m.monitoring {
   202  		return
   203  	}
   204  
   205  	m.objectTime.With(prometheus.Labels{
   206  		"operation": "put",
   207  		"step":      "inverted_total",
   208  	}).Observe(float64(took) / float64(time.Millisecond))
   209  }
   210  
   211  func (m *Metrics) InvertedDeleteOld(start time.Time) {
   212  	took := time.Since(start)
   213  	m.logger.WithField("action", "inverted_delete_old").
   214  		WithField("took", took).
   215  		Tracef("deleting old entries from inverted index %s", took)
   216  	if !m.monitoring {
   217  		return
   218  	}
   219  
   220  	m.objectTime.With(prometheus.Labels{
   221  		"operation": "put",
   222  		"step":      "inverted_delete",
   223  	}).Observe(float64(took) / float64(time.Millisecond))
   224  }
   225  
   226  func (m *Metrics) InvertedDeleteDelta(start time.Time) {
   227  	took := time.Since(start)
   228  	m.logger.WithField("action", "inverted_delete_delta").
   229  		WithField("took", took).
   230  		Tracef("deleting delta entries from inverted index %s", took)
   231  }
   232  
   233  func (m *Metrics) InvertedExtend(start time.Time, propCount int) {
   234  	took := time.Since(start)
   235  	m.logger.WithField("action", "inverted_extend").
   236  		WithField("took", took).
   237  		WithField("prop_count", propCount).
   238  		Tracef("extending inverted index took %s", took)
   239  
   240  	if !m.monitoring {
   241  		return
   242  	}
   243  
   244  	m.objectTime.With(prometheus.Labels{
   245  		"operation": "put",
   246  		"step":      "inverted_extend",
   247  	}).Observe(float64(took) / float64(time.Millisecond))
   248  }
   249  
   250  func (m *Metrics) ShardStartup(start time.Time) {
   251  	if !m.monitoring {
   252  		return
   253  	}
   254  
   255  	took := time.Since(start)
   256  	m.startupDurations.With(prometheus.Labels{
   257  		"operation": "shard_total_init",
   258  	}).Observe(float64(took) / float64(time.Millisecond))
   259  }
   260  
   261  func (m *Metrics) BatchDelete(start time.Time, op string) {
   262  	if !m.monitoring {
   263  		return
   264  	}
   265  
   266  	took := time.Since(start)
   267  	m.batchDeleteTime.With(prometheus.Labels{
   268  		"operation": op,
   269  	}).Observe(float64(took) / float64(time.Millisecond))
   270  }
   271  
   272  func (m *Metrics) FilteredVectorFilter(dur time.Duration) {
   273  	if !m.monitoring {
   274  		return
   275  	}
   276  
   277  	m.filteredVectorFilter.Observe(float64(dur) / float64(time.Millisecond))
   278  }
   279  
   280  func (m *Metrics) FilteredVectorVector(dur time.Duration) {
   281  	if !m.monitoring {
   282  		return
   283  	}
   284  
   285  	m.filteredVectorVector.Observe(float64(dur) / float64(time.Millisecond))
   286  }
   287  
   288  func (m *Metrics) FilteredVectorObjects(dur time.Duration) {
   289  	if !m.monitoring {
   290  		return
   291  	}
   292  
   293  	m.filteredVectorObjects.Observe(float64(dur) / float64(time.Millisecond))
   294  }
   295  
   296  func (m *Metrics) FilteredVectorSort(dur time.Duration) {
   297  	if !m.monitoring {
   298  		return
   299  	}
   300  
   301  	m.filteredVectorSort.Observe(float64(dur) / float64(time.Millisecond))
   302  }