(node ast.Node)
| 71 | } |
| 72 | |
| 73 | func (c *codegen) VisitBefore(node ast.Node) (ast.Visitor, ast.Node) { |
| 74 | switch n := node.(type) { |
| 75 | |
| 76 | case *ast.VarDecl: |
| 77 | var name string |
| 78 | if n.ExportedName != "" { |
| 79 | name = n.ExportedName |
| 80 | } else { |
| 81 | name = n.Name |
| 82 | } |
| 83 | // If the Type is not in the map, then default to metrics.Int. This is |
| 84 | // a hack for metrics that no type can be inferred, retaining |
| 85 | // historical behaviour. |
| 86 | t := n.Type() |
| 87 | if types.IsDimension(t) { |
| 88 | t = t.(*types.Operator).Args[len(t.(*types.Operator).Args)-1] |
| 89 | } |
| 90 | var dtyp metrics.Type |
| 91 | switch { |
| 92 | case types.Equals(types.Float, t): |
| 93 | dtyp = metrics.Float |
| 94 | case types.Equals(types.String, t): |
| 95 | dtyp = metrics.String |
| 96 | case types.Equals(types.Buckets, t): |
| 97 | dtyp = metrics.Buckets |
| 98 | default: |
| 99 | if !types.IsComplete(t) { |
| 100 | glog.Infof("Incomplete type %v for %#v", t, n) |
| 101 | } |
| 102 | dtyp = metrics.Int |
| 103 | } |
| 104 | m := metrics.NewMetric(name, c.name, n.Kind, dtyp, n.Keys...) |
| 105 | m.SetSource(n.Pos().String()) |
| 106 | // Scalar counters can be initialized to zero. Dimensioned counters we |
| 107 | // don't know the values of the labels yet. Gauges and Timers we can't |
| 108 | // assume start at zero. |
| 109 | if len(n.Keys) == 0 && n.Kind == metrics.Counter { |
| 110 | // Calling GetDatum here causes the storage to be allocated. |
| 111 | d, err := m.GetDatum() |
| 112 | if err != nil { |
| 113 | c.errorf(n.Pos(), "%s", err) |
| 114 | return nil, n |
| 115 | } |
| 116 | // Initialize to zero at the zero time. |
| 117 | switch dtyp { |
| 118 | case metrics.Int: |
| 119 | datum.SetInt(d, 0, time.Unix(0, 0)) |
| 120 | case metrics.Float: |
| 121 | datum.SetFloat(d, 0, time.Unix(0, 0)) |
| 122 | default: |
| 123 | c.errorf(n.Pos(), "Can't initialize to zero a %#v", n) |
| 124 | return nil, n |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if n.Kind == metrics.Histogram { |
| 129 | if len(n.Buckets) < 2 { |
| 130 | c.errorf(n.Pos(), "a histogram need at least two boundaries") |
nothing calls this directly
no test coverage detected