From 6ae19dcc07980f4daaa337edd624d6f68ae3578b Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 21 Jan 2019 22:11:25 +0100 Subject: grouped the functions in individual files and created a detailed README.md --- metrics.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 metrics.go (limited to 'metrics.go') diff --git a/metrics.go b/metrics.go new file mode 100644 index 0000000..5881c64 --- /dev/null +++ b/metrics.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "net/url" +) + +// metricHandler prints all the metrics to the ResponseWriter +func metricHandler(w http.ResponseWriter, r *http.Request) { + var metricsString string + metricsString += fmt.Sprintf("nr_galaxies %d\n", len(treeArray)) + + for i := 0; i < len(starCount); i++ { + metricsString += fmt.Sprintf("galaxy_star_count{galaxy_nr=\"%d\"} %d\n", i, starCount[i]) + } + + log.Println(metricsString) + _, _ = fmt.Fprintf(w, metricsString) +} + +// pushMetricsNumOfStars pushes the amount of stars in the given galaxy with the given index to the given host +// the host is (normally) the service bundling the metrics +func pushMetricsNumOfStars(host string, treeindex int64) { + + // define a post-request and send it to the given host + requestURL := fmt.Sprintf("%s", host) + resp, err := http.PostForm(requestURL, + url.Values{ + "key": {fmt.Sprintf("db_%s{nr=\"%s\"}", "stars_num", treeindex)}, + "value": {fmt.Sprintf("%d", starCount[treeindex])}, + }, + ) + if err != nil { + fmt.Printf("Cound not make a POST request to %s", requestURL) + } + + // close the response body + defer resp.Body.Close() +} -- cgit 1.4.1