func (carbon *CarbonHandler) dial(URL string) { for attempts := 0; ; attempts++ { carbon.sleep(attempts) conn, err := net.DialTimeout("tcp", URL, CarbonDialTimeout) if err == nil { klog.KPrintf("meter.carbon.dial.info", "connected to '%s'", URL) carbon.connC <- msgConn{URL, conn} return } klog.KPrintf("meter.carbon.dial.error", "unable to connect to '%s': %s", URL, err) } }
// Finish completes and returns the object. If errors were encountered during // loading, they're all returned here as type Errors. func (loader *Loader) Finish() (interface{}, error) { if loader.links != nil { for src, target := range loader.links { var value interface{} dst, err := loader.resolve(target, 0) klog.KPrintf("blueprint.loader.finish.debug", "src=%s, target=%s", src, dst) if err == nil { if value, err = dst.Get(loader.Values); err == nil && value == nil { err = fmt.Errorf("unable to link '%s' to nil value '%s'", src, dst) } } if err == nil { err = path.New(src).Set(loader.Values, value) } if err != nil { loader.ErrorAt(err, path.New(src)) } } } // Required otherwise we set the type param on the error interface which // makes the error non-nil. One of those fun parts of the go language. if loader.errors != nil { return nil, loader.errors } return loader.Values, nil }
// Link indicates that the object at the given src path should be equal to the // value at the given target path. All links are resolved when calling Finish // so there are no ordering constraints on links. func (loader *Loader) Link(src, target path.P) { klog.KPrintf("blueprint.loader.link.debug", "src=%s, target=%s", src, target) if loader.links == nil { loader.links = make(map[string]path.P) } loader.links[src.String()] = target }
// Type asserts the type of an object at the given path. This is useful when // dealing with interfaces which can't be pathed through unless they're // associated with a concrete type. func (loader *Loader) Type(src path.P, name string) { klog.KPrintf("blueprint.loader.type.debug", "src=%s, name=%s", src, name) if value, ok := New(name); !ok { loader.ErrorAt(fmt.Errorf("unknown type '%s'", name), src) } else if err := src.Set(loader.Values, value); err != nil { loader.ErrorAt(err, src) } }
func (carbon *CarbonHandler) send(values map[string]float64) { ts := time.Now().Unix() for URL, conn := range carbon.conns { if conn == nil { continue } if err := carbon.write(conn, values, ts); err != nil { klog.KPrintf("meter.carbon.send.error", "error when sending to '%s': %s", URL, err) carbon.connect(URL) } } }
// HandleMeters sends the given values to the configured remote HTTP endpoint. func (handler *HTTPHandler) HandleMeters(values map[string]float64) { handler.Init() var body struct { Timestamp int64 `json:"timestamp"` Values map[string]float64 `json:"values"` } body.Timestamp = time.Now().Unix() body.Values = values resp := handler.client.NewRequest(handler.Method).SetBody(body).Send() if err := resp.GetBody(nil); err != nil { klog.KPrintf("meter.http.send.error", "unable to send metrics: %s", err) } }
// Barebone basic usage example of klog. func Example_Simple() { // Default klog printer is a light wrapper around the standard log package // so we need to modify it a bit to make it work for our example. log.SetFlags(0) log.SetOutput(os.Stdout) log.SetPrefix("klog ") // KPrint and KPrintf are the only supported print functions where the first // argument is a key identifying the source of the log message. The key will // be used in future example to manipulate the log lines as they flow // through klog. klog.KPrint("a.b.c", "hello") klog.KPrintf("c.b.a", "%s", "world") // Output: // klog <a.b.c> hello // klog <c.b.a> world }
// Add sets the object at the given path to value. func (loader *Loader) Add(src path.P, value interface{}) { klog.KPrintf("blueprint.loader.add.debug", "src=%s, value={%T, %v}", src, value, value) err := src.Set(loader.Values, value) if err == nil { return } if err == path.ErrInvalidType { var typ reflect.Type if typ, err = src.Type(loader.Values); err == nil { if value, err = convert(typ, value); err == nil { err = src.Set(loader.Values, value) } } } loader.ErrorAt(err, src) }