about summary refs log tree commit diff
path: root/metrics.go
diff options
context:
space:
mode:
Diffstat (limited to 'metrics.go')
-rw-r--r--metrics.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/metrics.go b/metrics.go
new file mode 100644
index 0000000..32b7cb2
--- /dev/null
+++ b/metrics.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"net/http"
+	"net/url"
+	"time"
+)
+
+func initMetricPusher() {
+	log.Println("[metrics] Initializing the metricsPusher")
+	go metricsPusher(METRICBUNDLERURL, metricsPusherDelay)
+}
+
+func metricsPusher(metricsBundlerURL string, metricsPusherDelay int64) {
+	log.Printf("[metrics pusher] The metrics pusher was startet, it will push metrics every %d seconds to the metrics-bundler at %s", metricsPusherDelay, metricsBundlerURL)
+
+	// if the metrics map does not exist yet, create it
+	if len(metrics) == 0 {
+		metrics = make(map[string]float64)
+	}
+
+	// define the endpoint URL to where the metrics should be pushed
+	metricsEndpoint := fmt.Sprintf("http://%s/metrics", metricsBundlerURL)
+
+	// infinite loop posting the metrics every n seconds to the metrics-bundler
+	for {
+		log.Println("[metrics pusher] Pushing the metrics...")
+
+		// get all keys and values from all items in the metrics map
+		for key, value := range metrics {
+
+			// make a POST request to the metrics-bundler
+			_, err := http.PostForm(metricsEndpoint,
+				url.Values{
+					"key": {
+						fmt.Sprintf("%v", key),
+					},
+					"value": {
+						fmt.Sprintf("%v", value),
+					},
+				})
+
+			// handle potential errors by panicking
+			if err != nil {
+				panic(err)
+			}
+
+		}
+
+		log.Printf("[metrics pusher] Done pushing the metrics. Waiting %d seconds...", metricsPusherDelay)
+
+		// sleep for 5 seconds
+		time.Sleep(time.Second * time.Duration(metricsPusherDelay))
+	}
+}