示例#1
0
文件: plugin.go 项目: IRCody/snap
func catchPluginPanic(l *log.Logger) {
	if err := recover(); err != nil {
		trace := make([]byte, 4096)
		count := runtime.Stack(trace, true)
		l.Printf("Recover from panic: %s\n", err)
		l.Printf("Stack of %d bytes: %s\n", count, trace)
		panic(err)
	}
}
func publishDataToRmq(metrics []plugin.PluginMetricType, address string, exName string, rtKey string, exKind string, logger *log.Logger) error {
	conn, err := amqp.Dial("amqp://" + address)
	if err != nil {
		logger.Printf("RMQ Publisher: cannot open connection, %s", err)
		return err
	}
	defer conn.Close()

	c, err := conn.Channel()
	if err != nil {
		logger.Printf("RMQ Publisher: cannot open channel: %s", err)
		return err
	}

	err = c.ExchangeDeclare(exName, exKind, true, false, false, false, nil)
	if err != nil {
		logger.Printf("RMQ Publisher: cannot declare exchange: %v", err)
		return err
	}

	data, err := json.Marshal(metrics)
	if err != nil {
		logger.Printf("RMQ Publisher: cannot marshal metrics: %v", err)
		return err
	}

	msg := amqp.Publishing{
		DeliveryMode: amqp.Persistent,
		Timestamp:    time.Now(),
		ContentType:  "text/plain",
		Body:         data,
	}

	err = c.Publish(exName, rtKey, false, false, msg)
	if err != nil {
		logger.Printf("RMQ Publisher: cannot publish metric %v", err)
		return err
	}

	return nil
}