Exemple #1
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())
		}
	}
}
Exemple #2
0
// ParsePickle ...
func ParsePickle(pkt []byte) ([]*Points, error) {
	result, err := stalecucumber.Unpickle(bytes.NewReader(pkt))

	list, err := stalecucumber.ListOrTuple(result, err)
	if err != nil {
		return nil, err
	}

	msgs := []*Points{}
	for i := 0; i < len(list); i++ {
		metric, err := stalecucumber.ListOrTuple(list[i], nil)
		if err != nil {
			return nil, err
		}

		if len(metric) < 2 {
			return nil, errors.New("Unexpected array length while unpickling metric")
		}

		name, err := stalecucumber.String(metric[0], nil)
		if err != nil {
			return nil, err
		}

		msg := New()
		msg.Metric = name

		for j := 1; j < len(metric); j++ {
			v, err := stalecucumber.ListOrTuple(metric[j], nil)
			if err != nil {
				return nil, err
			}
			if len(v) != 2 {
				return nil, errors.New("Unexpected array length while unpickling data point")
			}
			timestamp, err := stalecucumber.Int(v[0], nil)
			if err != nil {
				timestampFloat, err := stalecucumber.Float(v[0], nil)
				if err != nil {
					return nil, err
				}
				timestamp = int64(timestampFloat)
			}
			if timestamp > math.MaxUint32 || timestamp < 0 {
				err = errors.New("Unexpected value for timestamp, cannot be cast to uint32")
				return nil, err
			}

			value, err := stalecucumber.Float(v[1], nil)
			if err != nil {
				valueInt, err := stalecucumber.Int(v[1], nil)
				if err != nil {
					return nil, err
				}
				value = float64(valueInt)
			}

			msg.Add(value, timestamp)
		}
		msgs = append(msgs, msg)
	}
	return msgs, nil
}