Skip to content

Commit

Permalink
feat: trace for upstream request (#931)
Browse files Browse the repository at this point in the history
Closes #928

Signed-off-by: sawadashota <[email protected]>
  • Loading branch information
sawadashota committed Mar 25, 2022
1 parent 4357b10 commit 21ff340
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
29 changes: 2 additions & 27 deletions pipeline/authn/authenticator_oauth2_introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (

"github.com/dgraph-io/ristretto"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"

"github.com/pkg/errors"
"golang.org/x/oauth2/clientcredentials"

Expand All @@ -28,6 +25,7 @@ import (
"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/oathkeeper/helper"
"github.com/ory/oathkeeper/pipeline"
"github.com/ory/oathkeeper/x"
)

type AuthenticatorOAuth2IntrospectionConfiguration struct {
Expand Down Expand Up @@ -171,29 +169,6 @@ func (a *AuthenticatorOAuth2Introspection) tokenToCache(config *AuthenticatorOAu
}
}

func (a *AuthenticatorOAuth2Introspection) traceRequest(ctx context.Context, req *http.Request) func() {
tracer := opentracing.GlobalTracer()
if tracer == nil {
return func() {}
}

parentSpan := opentracing.SpanFromContext(ctx)
opts := make([]opentracing.StartSpanOption, 0, 1)
if parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
}

urlStr := req.URL.String()
clientSpan := tracer.StartSpan(req.Method+" "+urlStr, opts...)

ext.SpanKindRPCClient.Set(clientSpan)
ext.HTTPUrl.Set(clientSpan, urlStr)
ext.HTTPMethod.Set(clientSpan, req.Method)

_ = tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
return clientSpan.Finish
}

func (a *AuthenticatorOAuth2Introspection) Authenticate(r *http.Request, session *AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
cf, client, err := a.Config(config)
if err != nil {
Expand Down Expand Up @@ -229,7 +204,7 @@ func (a *AuthenticatorOAuth2Introspection) Authenticate(r *http.Request, session
introspectReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")

// add tracing
closeSpan := a.traceRequest(r.Context(), introspectReq)
closeSpan := x.TraceRequest(r.Context(), introspectReq)

resp, err := client.Do(introspectReq.WithContext(r.Context()))

Expand Down
4 changes: 4 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (d *Proxy) RoundTrip(r *http.Request) (*http.Response, error) {
WithFields(fields).
Warn("Unable to type assert context")

// add tracing
closeSpan := x.TraceRequest(r.Context(), r)
defer closeSpan()

d.r.ProxyRequestHandler().HandleError(rw, r, rl, err)

return &http.Response{
Expand Down
33 changes: 33 additions & 0 deletions x/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package x

import (
"context"
"net/http"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)

func TraceRequest(ctx context.Context, req *http.Request) func() {
tracer := opentracing.GlobalTracer()
if tracer == nil {
return func() {}
}

parentSpan := opentracing.SpanFromContext(ctx)
opts := make([]opentracing.StartSpanOption, 0, 2)
opts = append(opts, ext.SpanKindRPCClient)
if parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
}

urlStr := req.URL.String()
clientSpan := tracer.StartSpan("HTTP "+req.Method, opts...)

ext.SpanKindRPCClient.Set(clientSpan)
ext.HTTPUrl.Set(clientSpan, urlStr)
ext.HTTPMethod.Set(clientSpan, req.Method)

_ = tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
return clientSpan.Finish
}

0 comments on commit 21ff340

Please sign in to comment.