Beispiel #1
0
func Publish(event *schema.ProbeEvent) error {
	if globalProducer == nil {
		log.Debug("droping event as publishing is disbaled")
		return nil
	}

	id := time.Now().UnixNano()
	data, err := msg.CreateProbeEventMsg(event, id, msg.FormatProbeEventMsgp)
	if err != nil {
		log.Fatal(4, "Fatal error creating event message: %s", err)
	}
	eventsPublished.Inc(1)
	messagesSize.Value(int64(len(data)))
	pre := time.Now()
	err = globalProducer.Publish(topic, data)
	publishDuration.Value(time.Since(pre))
	if err != nil {
		log.Fatal(4, "can't publish to nsqd: %s", err)
	}
	log.Debug("published event %d", id)

	return nil
}
func sendEvent(orgId int64, m *plugin.MetricType) {
	ns := m.Namespace().Strings()
	if len(ns) != 4 {
		log.Printf("Error: invalid event metric. Expected namesapce to be 4 fields.")
		return
	}
	if ns[0] != "worldping" || ns[1] != "event" {
		log.Printf("Error: invalid event metrics.  Metrics hould begin with 'worldping.event'")
		return
	}
	hostname, _ := os.Hostname()
	id := time.Now().UnixNano()
	event := &schema.ProbeEvent{
		OrgId:     orgId,
		EventType: ns[2],
		Severity:  ns[3],
		Source:    hostname,
		Timestamp: id / int64(time.Millisecond),
		Message:   m.Data().(string),
		Tags:      m.Tags(),
	}

	body, err := msg.CreateProbeEventMsg(event, id, msg.FormatProbeEventMsgp)
	if err != nil {
		log.Printf("Error: unable to convert event to ProbeEventMsgp. %s", err)
		return
	}
	sent := false
	for !sent {
		if err = PostData("events", Token, body); err != nil {
			log.Printf("Error: %s", err)
			time.Sleep(time.Second)
		} else {
			sent = true
		}
	}
}