Skip to content

Commit 2be385c

Browse files
authored
Merge pull request moby#5432 from tonistiigi/otel-auth-update
Better OTEL tracing for authprovider
2 parents 4b4a9aa + c4a9c39 commit 2be385c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

session/auth/authprovider/authprovider.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/moby/buildkit/session"
2525
"github.com/moby/buildkit/session/auth"
2626
"github.com/moby/buildkit/util/progress/progresswriter"
27+
"github.com/moby/buildkit/util/tracing"
2728
"github.com/pkg/errors"
2829
"golang.org/x/crypto/nacl/sign"
2930
"google.golang.org/grpc"
@@ -73,7 +74,7 @@ func (ap *authProvider) Register(server *grpc.Server) {
7374
}
7475

7576
func (ap *authProvider) FetchToken(ctx context.Context, req *auth.FetchTokenRequest) (rr *auth.FetchTokenResponse, err error) {
76-
ac, err := ap.getAuthConfig(req.Host)
77+
ac, err := ap.getAuthConfig(ctx, req.Host)
7778
if err != nil {
7879
return nil, err
7980
}
@@ -83,7 +84,7 @@ func (ap *authProvider) FetchToken(ctx context.Context, req *auth.FetchTokenRequ
8384
return toTokenResponse(ac.RegistryToken, time.Time{}, 0), nil
8485
}
8586

86-
creds, err := ap.credentials(req.Host)
87+
creds, err := ap.credentials(ctx, req.Host)
8788
if err != nil {
8889
return nil, err
8990
}
@@ -96,11 +97,11 @@ func (ap *authProvider) FetchToken(ctx context.Context, req *auth.FetchTokenRequ
9697
Secret: creds.Secret,
9798
}
9899

99-
httpClient := http.DefaultClient()
100+
httpClient := tracing.DefaultClient
100101
if tc, err := ap.tlsConfig(req.Host); err == nil && tc != nil {
101102
transport := http.DefaultTransport()
102103
transport.TLSClientConfig = tc
103-
httpClient.Transport = transport
104+
httpClient.Transport = tracing.NewTransport(transport)
104105
}
105106

106107
if creds.Secret != "" {
@@ -186,8 +187,8 @@ func (ap *authProvider) tlsConfig(host string) (*tls.Config, error) {
186187
return tc, nil
187188
}
188189

189-
func (ap *authProvider) credentials(host string) (*auth.CredentialsResponse, error) {
190-
ac, err := ap.getAuthConfig(host)
190+
func (ap *authProvider) credentials(ctx context.Context, host string) (*auth.CredentialsResponse, error) {
191+
ac, err := ap.getAuthConfig(ctx, host)
191192
if err != nil {
192193
return nil, err
193194
}
@@ -202,7 +203,7 @@ func (ap *authProvider) credentials(host string) (*auth.CredentialsResponse, err
202203
}
203204

204205
func (ap *authProvider) Credentials(ctx context.Context, req *auth.CredentialsRequest) (*auth.CredentialsResponse, error) {
205-
resp, err := ap.credentials(req.Host)
206+
resp, err := ap.credentials(ctx, req.Host)
206207
if err != nil || resp.Secret != "" {
207208
ap.mu.Lock()
208209
defer ap.mu.Unlock()
@@ -218,7 +219,7 @@ func (ap *authProvider) Credentials(ctx context.Context, req *auth.CredentialsRe
218219
}
219220

220221
func (ap *authProvider) GetTokenAuthority(ctx context.Context, req *auth.GetTokenAuthorityRequest) (*auth.GetTokenAuthorityResponse, error) {
221-
key, err := ap.getAuthorityKey(req.Host, req.Salt)
222+
key, err := ap.getAuthorityKey(ctx, req.Host, req.Salt)
222223
if err != nil {
223224
return nil, err
224225
}
@@ -227,7 +228,7 @@ func (ap *authProvider) GetTokenAuthority(ctx context.Context, req *auth.GetToke
227228
}
228229

229230
func (ap *authProvider) VerifyTokenAuthority(ctx context.Context, req *auth.VerifyTokenAuthorityRequest) (*auth.VerifyTokenAuthorityResponse, error) {
230-
key, err := ap.getAuthorityKey(req.Host, req.Salt)
231+
key, err := ap.getAuthorityKey(ctx, req.Host, req.Salt)
231232
if err != nil {
232233
return nil, err
233234
}
@@ -238,7 +239,7 @@ func (ap *authProvider) VerifyTokenAuthority(ctx context.Context, req *auth.Veri
238239
return &auth.VerifyTokenAuthorityResponse{Signed: sign.Sign(nil, req.Payload, priv)}, nil
239240
}
240241

241-
func (ap *authProvider) getAuthConfig(host string) (*types.AuthConfig, error) {
242+
func (ap *authProvider) getAuthConfig(ctx context.Context, host string) (*types.AuthConfig, error) {
242243
ap.mu.Lock()
243244
defer ap.mu.Unlock()
244245

@@ -247,7 +248,9 @@ func (ap *authProvider) getAuthConfig(host string) (*types.AuthConfig, error) {
247248
}
248249

249250
if _, exists := ap.authConfigCache[host]; !exists {
251+
span, _ := tracing.StartSpan(ctx, fmt.Sprintf("load credentials for %s", host))
250252
ac, err := ap.config.GetAuthConfig(host)
253+
tracing.FinishWithError(span, err)
251254
if err != nil {
252255
return nil, err
253256
}
@@ -257,12 +260,12 @@ func (ap *authProvider) getAuthConfig(host string) (*types.AuthConfig, error) {
257260
return ap.authConfigCache[host], nil
258261
}
259262

260-
func (ap *authProvider) getAuthorityKey(host string, salt []byte) (ed25519.PrivateKey, error) {
263+
func (ap *authProvider) getAuthorityKey(ctx context.Context, host string, salt []byte) (ed25519.PrivateKey, error) {
261264
if v, err := strconv.ParseBool(os.Getenv("BUILDKIT_NO_CLIENT_TOKEN")); err == nil && v {
262265
return nil, status.Errorf(codes.Unavailable, "client side tokens disabled")
263266
}
264267

265-
creds, err := ap.credentials(host)
268+
creds, err := ap.credentials(ctx, host)
266269
if err != nil {
267270
return nil, err
268271
}

0 commit comments

Comments
 (0)