From 2b9a1c10e4c5ff6cfd3468e2af7e2d6050294c44 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 22 Dec 2016 23:09:44 +0000 Subject: [PATCH] add metrics and sample node.js function --- .gitignore | 1 + gateway/metrics/metrics.go | 12 +++++++++--- gateway/server.go | 15 ++++++--------- sample-functions/Dockerfile | 9 +++++++++ sample-functions/main.js | 3 +++ sample-functions/package.json | 12 ++++++++++++ 6 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 .gitignore create mode 100644 sample-functions/Dockerfile create mode 100644 sample-functions/main.js create mode 100644 sample-functions/package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..512d37b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +fwatchdog diff --git a/gateway/metrics/metrics.go b/gateway/metrics/metrics.go index bb3be41d..c41d3abf 100644 --- a/gateway/metrics/metrics.go +++ b/gateway/metrics/metrics.go @@ -6,8 +6,14 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -// Handler returns the global http.Handler that provides the prometheus -// metrics format on GET requests -func Handler() http.Handler { +// MetricOptions to be used by web handlers +type MetricOptions struct { + GatewayRequestsTotal prometheus.Counter + GatewayServerlessServedTotal prometheus.Counter + GatewayFunctions prometheus.Histogram +} + +// PrometheusHandler Bootstraps prometheus for metrics collection +func PrometheusHandler() http.Handler { return prometheus.Handler() } diff --git a/gateway/server.go b/gateway/server.go index 20bb29e1..c3896f8e 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -19,12 +19,6 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -type MetricOptions struct { - GatewayRequestsTotal prometheus.Counter - GatewayServerlessServedTotal prometheus.Counter - GatewayFunctions prometheus.Histogram -} - func lookupSwarmService(serviceName string) (bool, error) { var c *client.Client var err error @@ -39,7 +33,7 @@ func lookupSwarmService(serviceName string) (bool, error) { return len(services) > 0, err } -func makeProxy(metrics MetricOptions) http.HandlerFunc { +func makeProxy(metrics metrics.MetricOptions) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { metrics.GatewayRequestsTotal.Inc() @@ -68,6 +62,9 @@ func makeProxy(metrics MetricOptions) http.HandlerFunc { metrics.GatewayServerlessServedTotal.Inc() metrics.GatewayFunctions.Observe(time.Since(start).Seconds()) + } else { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Provide an x-function header.")) } } } @@ -92,13 +89,13 @@ func main() { prometheus.Register(GatewayFunctions) r := mux.NewRouter() - r.HandleFunc("/", makeProxy(MetricOptions{ + r.HandleFunc("/", makeProxy(metrics.MetricOptions{ GatewayRequestsTotal: GatewayRequestsTotal, GatewayServerlessServedTotal: GatewayServerlessServedTotal, GatewayFunctions: GatewayFunctions, })) - metricsHandler := metrics.Handler() + metricsHandler := metrics.PrometheusHandler() r.Handle("/metrics", metricsHandler) log.Fatal(http.ListenAndServe(":8080", r)) } diff --git a/sample-functions/Dockerfile b/sample-functions/Dockerfile new file mode 100644 index 00000000..03a7fcd6 --- /dev/null +++ b/sample-functions/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine:latest +RUN apk --update add nodejs +COPY ./fwatchdog /usr/bin/ + +COPY package.json . +COPY main.js . +RUN npm i +ENV fprocess="node main.js" +CMD ["fwatchdog"] diff --git a/sample-functions/main.js b/sample-functions/main.js new file mode 100644 index 00000000..13374e73 --- /dev/null +++ b/sample-functions/main.js @@ -0,0 +1,3 @@ +'use strict' +let os = require('os'); +console.log(os.platform(), os.arch(), os.cpus(), os.uptime(), os.userInfo()); diff --git a/sample-functions/package.json b/sample-functions/package.json new file mode 100644 index 00000000..fa4598bc --- /dev/null +++ b/sample-functions/package.json @@ -0,0 +1,12 @@ +{ + "name": "sample-functions", + "version": "1.0.0", + "description": "", + "main": "main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +}