extractOpts extracts fields from a prometheus.*Opts composite literal.
(expr ast.Expr, decls declarations)
| 463 | |
| 464 | // extractOpts extracts fields from a prometheus.*Opts composite literal. |
| 465 | func extractOpts(expr ast.Expr, decls declarations) (metricOpts, bool) { |
| 466 | // Handle both direct composite literals and calls that return opts. |
| 467 | var lit *ast.CompositeLit |
| 468 | |
| 469 | switch e := expr.(type) { |
| 470 | case *ast.CompositeLit: |
| 471 | lit = e |
| 472 | case *ast.UnaryExpr: |
| 473 | // Handle &prometheus.GaugeOpts{...} |
| 474 | if l, ok := e.X.(*ast.CompositeLit); ok { |
| 475 | lit = l |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if lit == nil { |
| 480 | return metricOpts{}, false |
| 481 | } |
| 482 | |
| 483 | var opts metricOpts |
| 484 | for _, elt := range lit.Elts { |
| 485 | kv, ok := elt.(*ast.KeyValueExpr) |
| 486 | if !ok { |
| 487 | continue |
| 488 | } |
| 489 | |
| 490 | key, ok := kv.Key.(*ast.Ident) |
| 491 | if !ok { |
| 492 | continue |
| 493 | } |
| 494 | |
| 495 | value := resolveStringExpr(kv.Value, decls) |
| 496 | |
| 497 | switch key.Name { |
| 498 | case "Namespace": |
| 499 | opts.Namespace = value |
| 500 | case "Subsystem": |
| 501 | opts.Subsystem = value |
| 502 | case "Name": |
| 503 | opts.Name = value |
| 504 | case "Help": |
| 505 | opts.Help = value |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | return opts, opts.Name != "" |
| 510 | } |
| 511 | |
| 512 | // buildMetricName constructs the full metric name from namespace, subsystem, and name. |
| 513 | func buildMetricName(namespace, subsystem, name string) string { |
no test coverage detected