Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mutator tracing #1050

Merged
merged 14 commits into from
Jan 17, 2023
2 changes: 0 additions & 2 deletions driver/registry_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ type RegistryMemory struct {
errors map[string]pe.Handler

healthEventManager *health.DefaultHealthEventManager

ruleRepositoryLock sync.Mutex
}

func (r *RegistryMemory) Init() {
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ require (
github.com/ory/jsonschema/v3 v3.0.7
github.com/ory/ladon v1.1.0
github.com/ory/viper v1.7.5
github.com/ory/x v0.0.524
github.com/ory/x v0.0.529
github.com/pborman/uuid v1.2.1
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
Expand All @@ -76,7 +76,7 @@ require (
gocloud.dev v0.20.0
golang.org/x/crypto v0.1.0
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783
golang.org/x/tools v0.4.0
golang.org/x/tools v0.5.0
google.golang.org/api v0.84.0
google.golang.org/grpc v1.50.1
gopkg.in/square/go-jose.v2 v2.6.0
Expand Down Expand Up @@ -246,10 +246,10 @@ require (
go.opentelemetry.io/otel/sdk v1.11.1 // indirect
go.opentelemetry.io/proto/otlp v0.18.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221025140454-527a21cfbd71 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1313,8 +1313,8 @@ github.com/ory/x v0.0.93/go.mod h1:lfcTaGXpTZs7IEQAW00r9EtTCOxD//SiP5uWtNiz31g=
github.com/ory/x v0.0.110/go.mod h1:DJfkE3GdakhshNhw4zlKoRaL/ozg/lcTahA9OCih2BE=
github.com/ory/x v0.0.128/go.mod h1:ykx1XOsl9taQtoW2yNvuxl/feEfTfrZTcbY1U7841tI=
github.com/ory/x v0.0.162/go.mod h1:sj3z/MeCrAyNFFTfN6yK1nTmHXGSFnw+QwIIQ/Rowec=
github.com/ory/x v0.0.524 h1:U7JQKiaz+JpWWJvYYqdwVCqXcvI3W9uYO+4v7ew98Vk=
github.com/ory/x v0.0.524/go.mod h1:XBqhPZRppPHTxtsE0l0oI/B2Onf1QJtMRGPh3CpEpA0=
github.com/ory/x v0.0.529 h1:4E4i0XpIZaZxnkKZfzIZvXWhk+qAIPEpEy78tcyXeVM=
github.com/ory/x v0.0.529/go.mod h1:XBqhPZRppPHTxtsE0l0oI/B2Onf1QJtMRGPh3CpEpA0=
github.com/parnurzeal/gorequest v0.2.15/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand Down Expand Up @@ -1838,8 +1838,8 @@ golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221002022538-bcab6841153b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.3.0 h1:VWL6FNY2bEEmsGVKabSlHu5Irp34xmMRoqb/9lF9lxk=
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181003184128-c57b0facaced/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -2013,8 +2013,8 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand All @@ -2029,8 +2029,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -2143,8 +2143,8 @@ golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4=
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
43 changes: 20 additions & 23 deletions pipeline/mutate/mutator_hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import (
"time"

"github.com/dgraph-io/ristretto"

"github.com/ory/oathkeeper/pipeline/authn"
"github.com/ory/oathkeeper/x"

"github.com/ory/x/httpx"

"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"

"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/oathkeeper/pipeline"
"github.com/ory/oathkeeper/pipeline/authn"
"github.com/ory/oathkeeper/x"
"github.com/ory/x/httpx"
)

const (
Expand All @@ -37,12 +36,10 @@ const (
)

type MutatorHydrator struct {
c configuration.Provider
client *http.Client
d mutatorHydratorDependencies
c configuration.Provider
d mutatorHydratorDependencies

hydrateCache *ristretto.Cache
cacheTTL *time.Duration
}

type BasicAuth struct {
Expand Down Expand Up @@ -79,6 +76,7 @@ type MutatorHydratorConfig struct {

type mutatorHydratorDependencies interface {
x.RegistryLogger
Tracer() trace.Tracer
}

func NewMutatorHydrator(c configuration.Provider, d mutatorHydratorDependencies) *MutatorHydrator {
Expand All @@ -93,7 +91,6 @@ func NewMutatorHydrator(c configuration.Provider, d mutatorHydratorDependencies)
return &MutatorHydrator{
c: c,
d: d,
client: httpx.NewResilientClient().StandardClient(),
hydrateCache: cache,
}
}
Expand Down Expand Up @@ -172,32 +169,32 @@ func (a *MutatorHydrator) Mutate(r *http.Request, session *authn.AuthenticationS
}
req.Header.Set(contentTypeHeaderKey, contentTypeJSONHeaderValue)

var client *http.Client

client := http.DefaultClient
if a.d.Tracer() != nil {
client = otelhttp.DefaultClient
}
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
if cfg.Api.Retry != nil {
maxRetryDelay := time.Second
giveUpAfter := time.Millisecond * 50
giveUpAfter := time.Second
maxRetryDelay := 100 * time.Millisecond
if len(cfg.Api.Retry.MaxDelay) > 0 {
if d, err := time.ParseDuration(cfg.Api.Retry.MaxDelay); err != nil {
a.d.Logger().WithError(err).Warn("Unable to parse max_delay in the Hydrator Mutator, falling pack to default.")
a.d.Logger().WithError(err).Warnf("Unable to parse max_delay in the Hydrator Mutator, falling back to default (%v).", maxRetryDelay)
} else {
maxRetryDelay = d
}
}
if len(cfg.Api.Retry.GiveUpAfter) > 0 {
if d, err := time.ParseDuration(cfg.Api.Retry.GiveUpAfter); err != nil {
a.d.Logger().WithError(err).Warn("Unable to parse max_delay in the Hydrator Mutator, falling pack to default.")
a.d.Logger().WithError(err).Warnf("Unable to parse give_up_after in the Hydrator Mutator, falling back to default (%v).", giveUpAfter)
} else {
giveUpAfter = d
}
}

client = httpx.NewResilientClient(
httpx.ResilientClientWithMaxRetryWait(maxRetryDelay),
clientOpts := []httpx.ResilientOptions{
httpx.ResilientClientWithTracer(a.d.Tracer()),
httpx.ResilientClientWithConnectionTimeout(giveUpAfter),
).StandardClient()
} else {
client = http.DefaultClient
httpx.ResilientClientWithMaxRetryWait(maxRetryDelay)}
client = httpx.NewResilientClient(clientOpts...).StandardClient()
}

res, err := client.Do(req.WithContext(r.Context()))
Expand Down
4 changes: 2 additions & 2 deletions pipeline/mutate/mutator_hydrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -64,7 +64,7 @@ func defaultRouterSetup(actions ...func(a *authn.AuthenticationSession)) routerS
return func(t *testing.T) http.Handler {
router := httprouter.New()
router.POST("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
var data authn.AuthenticationSession
err = json.Unmarshal(body, &data)
Expand Down
75 changes: 15 additions & 60 deletions spec/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1914,70 +1914,25 @@
},
"sampling": {
"type": "object",
"propertyNames": {
"enum": ["type", "value", "server_url"]
},
"allOf": [
{
"oneOf": [
{
"properties": {
"type": {
"description": "The type of the sampler you want to use.",
"const": "const"
},
"value": {
"type": "integer",
"description": "The value passed to the sampler type that has been configured.",
"minimum": 0,
"maximum": 1
}
}
},
{
"properties": {
"type": {
"description": "The type of the sampler you want to use.",
"const": "rateLimiting"
},
"value": {
"type": "integer",
"description": "The value passed to the sampler type that has been configured.",
"minimum": 0
}
}
},
{
"properties": {
"type": {
"description": "The type of the sampler you want to use.",
"const": "probabilistic"
},
"value": {
"type": "number",
"description": "The value passed to the sampler type that has been configured.",
"minimum": 0,
"maximum": 1
}
}
}
]
"additionalProperties": false,
"properties": {
"server_url": {
"type": "string",
"description": "The address of jaeger-agent's HTTP sampling server.",
"format": "uri"
},
{
"properties": {
"server_url": {
"type": "string",
"description": "The address of jaeger-agent's HTTP sampling server",
"format": "uri"
}
}
"trace_id_ratio": {
"type": "number",
"description": "The initial sample ratio.",
"minimum": 0,
"maximum": 1,
"default": 1
}
],
},
"examples": [
{
"type": "const",
"value": 1,
"server_url": "http://localhost:5778/sampling"
"server_url": "http://localhost:5778/sampling",
"trace_id_ratio": 1
}
]
}
Expand Down
6 changes: 6 additions & 0 deletions x/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package x

import (
"go.opentelemetry.io/otel/trace"

"github.com/ory/x/logrusx"

"github.com/ory/herodot"
Expand All @@ -15,6 +17,10 @@ func (lp *TestLoggerProvider) Logger() *logrusx.Logger {
return logrusx.New("", "")
}

func (lp *TestLoggerProvider) Tracer() trace.Tracer {
return nil
}

type RegistryLogger interface {
Logger() *logrusx.Logger
}
Expand Down