-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add middlewares & bump
- Loading branch information
Showing
12 changed files
with
272 additions
and
34 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
FROM golang:1.15.2 | ||
|
||
ARG RHINO_VERSION=1.2.2 | ||
ARG RHINO_VERSION=1.3.0 | ||
|
||
ENV GO111MODULE=on | ||
|
||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2020 Clivern. All rights reserved. | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
// Metrics controller | ||
func Metrics() http.Handler { | ||
return promhttp.Handler() | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2020 Clivern. All rights reserved. | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package middleware | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/clivern/rhino/core/util" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Correlation middleware | ||
func Correlation() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
corralationID := c.Request.Header.Get("X-Correlation-ID") | ||
|
||
if strings.TrimSpace(corralationID) == "" { | ||
c.Request.Header.Add("X-Correlation-ID", util.GenerateUUID4()) | ||
} | ||
c.Next() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2020 Clivern. All rights reserved. | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package middleware | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
|
||
"github.com/gin-gonic/gin" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Logger middleware | ||
func Logger() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// before request | ||
var bodyBytes []byte | ||
|
||
// Workaround for issue https://github.com/gin-gonic/gin/issues/1651 | ||
if c.Request.Body != nil { | ||
bodyBytes, _ = ioutil.ReadAll(c.Request.Body) | ||
} | ||
|
||
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) | ||
|
||
log.WithFields(log.Fields{ | ||
"correlation_id": c.Request.Header.Get("X-Correlation-ID"), | ||
"http_method": c.Request.Method, | ||
"http_path": c.Request.URL.Path, | ||
"request_body": string(bodyBytes), | ||
}).Info("Request started") | ||
|
||
c.Next() | ||
|
||
// after request | ||
status := c.Writer.Status() | ||
size := c.Writer.Size() | ||
|
||
log.WithFields(log.Fields{ | ||
"correlation_id": c.Request.Header.Get("X-Correlation-ID"), | ||
"http_status": status, | ||
"response_size": size, | ||
}).Info(`Request finished`) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2020 Clivern. All rights reserved. | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package middleware | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/prometheus/client_golang/prometheus" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
httpRequests = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: "rhino", | ||
Name: "total_http_requests", | ||
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.", | ||
}, []string{"code", "method", "handler", "host", "url"}) | ||
|
||
requestDuration = prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Subsystem: "rhino", | ||
Name: "request_duration_seconds", | ||
Help: "The HTTP request latencies in seconds.", | ||
}, | ||
[]string{"code", "method", "url"}, | ||
) | ||
|
||
responseSize = prometheus.NewSummary( | ||
prometheus.SummaryOpts{ | ||
Namespace: "rhino", | ||
Name: "response_size_bytes", | ||
Help: "The HTTP response sizes in bytes.", | ||
}, | ||
) | ||
) | ||
|
||
func init() { | ||
prometheus.MustRegister(httpRequests) | ||
prometheus.MustRegister(requestDuration) | ||
prometheus.MustRegister(responseSize) | ||
} | ||
|
||
// Metric middleware | ||
func Metric() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// before request | ||
start := time.Now() | ||
|
||
c.Next() | ||
|
||
// after request | ||
elapsed := float64(time.Since(start)) / float64(time.Second) | ||
|
||
log.WithFields(log.Fields{ | ||
"correlation_id": c.Request.Header.Get("X-Correlation-ID"), | ||
}).Info(`Collecting metrics`) | ||
|
||
// Collect Metrics | ||
httpRequests.WithLabelValues( | ||
strconv.Itoa(c.Writer.Status()), | ||
c.Request.Method, | ||
c.HandlerName(), | ||
c.Request.Host, | ||
c.Request.URL.Path, | ||
).Inc() | ||
|
||
requestDuration.WithLabelValues( | ||
strconv.Itoa(c.Writer.Status()), | ||
c.Request.Method, | ||
c.Request.URL.Path, | ||
).Observe(elapsed) | ||
|
||
responseSize.Observe(float64(c.Writer.Size())) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,31 @@ | ||
version: '3' | ||
services: | ||
rhino: | ||
image: 'clivern/rhino:release-1.2.2' | ||
image: 'clivern/rhino:release-1.3.0' | ||
ports: | ||
- "8080:8080" | ||
command: '/app/rhino serve -c /app/configs/config.prod.json' | ||
volumes: | ||
- './configs/:/app/configs' | ||
restart: unless-stopped | ||
|
||
prometheus: | ||
image: 'prom/prometheus:v2.21.0' | ||
volumes: | ||
- './prometheus/:/etc/prometheus' | ||
command: '--config.file=/etc/prometheus/prometheus.yml' | ||
ports: | ||
- '9090:9090' | ||
restart: unless-stopped | ||
|
||
grafana: | ||
image: 'grafana/grafana:6.7.4' | ||
environment: | ||
- GF_SECURITY_ADMIN_USER=${ADMIN_USER:-admin} | ||
- GF_SECURITY_ADMIN_PASSWORD=${ADMIN_PASSWORD:-admin} | ||
- GF_USERS_ALLOW_SIGN_UP=false | ||
ports: | ||
- '3000:3000' | ||
depends_on: | ||
- prometheus | ||
restart: unless-stopped |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# my global config | ||
global: | ||
evaluation_interval: 15s | ||
external_labels: | ||
monitor: rhino-monitor | ||
scrape_interval: 15s | ||
rule_files: ~ | ||
scrape_configs: | ||
- | ||
job_name: prometheus | ||
scrape_interval: 5s | ||
static_configs: | ||
- | ||
targets: | ||
- "localhost:9090" | ||
- | ||
job_name: rhino | ||
metrics_path: /_metrics | ||
scrape_interval: 5s | ||
static_configs: | ||
- | ||
targets: | ||
- "localhost:8080" |
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
Oops, something went wrong.