···174174175175 cmd = exec.CommandContext(ctx, "git", fetchArgs...)
176176 cmd.Stdin = &bytes.Buffer{}
177177- if err := runCmd(cmd); err != nil {
177177+178178+ err = runCmd(cmd)
179179+ fetchDuration := time.Since(fetchStart)
180180+ if err != nil {
181181+ metricFetchDuration.WithLabelValues("false", repoNameForMetric(o.Name)).Observe(fetchDuration.Seconds())
178182 return err
179183 }
180184181181- debug.Printf("fetched git data for %q (%d commit(s)) in %s", o.Name, len(commits), time.Since(fetchStart))
185185+ metricFetchDuration.WithLabelValues("true", repoNameForMetric(o.Name)).Observe(fetchDuration.Seconds())
186186+ debug.Printf("fetched git data for %q (%d commit(s)) in %s", o.Name, len(commits), fetchDuration)
182187183188 // We then create the relevant refs for each fetched commit.
184189 for _, b := range o.Branches {
+26-9
cmd/zoekt-sourcegraph-indexserver/main.go
···6868 "name", // name of the repository that was indexed
6969 })
70707171+ metricFetchDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
7272+ Name: "index_fetch_seconds",
7373+ Help: "A histogram of latencies for fetching a repository.",
7474+ Buckets: []float64{.05, .1, .25, .5, 1, 2.5, 5, 10, 20, 30, 60, 180, 300, 600}, // 50ms -> 10 minutes
7575+ }, []string{
7676+ "success", // true|false
7777+ "name", // the name of the repository that the commits were fetched from
7878+ })
7979+7180 metricIndexIncrementalIndexState = promauto.NewCounterVec(prometheus.CounterOpts{
7281 Name: "index_incremental_index_state",
7382 Help: "A count of the state on disk vs what we want to build. See zoekt/build.IndexState.",
···351360 state, err := s.Index(args)
352361 s.muIndexDir.Unlock()
353362354354- // Check to see if we want to be able to capture separate indexing metrics for this repository.
355355- // If we don't, set to a default string to keep the cardinality for the Prometheus metric manageable.
356356- repoNameForMetric := ""
357357- if _, ok = reposWithSeparateIndexingMetrics[opts.Name]; ok {
358358- repoNameForMetric = opts.Name
359359- }
363363+ elapsed := time.Since(start)
360364361361- metricIndexDuration.WithLabelValues(string(state), repoNameForMetric).Observe(time.Since(start).Seconds())
365365+ metricIndexDuration.WithLabelValues(string(state), repoNameForMetric(opts.Name)).Observe(elapsed.Seconds())
362366363367 if err != nil {
364368 log.Printf("error indexing %s: %s", args.String(), err)
365369 }
370370+366371 switch state {
367372 case indexStateSuccess:
368368- log.Printf("updated index %s in %v", args.String(), time.Since(start))
373373+ log.Printf("updated index %s in %v", args.String(), elapsed)
369374 case indexStateSuccessMeta:
370370- log.Printf("updated meta %s in %v", args.String(), time.Since(start))
375375+ log.Printf("updated meta %s in %v", args.String(), elapsed)
371376 }
372377 s.queue.SetIndexed(opts, state)
373378 }
379379+}
380380+381381+// repoNameForMetric returns a normalized version of the given repository name that is
382382+// suitable for use with Prometheus metrics.
383383+func repoNameForMetric(repo string) string {
384384+ // Check to see if we want to be able to capture separate indexing metrics for this repository.
385385+ // If we don't, set to a default string to keep the cardinality for the Prometheus metric manageable.
386386+ if _, ok := reposWithSeparateIndexingMetrics[repo]; ok {
387387+ return repo
388388+ }
389389+390390+ return ""
374391}
375392376393func batched(slice []uint32, size int) <-chan []uint32 {