mirror of
https://github.com/openfaas/faas.git
synced 2025-06-23 15:23:29 +00:00
Merge pull request #9 from alexellis/labels_metrics
Add auto-scaling scaling via alerts and basic UI/portal
This commit is contained in:
@ -18,6 +18,8 @@ This one-shot script clones the code, initialises Docker swarm mode and then dep
|
|||||||
# docker swarm init --advertise-addr=$(ifconfig eth0| grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}') && \
|
# docker swarm init --advertise-addr=$(ifconfig eth0| grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}') && \
|
||||||
git clone https://github.com/alexellis/faas && \
|
git clone https://github.com/alexellis/faas && \
|
||||||
cd faas && \
|
cd faas && \
|
||||||
|
git checkout labels_metrics && \
|
||||||
|
(cd gateway && ./build.sh) && \
|
||||||
./deploy_stack.sh && \
|
./deploy_stack.sh && \
|
||||||
docker service ls
|
docker service ls
|
||||||
```
|
```
|
||||||
|
@ -2,4 +2,3 @@
|
|||||||
|
|
||||||
echo "Deploying stack"
|
echo "Deploying stack"
|
||||||
docker stack rm func ; docker stack deploy func --compose-file docker-compose.yml
|
docker stack rm func ; docker stack deploy func --compose-file docker-compose.yml
|
||||||
|
|
||||||
|
@ -1,27 +1,56 @@
|
|||||||
version: "3"
|
version: "3"
|
||||||
services:
|
services:
|
||||||
|
|
||||||
|
# Core API services are pinned, HA is provided for functions.
|
||||||
gateway:
|
gateway:
|
||||||
volumes:
|
volumes:
|
||||||
- "/var/run/docker.sock:/var/run/docker.sock"
|
- "/var/run/docker.sock:/var/run/docker.sock"
|
||||||
ports:
|
ports:
|
||||||
- 8080:8080
|
- 8080:8080
|
||||||
image: alexellis2/faas-gateway:latest-dev
|
image: alexellis2/faas-gateway:latest-dev3
|
||||||
networks:
|
networks:
|
||||||
- functions
|
- functions
|
||||||
|
deploy:
|
||||||
|
placement:
|
||||||
|
constraints: [node.role == manager]
|
||||||
|
|
||||||
prometheus:
|
prometheus:
|
||||||
image: quay.io/prometheus/prometheus:latest
|
image: quay.io/prometheus/prometheus:latest
|
||||||
volumes:
|
volumes:
|
||||||
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
|
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
|
||||||
command: "-config.file=/etc/prometheus/prometheus.yml -storage.local.path=/prometheus -storage.local.memory-chunks=10000"
|
- ./prometheus/alert.rules:/etc/prometheus/alert.rules
|
||||||
|
|
||||||
|
command: "-config.file=/etc/prometheus/prometheus.yml -storage.local.path=/prometheus -storage.local.memory-chunks=10000 --alertmanager.url=http://alertmanager:9093"
|
||||||
ports:
|
ports:
|
||||||
- 9090:9090
|
- 9090:9090
|
||||||
depends_on:
|
depends_on:
|
||||||
- gateway
|
- gateway
|
||||||
|
- alertmanager
|
||||||
environment:
|
environment:
|
||||||
no_proxy: "gateway"
|
no_proxy: "gateway"
|
||||||
networks:
|
networks:
|
||||||
- functions
|
- functions
|
||||||
|
deploy:
|
||||||
|
placement:
|
||||||
|
constraints: [node.role == manager]
|
||||||
|
|
||||||
|
|
||||||
|
alertmanager:
|
||||||
|
image: quay.io/prometheus/alertmanager
|
||||||
|
environment:
|
||||||
|
no_proxy: "gateway"
|
||||||
|
volumes:
|
||||||
|
- ./prometheus/alertmanager.yml:/alertmanager.yml
|
||||||
|
command:
|
||||||
|
- '-config.file=/alertmanager.yml'
|
||||||
|
networks:
|
||||||
|
- functions
|
||||||
|
ports:
|
||||||
|
- 9093:9093
|
||||||
|
deploy:
|
||||||
|
placement:
|
||||||
|
constraints: [node.role == manager]
|
||||||
|
|
||||||
|
|
||||||
# Sample functions go here.
|
# Sample functions go here.
|
||||||
webhookstash:
|
webhookstash:
|
||||||
@ -75,6 +104,7 @@ services:
|
|||||||
fprocess: "wc"
|
fprocess: "wc"
|
||||||
no_proxy: "gateway"
|
no_proxy: "gateway"
|
||||||
https_proxy: $https_proxy
|
https_proxy: $https_proxy
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
functions:
|
functions:
|
||||||
driver: overlay
|
driver: overlay
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|
||||||
COPY gateway .
|
WORKDIR /root/
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
ENV http_proxy ""
|
ENV http_proxy ""
|
||||||
ENV https_proxy ""
|
ENV https_proxy ""
|
||||||
|
|
||||||
|
COPY gateway .
|
||||||
|
|
||||||
|
COPY assets assets
|
||||||
|
|
||||||
CMD ["./gateway"]
|
CMD ["./gateway"]
|
||||||
|
@ -6,11 +6,15 @@ RUN go get -d github.com/docker/docker/api/types \
|
|||||||
&& go get -d github.com/docker/docker/client \
|
&& go get -d github.com/docker/docker/client \
|
||||||
&& go get github.com/gorilla/mux \
|
&& go get github.com/gorilla/mux \
|
||||||
&& go get github.com/prometheus/client_golang/prometheus
|
&& go get github.com/prometheus/client_golang/prometheus
|
||||||
|
RUN go get -d github.com/Sirupsen/logrus
|
||||||
|
|
||||||
WORKDIR /go/src/github.com/alexellis/faas/gateway
|
WORKDIR /go/src/github.com/alexellis/faas/gateway
|
||||||
|
|
||||||
COPY metrics metrics
|
COPY metrics metrics
|
||||||
COPY server.go .
|
COPY requests requests
|
||||||
|
COPY tests tests
|
||||||
|
COPY handlers handlers
|
||||||
|
|
||||||
|
COPY server.go .
|
||||||
|
|
||||||
RUN find /go/src/github.com/alexellis/faas/gateway/
|
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
||||||
|
99
gateway/assets/index.html
Normal file
99
gateway/assets/index.html
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
<html ng-app="faasGateway" >
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic" />
|
||||||
|
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">
|
||||||
|
<!-- Angular Material CSS now available via Google CDN; version 1.0.7 used here -->
|
||||||
|
<link rel="stylesheet" href="style/angular-material.min.css">
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="style/bootstrap.css">
|
||||||
|
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body ng-controller="home">
|
||||||
|
<div layout="column" style="height:100%;" ng-cloak>
|
||||||
|
|
||||||
|
<section id="popupContainer" layout="row" flex>
|
||||||
|
<md-sidenav
|
||||||
|
class="md-sidenav-left"
|
||||||
|
md-component-id="left"
|
||||||
|
md-is-locked-open="$mdMedia('gt-sm')"
|
||||||
|
md-whiteframe="4" layout="column">
|
||||||
|
|
||||||
|
<md-toolbar class="md-theme-indigo">
|
||||||
|
<h1 class="md-toolbar-tools">FaaS Gateway</h1>
|
||||||
|
</md-toolbar>
|
||||||
|
|
||||||
|
<md-content layout-padding>
|
||||||
|
<md-button ng-click="newFunction()" ng-disabled="isFunctionBeingCreated" class="md-primary">New function</md-button>
|
||||||
|
|
||||||
|
<md-list>
|
||||||
|
<md-list-item ng-switch class="md-3-line" ng-click="showFunction(function)" ng-repeat="function in functions | orderBy: '-invocationCount'" ng-class="function.name == selectedFunction.name ? 'selected' : false">
|
||||||
|
<md-icon ng-switch-when="true" style="color: blue" md-svg-icon="person"></md-icon>
|
||||||
|
<md-icon ng-switch-when="false" md-svg-icon="person-outline"></md-icon>
|
||||||
|
<p>{{function.name}}</p>
|
||||||
|
<md-divider ng-if="!$last"></md-divider>
|
||||||
|
</md-list-item>
|
||||||
|
</md-list>
|
||||||
|
</md-content>
|
||||||
|
</md-sidenav>
|
||||||
|
|
||||||
|
<md-content flex layout-padding ng-if="!selectedFunction" ng-show="functions.length">
|
||||||
|
<div layout="column" layout-align="top center">
|
||||||
|
<p>Select a function.</p>
|
||||||
|
</div>
|
||||||
|
<div flex></div>
|
||||||
|
</md-content>
|
||||||
|
|
||||||
|
<md-content flex layout-padding ng-if="!functions.length">
|
||||||
|
<div layout="column" layout-align="top center">
|
||||||
|
<p>No functions found in FaaS.</p>
|
||||||
|
</div>
|
||||||
|
<div flex></div>
|
||||||
|
</md-content>
|
||||||
|
<md-content flex layout="column" ng-repeat="function in functions" ng-show="function.name == selectedFunction.name">
|
||||||
|
|
||||||
|
<md-card md-theme="default" md-theme-watch>
|
||||||
|
<md-card-title>
|
||||||
|
|
||||||
|
<md-card-title-text>
|
||||||
|
|
||||||
|
<span class="md-headline">
|
||||||
|
{{function.name}}
|
||||||
|
</span>
|
||||||
|
<div layout-gt-sm="row">
|
||||||
|
<md-input-container class="md-icon-float md-block">
|
||||||
|
<label>Replicas</label>
|
||||||
|
<input ng-model="function.replicas" type="text" readonly="readonly">
|
||||||
|
</md-input-container>
|
||||||
|
<md-input-container class="md-block" flex-gt-sm>
|
||||||
|
<label>Invocation count</label>
|
||||||
|
<input ng-model="function.invocationCount" type="text" readonly="readonly">
|
||||||
|
</md-input-container>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div layout-gt-sm="row">
|
||||||
|
<md-input-container class="md-block" flex-gt-sm>
|
||||||
|
<label>Image</label>
|
||||||
|
<input ng-model="function.image" type="text" readonly="readonly">
|
||||||
|
</md-input-container>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<md-card-title-text>
|
||||||
|
|
||||||
|
</md-card-title>
|
||||||
|
</md-card>
|
||||||
|
|
||||||
|
</md-content>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
|
||||||
|
<script src="script/bootstrap.js"></script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
31
gateway/assets/script/bootstrap.js
vendored
Normal file
31
gateway/assets/script/bootstrap.js
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
"use strict"
|
||||||
|
var app = angular.module('faasGateway', ['ngMaterial']);
|
||||||
|
|
||||||
|
app.controller("home", ['$scope', '$log', '$http', '$location', '$timeout', function($scope, $log, $http, $location, $timeout) {
|
||||||
|
$scope.functions = [];
|
||||||
|
|
||||||
|
setInterval(function() {
|
||||||
|
fetch();
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
var fetch = function() {
|
||||||
|
$http.get("/system/functions").then(function(response) {
|
||||||
|
$scope.functions = response.data;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
$scope.showFunction = function(fn) {
|
||||||
|
$scope.selectedFunction = fn;
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: popup + form to create new Docker service.
|
||||||
|
$scope.newFunction = function() {
|
||||||
|
$scope.functions.push({
|
||||||
|
name: "f" +($scope.functions.length+2),
|
||||||
|
replicas: 0,
|
||||||
|
invokedCount: 0
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
fetch();
|
||||||
|
}]);
|
50
gateway/assets/style/bootstrap.css
vendored
Normal file
50
gateway/assets/style/bootstrap.css
vendored
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
@import url('https://fonts.googleapis.com/css?family=Rationale');
|
||||||
|
|
||||||
|
|
||||||
|
/*Taken from PWD, remove styles not used.*/
|
||||||
|
|
||||||
|
.selected button {
|
||||||
|
background-color: rgba(158,158,158,0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
md-card-content.terminal-container {
|
||||||
|
background-color: #000;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.clock {
|
||||||
|
font-family: 'Rationale', sans-serif;
|
||||||
|
font-size: 3.0em;
|
||||||
|
color: #1da4eb;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.welcome {
|
||||||
|
background-color: #e7e7e7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.welcome > div {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.welcome > div > img {
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.g-recaptcha div {
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
margin-bottom: auto;
|
||||||
|
margin-top: 50px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.disconnected {
|
||||||
|
background-color: #FDF4B6;
|
||||||
|
}
|
||||||
|
md-input-container {
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
||||||
|
md-input-container .md-errors-spacer {
|
||||||
|
height: 0;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
@ -10,4 +10,4 @@ docker rm -f gateway_extract
|
|||||||
|
|
||||||
echo Building alexellis2/faas-gateway:latest
|
echo Building alexellis2/faas-gateway:latest
|
||||||
|
|
||||||
docker build -t alexellis2/faas-gateway:latest .
|
docker build -t alexellis2/faas-gateway:latest-dev3 .
|
||||||
|
89
gateway/handlers/alerthandler.go
Normal file
89
gateway/handlers/alerthandler.go
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/alexellis/faas/gateway/requests"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
func scaleService(req requests.PrometheusAlert, c *client.Client) error {
|
||||||
|
var err error
|
||||||
|
//Todo: convert to loop / handler.
|
||||||
|
serviceName := req.Alerts[0].Labels.FunctionName
|
||||||
|
service, _, inspectErr := c.ServiceInspectWithRaw(context.Background(), serviceName)
|
||||||
|
if inspectErr == nil {
|
||||||
|
var replicas uint64
|
||||||
|
|
||||||
|
if req.Status == "firing" {
|
||||||
|
if *service.Spec.Mode.Replicated.Replicas < 20 {
|
||||||
|
replicas = *service.Spec.Mode.Replicated.Replicas + uint64(5)
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else { // Resolved event.
|
||||||
|
// Previously decremented by 5, but event only fires once, so set to 1/1.
|
||||||
|
if *service.Spec.Mode.Replicated.Replicas > 1 {
|
||||||
|
// replicas = *service.Spec.Mode.Replicated.Replicas - uint64(5)
|
||||||
|
// if replicas < 1 {
|
||||||
|
// replicas = 1
|
||||||
|
// }
|
||||||
|
// return nil
|
||||||
|
|
||||||
|
replicas = 1
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Scaling %s to %d replicas.\n", serviceName, replicas)
|
||||||
|
|
||||||
|
service.Spec.Mode.Replicated.Replicas = &replicas
|
||||||
|
updateOpts := types.ServiceUpdateOptions{}
|
||||||
|
updateOpts.RegistryAuthFrom = types.RegistryAuthFromSpec
|
||||||
|
|
||||||
|
response, updateErr := c.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, updateOpts)
|
||||||
|
if updateErr != nil {
|
||||||
|
err = updateErr
|
||||||
|
}
|
||||||
|
log.Println(response)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
err = inspectErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeAlertHandler(c *client.Client) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Println("Alert received.")
|
||||||
|
body, readErr := ioutil.ReadAll(r.Body)
|
||||||
|
if readErr != nil {
|
||||||
|
log.Println(readErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req requests.PrometheusAlert
|
||||||
|
err := json.Unmarshal(body, &req)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(req.Alerts) > 0 {
|
||||||
|
err := scaleService(req, c)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
56
gateway/handlers/functionshandler.go
Normal file
56
gateway/handlers/functionshandler.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/alexellis/faas/gateway/metrics"
|
||||||
|
"github.com/alexellis/faas/gateway/requests"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/docker/docker/client"
|
||||||
|
io_prometheus_client "github.com/prometheus/client_model/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MakeFunctionReader gives a summary of Function structs with Docker service stats overlaid with Prometheus counters.
|
||||||
|
func MakeFunctionReader(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
serviceFilter := filters.NewArgs()
|
||||||
|
|
||||||
|
options := types.ServiceListOptions{
|
||||||
|
Filters: serviceFilter,
|
||||||
|
}
|
||||||
|
|
||||||
|
services, err := c.ServiceList(context.Background(), options)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Filter only "faas" functions (via metadata?)
|
||||||
|
functions := make([]requests.Function, 0)
|
||||||
|
for _, service := range services {
|
||||||
|
counter, _ := metricsOptions.GatewayFunctionInvocation.GetMetricWithLabelValues(service.Spec.Name)
|
||||||
|
|
||||||
|
// Get the metric's value from ProtoBuf interface (idea via Julius Volz)
|
||||||
|
var protoMetric io_prometheus_client.Metric
|
||||||
|
counter.Write(&protoMetric)
|
||||||
|
invocations := protoMetric.GetCounter().GetValue()
|
||||||
|
|
||||||
|
f := requests.Function{
|
||||||
|
Name: service.Spec.Name,
|
||||||
|
Image: service.Spec.TaskTemplate.ContainerSpec.Image,
|
||||||
|
InvocationCount: invocations,
|
||||||
|
Replicas: *service.Spec.Mode.Replicated.Replicas,
|
||||||
|
}
|
||||||
|
functions = append(functions, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
functionBytes, _ := json.Marshal(functions)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(200)
|
||||||
|
w.Write(functionBytes)
|
||||||
|
}
|
||||||
|
}
|
142
gateway/handlers/proxy.go
Normal file
142
gateway/handlers/proxy.go
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/alexellis/faas/gateway/metrics"
|
||||||
|
"github.com/alexellis/faas/gateway/requests"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/docker/docker/client"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
// makeProxy creates a proxy for HTTP web requests which can be routed to a function.
|
||||||
|
func MakeProxy(metrics metrics.MetricOptions, wildcard bool, c *client.Client, logger *logrus.Logger) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
metrics.GatewayRequestsTotal.Inc()
|
||||||
|
|
||||||
|
if r.Method == "POST" {
|
||||||
|
logger.Infoln(r.Header)
|
||||||
|
header := r.Header["X-Function"]
|
||||||
|
logger.Infoln(header)
|
||||||
|
|
||||||
|
if wildcard == true {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
name := vars["name"]
|
||||||
|
fmt.Println("invoke by name")
|
||||||
|
lookupInvoke(w, r, metrics, name, c, logger)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
} else if len(header) > 0 {
|
||||||
|
lookupInvoke(w, r, metrics, header[0], c, logger)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
} else {
|
||||||
|
requestBody, _ := ioutil.ReadAll(r.Body)
|
||||||
|
defer r.Body.Close()
|
||||||
|
alexaService := IsAlexa(requestBody)
|
||||||
|
fmt.Println(alexaService)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
if len(alexaService.Session.SessionId) > 0 &&
|
||||||
|
len(alexaService.Session.Application.ApplicationId) > 0 &&
|
||||||
|
len(alexaService.Request.Intent.Name) > 0 {
|
||||||
|
|
||||||
|
fmt.Println("Alexa SDK request found")
|
||||||
|
fmt.Printf("SessionId=%s, Intent=%s, AppId=%s\n", alexaService.Session.SessionId, alexaService.Request.Intent.Name, alexaService.Session.Application.ApplicationId)
|
||||||
|
|
||||||
|
invokeService(w, r, metrics, alexaService.Request.Intent.Name, requestBody, logger)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
w.Write([]byte("Provide an x-function header or a valid Alexa SDK request."))
|
||||||
|
defer r.Body.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsAlexa(requestBody []byte) requests.AlexaRequestBody {
|
||||||
|
body := requests.AlexaRequestBody{}
|
||||||
|
buf := bytes.NewBuffer(requestBody)
|
||||||
|
// fmt.Println(buf)
|
||||||
|
str := buf.String()
|
||||||
|
parts := strings.Split(str, "sessionId")
|
||||||
|
if len(parts) > 1 {
|
||||||
|
json.Unmarshal(requestBody, &body)
|
||||||
|
}
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
func lookupInvoke(w http.ResponseWriter, r *http.Request, metrics metrics.MetricOptions, name string, c *client.Client, logger *logrus.Logger) {
|
||||||
|
exists, err := lookupSwarmService(name, c)
|
||||||
|
if err != nil || exists == false {
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatalln(err)
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte("Error resolving service."))
|
||||||
|
defer r.Body.Close()
|
||||||
|
}
|
||||||
|
if exists == true {
|
||||||
|
requestBody, _ := ioutil.ReadAll(r.Body)
|
||||||
|
invokeService(w, r, metrics, name, requestBody, logger)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func lookupSwarmService(serviceName string, c *client.Client) (bool, error) {
|
||||||
|
fmt.Printf("Resolving: '%s'\n", serviceName)
|
||||||
|
serviceFilter := filters.NewArgs()
|
||||||
|
serviceFilter.Add("name", serviceName)
|
||||||
|
services, err := c.ServiceList(context.Background(), types.ServiceListOptions{Filters: serviceFilter})
|
||||||
|
|
||||||
|
return len(services) > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.MetricOptions, service string, requestBody []byte, logger *logrus.Logger) {
|
||||||
|
metrics.GatewayFunctionInvocation.WithLabelValues(service).Add(1)
|
||||||
|
|
||||||
|
stamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
buf := bytes.NewBuffer(requestBody)
|
||||||
|
url := "http://" + service + ":" + strconv.Itoa(8080) + "/"
|
||||||
|
fmt.Printf("[%s] Forwarding request to: %s\n", stamp, url)
|
||||||
|
|
||||||
|
response, err := http.Post(url, "text/plain", buf)
|
||||||
|
if err != nil {
|
||||||
|
logger.Infoln(err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
buf := bytes.NewBufferString("Can't reach service: " + service)
|
||||||
|
w.Write(buf.Bytes())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
responseBody, readErr := ioutil.ReadAll(response.Body)
|
||||||
|
if readErr != nil {
|
||||||
|
fmt.Println(readErr)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
buf := bytes.NewBufferString("Error reading response from service: " + service)
|
||||||
|
w.Write(buf.Bytes())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(responseBody)
|
||||||
|
|
||||||
|
seconds := time.Since(start).Seconds()
|
||||||
|
fmt.Printf("[%s] took %f seconds\n", stamp, seconds)
|
||||||
|
metrics.GatewayServerlessServedTotal.Inc()
|
||||||
|
metrics.GatewayFunctions.Observe(seconds)
|
||||||
|
}
|
@ -11,9 +11,48 @@ type MetricOptions struct {
|
|||||||
GatewayRequestsTotal prometheus.Counter
|
GatewayRequestsTotal prometheus.Counter
|
||||||
GatewayServerlessServedTotal prometheus.Counter
|
GatewayServerlessServedTotal prometheus.Counter
|
||||||
GatewayFunctions prometheus.Histogram
|
GatewayFunctions prometheus.Histogram
|
||||||
|
GatewayFunctionInvocation *prometheus.CounterVec
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrometheusHandler Bootstraps prometheus for metrics collection
|
// PrometheusHandler Bootstraps prometheus for metrics collection
|
||||||
func PrometheusHandler() http.Handler {
|
func PrometheusHandler() http.Handler {
|
||||||
return prometheus.Handler()
|
return prometheus.Handler()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildMetricsOptions() MetricOptions {
|
||||||
|
GatewayRequestsTotal := prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "gateway_requests_total",
|
||||||
|
Help: "Total amount of HTTP requests to the gateway",
|
||||||
|
})
|
||||||
|
GatewayServerlessServedTotal := prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
|
Name: "gateway_serverless_invocation_total",
|
||||||
|
Help: "Total amount of serverless function invocations",
|
||||||
|
})
|
||||||
|
GatewayFunctions := prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||||
|
Name: "gateway_functions",
|
||||||
|
Help: "Gateway functions",
|
||||||
|
})
|
||||||
|
GatewayFunctionInvocation := prometheus.NewCounterVec(
|
||||||
|
prometheus.CounterOpts{
|
||||||
|
Name: "gateway_function_invocation_total",
|
||||||
|
Help: "Individual function metrics",
|
||||||
|
},
|
||||||
|
[]string{"function_name"},
|
||||||
|
)
|
||||||
|
|
||||||
|
metricsOptions := MetricOptions{
|
||||||
|
GatewayRequestsTotal: GatewayRequestsTotal,
|
||||||
|
GatewayServerlessServedTotal: GatewayServerlessServedTotal,
|
||||||
|
GatewayFunctions: GatewayFunctions,
|
||||||
|
GatewayFunctionInvocation: GatewayFunctionInvocation,
|
||||||
|
}
|
||||||
|
|
||||||
|
return metricsOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterMetrics(metricsOptions MetricOptions) {
|
||||||
|
prometheus.Register(metricsOptions.GatewayRequestsTotal)
|
||||||
|
prometheus.Register(metricsOptions.GatewayServerlessServedTotal)
|
||||||
|
prometheus.Register(metricsOptions.GatewayFunctions)
|
||||||
|
prometheus.Register(metricsOptions.GatewayFunctionInvocation)
|
||||||
|
}
|
||||||
|
49
gateway/requests/requests.go
Normal file
49
gateway/requests/requests.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package requests
|
||||||
|
|
||||||
|
type AlexaSessionApplication struct {
|
||||||
|
ApplicationId string `json:"applicationId"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AlexaSession struct {
|
||||||
|
SessionId string `json:"sessionId"`
|
||||||
|
Application AlexaSessionApplication `json:"application"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AlexaIntent struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AlexaRequest struct {
|
||||||
|
Intent AlexaIntent `json:"intent"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AlexaRequestBody top-level request produced by Alexa SDK
|
||||||
|
type AlexaRequestBody struct {
|
||||||
|
Session AlexaSession `json:"session"`
|
||||||
|
Request AlexaRequest `json:"request"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PrometheusInnerAlertLabel struct {
|
||||||
|
AlertName string `json:"alertname"`
|
||||||
|
FunctionName string `json:"function_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PrometheusInnerAlert struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
Labels PrometheusInnerAlertLabel `json:"labels"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrometheusAlert as produced by AlertManager
|
||||||
|
type PrometheusAlert struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
Receiver string `json:"receiver"`
|
||||||
|
Alerts []PrometheusInnerAlert `json:"alerts"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function exported for system/functions endpoint
|
||||||
|
type Function struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
InvocationCount float64 `json:"invocationCount"`
|
||||||
|
Replicas uint64 `json:"replicas"`
|
||||||
|
}
|
@ -1,195 +1,49 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"io/ioutil"
|
"github.com/Sirupsen/logrus"
|
||||||
|
faashandlers "github.com/alexellis/faas/gateway/handlers"
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/alexellis/faas/gateway/metrics"
|
"github.com/alexellis/faas/gateway/metrics"
|
||||||
"github.com/docker/docker/api/types"
|
|
||||||
"github.com/docker/docker/api/types/filters"
|
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AlexaSessionApplication struct {
|
func main() {
|
||||||
ApplicationId string `json:"applicationId"`
|
logger := logrus.Logger{}
|
||||||
}
|
logrus.SetFormatter(&logrus.TextFormatter{})
|
||||||
|
|
||||||
type AlexaSession struct {
|
var dockerClient *client.Client
|
||||||
SessionId string `json:"sessionId"`
|
|
||||||
Application AlexaSessionApplication `json:"application"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AlexaIntent struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AlexaRequest struct {
|
|
||||||
Intent AlexaIntent `json:"intent"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AlexaRequestBody struct {
|
|
||||||
Session AlexaSession `json:"session"`
|
|
||||||
Request AlexaRequest `json:"request"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupSwarmService(serviceName string) (bool, error) {
|
|
||||||
var c *client.Client
|
|
||||||
var err error
|
var err error
|
||||||
c, err = client.NewEnvClient()
|
dockerClient, err = client.NewEnvClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error with Docker client.")
|
log.Fatal("Error with Docker client.")
|
||||||
}
|
}
|
||||||
fmt.Printf("Resolving: '%s'\n", serviceName)
|
dockerVersion, err := dockerClient.ServerVersion(context.Background())
|
||||||
serviceFilter := filters.NewArgs()
|
|
||||||
serviceFilter.Add("name", serviceName)
|
|
||||||
services, err := c.ServiceList(context.Background(), types.ServiceListOptions{Filters: serviceFilter})
|
|
||||||
|
|
||||||
return len(services) > 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func isAlexa(requestBody []byte) AlexaRequestBody {
|
|
||||||
body := AlexaRequestBody{}
|
|
||||||
buf := bytes.NewBuffer(requestBody)
|
|
||||||
fmt.Println(buf)
|
|
||||||
str := buf.String()
|
|
||||||
parts := strings.Split(str, "sessionId")
|
|
||||||
if len(parts) > 1 {
|
|
||||||
json.Unmarshal(requestBody, &body)
|
|
||||||
}
|
|
||||||
return body
|
|
||||||
}
|
|
||||||
|
|
||||||
func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.MetricOptions, service string, requestBody []byte) {
|
|
||||||
stamp := strconv.FormatInt(time.Now().Unix(), 10)
|
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
buf := bytes.NewBuffer(requestBody)
|
|
||||||
url := "http://" + service + ":" + strconv.Itoa(8080) + "/"
|
|
||||||
fmt.Printf("[%s] Forwarding request to: %s\n", stamp, url)
|
|
||||||
response, err := http.Post(url, "text/plain", buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Fatal("Error with Docker server.\n", err)
|
||||||
w.WriteHeader(500)
|
|
||||||
buf := bytes.NewBufferString("Can't reach service: " + service)
|
|
||||||
w.Write(buf.Bytes())
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
log.Printf("API version: %s, %s\n", dockerVersion.APIVersion, dockerVersion.Version)
|
||||||
|
|
||||||
responseBody, readErr := ioutil.ReadAll(response.Body)
|
metricsOptions := metrics.BuildMetricsOptions()
|
||||||
if readErr != nil {
|
metrics.RegisterMetrics(metricsOptions)
|
||||||
fmt.Println(readErr)
|
|
||||||
w.WriteHeader(500)
|
|
||||||
buf := bytes.NewBufferString("Error reading response from service: " + service)
|
|
||||||
w.Write(buf.Bytes())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write(responseBody)
|
|
||||||
seconds := time.Since(start).Seconds()
|
|
||||||
fmt.Printf("[%s] took %f seconds\n", stamp, seconds)
|
|
||||||
metrics.GatewayServerlessServedTotal.Inc()
|
|
||||||
metrics.GatewayFunctions.Observe(seconds)
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupInvoke(w http.ResponseWriter, r *http.Request, metrics metrics.MetricOptions, name string) {
|
|
||||||
exists, err := lookupSwarmService(name)
|
|
||||||
if err != nil || exists == false {
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
w.Write([]byte("Error resolving service."))
|
|
||||||
}
|
|
||||||
if exists == true {
|
|
||||||
requestBody, _ := ioutil.ReadAll(r.Body)
|
|
||||||
invokeService(w, r, metrics, name, requestBody)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeProxy(metrics metrics.MetricOptions, wildcard bool) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
metrics.GatewayRequestsTotal.Inc()
|
|
||||||
|
|
||||||
if r.Method == "POST" {
|
|
||||||
log.Println(r.Header)
|
|
||||||
header := r.Header["X-Function"]
|
|
||||||
log.Println(header)
|
|
||||||
fmt.Println(wildcard)
|
|
||||||
|
|
||||||
if wildcard == true {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
name := vars["name"]
|
|
||||||
fmt.Println("invoke by name")
|
|
||||||
lookupInvoke(w, r, metrics, name)
|
|
||||||
} else if len(header) > 0 {
|
|
||||||
lookupInvoke(w, r, metrics, header[0])
|
|
||||||
} else {
|
|
||||||
requestBody, _ := ioutil.ReadAll(r.Body)
|
|
||||||
alexaService := isAlexa(requestBody)
|
|
||||||
fmt.Println(alexaService)
|
|
||||||
|
|
||||||
if len(alexaService.Session.SessionId) > 0 &&
|
|
||||||
len(alexaService.Session.Application.ApplicationId) > 0 &&
|
|
||||||
len(alexaService.Request.Intent.Name) > 0 {
|
|
||||||
|
|
||||||
fmt.Println("Alexa SDK request found")
|
|
||||||
fmt.Printf("SessionId=%s, Intent=%s, AppId=%s\n", alexaService.Session.SessionId, alexaService.Request.Intent.Name, alexaService.Session.Application.ApplicationId)
|
|
||||||
|
|
||||||
invokeService(w, r, metrics, alexaService.Request.Intent.Name, requestBody)
|
|
||||||
} else {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
w.Write([]byte("Provide an x-function header or a valid Alexa SDK request."))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
GatewayRequestsTotal := prometheus.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "gateway_requests_total",
|
|
||||||
Help: "Total amount of HTTP requests to the gateway",
|
|
||||||
})
|
|
||||||
GatewayServerlessServedTotal := prometheus.NewCounter(prometheus.CounterOpts{
|
|
||||||
Name: "gateway_serverless_invocation_total",
|
|
||||||
Help: "Total amount of serverless function invocations",
|
|
||||||
})
|
|
||||||
GatewayFunctions := prometheus.NewHistogram(prometheus.HistogramOpts{
|
|
||||||
Name: "gateway_functions",
|
|
||||||
Help: "Gateway functions",
|
|
||||||
})
|
|
||||||
|
|
||||||
prometheus.Register(GatewayRequestsTotal)
|
|
||||||
prometheus.Register(GatewayServerlessServedTotal)
|
|
||||||
prometheus.Register(GatewayFunctions)
|
|
||||||
|
|
||||||
metricsOptions := metrics.MetricOptions{
|
|
||||||
GatewayRequestsTotal: GatewayRequestsTotal,
|
|
||||||
GatewayServerlessServedTotal: GatewayServerlessServedTotal,
|
|
||||||
GatewayFunctions: GatewayFunctions,
|
|
||||||
}
|
|
||||||
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/", makeProxy(metricsOptions, false))
|
r.HandleFunc("/function/{name:[a-zA-Z_]+}", faashandlers.MakeProxy(metricsOptions, true, dockerClient, &logger))
|
||||||
|
|
||||||
r.HandleFunc("/function/{name:[a-zA-Z_]+}", makeProxy(metricsOptions, true))
|
r.HandleFunc("/system/alert", faashandlers.MakeAlertHandler(dockerClient))
|
||||||
|
r.HandleFunc("/system/functions", faashandlers.MakeFunctionReader(metricsOptions, dockerClient)).Methods("GET")
|
||||||
|
|
||||||
|
r.HandleFunc("/", faashandlers.MakeProxy(metricsOptions, false, dockerClient, &logger)).Methods("POST")
|
||||||
|
|
||||||
metricsHandler := metrics.PrometheusHandler()
|
metricsHandler := metrics.PrometheusHandler()
|
||||||
r.Handle("/metrics", metricsHandler)
|
r.Handle("/metrics", metricsHandler)
|
||||||
|
|
||||||
|
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./assets/"))).Methods("GET")
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
ReadTimeout: 8 * time.Second,
|
ReadTimeout: 8 * time.Second,
|
||||||
|
24
gateway/tests/alexhostname_request.json
Normal file
24
gateway/tests/alexhostname_request.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"session": {
|
||||||
|
"sessionId": "SessionId.ea96e58d-dc16-43e1-b238-daac4541110c",
|
||||||
|
"application": {
|
||||||
|
"applicationId": "amzn1.ask.skill.72fb1025-aacc-4d05-a582-21344940c023"
|
||||||
|
},
|
||||||
|
"attributes": {},
|
||||||
|
"user": {
|
||||||
|
"userId": "amzn1.ask.account.AEN7KA5DBXAAWQPDUXTXFWBARZ5YZ6TNOQR5CUMV5LCCJTMBZVFP45SZVLGDD5GQBOM7QMELRS7LHG3F2FN2QQQMTBURDL5I4PQ33EHMNNGO4TXWG732Y6SDM2YZKHSPWIIWBH3GSE3Q3TTFAYN2Y66RHBKRANYCNMX2WORMASUGVRHUNBB4HZMJEC7HQDWUSXAOMP77WGJU4AY"
|
||||||
|
},
|
||||||
|
"new": true
|
||||||
|
},
|
||||||
|
"request": {
|
||||||
|
"type": "IntentRequest",
|
||||||
|
"requestId": "EdwRequestId.a934104e-3282-4620-b056-4aa4c5995503",
|
||||||
|
"locale": "en-GB",
|
||||||
|
"timestamp": "2016-12-07T15:50:01Z",
|
||||||
|
"intent": {
|
||||||
|
"name": "HostnameIntent",
|
||||||
|
"slots": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"version": "1.0"
|
||||||
|
}
|
92
gateway/tests/integration/routes_test.go
Normal file
92
gateway/tests/integration/routes_test.go
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
package inttests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Before running these tests do a Docker stack deploy.
|
||||||
|
|
||||||
|
func fireRequest(url string, method string, reqBody string) (string, int, error) {
|
||||||
|
return fireRequestWithHeader(url, method, reqBody, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func fireRequestWithHeader(url string, method string, reqBody string, xheader string) (string, int, error) {
|
||||||
|
httpClient := http.Client{
|
||||||
|
Timeout: time.Second * 2, // Maximum of 2 secs
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest(method, url, bytes.NewBufferString(reqBody))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("User-Agent", "spacecount-tutorial")
|
||||||
|
if len(xheader) != 0 {
|
||||||
|
req.Header.Set("X-Function", xheader)
|
||||||
|
}
|
||||||
|
res, getErr := httpClient.Do(req)
|
||||||
|
if getErr != nil {
|
||||||
|
log.Fatal(getErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, readErr := ioutil.ReadAll(res.Body)
|
||||||
|
defer req.Body.Close()
|
||||||
|
if readErr != nil {
|
||||||
|
log.Fatal(readErr)
|
||||||
|
}
|
||||||
|
return string(body), res.StatusCode, readErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Get_Rejected(t *testing.T) {
|
||||||
|
var reqBody string
|
||||||
|
_, code, err := fireRequest("http://localhost:8080/function/func_echoit", http.MethodGet, reqBody)
|
||||||
|
if code != http.StatusInternalServerError {
|
||||||
|
t.Log("Failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_EchoIt_Post_Route_Handler(t *testing.T) {
|
||||||
|
reqBody := "test message"
|
||||||
|
body, code, err := fireRequest("http://localhost:8080/function/func_echoit", http.MethodPost, reqBody)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if code != http.StatusOK {
|
||||||
|
t.Log("Failed")
|
||||||
|
}
|
||||||
|
if body != reqBody {
|
||||||
|
t.Log("Expected body returned")
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_EchoIt_Post_Header_Handler(t *testing.T) {
|
||||||
|
reqBody := "test message"
|
||||||
|
body, code, err := fireRequestWithHeader("http://localhost:8080/", http.MethodPost, reqBody, "func_echoit")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if code != http.StatusOK {
|
||||||
|
t.Log("Failed")
|
||||||
|
}
|
||||||
|
if body != reqBody {
|
||||||
|
t.Log("Expected body returned")
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
gateway/tests/isalexa_test.go
Normal file
27
gateway/tests/isalexa_test.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/alexellis/faas/gateway/handlers"
|
||||||
|
"github.com/alexellis/faas/gateway/requests"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsAlexa(t *testing.T) {
|
||||||
|
requestBody, _ := ioutil.ReadFile("./alexhostname_request.json")
|
||||||
|
var result requests.AlexaRequestBody
|
||||||
|
|
||||||
|
result = handlers.IsAlexa(requestBody)
|
||||||
|
|
||||||
|
if len(result.Session.Application.ApplicationId) == 0 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if len(result.Session.SessionId) == 0 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if len(result.Request.Intent.Name) == 0 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
47
gateway/tests/test_alert.json
Normal file
47
gateway/tests/test_alert.json
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
{
|
||||||
|
"receiver":"scale-up",
|
||||||
|
"status":"firing",
|
||||||
|
"alerts":[
|
||||||
|
{
|
||||||
|
"status":"firing",
|
||||||
|
"labels":{
|
||||||
|
"alertname":"APIHighInvocationRate",
|
||||||
|
"function_name":"func_echoit",
|
||||||
|
"instance":"gateway:8080",
|
||||||
|
"job":"gateway",
|
||||||
|
"monitor":"faas-monitor",
|
||||||
|
"service":"gateway",
|
||||||
|
"severity":"major",
|
||||||
|
"value":"8"
|
||||||
|
},
|
||||||
|
"annotations":{
|
||||||
|
"description":"High invocation total on gateway:8080",
|
||||||
|
"summary":"High invocation total on gateway:8080"
|
||||||
|
},
|
||||||
|
"startsAt":"2017-01-22T10:40:52.804Z",
|
||||||
|
"endsAt":"0001-01-01T00:00:00Z",
|
||||||
|
"generatorURL":"http://bb1b23e87070:9090/graph?g0.expr=rate%28gateway_function_invocation_total%5B10s%5D%29+%3E+5\u0026g0.tab=0"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"groupLabels":{
|
||||||
|
"alertname":"APIHighInvocationRate",
|
||||||
|
"service":"gateway"
|
||||||
|
},
|
||||||
|
"commonLabels":{
|
||||||
|
"alertname":"APIHighInvocationRate",
|
||||||
|
"function_name":"func_echoit",
|
||||||
|
"instance":"gateway:8080",
|
||||||
|
"job":"gateway",
|
||||||
|
"monitor":"faas-monitor",
|
||||||
|
"service":"gateway",
|
||||||
|
"severity":"major",
|
||||||
|
"value":"8"
|
||||||
|
},
|
||||||
|
"commonAnnotations":{
|
||||||
|
"description":"High invocation total on gateway:8080",
|
||||||
|
"summary":"High invocation total on gateway:8080"
|
||||||
|
},
|
||||||
|
"externalURL":"http://c052c835bcee:9093",
|
||||||
|
"version":"3",
|
||||||
|
"groupKey":18195285354214864953
|
||||||
|
}
|
38
gateway/tests/unmarshall_test.go
Normal file
38
gateway/tests/unmarshall_test.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/alexellis/faas/gateway/requests"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestUnmarshallAlert is an exploratory test from TDD'ing the struct to parse a Prometheus alert
|
||||||
|
func TestUnmarshallAlert(t *testing.T) {
|
||||||
|
file, _ := ioutil.ReadFile("./test_alert.json")
|
||||||
|
|
||||||
|
var alert requests.PrometheusAlert
|
||||||
|
err := json.Unmarshal(file, &alert)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if (len(alert.Status)) == 0 {
|
||||||
|
t.Fatal("No status read")
|
||||||
|
}
|
||||||
|
if (len(alert.Receiver)) == 0 {
|
||||||
|
t.Fatal("No status read")
|
||||||
|
}
|
||||||
|
if (len(alert.Alerts)) == 0 {
|
||||||
|
t.Fatal("No alerts read")
|
||||||
|
}
|
||||||
|
if (len(alert.Alerts[0].Labels.AlertName)) == 0 {
|
||||||
|
t.Fatal("No alerts name")
|
||||||
|
}
|
||||||
|
if (len(alert.Alerts[0].Labels.FunctionName)) == 0 {
|
||||||
|
t.Fatal("No function name read")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
prometheus/alert.rules
Normal file
15
prometheus/alert.rules
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
ALERT service_down
|
||||||
|
IF up == 0
|
||||||
|
|
||||||
|
ALERT APIHighInvocationRate
|
||||||
|
IF rate ( gateway_function_invocation_total [10s] ) > 5
|
||||||
|
FOR 5s
|
||||||
|
LABELS {
|
||||||
|
service = "gateway",
|
||||||
|
severity = "major",
|
||||||
|
value = "{{$value}}"
|
||||||
|
}
|
||||||
|
ANNOTATIONS {
|
||||||
|
summary = "High invocation total on {{ $labels.instance }}",
|
||||||
|
description = "High invocation total on {{ $labels.instance }}"
|
||||||
|
}
|
69
prometheus/alertmanager.yml
Normal file
69
prometheus/alertmanager.yml
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
global:
|
||||||
|
# The smarthost and SMTP sender used for mail notifications.
|
||||||
|
smtp_smarthost: 'localhost:25'
|
||||||
|
smtp_from: 'alertmanager@example.org'
|
||||||
|
smtp_auth_username: 'alertmanager'
|
||||||
|
smtp_auth_password: 'password'
|
||||||
|
# The auth token for Hipchat.
|
||||||
|
hipchat_auth_token: '1234556789'
|
||||||
|
# Alternative host for Hipchat.
|
||||||
|
hipchat_url: 'https://hipchat.foobar.org/'
|
||||||
|
|
||||||
|
# The directory from which notification templates are read.
|
||||||
|
templates:
|
||||||
|
- '/etc/alertmanager/template/*.tmpl'
|
||||||
|
|
||||||
|
# The root route on which each incoming alert enters.
|
||||||
|
route:
|
||||||
|
# The labels by which incoming alerts are grouped together. For example,
|
||||||
|
# multiple alerts coming in for cluster=A and alertname=LatencyHigh would
|
||||||
|
# be batched into a single group.
|
||||||
|
group_by: ['alertname', 'cluster', 'service']
|
||||||
|
|
||||||
|
# When a new group of alerts is created by an incoming alert, wait at
|
||||||
|
# least 'group_wait' to send the initial notification.
|
||||||
|
# This way ensures that you get multiple alerts for the same group that start
|
||||||
|
# firing shortly after another are batched together on the first
|
||||||
|
# notification.
|
||||||
|
group_wait: 5s
|
||||||
|
|
||||||
|
# When the first notification was sent, wait 'group_interval' to send a batch
|
||||||
|
# of new alerts that started firing for that group.
|
||||||
|
group_interval: 10s
|
||||||
|
|
||||||
|
# If an alert has successfully been sent, wait 'repeat_interval' to
|
||||||
|
# resend them.
|
||||||
|
repeat_interval: 30s
|
||||||
|
|
||||||
|
# A default receiver
|
||||||
|
receiver: scale-up
|
||||||
|
|
||||||
|
# All the above attributes are inherited by all child routes and can
|
||||||
|
# overwritten on each.
|
||||||
|
|
||||||
|
# The child route trees.
|
||||||
|
routes:
|
||||||
|
- match:
|
||||||
|
service: gateway
|
||||||
|
receiver: scale-up
|
||||||
|
severity: major
|
||||||
|
|
||||||
|
|
||||||
|
# Inhibition rules allow to mute a set of alerts given that another alert is
|
||||||
|
# firing.
|
||||||
|
# We use this to mute any warning-level notifications if the same alert is
|
||||||
|
# already critical.
|
||||||
|
inhibit_rules:
|
||||||
|
- source_match:
|
||||||
|
severity: 'critical'
|
||||||
|
target_match:
|
||||||
|
severity: 'warning'
|
||||||
|
# Apply inhibition if the alertname is the same.
|
||||||
|
equal: ['alertname', 'cluster', 'service']
|
||||||
|
|
||||||
|
receivers:
|
||||||
|
- name: 'scale-up'
|
||||||
|
webhook_configs:
|
||||||
|
- url: http://gateway:8080/system/alert
|
||||||
|
send_resolved: true
|
||||||
|
|
@ -7,12 +7,12 @@ global:
|
|||||||
# Attach these labels to any time series or alerts when communicating with
|
# Attach these labels to any time series or alerts when communicating with
|
||||||
# external systems (federation, remote storage, Alertmanager).
|
# external systems (federation, remote storage, Alertmanager).
|
||||||
external_labels:
|
external_labels:
|
||||||
monitor: 'codelab-monitor'
|
monitor: 'faas-monitor'
|
||||||
|
|
||||||
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
|
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
|
||||||
rule_files:
|
rule_files:
|
||||||
# - "first.rules"
|
- 'alert.rules'
|
||||||
# - "second.rules"
|
|
||||||
|
|
||||||
# A scrape configuration containing exactly one endpoint to scrape:
|
# A scrape configuration containing exactly one endpoint to scrape:
|
||||||
# Here it's Prometheus itself.
|
# Here it's Prometheus itself.
|
||||||
@ -29,6 +29,6 @@ scrape_configs:
|
|||||||
- targets: ['localhost:9090']
|
- targets: ['localhost:9090']
|
||||||
|
|
||||||
- job_name: "gateway"
|
- job_name: "gateway"
|
||||||
scrape_interval: "15s"
|
scrape_interval: 5s
|
||||||
static_configs:
|
static_configs:
|
||||||
- targets: ['gateway:8080']
|
- targets: ['gateway:8080']
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|
||||||
COPY fwatchdog /usr/bin/
|
ADD https://github.com/alexellis/faas/releases/download/v0.1-alpha/fwatchdog /usr/bin
|
||||||
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
|
||||||
# Populate example here
|
# Populate example here
|
||||||
# ENV fprocess="wc -l"
|
# ENV fprocess="wc -l"
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
RUN apk --update add nodejs
|
RUN apk --update add nodejs
|
||||||
COPY ./fwatchdog /usr/bin/
|
|
||||||
|
ADD https://github.com/alexellis/faas/releases/download/v0.1-alpha/fwatchdog /usr/bin
|
||||||
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
|
||||||
COPY package.json .
|
COPY package.json .
|
||||||
COPY handler.js .
|
COPY handler.js .
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
RUN apk --update add nodejs
|
RUN apk --update add nodejs
|
||||||
COPY ./fwatchdog /usr/bin/
|
|
||||||
|
ADD https://github.com/alexellis/faas/releases/download/v0.1-alpha/fwatchdog /usr/bin
|
||||||
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
|
||||||
COPY package.json .
|
COPY package.json .
|
||||||
COPY handler.js .
|
COPY handler.js .
|
||||||
|
@ -7,7 +7,10 @@ WORKDIR /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats
|
|||||||
COPY . /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats
|
COPY . /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats
|
||||||
|
|
||||||
RUN make
|
RUN make
|
||||||
COPY fwatchdog /usr/bin/fwatchdog
|
|
||||||
|
ADD https://github.com/alexellis/faas/releases/download/v0.1-alpha/fwatchdog /usr/bin
|
||||||
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
|
||||||
ENV fprocess "/go/bin/DockerHubStats"
|
ENV fprocess "/go/bin/DockerHubStats"
|
||||||
|
|
||||||
CMD [ "/usr/bin/fwatchdog"]
|
CMD [ "/usr/bin/fwatchdog"]
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
RUN apk --update add nodejs
|
RUN apk --update add nodejs
|
||||||
COPY ./fwatchdog /usr/bin/
|
|
||||||
|
ADD https://github.com/alexellis/faas/releases/download/v0.1-alpha/fwatchdog /usr/bin
|
||||||
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
|
||||||
COPY package.json .
|
COPY package.json .
|
||||||
COPY handler.js .
|
COPY handler.js .
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
RUN apk --update add nodejs
|
RUN apk --update add nodejs
|
||||||
COPY ./fwatchdog /usr/bin/
|
|
||||||
|
ADD https://github.com/alexellis/faas/releases/download/v0.1-alpha/fwatchdog /usr/bin
|
||||||
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
|
||||||
COPY package.json .
|
COPY package.json .
|
||||||
COPY main.js .
|
COPY main.js .
|
||||||
|
@ -5,7 +5,9 @@ WORKDIR /go/src/app
|
|||||||
RUN go get -d -v
|
RUN go get -d -v
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
||||||
|
|
||||||
COPY fwatchdog /usr/bin/
|
ADD https://github.com/alexellis/faas/releases/download/v0.1-alpha/fwatchdog /usr/bin
|
||||||
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
# COPY fwatchdog /usr/bin/
|
||||||
|
|
||||||
ENV fprocess="/go/src/app/app"
|
ENV fprocess="/go/src/app/app"
|
||||||
CMD ["fwatchdog"]
|
CMD ["fwatchdog"]
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
FROM alexellis2/faas-alpinefunction
|
FROM alexellis2/faas-alpinefunction:latest
|
||||||
|
|
||||||
COPY fwatchdog /usr/bin/
|
|
||||||
|
|
||||||
# Populate example here
|
# Populate example here
|
||||||
ENV fprocess="wc -l"
|
ENV fprocess="wc"
|
||||||
CMD ["fwatchdog"]
|
CMD ["fwatchdog"]
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
ENTRYPOINT []
|
ENTRYPOINT []
|
||||||
|
|
||||||
COPY ./fwatchdog /usr/bin/fwatchdog
|
ADD https://github.com/alexellis/faas/releases/download/v0.1-alpha/fwatchdog /usr/bin
|
||||||
ENV fprocess "cat"
|
RUN chmod +x /usr/bin/fwatchdog
|
||||||
|
|
||||||
|
ENV fprocess "/bin/cat"
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
CMD ["/usr/bin/fwatchdog"]
|
CMD ["/usr/bin/fwatchdog"]
|
||||||
|
Reference in New Issue
Block a user