From f03205631dfea0be15f82814db54b96307b4dce1 Mon Sep 17 00:00:00 2001 From: Patrick Tescher Date: Mon, 9 Mar 2020 14:05:58 -0700 Subject: [PATCH] Enable tracing --- cmd/server/server.go | 5 +++++ docker-compose.yml | 17 +++++++++++++++++ driver/configuration/provider.go | 5 +++++ driver/configuration/provider_viper.go | 22 ++++++++++++++++++++++ driver/registry.go | 2 ++ driver/registry_memory.go | 19 +++++++++++++++++++ 6 files changed, 70 insertions(+) diff --git a/cmd/server/server.go b/cmd/server/server.go index 0e4c6b637f..cf6bdd05a9 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -182,6 +182,11 @@ func RunServe(version, build, date string) func(cmd *cobra.Command, args []strin adminmw.Use(metrics) publicmw.Use(metrics) + if tracer := d.Registry().Tracer(); tracer.IsLoaded() { + adminmw.Use(tracer) + publicmw.Use(tracer) + } + var wg sync.WaitGroup tasks := []func(){ runAPI(d, adminmw, logger), diff --git a/docker-compose.yml b/docker-compose.yml index 5b20543daf..66dbf230ec 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,8 +10,25 @@ services: - "4456:4456" command: serve --config=/etc/config/oathkeeper/config.yaml + environment: + - TRACING_PROVIDER=jaeger + - TRACING_PROVIDER_JAEGER_SAMPLING_SERVER_URL=http://jaeger:5778/sampling + - TRACING_PROVIDER_JAEGER_LOCAL_AGENT_ADDRESS=jaeger:6831 + - TRACING_PROVIDER_JAEGER_SAMPLING_TYPE=const + - TRACING_PROVIDER_JAEGER_SAMPLING_VALUE=1 volumes: - type: bind source: ./.docker_compose target: /etc/config/oathkeeper restart: on-failure + jaeger: + image: jaegertracing/all-in-one + ports: + - "16686:16686" # The UI port +# These are ports for collecting, sampling, agents, ... +# - "5775:5775/udp" +# - "6831:6831/udp" +# - "6832:6832/udp" +# - "5778:5778" +# - "14268:14268" +# - "9411:9411" diff --git a/driver/configuration/provider.go b/driver/configuration/provider.go index 9400bc6411..62dbcb3337 100644 --- a/driver/configuration/provider.go +++ b/driver/configuration/provider.go @@ -9,6 +9,7 @@ import ( "github.com/sirupsen/logrus" "github.com/ory/fosite" + "github.com/ory/x/tracing" "github.com/rs/cors" ) @@ -51,6 +52,10 @@ type Provider interface { ToScopeStrategy(value string, key string) fosite.ScopeStrategy ParseURLs(sources []string) ([]url.URL, error) JSONWebKeyURLs() []string + + TracingServiceName() string + TracingProvider() string + TracingJaegerConfig() *tracing.JaegerConfig } type ProviderErrorHandlers interface { diff --git a/driver/configuration/provider_viper.go b/driver/configuration/provider_viper.go index 42b62d063f..2250028ad3 100644 --- a/driver/configuration/provider_viper.go +++ b/driver/configuration/provider_viper.go @@ -23,6 +23,7 @@ import ( "github.com/ory/fosite" "github.com/ory/gojsonschema" "github.com/ory/x/corsx" + "github.com/ory/x/tracing" "github.com/ory/x/urlx" "github.com/ory/x/viperx" @@ -373,3 +374,24 @@ func (v *ViperProvider) MutatorConfig(id string, override json.RawMessage, dest func (v *ViperProvider) JSONWebKeyURLs() []string { return viperx.GetStringSlice(v.l, ViperKeyMutatorIDTokenJWKSURL, []string{}) } + +func (v *ViperProvider) TracingServiceName() string { + return viperx.GetString(v.l, "tracing.service_name", "ORY Oathkeeper") +} + +func (v *ViperProvider) TracingProvider() string { + return viperx.GetString(v.l, "tracing.provider", "", "TRACING_PROVIDER") +} + +func (v *ViperProvider) TracingJaegerConfig() *tracing.JaegerConfig { + return &tracing.JaegerConfig{ + LocalAgentHostPort: viperx.GetString(v.l, "tracing.providers.jaeger.local_agent_address", "", "TRACING_PROVIDER_JAEGER_LOCAL_AGENT_ADDRESS"), + SamplerType: viperx.GetString(v.l, "tracing.providers.jaeger.sampling.type", "const", "TRACING_PROVIDER_JAEGER_SAMPLING_TYPE"), + SamplerValue: viperx.GetFloat64(v.l, "tracing.providers.jaeger.sampling.value", float64(1), "TRACING_PROVIDER_JAEGER_SAMPLING_VALUE"), + SamplerServerURL: viperx.GetString(v.l, "tracing.providers.jaeger.sampling.server_url", "", "TRACING_PROVIDER_JAEGER_SAMPLING_SERVER_URL"), + Propagation: stringsx.Coalesce( + viper.GetString("JAEGER_PROPAGATION"), // Standard Jaeger client config + viperx.GetString(v.l, "tracing.providers.jaeger.propagation", "", "TRACING_PROVIDER_JAEGER_PROPAGATION"), + ), + } +} diff --git a/driver/registry.go b/driver/registry.go index 62ee1f0ffe..f8e62ca82c 100644 --- a/driver/registry.go +++ b/driver/registry.go @@ -15,6 +15,7 @@ import ( "github.com/ory/oathkeeper/rule" "github.com/ory/oathkeeper/x" "github.com/ory/x/healthx" + "github.com/ory/x/tracing" ) type Registry interface { @@ -34,6 +35,7 @@ type Registry interface { CredentialHandler() *api.CredentialsHandler Proxy() *proxy.Proxy + Tracer() *tracing.Tracer authn.Registry authz.Registry diff --git a/driver/registry_memory.go b/driver/registry_memory.go index b66d718d9f..46d7cd5e8c 100644 --- a/driver/registry_memory.go +++ b/driver/registry_memory.go @@ -10,6 +10,7 @@ import ( "github.com/ory/oathkeeper/proxy" "github.com/ory/x/logrusx" + "github.com/ory/x/tracing" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -38,6 +39,7 @@ type RegistryMemory struct { logger logrus.FieldLogger writer herodot.Writer c configuration.Provider + trc *tracing.Tracer ch *api.CredentialsHandler @@ -390,3 +392,20 @@ func (r *RegistryMemory) prepareMutators() { } } } + +func (r *RegistryMemory) Tracer() *tracing.Tracer { + if r.trc == nil { + r.trc = &tracing.Tracer{ + ServiceName: r.c.TracingServiceName(), + JaegerConfig: r.c.TracingJaegerConfig(), + Provider: r.c.TracingProvider(), + Logger: r.Logger(), + } + + if err := r.trc.Setup(); err != nil { + r.Logger().WithError(err).Fatalf("Unable to initialize Tracer.") + } + } + + return r.trc +}