Ejemplo n.º 1
0
Archivo: render.go Proyecto: kuba--/yag
/*
 * JSON Response:
 * [
 *  {"target": "status.200", "datapoints": [[1720.0, 1370846820], ...], },
 *  {"target": "status.204", "datapoints": [[null, 1370846820], ..., ]}
 * ]s
 */
func (h *RenderHandler) jsonResponse(w http.ResponseWriter, data interface{}) {
	if m, ok := data.([]*metrics.Metrics); ok {
		w.WriteHeader(http.StatusOK)

		fmt.Fprintf(w, "%s([", h.jsonp)
		for i, mi := range m {
			if i > 0 {
				fmt.Fprintf(w, ",")
			}

			fmt.Fprintf(w, `{"target":"%s","datapoints":[`, mi.Target)
			for ii := 0; ii < len(mi.Datapoints); ii++ {
				if ii > 0 {
					fmt.Fprintf(w, ",")
				}

				val := "null"
				if mi.Datapoints[ii][0] != nil {
					val = fmt.Sprintf("%.2f", *mi.Datapoints[ii][0])
				}

				fmt.Fprintf(w, "[%s, %.0f]", val, *mi.Datapoints[ii][1])
			}
			fmt.Fprintf(w, "]}")
		}
		fmt.Fprintf(w, "])")
		glog.Infof("%v\n", data)
	} else {
		glog.Errorf("%v\n", data)
		http.Error(w, fmt.Sprintf("%v", data), http.StatusBadRequest)
	}
}
Ejemplo n.º 2
0
func main() {
	if config.Pprof.Cpu != "" {
		// Start Profiler
		f, err := os.Create(config.Pprof.Cpu)
		if err != nil {
			glog.Fatal(err)
		}
		defer f.Close()

		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	http.HandleFunc("/render", render.Handler)

	glog.Infoln("ListenAndServe", config.Cfg.Webserver.Addr)
	go func() {
		glog.Fatal(http.ListenAndServe(config.Cfg.Webserver.Addr, nil))
	}()

	// Handle SIGINT and SIGTERM.
	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt, os.Kill)
	glog.Errorf("Syscall: %v - Exit\n", <-sig)

	if config.Pprof.Mem != "" {
		// Start Mem Profiler
		f, err := os.Create(config.Pprof.Mem)
		if err != nil {
			glog.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
	}
}
Ejemplo n.º 3
0
func main() {
	if config.Pprof.Cpu != "" {
		// Start Cpu Profiler
		f, err := os.Create(config.Pprof.Cpu)
		if err != nil {
			glog.Fatal(err)
		}
		defer f.Close()

		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	ln, err := net.Listen("tcp", config.Cfg.Listener.Addr)
	if err != nil {
		glog.Fatalln(err)
	}
	glog.Infoln("ListenAndServe", config.Cfg.Listener.Addr)
	go func() {
		for {
			conn, err := ln.Accept()
			if err != nil {
				glog.Errorln(err)
				continue
			}
			go handle(conn)
		}
	}()

	// Handle SIGINT and SIGTERM.
	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt, os.Kill)
	glog.Errorf("Syscall: %v - Exit\n", <-sig)

	if config.Pprof.Mem != "" {
		// Start Mem Profiler
		f, err := os.Create(config.Pprof.Mem)
		if err != nil {
			glog.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
	}
}