Пример #1
0
// Handles incoming requests for both TCP and UDP
func handleGraphiteTextProtocol(rcvr *receiver.Receiver, conn net.Conn, timeout int) {

	defer conn.Close() // decrements graceful.TcpWg

	if timeout != 0 {
		conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
	}

	// We use the Scanner, becase it has a MaxScanTokenSize of 64K

	connbuf := bufio.NewScanner(conn)

	for connbuf.Scan() {
		packetStr := connbuf.Text()

		if name, ts, v, err := parseGraphitePacket(packetStr); err != nil {
			log.Printf("handleGraphiteTextProtocol(): bad backet: %v")
		} else {
			rcvr.QueueDataPoint(name, ts, v)
		}

		if timeout != 0 {
			conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
		}
	}

	if err := connbuf.Err(); err != nil {
		if !strings.Contains(err.Error(), "use of closed") {
			log.Println("handleGraphiteTextProtocol(): Error reading: %v", err)
		}
	}
}
Пример #2
0
func handleGraphitePickleProtocol(rcvr *receiver.Receiver, conn net.Conn, timeout int) {

	defer conn.Close() // decrements graceful.TcpWg

	if timeout != 0 {
		conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
	}

	// We use the Scanner, becase it has a MaxScanTokenSize of 64K

	var (
		name                 string
		tstamp               int64
		int_value            int64
		value                float64
		err                  error
		item                 interface{}
		items, itemSlice, dp []interface{}
	)

	items, err = pickle.ListOrTuple(pickle.Unpickle(conn))
	if err == nil {
		for _, item = range items {
			itemSlice, err = pickle.ListOrTuple(item, err)
			if len(itemSlice) == 2 {
				name, err = pickle.String(itemSlice[0], err)
				dp, err = pickle.ListOrTuple(itemSlice[1], err)
				if len(dp) == 2 {
					tstamp, err = pickle.Int(dp[0], err)
					if value, err = pickle.Float(dp[1], err); err != nil {
						if _, ok := err.(pickle.WrongTypeError); ok {
							if int_value, err = pickle.Int(dp[1], nil); err == nil {
								value = float64(int_value)
							}
						}
					}
					rcvr.QueueDataPoint(name, time.Unix(tstamp, 0), value)
				} else {
					err = fmt.Errorf("dp wrong length: %d", len(dp))
					break
				}
			} else {
				err = fmt.Errorf("item wrong length: %d", len(itemSlice))
				break
			}
		}
	}

	if timeout != 0 {
		conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
	}

	if err != nil {
		if !strings.Contains(err.Error(), "use of closed") {
			log.Println("handleGraphitePickleProtocol(): Error reading: %v", err.Error())
		}
	}
}
Пример #3
0
func PixelHandler(rcvr *receiver.Receiver) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if rc := recover(); rc != nil {
				log.Printf("PixelHandler: Recovered (this request is dropped): %v", rc)
			}
		}()

		sendPixel(w)

		err := r.ParseForm()
		if err != nil {
			log.Printf("PixelHandler: error from ParseForm(): %v", err)
			return
		}

		for name, vals := range r.Form {

			// foo.bar.baz=12.345@1425959940

			for _, valStr := range vals {

				var val, ut float64
				n, _ := fmt.Sscanf(valStr, "%f@%f", &val, &ut)
				if n < 1 {
					log.Printf("PixelHandler: error parsing %q", valStr)
					return
				}

				var ts time.Time
				if ut == 0 {
					ts = time.Now()
				} else {
					nsec := int64(ut*1000000000) % 1000000000
					ts = time.Unix(int64(ut), nsec)
				}

				rcvr.QueueDataPoint(misc.SanitizeName(name), ts, val)
			}
		}

	}
}