extractPromautoMetric extracts a metric from promauto.With().New*() or factory.New*() calls. Supported patterns: - promauto.With(reg).NewCounterVec(prometheus.CounterOpts{...}, labels) - factory.NewGaugeVec(prometheus.GaugeOpts{...}, labels) where factory := promauto.With(reg)
(call *ast.CallExpr, decls declarations)
| 625 | // - promauto.With(reg).NewCounterVec(prometheus.CounterOpts{...}, labels) |
| 626 | // - factory.NewGaugeVec(prometheus.GaugeOpts{...}, labels) where factory := promauto.With(reg) |
| 627 | func extractPromautoMetric(call *ast.CallExpr, decls declarations) (Metric, bool) { |
| 628 | sel, ok := call.Fun.(*ast.SelectorExpr) |
| 629 | if !ok { |
| 630 | return Metric{}, false |
| 631 | } |
| 632 | |
| 633 | funcName := sel.Sel.Name |
| 634 | metricType, isVec := parseMetricFuncName(funcName) |
| 635 | if metricType == "" { |
| 636 | return Metric{}, false |
| 637 | } |
| 638 | |
| 639 | // Check if this is a promauto call by examining the receiver. |
| 640 | if !isPromautoCall(sel.X) { |
| 641 | return Metric{}, false |
| 642 | } |
| 643 | |
| 644 | // Need at least one argument (the Opts struct). |
| 645 | if len(call.Args) < 1 { |
| 646 | return Metric{}, false |
| 647 | } |
| 648 | |
| 649 | // Extract metric info from the Opts struct. |
| 650 | opts, ok := extractOpts(call.Args[0], decls) |
| 651 | if !ok { |
| 652 | warnf("extractPromautoMetric: skipping promauto.%s() call: could not extract opts", funcName) |
| 653 | return Metric{}, false |
| 654 | } |
| 655 | |
| 656 | // Extract labels for Vec types. |
| 657 | var labels []string |
| 658 | if isVec && len(call.Args) >= 2 { |
| 659 | labels = extractLabels(call.Args[1], decls) |
| 660 | } |
| 661 | |
| 662 | // Build the full metric name. |
| 663 | name := buildMetricName(opts.Namespace, opts.Subsystem, opts.Name) |
| 664 | if name == "" { |
| 665 | warnf("extractPromautoMetric: skipping promauto.%s() call: could not build metric name", funcName) |
| 666 | return Metric{}, false |
| 667 | } |
| 668 | |
| 669 | return Metric{ |
| 670 | Name: name, |
| 671 | Type: metricType, |
| 672 | Help: opts.Help, |
| 673 | Labels: labels, |
| 674 | }, true |
| 675 | } |
| 676 | |
| 677 | // extractMetricFromCall attempts to extract a Metric from a function call expression. |
| 678 | // It returns the metric and true if successful, or an empty metric and false if |
no test coverage detected