Skip to content

Commit

Permalink
cmd: Adds and improves metrics middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored and arekkas committed May 29, 2018
1 parent 3f259da commit b0dfd97
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

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

2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestCommandLineInterface(t *testing.T) {
expectErr bool
}{
{
args: []string{"serve", "api"},
args: []string{"--disable-telemetry", "serve", "api"},
wait: func() bool {
res, err := http.Get("http://localhost:4456")
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ package cmd
import (
"fmt"

"os"
"strconv"

"github.com/spf13/cobra"
)

Expand All @@ -35,4 +38,7 @@ var serveCmd = &cobra.Command{

func init() {
RootCmd.AddCommand(serveCmd)

disableTelemetryEnv, _ := strconv.ParseBool(os.Getenv("DISABLE_TELEMETRY"))
serveCmd.PersistentFlags().Bool("disable-telemetry", disableTelemetryEnv, "Disable anonymized telemetry reports - for more information please visit https://www.ory.sh/docs/guides/telemetry")
}
23 changes: 19 additions & 4 deletions cmd/serve_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ory/go-convenience/corsx"
"github.com/ory/graceful"
"github.com/ory/herodot"
"github.com/ory/metrics-middleware"
"github.com/ory/oathkeeper/rsakey"
"github.com/ory/oathkeeper/rule"
"github.com/rs/cors"
Expand All @@ -37,8 +38,8 @@ import (
"github.com/urfave/negroni"
)

// managementCmd represents the management command
var managementCmd = &cobra.Command{
// serveApiCmd represents the management command
var serveApiCmd = &cobra.Command{
Use: "api",
Short: "Starts the ORY Oathkeeper HTTP API",
Long: `This starts a HTTP/2 REST API for managing ORY Oathkeeper.
Expand Down Expand Up @@ -80,8 +81,22 @@ HTTP CONTROLS

n := negroni.New()
n.Use(negronilogrus.NewMiddlewareFromLogger(logger, "oathkeeper-api"))
n.UseHandler(router)

if ok, _ := cmd.Flags().GetBool("disable-telemetry"); !ok {
segmentMiddleware := metrics.NewMetricsManager(
metrics.Hash(viper.GetString("DATABASE_URL")),
viper.GetString("DATABASE_URL") != "memory",
"MSx9A6YQ1qodnkzEFOv22cxOmOCJXMFa",
[]string{"/rules", "/.well-known/jwks.json"},
logger,
"ory-oathkeeper-api",
)
go segmentMiddleware.RegisterSegment(Version, GitHash, BuildTime)
go segmentMiddleware.CommitMemoryStatistics()
n.Use(segmentMiddleware)
}

n.UseHandler(router)
ch := cors.New(corsx.ParseOptions()).Handler(n)

go refreshKeys(keyManager, 0)
Expand All @@ -102,5 +117,5 @@ HTTP CONTROLS
}

func init() {
serveCmd.AddCommand(managementCmd)
serveCmd.AddCommand(serveApiCmd)
}
27 changes: 15 additions & 12 deletions cmd/serve_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,24 @@ OTHER CONTROLS
Transport: d,
}

segmentMiddleware := metrics.NewMetricsManager(
metrics.Hash(viper.GetString("DATABASE_URL")),
viper.GetString("DATABASE_URL") != "memory",
"MSx9A6YQ1qodnkzEFOv22cxOmOCJXMFa",
[]string{"/"},
logger,
)
go segmentMiddleware.RegisterSegment(Version, GitHash, BuildTime)
go segmentMiddleware.CommitMemoryStatistics()

n := negroni.New()
n.Use(negronilogrus.NewMiddlewareFromLogger(logger, "oathkeeper-proxy"))
n.Use(segmentMiddleware)
n.UseHandler(handler)

if ok, _ := cmd.Flags().GetBool("disable-telemetry"); !ok {
segmentMiddleware := metrics.NewMetricsManager(
metrics.Hash(viper.GetString("DATABASE_URL")),
viper.GetString("DATABASE_URL") != "memory",
"MSx9A6YQ1qodnkzEFOv22cxOmOCJXMFa",
[]string{"/"},
logger,
"ory-oathkeeper-proxy",
)
go segmentMiddleware.RegisterSegment(Version, GitHash, BuildTime)
go segmentMiddleware.CommitMemoryStatistics()
n.Use(segmentMiddleware)
}

n.UseHandler(handler)
ch := cors.New(corsx.ParseOptions()).Handler(n)

var cert tls.Certificate
Expand Down

0 comments on commit b0dfd97

Please sign in to comment.