Skip to content

Commit

Permalink
[Prometheus] Add new prometheus metrics and metrics endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dolbik <dolbik@gmail.com>
  • Loading branch information
ddolbik committed Apr 26, 2018
1 parent 174b6fd commit 888fd6b
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 26 deletions.
50 changes: 49 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,7 @@
[[constraint]]
branch = "v2"
name = "gopkg.in/yaml.v2"

[[constraint]]
name = "github.com/prometheus/client_golang"
version = "0.8.0"
10 changes: 6 additions & 4 deletions cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ func RunHost(c *config.Config) func(cmd *cobra.Command, args []string) {
n := negroni.New()

if ok, _ := cmd.Flags().GetBool("disable-telemetry"); !ok && os.Getenv("DISABLE_TELEMETRY") != "1" {
metrics := c.GetMetrics()
go metrics.RegisterSegment()
go metrics.CommitMemoryStatistics()
n.Use(metrics)
telemetryMetrics := c.GetTelemetryMetrics()
go telemetryMetrics.RegisterSegment()
go telemetryMetrics.CommitMemoryStatistics()
n.Use(telemetryMetrics)
}

n.Use(c.GetPrometheusMetrics())

n.Use(negronilogrus.NewMiddlewareFromLogger(logger, c.Issuer))
n.UseFunc(serverHandler.rejectInsecureRequests)
n.UseHandler(router)
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/handler_health_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

func newHealthHandler(c *config.Config, router *httprouter.Router) *health.Handler {
h := &health.Handler{
Metrics: c.GetMetrics(),
Metrics: c.GetTelemetryMetrics(),
H: herodot.NewJSONWriter(c.GetLogger()),
W: c.Context().Warden,
ResourcePrefix: c.AccessControlResourcePrefix,
Expand Down
39 changes: 25 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import (
foauth2 "github.com/ory/fosite/handler/oauth2"
"github.com/ory/fosite/token/hmac"
"github.com/ory/hydra/health"
"github.com/ory/hydra/metrics"
"github.com/ory/hydra/metrics/telemetry"
"github.com/ory/hydra/metrics/prometheus"
"github.com/ory/hydra/pkg"
"github.com/ory/hydra/warden/group"
"github.com/ory/ladon"
Expand Down Expand Up @@ -84,15 +85,16 @@ type Config struct {
SendOAuth2DebugMessagesToClients bool `mapstructure:"OAUTH2_SHARE_ERROR_DEBUG" yaml:"-"`
ForceHTTP bool `yaml:"-"`

BuildVersion string `yaml:"-"`
BuildHash string `yaml:"-"`
BuildTime string `yaml:"-"`
logger *logrus.Logger `yaml:"-"`
metrics *metrics.MetricsManager `yaml:"-"`
cluster *url.URL `yaml:"-"`
oauth2Client *http.Client `yaml:"-"`
context *Context `yaml:"-"`
systemSecret []byte `yaml:"-"`
BuildVersion string `yaml:"-"`
BuildHash string `yaml:"-"`
BuildTime string `yaml:"-"`
logger *logrus.Logger `yaml:"-"`
telemetry *telemetry.MetricsManager `yaml:"-"`
prometheus *prometheus.MetricsManager `yaml:"-"`
cluster *url.URL `yaml:"-"`
oauth2Client *http.Client `yaml:"-"`
context *Context `yaml:"-"`
systemSecret []byte `yaml:"-"`
}

func (c *Config) GetClusterURLWithoutTailingSlash() string {
Expand Down Expand Up @@ -154,12 +156,21 @@ func (c *Config) GetLogger() *logrus.Logger {
return c.logger
}

func (c *Config) GetMetrics() *metrics.MetricsManager {
if c.metrics == nil {
c.metrics = metrics.NewMetricsManager(c.Issuer, c.DatabaseURL, c.GetLogger(), c.BuildVersion, c.BuildHash, c.BuildTime)
func (c *Config) GetTelemetryMetrics() *telemetry.MetricsManager {
if c.telemetry == nil {
c.telemetry = telemetry.NewMetricsManager(c.Issuer, c.DatabaseURL, c.GetLogger(), c.BuildVersion, c.BuildHash, c.BuildTime)
}

return c.metrics
return c.telemetry
}

func (c *Config) GetPrometheusMetrics() *prometheus.MetricsManager {
if c.prometheus == nil {
c.GetLogger().Info("Setting up Prometheus metrics")
c.prometheus = prometheus.NewMetricsManager(c.BuildVersion, c.BuildHash, c.BuildTime)
}

return c.prometheus
}

func (c *Config) DoesRequestSatisfyTermination(r *http.Request) error {
Expand Down
9 changes: 6 additions & 3 deletions health/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/hydra/firewall"
"github.com/ory/hydra/metrics"
"github.com/ory/hydra/metrics/telemetry"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
HealthStatusPath = "/health/status"
HealthStatusPath = "/health/status"
PrometheusStatusPath = "/health/prometheus"
)

type Handler struct {
Metrics *metrics.MetricsManager
Metrics *telemetry.MetricsManager
H *herodot.JSONWriter
W firewall.Firewall
ResourcePrefix string
Expand All @@ -54,6 +56,7 @@ func (h *Handler) PrefixResource(resource string) string {

func (h *Handler) SetRoutes(r *httprouter.Router) {
r.GET(HealthStatusPath, h.Health)
r.Handler("GET", PrometheusStatusPath, promhttp.Handler())
}

// swagger:route GET /health/status health getInstanceStatus
Expand Down
37 changes: 37 additions & 0 deletions metrics/prometheus/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package prometheus

import "github.com/prometheus/client_golang/prometheus"

type Metrics struct {
Counter *prometheus.CounterVec
ResponseTime *prometheus.HistogramVec
}

func NewMetrics(version, hash, buildTime string) *Metrics {
labels := map[string]string{
"version": version,
"hash": hash,
"buildTime": buildTime,
}
pm := &Metrics{
Counter: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "sts_requests_total",
Help: "Secure token service requests served per endpoint",
ConstLabels: labels,
},
[]string{"endpoint"},
),
ResponseTime: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "sts_response_time_seconds",
Help: "Secure token service response time served per endpoint",
ConstLabels: labels,
},
[]string{"endpoint"},
),
}
prometheus.Register(pm.Counter)
prometheus.Register(pm.ResponseTime)
return pm
}
23 changes: 23 additions & 0 deletions metrics/prometheus/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package prometheus

import (
"net/http"
"time"
)

type MetricsManager struct {
prometheusMetrics *Metrics
}

func NewMetricsManager(version, hash, buildTime string) *MetricsManager {
return &MetricsManager{
prometheusMetrics: NewMetrics(version, hash, buildTime),
}
}

func (pmm *MetricsManager) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
next(rw, r)
pmm.prometheusMetrics.Counter.WithLabelValues(r.URL.Path).Inc()
pmm.prometheusMetrics.ResponseTime.WithLabelValues(r.URL.Path).Observe(time.Since(start).Seconds())
}
2 changes: 1 addition & 1 deletion metrics/metrics.go → metrics/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @license Apache-2.0
*/

package metrics
package telemetry

import (
"runtime"
Expand Down
3 changes: 2 additions & 1 deletion metrics/middleware.go → metrics/telemetry/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @license Apache-2.0
*/

package metrics
package telemetry

import (
"crypto/sha256"
Expand Down Expand Up @@ -100,6 +100,7 @@ func NewMetricsManager(issuerURL string, databaseURL string, l logrus.FieldLogge
salt: uuid.New(),
BuildTime: buildTime, BuildVersion: version, BuildHash: hash,
}

return mm
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @license Apache-2.0
*/

package metrics
package telemetry

import (
"net/url"
Expand Down

0 comments on commit 888fd6b

Please sign in to comment.