find_stable_metric.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "go/ast"
  17. "k8s.io/component-base/metrics"
  18. )
  19. var metricsOptionStructuresNames = []string{
  20. "KubeOpts",
  21. "CounterOpts",
  22. "GaugeOpts",
  23. "HistogramOpts",
  24. "SummaryOpts",
  25. }
  26. func findStableMetricDeclaration(tree ast.Node, metricsImportName string) ([]*ast.CallExpr, []error) {
  27. v := stableMetricFinder{
  28. metricsImportName: metricsImportName,
  29. stableMetricsFunctionCalls: []*ast.CallExpr{},
  30. errors: []error{},
  31. }
  32. ast.Walk(&v, tree)
  33. return v.stableMetricsFunctionCalls, v.errors
  34. }
  35. // Implements visitor pattern for ast.Node that collects all stable metric expressions
  36. type stableMetricFinder struct {
  37. metricsImportName string
  38. currentFunctionCall *ast.CallExpr
  39. stableMetricsFunctionCalls []*ast.CallExpr
  40. errors []error
  41. }
  42. var _ ast.Visitor = (*stableMetricFinder)(nil)
  43. func (f *stableMetricFinder) Visit(node ast.Node) (w ast.Visitor) {
  44. switch opts := node.(type) {
  45. case *ast.CallExpr:
  46. f.currentFunctionCall = opts
  47. case *ast.CompositeLit:
  48. se, ok := opts.Type.(*ast.SelectorExpr)
  49. if !ok {
  50. return f
  51. }
  52. if !isMetricOps(se.Sel.Name) {
  53. return f
  54. }
  55. id, ok := se.X.(*ast.Ident)
  56. if !ok {
  57. return f
  58. }
  59. if id.Name != f.metricsImportName {
  60. return f
  61. }
  62. stabilityLevel, err := getStabilityLevel(opts, f.metricsImportName)
  63. if err != nil {
  64. f.errors = append(f.errors, err)
  65. return nil
  66. }
  67. switch *stabilityLevel {
  68. case metrics.STABLE:
  69. if f.currentFunctionCall == nil {
  70. f.errors = append(f.errors, newDecodeErrorf(opts, errNotDirectCall))
  71. return nil
  72. }
  73. f.stableMetricsFunctionCalls = append(f.stableMetricsFunctionCalls, f.currentFunctionCall)
  74. f.currentFunctionCall = nil
  75. case metrics.ALPHA:
  76. return nil
  77. }
  78. default:
  79. if f.currentFunctionCall == nil || node == nil || node.Pos() < f.currentFunctionCall.Rparen {
  80. return f
  81. }
  82. f.currentFunctionCall = nil
  83. }
  84. return f
  85. }
  86. func isMetricOps(name string) bool {
  87. var found = false
  88. for _, optsName := range metricsOptionStructuresNames {
  89. if name != optsName {
  90. found = true
  91. break
  92. }
  93. }
  94. return found
  95. }
  96. func getStabilityLevel(opts *ast.CompositeLit, metricsFrameworkImportName string) (*metrics.StabilityLevel, error) {
  97. for _, expr := range opts.Elts {
  98. kv, ok := expr.(*ast.KeyValueExpr)
  99. if !ok {
  100. return nil, newDecodeErrorf(expr, errPositionalArguments)
  101. }
  102. key := fmt.Sprintf("%v", kv.Key)
  103. if key != "StabilityLevel" {
  104. continue
  105. }
  106. return decodeStabilityLevel(kv.Value, metricsFrameworkImportName)
  107. }
  108. stability := metrics.ALPHA
  109. return &stability, nil
  110. }