func handler(w http.ResponseWriter, r *http.Request, client *statsd.Client) {
	if r.Method != "POST" {
		log.Fatal("Expected POST request")
	}

	fmt.Fprintf(w, "Thanks!")

	buf := new(bytes.Buffer)
	buf.ReadFrom(r.Body)
	var ec []map[string]interface{}

	if err := json.Unmarshal(buf.Bytes(), &ec); err != nil {
		log.Fatal(err)
	}
	for _, item := range ec {
		err := client.Inc("mail.event_type."+item["event"].(string), 1, 1.0)
		// handle any errors
		if err != nil {
			log.Fatalf("Error sending metric: %+v", err)
		}
		err = client.Inc("mail.event", 1, 1.0)
		// handle any errors
		if err != nil {
			log.Fatalf("Error sending metric: %+v", err)
		}

		log.Printf("%s(%s): %s\n", item["email"], time.Unix(int64(item["timestamp"].(float64)), 0), item["event"])
	}
}
Example #2
0
func test(target string, s *statsd.Client) {
	pre := time.Now()
	conn, err := net.Dial("tcp", target)
	if err != nil {
		fmt.Println("connect error", target)
		s.Inc(fmt.Sprintf("error.%s.dial_failed", target), 1, 1)
		return
	}
	duration := time.Since(pre)
	tuple := strings.Split(target, ":")
	host := strings.Replace(tuple[0], ".", "_", -1)
	port := tuple[1]
	ms := int64(duration / time.Millisecond)
	fmt.Printf("%s.%s.duration %d\n", host, port, ms)
	s.Timing(fmt.Sprintf("dial.%s.%s", host, port), ms, 1)
	conn.Close()
}