Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openfaas-faas
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Team Jaz CS 598 CCC Final Project
openfaas-faas
Commits
6073cd79
Commit
6073cd79
authored
8 years ago
by
Alex
Browse files
Options
Downloads
Patches
Plain Diff
Add metrics
parent
191fdb03
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
README.md
+2
-0
2 additions, 0 deletions
README.md
gateway/Dockerfile
+8
-4
8 additions, 4 deletions
gateway/Dockerfile
gateway/metrics/metrics.go
+13
-0
13 additions, 0 deletions
gateway/metrics/metrics.go
gateway/server.go
+58
-17
58 additions, 17 deletions
gateway/server.go
with
81 additions
and
21 deletions
README.md
+
2
−
0
View file @
6073cd79
# faas
Functions as a service
Minimum requirements: Docker 1.13
gateway
=======
...
...
This diff is collapsed.
Click to expand it.
gateway/Dockerfile
+
8
−
4
View file @
6073cd79
...
...
@@ -4,12 +4,16 @@ RUN go get -d github.com/docker/docker/api/types \
&&
go get
-d
github.com/docker/docker/api/types/filters
\
&&
go get
-d
github.com/docker/docker/api/types/swarm
\
&&
go get
-d
github.com/docker/docker/client
RUN
go get github.com/gorilla/mux
\
&&
go get github.com/prometheus/client_golang/prometheus
WORKDIR
/go/src/app/
RUN
go get github.com/gorilla/mux
WORKDIR
/go/src/github.com/alexellis/faas/gateway
COPY
metrics metrics
COPY
server.go .
RUN
find /go/src/github.com/alexellis/faas/gateway/
RUN
go build
EXPOSE
8080
CMD
["./app"]
CMD
["./gateway"]
This diff is collapsed.
Click to expand it.
gateway/metrics/metrics.go
0 → 100644
+
13
−
0
View file @
6073cd79
package
metrics
import
(
"net/http"
"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
{
return
prometheus
.
Handler
()
}
This diff is collapsed.
Click to expand it.
gateway/server.go
+
58
−
17
View file @
6073cd79
...
...
@@ -5,17 +5,26 @@ import (
"context"
"log"
"net/http"
"time"
"io/ioutil"
"strconv"
"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/gorilla/mux"
"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
...
...
@@ -30,34 +39,66 @@ func lookupSwarmService(serviceName string) (bool, error) {
return
len
(
services
)
>
0
,
err
}
func
proxy
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
Method
==
"POST"
{
log
.
Println
(
r
.
Header
)
header
:=
r
.
Header
[
"X-Function"
]
log
.
Println
(
header
)
func
makeProxy
(
metrics
MetricOptions
)
http
.
HandlerFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
metrics
.
GatewayRequestsTotal
.
Inc
()
exists
,
err
:=
lookupSwarmService
(
header
[
0
])
if
err
!=
nil
{
log
.
Fatalln
(
err
)
}
start
:=
time
.
Now
()
if
exists
==
true
{
// client := http.Client{Timeout: time.Second * 2}
requestBody
,
_
:=
ioutil
.
ReadAll
(
r
.
Body
)
buf
:=
bytes
.
NewBuffer
(
requestBody
)
if
r
.
Method
==
"POST"
{
log
.
Println
(
r
.
Header
)
header
:=
r
.
Header
[
"X-Function"
]
log
.
Println
(
header
)
response
,
err
:=
http
.
Post
(
"http://"
+
header
[
0
]
+
":"
+
strconv
.
Itoa
(
8080
)
+
"/"
,
"text/plain"
,
buf
)
exists
,
err
:=
lookupSwarmService
(
header
[
0
]
)
if
err
!=
nil
{
log
.
Fatalln
(
err
)
}
responseBody
,
_
:=
ioutil
.
ReadAll
(
response
.
Body
)
w
.
Write
(
responseBody
)
if
exists
==
true
{
requestBody
,
_
:=
ioutil
.
ReadAll
(
r
.
Body
)
buf
:=
bytes
.
NewBuffer
(
requestBody
)
response
,
err
:=
http
.
Post
(
"http://"
+
header
[
0
]
+
":"
+
strconv
.
Itoa
(
8080
)
+
"/"
,
"text/plain"
,
buf
)
if
err
!=
nil
{
log
.
Fatalln
(
err
)
}
responseBody
,
_
:=
ioutil
.
ReadAll
(
response
.
Body
)
w
.
Write
(
responseBody
)
metrics
.
GatewayServerlessServedTotal
.
Inc
()
metrics
.
GatewayFunctions
.
Observe
(
time
.
Since
(
start
)
.
Seconds
())
}
}
}
}
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
)
r
:=
mux
.
NewRouter
()
r
.
HandleFunc
(
"/"
,
proxy
)
r
.
HandleFunc
(
"/"
,
makeProxy
(
MetricOptions
{
GatewayRequestsTotal
:
GatewayRequestsTotal
,
GatewayServerlessServedTotal
:
GatewayServerlessServedTotal
,
GatewayFunctions
:
GatewayFunctions
,
}))
metricsHandler
:=
metrics
.
Handler
()
r
.
Handle
(
"/metrics"
,
metricsHandler
)
log
.
Fatal
(
http
.
ListenAndServe
(
":8080"
,
r
))
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment