Commit 3d44524a authored by ale's avatar ale

Add tracing support to the authentication client

This does *not* add context propagation, just a span representing
the client-side Authenticate call.
parent ecc0ff2f
Pipeline #5934 passed with stages
in 1 minute and 36 seconds
......@@ -6,6 +6,7 @@ import (
"net/textproto"
"github.com/cenkalti/backoff"
"go.opencensus.io/trace"
"git.autistici.org/id/auth"
)
......@@ -29,19 +30,31 @@ func New(socketPath string) Client {
}
func (c *socketClient) Authenticate(ctx context.Context, req *auth.Request) (*auth.Response, error) {
// Create a tracing span for the authentication request.
sctx, span := trace.StartSpan(ctx, "auth-server.Authenticate",
trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
span.AddAttributes(
trace.StringAttribute("auth.service", req.Service),
trace.StringAttribute("auth.username", req.Username),
)
// Retry the request, with backoff, if we get a temporary
// network error.
var resp *auth.Response
err := backoff.Retry(func() error {
var err error
resp, err = c.doAuthenticate(ctx, req)
resp, err = c.doAuthenticate(sctx, req)
if err == nil {
return nil
} else if netErr, ok := err.(net.Error); ok && netErr.Temporary() {
return netErr
}
return backoff.Permanent(err)
}, backoff.WithContext(backoff.NewExponentialBackOff(), ctx))
}, backoff.WithContext(backoff.NewExponentialBackOff(), sctx))
span.SetStatus(responseToTraceStatus(resp, err))
return resp, err
}
......@@ -93,3 +106,23 @@ func (c *socketClient) doAuthenticate(ctx context.Context, req *auth.Request) (*
return nil, ctx.Err()
}
}
func responseToTraceStatus(resp *auth.Response, err error) trace.Status {
switch err {
case nil:
switch resp.Status {
case auth.StatusOK:
return trace.Status{Code: trace.StatusCodeOK, Message: "OK"}
case auth.StatusInsufficientCredentials:
return trace.Status{Code: trace.StatusCodePermissionDenied, Message: "Insufficient Credentials"}
default:
return trace.Status{Code: trace.StatusCodePermissionDenied, Message: "Authentication Failure"}
}
case context.Canceled:
return trace.Status{Code: trace.StatusCodeCancelled, Message: "CANCELED"}
case context.DeadlineExceeded:
return trace.Status{Code: trace.StatusCodeDeadlineExceeded, Message: "DEADLINE_EXCEEDED"}
default:
return trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment