예제 #1
0
파일: msg.go 프로젝트: ptqa/carbon-relay-ng
func CreateProbeEventMsg(event *schema.ProbeEvent, id int64, version Format) ([]byte, error) {
	if event.Id == "" {
		// per http://blog.mikemccandless.com/2014/05/choosing-fast-unique-identifier-uuid.html,
		// using V1 UUIDs is much faster than v4 like we were using
		u := uuid.NewUUID()
		event.Id = u.String()
	}
	buf := new(bytes.Buffer)
	err := binary.Write(buf, binary.LittleEndian, uint8(version))
	if err != nil {
		return nil, fmt.Errorf(errFmtBinWriteFailed, err)
	}
	err = binary.Write(buf, binary.BigEndian, id)
	if err != nil {
		return nil, fmt.Errorf(errFmtBinWriteFailed, err)
	}
	var msg []byte
	switch version {
	case FormatProbeEventJson:
		msg, err = json.Marshal(event)
	case FormatProbeEventMsgp:
		msg, err = event.MarshalMsg(nil)
	default:
		return nil, fmt.Errorf(errFmtUnknownFormat, version)
	}
	if err != nil {
		return nil, fmt.Errorf("Failed to marshal metrics payload: %s", err)
	}
	_, err = buf.Write(msg)
	if err != nil {
		return nil, fmt.Errorf(errFmtBinWriteFailed, err)
	}
	return buf.Bytes(), nil
}
예제 #2
0
파일: socketio.go 프로젝트: 0x20h/grafana
func (c *CollectorContext) OnEvent(msg *schema.ProbeEvent) {
	log.Info(fmt.Sprintf("received event from %s", c.Collector.Name))
	if !c.Collector.Public {
		msg.OrgId = c.OrgId
	}

	if err := collectoreventpublisher.Publish(msg); err != nil {
		log.Error(0, "failed to publish event.", err)
	}
}
예제 #3
0
func Save(e *schema.ProbeEvent) error {
	if e.Id == "" {
		u := uuid.NewRandom()
		e.Id = u.String()
	}
	if e.Timestamp == 0 {
		// looks like this expects timestamps in milliseconds
		e.Timestamp = time.Now().UnixNano() / int64(time.Millisecond)
	}
	if err := e.Validate(); err != nil {
		return err
	}
	log.Debug("saving event to elasticsearch.")
	resp, err := es.Index("events", e.EventType, e.Id, nil, e)
	log.Debug("elasticsearch response: %v", resp)
	if err != nil {
		return err
	}

	return nil
}