MCPcopy Create free account
hub / github.com/coder/coder / extractOptsMetric

Function extractOptsMetric

scripts/metricsdocgen/scanner/scanner.go:539–590  ·  view source on GitHub ↗

extractOptsMetric extracts a metric from prometheus.New*() or prometheus.New*Vec() calls. Supported patterns: - prometheus.NewGauge(prometheus.GaugeOpts{...}) - prometheus.NewCounter(prometheus.CounterOpts{...}) - prometheus.NewHistogram(prometheus.HistogramOpts{...}) - prometheus.NewSummary(prometh

(call *ast.CallExpr, decls declarations)

Source from the content-addressed store, hash-verified

537// - prometheus.NewHistogramVec(prometheus.HistogramOpts{...}, labels)
538// - prometheus.NewSummaryVec(prometheus.SummaryOpts{...}, labels)
539func extractOptsMetric(call *ast.CallExpr, decls declarations) (Metric, bool) {
540 sel, ok := call.Fun.(*ast.SelectorExpr)
541 if !ok {
542 return Metric{}, false
543 }
544
545 // Match calls that are exactly "prometheus.New*(...)". This checks the local
546 // package identifier, not the resolved import path. If the prometheus package
547 // is imported with an alias, this will not match.
548 ident, ok := sel.X.(*ast.Ident)
549 if !ok || ident.Name != "prometheus" {
550 return Metric{}, false
551 }
552
553 funcName := sel.Sel.Name
554 metricType, isVec := parseMetricFuncName(funcName)
555 if metricType == "" {
556 return Metric{}, false
557 }
558
559 // Need at least one argument (the Opts struct).
560 if len(call.Args) < 1 {
561 return Metric{}, false
562 }
563
564 // Extract metric info from the Opts struct.
565 opts, ok := extractOpts(call.Args[0], decls)
566 if !ok {
567 warnf("extractOptsMetric: skipping prometheus.%s() call: could not extract opts", funcName)
568 return Metric{}, false
569 }
570
571 // Extract labels for Vec types.
572 var labels []string
573 if isVec && len(call.Args) >= 2 {
574 labels = extractLabels(call.Args[1], decls)
575 }
576
577 // Build the full metric name.
578 name := buildMetricName(opts.Namespace, opts.Subsystem, opts.Name)
579 if name == "" {
580 warnf("extractOptsMetric: skipping prometheus.%s() call: could not build metric name", funcName)
581 return Metric{}, false
582 }
583
584 return Metric{
585 Name: name,
586 Type: metricType,
587 Help: opts.Help,
588 Labels: labels,
589 }, true
590}
591
592// isPromautoCall checks if an expression is a promauto factory call.
593// Matches:

Callers 1

extractMetricFromCallFunction · 0.85

Calls 5

parseMetricFuncNameFunction · 0.85
extractOptsFunction · 0.85
extractLabelsFunction · 0.85
buildMetricNameFunction · 0.85
warnfFunction · 0.70

Tested by

no test coverage detected