Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feature: add prometheus metrics (#990)
- Loading branch information
Showing
18 changed files
with
282 additions
and
33 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
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
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,47 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/getfider/fider/app/pkg/env" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var TotalTenants = prometheus.NewCounter( | ||
prometheus.CounterOpts{ | ||
Name: "fider_tenants_total", | ||
Help: "Number of Fider tenants.", | ||
}, | ||
) | ||
|
||
var TotalPosts = prometheus.NewCounter( | ||
prometheus.CounterOpts{ | ||
Name: "fider_posts_total", | ||
Help: "Number of Fider posts.", | ||
}, | ||
) | ||
|
||
var TotalComments = prometheus.NewCounter( | ||
prometheus.CounterOpts{ | ||
Name: "fider_comments_total", | ||
Help: "Number of Fider comments.", | ||
}, | ||
) | ||
|
||
var TotalVotes = prometheus.NewCounter( | ||
prometheus.CounterOpts{ | ||
Name: "fider_votes_total", | ||
Help: "Number of Fider votes.", | ||
}, | ||
) | ||
|
||
var fiderInfo = prometheus.NewGauge( | ||
prometheus.GaugeOpts{ | ||
Name: "fider_info", | ||
Help: "Information about Fider environment.", | ||
ConstLabels: prometheus.Labels{"version": env.Version()}, | ||
}, | ||
) | ||
|
||
func init() { | ||
fiderInfo.Inc() | ||
prometheus.MustRegister(TotalTenants, TotalPosts, TotalComments, TotalVotes, fiderInfo) | ||
} |
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,21 @@ | ||
package metrics | ||
|
||
import "github.com/prometheus/client_golang/prometheus" | ||
|
||
var HttpRequests = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "http_requests_total", | ||
Help: "Number of HTTP requests.", | ||
}, | ||
[]string{"code", "operation"}, | ||
) | ||
|
||
var HttpDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Name: "http_request_duration_seconds", | ||
Help: "Duration of HTTP requests.", | ||
Buckets: []float64{0.2, 0.5, 1, 2, 5}, | ||
}, []string{"operation"}) | ||
|
||
func init() { | ||
prometheus.MustRegister(HttpRequests, HttpDuration) | ||
} |
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,34 @@ | ||
package middlewares | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/getfider/fider/app/metrics" | ||
"github.com/getfider/fider/app/pkg/env" | ||
"github.com/getfider/fider/app/pkg/web" | ||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
// Instrumentation adds Prometheus HTTP Middlewares | ||
func Instrumentation() web.MiddlewareFunc { | ||
if !env.Config.Metrics.Enabled { | ||
return nil | ||
} | ||
|
||
return func(next web.HandlerFunc) web.HandlerFunc { | ||
return func(c *web.Context) error { | ||
begin := time.Now() | ||
|
||
err := next(c) | ||
|
||
operation := fmt.Sprintf("%s /%s", c.Request.Method, c.Param(httprouter.MatchedRoutePathParam)) | ||
code := strconv.Itoa(c.ResponseStatusCode) | ||
metrics.HttpRequests.WithLabelValues(code, operation).Inc() | ||
metrics.HttpDuration.WithLabelValues(operation).Observe(time.Since(begin).Seconds()) | ||
|
||
return err | ||
} | ||
} | ||
} |
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,58 @@ | ||
package middlewares_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/getfider/fider/app/metrics" | ||
"github.com/getfider/fider/app/middlewares" | ||
. "github.com/getfider/fider/app/pkg/assert" | ||
"github.com/getfider/fider/app/pkg/env" | ||
"github.com/getfider/fider/app/pkg/mock" | ||
"github.com/getfider/fider/app/pkg/web" | ||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
func TestInstrumentation_Disabled(t *testing.T) { | ||
RegisterT(t) | ||
|
||
defer func() { | ||
env.Config.Metrics.Enabled = true | ||
}() | ||
|
||
server := mock.NewServer() | ||
env.Config.Metrics.Enabled = false | ||
server.Use(middlewares.Instrumentation()) | ||
handler := func(c *web.Context) error { | ||
return c.NoContent(http.StatusOK) | ||
} | ||
|
||
status, _ := server.Execute(handler) | ||
|
||
Expect(status).Equals(http.StatusOK) | ||
Expect(getCounterValue(metrics.HttpRequests, "200", "GET /")).Equals(float64(0)) | ||
} | ||
|
||
func TestInstrumentation_Enabled(t *testing.T) { | ||
RegisterT(t) | ||
|
||
server := mock.NewServer() | ||
server.Use(middlewares.Instrumentation()) | ||
handler := func(c *web.Context) error { | ||
return c.NoContent(http.StatusOK) | ||
} | ||
|
||
status, _ := server.Execute(handler) | ||
|
||
Expect(status).Equals(http.StatusOK) | ||
Expect(getCounterValue(metrics.HttpRequests, "200", "GET /")).Equals(float64(1)) | ||
} | ||
|
||
func getCounterValue(metric *prometheus.CounterVec, lvs ...string) float64 { | ||
var m = &dto.Metric{} | ||
if err := metric.WithLabelValues(lvs...).Write(m); err != nil { | ||
return 0 | ||
} | ||
return m.Counter.GetValue() | ||
} |
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
Oops, something went wrong.