Created
June 8, 2017 18:39
-
-
Save ionutvilie/6ff915d19eaa3116b9b796463276c6dd to your computer and use it in GitHub Desktop.
prometheus.io converter / receive json , parse and set to metrics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// prometheus receive json and transform to prometheus metrics | |
// https://groups.google.com/forum/#!topic/prometheus-users/a1ft04aYKNc | |
import ( | |
"fmt" | |
"net/http" | |
"flag" | |
"github.com/prometheus/client_golang/prometheus" | |
"github.com/prometheus/client_golang/prometheus/promhttp" | |
"strconv" | |
"io/ioutil" | |
"encoding/json" | |
) | |
/* | |
http://localhost:8081/post | |
POST: | |
{ | |
"process":{ | |
"maximumWaitTimeMS":-9223372036854775808, | |
"totalResponseTimeMS":0, | |
"maximumResponseTimeMS":-9223372036854775808, | |
"minimumWaitTimeMS":9223372036854775807, | |
"averageTimeMS":0, | |
"totalWaitTimeMS":0, | |
"lastResponseTimeMS":-1, | |
"totalRequests":0, | |
"averageWaitTimeMS":0, | |
"minimumResponseTimeMS":9223372036854775807 | |
}, | |
"memory":{ | |
"usedMemory":448862416, | |
"totalMemory":1005060096, | |
"freeMemory":556197680, | |
"maxMemory":3817865216 | |
} | |
} | |
*/ | |
var ( | |
// | |
addr = flag.String("listen-address", ":8081", "The address to listen on for HTTP requests.") | |
// counter | |
httpResponsesTotal = prometheus.NewCounterVec( | |
prometheus.CounterOpts{ | |
//Namespace: "testvertical", | |
//Subsystem: "http_server", | |
Name: "http_requests_total", | |
Help: "http_requests_total_help", | |
}, | |
[]string{"code", "method"}, | |
) | |
// gauge | |
postjson = prometheus.NewGaugeVec( | |
prometheus.GaugeOpts{ | |
Name: "receivedjson", | |
Help: "the values from the received json", | |
}, | |
[]string{"code", "method"}, | |
) | |
) | |
type WrapHTTPHandler struct { | |
handler http.Handler | |
} | |
type LoggedResponse struct { | |
http.ResponseWriter | |
status int | |
} | |
// ---- >> change here << ---- | |
type PostJson struct { | |
Process map[string]interface{} `json:"process"` | |
Memory map[string]interface{} `json:"memory"` | |
} | |
func (wrappedHandler *WrapHTTPHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { | |
loggedWriter := &LoggedResponse{ResponseWriter: writer, status: 200} | |
wrappedHandler.handler.ServeHTTP(loggedWriter, request) | |
status := strconv.Itoa(loggedWriter.status) | |
//log.SetPrefix("[Info]") | |
//log.Printf("[RemoteAddr: %s] URL: %s, STATUS: %d, Time Elapsed: %dnanoseconds.\n", | |
// request.RemoteAddr, request.URL, loggedWriter.status, elapsed) | |
httpResponsesTotal.WithLabelValues(status, request.Method).Inc() | |
} | |
func (loggedResponse *LoggedResponse) WriteHeader(status int) { | |
loggedResponse.status = status | |
loggedResponse.ResponseWriter.WriteHeader(status) | |
} | |
func init() { | |
flag.Parse() | |
// ---- >> change here << ---- | |
prometheus.MustRegister(httpResponsesTotal) | |
prometheus.MustRegister(postjson) | |
} | |
func statusHandler(writer http.ResponseWriter, request *http.Request) { | |
writer.Write([]byte ("OK")) | |
} | |
func mainHandler(writer http.ResponseWriter, request *http.Request) { | |
// The "/" pattern matches everything, so we need to check that we're at the root here. | |
if request.URL.Path != "/" { | |
http.NotFound(writer, request) | |
return | |
} | |
fmt.Fprintf(writer, "home page.") | |
} | |
func postJson(rw http.ResponseWriter, req *http.Request) { | |
body, err := ioutil.ReadAll(req.Body) | |
if err != nil { | |
//panic(err) | |
http.Error(rw, err.Error(), http.StatusBadRequest) | |
} | |
//log.Println(string(body)) | |
var t PostJson | |
err = json.Unmarshal(body, &t) | |
if err != nil { | |
//panic(err) | |
http.Error(rw, err.Error(), http.StatusBadRequest) | |
} | |
// ---- >> change here << ---- | |
for k, v :=range t.Process { | |
fmt.Println(k , v) | |
postjson.WithLabelValues("process",fmt.Sprintf("%s", k)). | |
Set(v.(float64)) | |
} | |
for k, v :=range t.Memory { | |
fmt.Println(k , v) | |
postjson.WithLabelValues("memory",fmt.Sprintf("%s", k)). | |
Set(v.(float64)) | |
} | |
} | |
func main() { | |
http.HandleFunc("/", mainHandler) | |
http.Handle("/metrics", promhttp.Handler()) | |
http.HandleFunc("/post", postJson) | |
http.HandleFunc("/live", statusHandler) | |
http.HandleFunc("/ready", statusHandler) | |
http.ListenAndServe(*addr, &WrapHTTPHandler{http.DefaultServeMux}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"process":{ | |
"maximumWaitTimeMS":-9223372036854775808, | |
"totalResponseTimeMS":0, | |
"maximumResponseTimeMS":-9223372036854775808, | |
"minimumWaitTimeMS":9223372036854775807, | |
"averageTimeMS":0, | |
"totalWaitTimeMS":0, | |
"lastResponseTimeMS":-1, | |
"totalRequests":0, | |
"averageWaitTimeMS":0, | |
"minimumResponseTimeMS":9223372036854775807 | |
}, | |
"memory":{ | |
"usedMemory":448862416, | |
"totalMemory":1005060096, | |
"freeMemory":556197680, | |
"maxMemory":3817865216 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment