예제 #1
0
func metricHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != POST {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	rabbitmqUrl, _ := Config.String(ENV, "rabbitmq-url")
	conn, err := rabbitmq.Dial(rabbitmqUrl)
	if err != nil {
		log.Fatalf("Failed to get connection. ERR: %+v", err)
	}
	defer conn.Close()

	ch, err := rabbitmq.Channel(conn)
	if err != nil {
		log.Fatalf("Failed to open channel. ERR: %+v", err)
	}
	defer ch.Close()
	ch.Confirm(false)

	ack, nack := rabbitmq.GetAckNack(ch)
	exchange, _ := Config.String(ENV, "exchange")
	err = rabbitmq.Exchange(exchange, ch)
	if err != nil {
		log.Fatalf("Failed to declare an exchange. ERR: %+v", err)
	}

	decoder := json.NewDecoder(r.Body)
	var metric data.Metric
	err = decoder.Decode(&metric)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	metricJson, err := json.Marshal(metric)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	err = rabbitmq.PublishJson(metricJson, exchange, ch)
	if err != nil {
		log.Fatalf("Failed to publish message. ERR: %+v", err)
	}

	select {
	case <-ack:
		log.Printf("Pushed. Metric: %+v\n", metric)
		w.WriteHeader(http.StatusOK)
	case <-nack:
		log.Printf("Failed tp push. Metric: %+v\n", metric)
		w.WriteHeader(http.StatusInternalServerError)
	}

}
예제 #2
0
func main() {
	setENV()
	loadConfig()
	rabbitmqUrl, _ := Config.String(ENV, "rabbitmq-url")
	conn, err := rabbitmq.Dial(rabbitmqUrl)
	if err != nil {
		log.Fatalf("Failed to get connection. ERR: %+v", err)
	}
	defer conn.Close()

	ch, err := rabbitmq.Channel(conn)
	if err != nil {
		log.Fatalf("Failed to open channel. ERR: %+v", err)
	}

	defer ch.Close()
	exchange, _ := Config.String(ENV, "exchange")
	err = rabbitmq.Exchange(exchange, ch)
	if err != nil {
		log.Fatalf("Failed to declare an exchange. ERR: %+v", err)
	}

	logq, _ := Config.String(ENV, "logq")
	q, err := rabbitmq.Queue(logq, ch)
	if err != nil {
		log.Fatalf("Failed to declare a queue. ERR: %+v", err)
	}

	err = rabbitmq.QueueBind(q, exchange, ch)
	if err != nil {
		log.Fatalf("Failed to bind. ERR: %+v", err)
	}

	msgs, err := rabbitmq.Consume(q, ch)
	if err != nil {
		log.Fatalf("Failed to register name collector. ERR: %+v", err)
	}

	session, err := initMongoDB()
	if err != nil {
		log.Fatalf("Failed to start mongodb connection. ERR: %+v", err)
	}
	defer session.Close()
	forever := make(chan bool)
	go func() {
		for d := range msgs {
			var metric data.Metric
			err := json.Unmarshal(d.Body, &metric)
			if err != nil {
				log.Fatalf("Error unmarshalling metric. ERR: %+v", err)
			}
			err = processLog(metric, session)
			if err != nil {
				d.Nack(true, true)
			}
			d.Ack(false)
		}
		fmt.Println("end")
	}()
	log.Printf("Waiting for metrics....")
	<-forever
}
func main() {
	setENV()
	loadConfig()

	rabbitmqUrl, _ := Config.String(ENV, "rabbitmq-url")
	conn, err := rabbitmq.Dial(rabbitmqUrl)
	if err != nil {
		log.Fatalf("Failed to get connection. ERR: %+v", err)
	}
	defer conn.Close()

	ch, err := rabbitmq.Channel(conn)
	if err != nil {
		log.Fatalf("Failed to open channel. ERR: %+v", err)
	}
	defer ch.Close()
	exchange, _ := Config.String(ENV, "exchange")
	err = rabbitmq.Exchange(exchange, ch)
	if err != nil {
		log.Fatalf("Failed to declare an exchange. ERR: %+v", err)
	}

	nameq, _ := Config.String(ENV, "nameq")
	q, err := rabbitmq.Queue(nameq, ch)
	if err != nil {
		log.Fatalf("Failed to declare a queue. ERR: %+v", err)
	}

	err = rabbitmq.QueueBind(q, exchange, ch)
	if err != nil {
		log.Fatalf("Failed to bind. ERR: %+v", err)
	}

	msgs, err := rabbitmq.Consume(q, ch)
	if err != nil {
		log.Fatalf("Failed to register name collector. ERR: %+v", err)
	}

	forever := make(chan bool)

	go func() {
		for d := range msgs {
			client := initRedisClient()
			var metric data.Metric
			err := json.Unmarshal(d.Body, &metric)
			if err != nil {
				log.Fatalf("Error unmarshalling metric. ERR: %+v", err)
			}
			err = process(metric, client)
			if err != nil {
				d.Nack(true, true)
			}
			d.Ack(false)
			client.Close()
		}
	}()

	// // if curator panics, recovers and invoked the curator again after sleeping
	// // for 10 seconds
	// go func() {

	// }()
	go curateLogs()

	log.Printf("Waiting for metrics....")
	<-forever
}