mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
add metrics and sample node.js function
This commit is contained in:
parent
6073cd79f6
commit
2b9a1c10e4
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
fwatchdog
|
@ -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()
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
9
sample-functions/Dockerfile
Normal file
9
sample-functions/Dockerfile
Normal file
@ -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"]
|
3
sample-functions/main.js
Normal file
3
sample-functions/main.js
Normal file
@ -0,0 +1,3 @@
|
||||
'use strict'
|
||||
let os = require('os');
|
||||
console.log(os.platform(), os.arch(), os.cpus(), os.uptime(), os.userInfo());
|
12
sample-functions/package.json
Normal file
12
sample-functions/package.json
Normal file
@ -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"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user