Skip to content

Commit

Permalink
Prometheous (#7)
Browse files Browse the repository at this point in the history
* OData no longer defaulted, using header to cause it

* Simpler value

* Add Prometheus endpoint

Based on ory#827

JIRA: WAP-1891
  • Loading branch information
mgloystein authored May 14, 2018
1 parent 95d1755 commit f8ef740
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 20 deletions.
12 changes: 7 additions & 5 deletions cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ func RunHost(c *config.Config) func(cmd *cobra.Command, args []string) {

n := negroni.New()

metrics := c.GetMetrics()
telemetryMetrics := c.GetTelemetryMetrics()
if ok, _ := cmd.Flags().GetBool("disable-telemetry"); !ok && os.Getenv("DISABLE_TELEMETRY") != "1" {
go metrics.RegisterSegment(c.BuildVersion, c.BuildHash, c.BuildTime)
go metrics.CommitTelemetry()
go metrics.TickKeepAlive()
n.Use(metrics)
go telemetryMetrics.RegisterSegment(c.BuildVersion, c.BuildHash, c.BuildTime)
go telemetryMetrics.CommitTelemetry()
go telemetryMetrics.TickKeepAlive()
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 @@ -23,7 +23,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
28 changes: 19 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"os"
"strings"
"time"

"github.com/ory/ladon"
lmem "github.com/ory/ladon/manager/memory"
lsql "github.com/ory/ladon/manager/sql"
Expand All @@ -38,11 +37,12 @@ import (
foauth2 "github.com/spotxchange/fosite/handler/oauth2"
"github.com/spotxchange/fosite/token/hmac"
"github.com/spotxchange/hydra/health"
"github.com/spotxchange/hydra/metrics"
"github.com/spotxchange/hydra/pkg"
"github.com/spotxchange/hydra/metrics/telemetry"
"github.com/spotxchange/hydra/metrics/prometheus"
"github.com/spotxchange/hydra/warden/group"
"golang.org/x/oauth2"
"github.com/spotxchange/hydra/pkg"
"golang.org/x/oauth2/clientcredentials"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -81,7 +81,8 @@ type Config struct {
BuildHash string `yaml:"-"`
BuildTime string `yaml:"-"`
logger *logrus.Logger `yaml:"-"`
metrics *metrics.MetricsManager `yaml:"-"`
telemetry *telemetry.MetricsManager `yaml:"-"`
prometheus *prometheus.MetricsManager `yaml:"-"`
cluster *url.URL `yaml:"-"`
oauth2Client *http.Client `yaml:"-"`
context *Context `yaml:"-"`
Expand Down Expand Up @@ -143,12 +144,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())
func (c *Config) GetTelemetryMetrics() *telemetry.MetricsManager {
if c.telemetry == nil {
c.telemetry = telemetry.NewMetricsManager(c.Issuer, c.DatabaseURL, c.GetLogger())
}

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.metrics
return c.prometheus
}

func (c *Config) DoesRequestSatisfyTermination(r *http.Request) error {
Expand Down
7 changes: 5 additions & 2 deletions health/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/spotxchange/hydra/firewall"
"github.com/spotxchange/hydra/metrics"
"github.com/spotxchange/hydra/metrics/telemetry"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

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

type Handler struct {
Metrics *metrics.MetricsManager
Metrics *telemetry.MetricsManager
H *herodot.JSONWriter
W firewall.Firewall
ResourcePrefix string
Expand All @@ -50,6 +52,7 @@ func (h *Handler) PrefixResource(resource string) string {
func (h *Handler) SetRoutes(r *httprouter.Router) {
r.GET(HealthStatusPath, h.Health)
r.GET(HealthMetricsPath, h.Statistics)
r.Handler("GET", PrometheusStatusPath, promhttp.Handler())
}

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

// Metrics prototypes
// Example:
// Counter *prometheus.CounterVec
// ResponseTime *prometheus.HistogramVec
type Metrics struct{}

// Method for creation new custom Prometheus metrics
// Example:
// pm := &Metrics{
// Counter: prometheus.NewCounterVec(
// prometheus.CounterOpts{
// Name: "servicename_requests_total",
// Help: "Description",
// ConstLabels: map[string]string{
// "version": version,
// "hash": hash,
// "buildTime": buildTime,
// },
// },
// []string{"endpoint"},
// ),
// ResponseTime: prometheus.NewHistogramVec(
// prometheus.HistogramOpts{
// Name: "servicename_response_time_seconds",
// Help: "Description",
// ConstLabels: map[string]string{
// "version": version,
// "hash": hash,
// "buildTime": buildTime,
// },
// },
// []string{"endpoint"},
// ),
// }
// prometheus.Register(pm.Counter)
// prometheus.Register(pm.ResponseTime)
func NewMetrics(version, hash, buildTime string) *Metrics {
pm := &Metrics{}
return pm
}
27 changes: 27 additions & 0 deletions metrics/prometheus/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package prometheus

import (
"net/http"
)

type MetricsManager struct {
prometheusMetrics *Metrics
}

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

// Main middleware method to collect metrics for Prometheus.
// Example:
// start := time.Now()
// next(rw, r)
// Request counter metric
// pmm.prometheusMetrics.Counter.WithLabelValues(r.URL.Path).Inc()
// Response time metric
// pmm.prometheusMetrics.ResponseTime.WithLabelValues(r.URL.Path).Observe(time.Since(start).Seconds())
func (pmm *MetricsManager) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
next(rw, r)
}
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 @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics
package telemetry

import (
"runtime"
Expand Down
2 changes: 1 addition & 1 deletion metrics/middleware.go → metrics/telemetry/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics
package telemetry

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics_test
package telemetry

import (
"fmt"
Expand Down

0 comments on commit f8ef740

Please sign in to comment.