func roundFloat64(x exact.Value) exact.Value { f, _ := exact.Float64Val(x) if !math.IsInf(f, 0) { return exact.MakeFloat64(f) } return nil }
func roundFloat32(x exact.Value) exact.Value { f32, _ := exact.Float32Val(x) f := float64(f32) if !math.IsInf(f, 0) { return exact.MakeFloat64(f) } return nil }
func TestNewScopeAndPackage2(t *testing.T) { c := initTestChecker() c.NewScopeAndPackage() c.sc.Insert(types.NewConst(0, c.pkg, "testfloat1", types.New("float64"), exact.MakeFloat64(1))) c.NewScopeAndPackage() for _, name := range c.sc.Names() { if name == "testfloat1" { t.Errorf("scope not reset") } } o := c.sc.Insert(types.NewConst(0, c.pkg, "testfloat1", types.New("float64"), exact.MakeFloat64(2))) if o != nil { t.Errorf("did not reset scope") } }
//insertMetricValues inserts the values and rates of the metrics collected // as constants into the scope used to evaluate the expressions func (c *checker) InsertMetricValuesFromJSON() error { //get metrics from json package //TODO: get directly from metric context if available resp, err := http.Get("http://" + c.hostport + "/api/v1/metrics.json/") if err != nil { return err } defer resp.Body.Close() d := json.NewDecoder(resp.Body) var metrics []metrics.MetricJSON err = d.Decode(&metrics) if err != nil { return err } //insert metric value into scope for _, m := range metrics { switch val := m.Value.(type) { case float64: name := strings.Replace(m.Name, ".", "_", -1) + "_value" c.sc.Insert(types.NewConst(0, c.pkg, name, types.New("float64"), exact.MakeFloat64(val))) case map[string]interface{}: //TODO: make sure we don't panic in case something is not formatted // like expected if current, ok := val["current"]; ok { name := strings.Replace(m.Name, ".", "_", -1) + "_current" c.sc.Insert(types.NewConst(0, c.pkg, name, types.New("float64"), exact.MakeFloat64(current.(float64)))) } if rate, ok := val["rate"]; ok { name := strings.Replace(m.Name, ".", "_", -1) + "_rate" c.sc.Insert(types.NewConst(0, c.pkg, name, types.New("float64"), exact.MakeFloat64(rate.(float64)))) } default: //a value type came up that wasn't anticipated fmt.Fprintln(os.Stderr, reflect.TypeOf(val)) } } return nil }
func (c *checker) InsertMetricValuesFromContext(m *metrics.MetricContext) error { for metricName, metric := range m.Gauges { name := strings.Replace(metricName, ".", "_", -1) + "_value" c.sc.Insert(types.NewConst(0, c.pkg, name, types.New("float64"), exact.MakeFloat64(metric.Get()))) sname := name + "_string" c.sc.Insert(types.NewConst(0, c.pkg, sname, types.New("string"), exact.MakeString(fmt.Sprintf("%0.2f", metric.Get())))) } for metricName, metric := range m.Counters { name := strings.Replace(metricName, ".", "_", -1) + "_current" c.sc.Insert(types.NewConst(0, c.pkg, name, types.New("float64"), exact.MakeUint64(metric.Get()))) sname := name + "_string" c.sc.Insert(types.NewConst(0, c.pkg, sname, types.New("string"), exact.MakeString(fmt.Sprintf("%d", metric.Get())))) name = strings.Replace(metricName, ".", "_", -1) + "_rate" c.sc.Insert(types.NewConst(0, c.pkg, name, types.New("float64"), exact.MakeFloat64(metric.ComputeRate()))) } return nil }