Skip to content

Commit

Permalink
Chores:
Browse files Browse the repository at this point in the history
- Added CHANGELOG.md
- Added a method to support NewWith(..) to pass servicename, namespace and subsystem
- fixed failing tests

Signed-off-by: Ankur Srivastava <best.ankur@gmail.com>
  • Loading branch information
ansrivas committed Jul 8, 2020
1 parent 162debf commit 1fdb18c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Change Log
---

## [2020-07-08] - 0.3.0
### Enhancements:
- Support a new method to provide a namespace and a subsystem for the service
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# fiberprometheus

Prometheus middleware for gofiber.

![Release](https://img.shields.io/github/release/ansrivas/fiberprometheus.svg)
Expand Down Expand Up @@ -32,10 +32,12 @@ import (
func main() {
app := fiber.New()

// This here will appear as a label, one can also use
// fiberprometheus.NewWith(servicename, namespace, subsystem )
prometheus := fiberprometheus.New("my-service-name")
prometheus.RegisterAt(app, "/metrics")
app.Use(prometheus.Middleware)

app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello World")
})
Expand All @@ -47,3 +49,6 @@ func main() {
app.Listen(3000)
}
```
### Result
- Hit the default url at http://localhost:3000
- Navigate to http://localhost:3000/metrics
25 changes: 20 additions & 5 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@ type FiberPrometheus struct {
defaultURL string
}

// New creates a new instance of FiberPrometheus middleware
func New(servicename string) *FiberPrometheus {
func create(servicename, namespace, subsystem string) *FiberPrometheus {
counter := promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Name: prometheus.BuildFQName(namespace, subsystem, "requests_total"),
Help: "Count all http requests by status code, method and path.",
ConstLabels: prometheus.Labels{"service": servicename},
},
[]string{"status_code", "method", "path"},
)
histogram := promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Name: prometheus.BuildFQName(namespace, subsystem, "request_duration_seconds"),
Help: "Duration of all HTTP requests by status code, method and path.",
ConstLabels: prometheus.Labels{"service": servicename},
},
[]string{"status_code", "method", "path"},
)

gauge := promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "http_requests_in_progress_total",
Name: prometheus.BuildFQName(namespace, subsystem, "requests_in_progress_total"),
Help: "All the requests in progress",
ConstLabels: prometheus.Labels{"service": servicename},
}, []string{"method", "path"})
Expand All @@ -51,6 +50,22 @@ func New(servicename string) *FiberPrometheus {
}
}

// New creates a new instance of FiberPrometheus middleware
// servicename is available as a const label
func New(servicename string) *FiberPrometheus {
return create(servicename, "http", "")
}

// NewWith creates a new instance of FiberPrometheus middleware but with an ability
// to pass namespace and a custom subsystem
// Here servicename is created as a constant-label for the metrics
// Namespace, subsystem get prefixed to the metrics.
// For e.g namespace = "my_app", subsyste = "http" then then metrics would be
// my_app_http_requests_total{...,"service": servicename}
func NewWith(servicename, namespace, subsystem string) *FiberPrometheus {
return create(servicename, namespace, subsystem)
}

func (ps *FiberPrometheus) handler(c *fiber.Ctx) {
p := fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler())
p(c.Fasthttp)
Expand Down
54 changes: 49 additions & 5 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,57 @@ func TestMiddleware(t *testing.T) {
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)
if !strings.Contains(string(body), `http_requests_total{method="GET",path="/",service="test-service",status_code="200"} 1`) {
t.Fail()
got := string(body)
want := `http_requests_total{method="GET",path="/",service="test-service",status_code="200"} 1`
if !strings.Contains(got, want) {
t.Errorf("got %s; want %s", got, want)
}
if !strings.Contains(string(body), `http_request_duration_seconds_count{method="GET",path="/",service="test-service",status_code="200"} 1`) {
t.Fail()

want = `http_request_duration_seconds_count{method="GET",path="/",service="test-service",status_code="200"} 1`
if !strings.Contains(got, want) {
t.Errorf("got %s; want %s", got, want)
}

want = `http_requests_in_progress_total{method="GET",path="/",service="test-service"} 0`
if !strings.Contains(got, want) {
t.Errorf("got %s; want %s", got, want)

}
if !strings.Contains(string(body), `http_requests_in_progress_total{method="GET",path="/",service="test-service"} 0`) {
}

func TestMiddlewareWithServiceName(t *testing.T) {
app := fiber.New()

prometheus := NewWith("test-service", "my_service", "http")
prometheus.RegisterAt(app, "/metrics")
app.Use(prometheus.Middleware)
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello World")
})
req := httptest.NewRequest("GET", "/", nil)
resp, _ := app.Test(req)
if resp.StatusCode != 200 {
t.Fail()
}

req = httptest.NewRequest("GET", "/metrics", nil)
resp, _ = app.Test(req)
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)
got := string(body)
want := `my_service_http_requests_total{method="GET",path="/",service="test-service",status_code="200"} 1`
if !strings.Contains(got, want) {
t.Errorf("got %s; want %s", got, want)
}

want = `my_service_http_request_duration_seconds_count{method="GET",path="/",service="test-service",status_code="200"} 1`
if !strings.Contains(got, want) {
t.Errorf("got %s; want %s", got, want)
}

want = `my_service_http_requests_in_progress_total{method="GET",path="/",service="test-service"} 0`
if !strings.Contains(got, want) {
t.Errorf("got %s; want %s", got, want)
}
}

0 comments on commit 1fdb18c

Please sign in to comment.