Skip to content

Commit

Permalink
all: Moves to metrics-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored and arekkas committed Jun 7, 2018
1 parent 2b336e0 commit eb22c24
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 433 deletions.
12 changes: 9 additions & 3 deletions Gopkg.lock

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

10 changes: 5 additions & 5 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@
branch = "master"
name = "github.com/rubenv/sql-migrate"

[[constraint]]
name = "github.com/segmentio/analytics-go"
version = "3.0.0"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.3"
Expand All @@ -135,7 +131,7 @@

[[constraint]]
name = "github.com/urfave/negroni"
version = "0.2.0"
version = "0.3.0"

[[constraint]]
name = "github.com/ory/go-convenience"
Expand All @@ -149,6 +145,10 @@
branch = "v2"
name = "gopkg.in/yaml.v2"

[[constraint]]
branch = "master"
name = "github.com/ory/metrics-middleware"

[[constraint]]
name = "github.com/prometheus/client_golang"
version = "0.8.0"
8 changes: 7 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
package cmd

import (
"os"
"strconv"

"github.com/ory/hydra/cmd/server"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -210,7 +213,10 @@ func init() {
// is called directly, e.g.:
serveCmd.Flags().BoolVar(&c.ForceHTTP, "dangerous-force-http", false, "Disable HTTP/2 over TLS (HTTPS) and serve HTTP instead. Never use this in production.")
//serveCmd.Flags().Bool("dangerous-auto-logon", false, "Stores the root credentials in ~/.hydra.yml. Do not use in production.")
serveCmd.Flags().Bool("disable-telemetry", false, "Disable telemetry collection and sharing")

disableTelemetryEnv, _ := strconv.ParseBool(os.Getenv("DISABLE_TELEMETRY"))
serveCmd.Flags().Bool("disable-telemetry", disableTelemetryEnv, "Disable anonymized telemetry reports - for more information please visit https://www.ory.sh/docs/guides/telemetry")

serveCmd.Flags().String("https-tls-key-path", "", "Path to the key file for HTTP/2 over TLS (https). You can set HTTPS_TLS_KEY_PATH or HTTPS_TLS_KEY instead.")
serveCmd.Flags().String("https-tls-cert-path", "", "Path to the certificate file for HTTP/2 over TLS (https). You can set HTTPS_TLS_CERT_PATH or HTTPS_TLS_CERT instead.")
}
48 changes: 39 additions & 9 deletions cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"strings"

"github.com/gorilla/context"
"github.com/julienschmidt/httprouter"
Expand All @@ -36,9 +36,11 @@ import (
"github.com/ory/hydra/client"
"github.com/ory/hydra/config"
"github.com/ory/hydra/consent"
"github.com/ory/hydra/health"
"github.com/ory/hydra/jwk"
"github.com/ory/hydra/oauth2"
"github.com/ory/hydra/pkg"
"github.com/ory/metrics-middleware"
"github.com/pkg/errors"
"github.com/rs/cors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -72,17 +74,45 @@ func RunHost(c *config.Config) func(cmd *cobra.Command, args []string) {
}

n := negroni.New()
n.Use(negronilogrus.NewMiddlewareFromLogger(logger, c.Issuer))
n.Use(c.GetPrometheusMetrics())

if ok, _ := cmd.Flags().GetBool("disable-telemetry"); !ok && os.Getenv("DISABLE_TELEMETRY") != "1" {
telemetryMetrics := c.GetTelemetryMetrics()
go telemetryMetrics.RegisterSegment()
go telemetryMetrics.CommitMemoryStatistics()
n.Use(telemetryMetrics)
if ok, _ := cmd.Flags().GetBool("disable-telemetry"); !ok {
c.GetLogger().Println("Transmission of telemetry data is enabled, to learn more go to: https://www.ory.sh/docs/guides/latest/telemetry/")

enable := !(c.DatabaseURL == "" || c.DatabaseURL == "memory" || c.Issuer == "" || strings.Contains(c.Issuer, "localhost"))
m := metrics.NewMetricsManager(
metrics.Hash(c.Issuer+"|"+c.DatabaseURL),
enable,
"h8dRH3kVCWKkIFWydBmWsyYHR4M0u0vr",
[]string{
client.ClientsHandlerPath,
jwk.KeyHandlerPath,
jwk.WellKnownKeysPath,
oauth2.DefaultConsentPath,
oauth2.TokenPath,
oauth2.AuthPath,
oauth2.UserinfoPath,
oauth2.WellKnownPath,
oauth2.IntrospectPath,
oauth2.RevocationPath,
consent.ConsentPath,
consent.LoginPath,
health.AliveCheckPath,
health.ReadyCheckPath,
health.VersionPath,
health.MetricsPrometheusPath,
"/health/status",
"/",
},
logger,
"ory-hydra",
)
go m.RegisterSegment(c.BuildVersion, c.BuildHash, c.BuildTime)
go m.CommitMemoryStatistics()
n.Use(m)
}

n.Use(c.GetPrometheusMetrics())

n.Use(negronilogrus.NewMiddlewareFromLogger(logger, c.Issuer))
n.UseFunc(serverHandler.rejectInsecureRequests)
n.UseHandler(router)
corsHandler := cors.New(corsx.ParseOptions()).Handler(n)
Expand Down
1 change: 0 additions & 1 deletion cmd/server/handler_health_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func newHealthHandler(c *config.Config, router *httprouter.Router) *health.Handl
}

h := &health.Handler{
Metrics: c.GetTelemetryMetrics(),
H: herodot.NewJSONWriter(c.GetLogger()),
VersionString: c.BuildVersion,
ReadyChecks: map[string]health.ReadyChecker{
Expand Down
18 changes: 7 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"github.com/ory/go-convenience/urlx"
"github.com/ory/hydra/health"
"github.com/ory/hydra/metrics/prometheus"
"github.com/ory/hydra/metrics/telemetry"
"github.com/ory/hydra/pkg"
"github.com/ory/sqlcon"
"github.com/pkg/errors"
Expand Down Expand Up @@ -83,7 +82,6 @@ type Config struct {
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:"-"`
Expand Down Expand Up @@ -153,17 +151,10 @@ func (c *Config) GetLogger() *logrus.Logger {
return c.logger
}

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.telemetry
}

func (c *Config) GetPrometheusMetrics() *prometheus.MetricsManager {
c.GetLogger().Info("Setting up Prometheus middleware")

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

Expand Down Expand Up @@ -316,6 +307,11 @@ func (c *Config) GetSystemSecret() []byte {
return secret
}

if len(c.SystemSecret) > 0 {
c.GetLogger().Fatalln("System secret must be undefined or have at least 16 characters, but it has %d characters.", len(c.SystemSecret))
return nil
}

c.GetLogger().Warnf("Expected system secret to be at least %d characters long, got %d characters.", 32, len(c.SystemSecret))
c.GetLogger().Infoln("Generating a random system secret...")
var err error
Expand Down
17 changes: 11 additions & 6 deletions consent/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type Handler struct {
RequestMaxAge time.Duration
}

const (
LoginPath = "/oauth2/auth/requests/login"
ConsentPath = "/oauth2/auth/requests/consent"
)

func NewHandler(
h herodot.Writer,
m Manager,
Expand All @@ -49,13 +54,13 @@ func NewHandler(
}

func (h *Handler) SetRoutes(r *httprouter.Router) {
r.GET("/oauth2/auth/requests/login/:challenge", h.GetLoginRequest)
r.PUT("/oauth2/auth/requests/login/:challenge/accept", h.AcceptLoginRequest)
r.PUT("/oauth2/auth/requests/login/:challenge/reject", h.RejectLoginRequest)
r.GET(LoginPath+"/:challenge", h.GetLoginRequest)
r.PUT(LoginPath+"/:challenge/accept", h.AcceptLoginRequest)
r.PUT(LoginPath+"/:challenge/reject", h.RejectLoginRequest)

r.GET("/oauth2/auth/requests/consent/:challenge", h.GetConsentRequest)
r.PUT("/oauth2/auth/requests/consent/:challenge/accept", h.AcceptConsentRequest)
r.PUT("/oauth2/auth/requests/consent/:challenge/reject", h.RejectConsentRequest)
r.GET(ConsentPath+"/:challenge", h.GetConsentRequest)
r.PUT(ConsentPath+"/:challenge/accept", h.AcceptConsentRequest)
r.PUT(ConsentPath+"/:challenge/reject", h.RejectConsentRequest)
}

// swagger:route GET /oauth2/auth/requests/login/{challenge} oAuth2 getLoginRequest
Expand Down
2 changes: 0 additions & 2 deletions health/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/hydra/metrics/telemetry"
"github.com/ory/hydra/pkg"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
Expand All @@ -40,7 +39,6 @@ const (
type ReadyChecker func() error

type Handler struct {
Metrics *telemetry.MetricsManager
H *herodot.JSONWriter
VersionString string
ReadyChecks map[string]ReadyChecker
Expand Down
83 changes: 0 additions & 83 deletions metrics/telemetry/metrics.go

This file was deleted.

Loading

0 comments on commit eb22c24

Please sign in to comment.