Skip to content

Commit

Permalink
chore(storage): add retry metric headers (#6013)
Browse files Browse the repository at this point in the history
Adds retry metrics to X-Goog-Api-Client header.

Format will be:
X-Goog-Api-Client: gccl-invocation-id/7a808dc9-2865-4777-beee-cf8259ef7fe0 gccl-attempt-count/1 gl-go/1.18.0 gccl/1.22.0

Does not include uploads & downloads, or experimental gRPC methods. Support for these will be added later.
  • Loading branch information
tritone authored May 18, 2022
1 parent 50c266b commit 20e9b2a
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 79 deletions.
28 changes: 14 additions & 14 deletions storage/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func (a *ACLHandle) List(ctx context.Context) (rules []ACLRule, err error) {
func (a *ACLHandle) bucketDefaultList(ctx context.Context) ([]ACLRule, error) {
var acls *raw.ObjectAccessControls
var err error
req := a.c.raw.DefaultObjectAccessControls.List(a.bucket)
a.configureCall(ctx, req)
err = run(ctx, func() error {
req := a.c.raw.DefaultObjectAccessControls.List(a.bucket)
a.configureCall(ctx, req)
acls, err = req.Do()
return err
}, a.retry, true)
}, a.retry, true, setRetryHeaderHTTP(req))
if err != nil {
return nil, err
}
Expand All @@ -139,18 +139,18 @@ func (a *ACLHandle) bucketDefaultDelete(ctx context.Context, entity ACLEntity) e

return run(ctx, func() error {
return req.Do()
}, a.retry, false)
}, a.retry, false, setRetryHeaderHTTP(req))
}

func (a *ACLHandle) bucketList(ctx context.Context) ([]ACLRule, error) {
var acls *raw.BucketAccessControls
var err error
req := a.c.raw.BucketAccessControls.List(a.bucket)
a.configureCall(ctx, req)
err = run(ctx, func() error {
req := a.c.raw.BucketAccessControls.List(a.bucket)
a.configureCall(ctx, req)
acls, err = req.Do()
return err
}, a.retry, true)
}, a.retry, true, setRetryHeaderHTTP(req))
if err != nil {
return nil, err
}
Expand All @@ -168,26 +168,26 @@ func (a *ACLHandle) bucketSet(ctx context.Context, entity ACLEntity, role ACLRol
return run(ctx, func() error {
_, err := req.Do()
return err
}, a.retry, false)
}, a.retry, false, setRetryHeaderHTTP(req))
}

func (a *ACLHandle) bucketDelete(ctx context.Context, entity ACLEntity) error {
req := a.c.raw.BucketAccessControls.Delete(a.bucket, string(entity))
a.configureCall(ctx, req)
return run(ctx, func() error {
return req.Do()
}, a.retry, false)
}, a.retry, false, setRetryHeaderHTTP(req))
}

func (a *ACLHandle) objectList(ctx context.Context) ([]ACLRule, error) {
var acls *raw.ObjectAccessControls
var err error
req := a.c.raw.ObjectAccessControls.List(a.bucket, a.object)
a.configureCall(ctx, req)
err = run(ctx, func() error {
req := a.c.raw.ObjectAccessControls.List(a.bucket, a.object)
a.configureCall(ctx, req)
acls, err = req.Do()
return err
}, a.retry, true)
}, a.retry, true, setRetryHeaderHTTP(req))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -215,15 +215,15 @@ func (a *ACLHandle) objectSet(ctx context.Context, entity ACLEntity, role ACLRol
return run(ctx, func() error {
_, err := req.Do()
return err
}, a.retry, false)
}, a.retry, false, setRetryHeaderHTTP(req))
}

func (a *ACLHandle) objectDelete(ctx context.Context, entity ACLEntity) error {
req := a.c.raw.ObjectAccessControls.Delete(a.bucket, a.object, string(entity))
a.configureCall(ctx, req)
return run(ctx, func() error {
return req.Do()
}, a.retry, false)
}, a.retry, false, setRetryHeaderHTTP(req))
}

func (a *ACLHandle) configureCall(ctx context.Context, call interface{ Header() http.Header }) {
Expand Down
14 changes: 7 additions & 7 deletions storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (b *BucketHandle) Create(ctx context.Context, projectID string, attrs *Buck
if attrs != nil && attrs.PredefinedDefaultObjectACL != "" {
req.PredefinedDefaultObjectAcl(attrs.PredefinedDefaultObjectACL)
}
return run(ctx, func() error { _, err := req.Context(ctx).Do(); return err }, b.retry, true)
return run(ctx, func() error { _, err := req.Context(ctx).Do(); return err }, b.retry, true, setRetryHeaderHTTP(req))
}

// Delete deletes the Bucket.
Expand All @@ -116,7 +116,7 @@ func (b *BucketHandle) Delete(ctx context.Context) (err error) {
return err
}

return run(ctx, func() error { return req.Context(ctx).Do() }, b.retry, true)
return run(ctx, func() error { return req.Context(ctx).Do() }, b.retry, true, setRetryHeaderHTTP(req))
}

func (b *BucketHandle) newDeleteCall() (*raw.BucketsDeleteCall, error) {
Expand Down Expand Up @@ -184,7 +184,7 @@ func (b *BucketHandle) Attrs(ctx context.Context) (attrs *BucketAttrs, err error
err = run(ctx, func() error {
resp, err = req.Context(ctx).Do()
return err
}, b.retry, true)
}, b.retry, true, setRetryHeaderHTTP(req))
var e *googleapi.Error
if ok := errors.As(err, &e); ok && e.Code == http.StatusNotFound {
return nil, ErrBucketNotExist
Expand Down Expand Up @@ -232,7 +232,7 @@ func (b *BucketHandle) Update(ctx context.Context, uattrs BucketAttrsToUpdate) (
return err
}

if err := run(ctx, call, b.retry, isIdempotent); err != nil {
if err := run(ctx, call, b.retry, isIdempotent, setRetryHeaderHTTP(req)); err != nil {
return nil, err
}
return newBucket(rawBucket)
Expand Down Expand Up @@ -1341,7 +1341,7 @@ func (b *BucketHandle) LockRetentionPolicy(ctx context.Context) error {
return run(ctx, func() error {
_, err := req.Context(ctx).Do()
return err
}, b.retry, true)
}, b.retry, true, setRetryHeaderHTTP(req))
}

// applyBucketConds modifies the provided call using the conditions in conds.
Expand Down Expand Up @@ -1999,7 +1999,7 @@ func (it *ObjectIterator) fetch(pageSize int, pageToken string) (string, error)
err = run(it.ctx, func() error {
resp, err = req.Context(it.ctx).Do()
return err
}, it.bucket.retry, true)
}, it.bucket.retry, true, setRetryHeaderHTTP(req))
if err != nil {
var e *googleapi.Error
if ok := errors.As(err, &e); ok && e.Code == http.StatusNotFound {
Expand Down Expand Up @@ -2086,7 +2086,7 @@ func (it *BucketIterator) fetch(pageSize int, pageToken string) (token string, e
err = run(it.ctx, func() error {
resp, err = req.Context(it.ctx).Do()
return err
}, it.client.retry, true)
}, it.client.retry, true, setRetryHeaderHTTP(req))
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions storage/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (c *Copier) callRewrite(ctx context.Context, rawObj *raw.Object) (*raw.Rewr
retryCall := func() error { res, err = call.Do(); return err }
isIdempotent := c.dst.conds != nil && (c.dst.conds.GenerationMatch != 0 || c.dst.conds.DoesNotExist)

if err := run(ctx, retryCall, c.dst.retry, isIdempotent); err != nil {
if err := run(ctx, retryCall, c.dst.retry, isIdempotent, setRetryHeaderHTTP(call)); err != nil {
return nil, err
}
c.RewriteToken = res.RewriteToken
Expand Down Expand Up @@ -237,7 +237,7 @@ func (c *Composer) Run(ctx context.Context) (attrs *ObjectAttrs, err error) {
retryCall := func() error { obj, err = call.Do(); return err }
isIdempotent := c.dst.conds != nil && (c.dst.conds.GenerationMatch != 0 || c.dst.conds.DoesNotExist)

if err := run(ctx, retryCall, c.dst.retry, isIdempotent); err != nil {
if err := run(ctx, retryCall, c.dst.retry, isIdempotent, setRetryHeaderHTTP(call)); err != nil {
return nil, err
}
return newObject(obj), nil
Expand Down
1 change: 1 addition & 0 deletions storage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
cloud.google.com/go/iam v0.3.0
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.7
github.com/google/uuid v1.1.2
github.com/googleapis/gax-go/v2 v2.3.0
github.com/googleapis/go-type-adapters v1.0.0
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
Expand Down
1 change: 1 addition & 0 deletions storage/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down
22 changes: 11 additions & 11 deletions storage/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *grpcStorageClient) GetServiceAccount(ctx context.Context, project strin
var err error
resp, err = c.raw.GetServiceAccount(ctx, req, s.gax...)
return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -154,7 +154,7 @@ func (c *grpcStorageClient) CreateBucket(ctx context.Context, project string, at
battrs = newBucketFromProto(res)

return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))

return battrs, err
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (c *grpcStorageClient) ListBuckets(ctx context.Context, project string, opt
err = run(it.ctx, func() error {
buckets, next, err = gitr.InternalFetch(pageSize, pageToken)
return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func (c *grpcStorageClient) DeleteBucket(ctx context.Context, bucket string, con

return run(ctx, func() error {
return c.raw.DeleteBucket(ctx, req, s.gax...)
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))
}

func (c *grpcStorageClient) GetBucket(ctx context.Context, bucket string, conds *BucketConditions, opts ...storageOption) (*BucketAttrs, error) {
Expand All @@ -245,7 +245,7 @@ func (c *grpcStorageClient) GetBucket(ctx context.Context, bucket string, conds
battrs = newBucketFromProto(res)

return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))

if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
return nil, ErrBucketNotExist
Expand Down Expand Up @@ -333,7 +333,7 @@ func (c *grpcStorageClient) UpdateBucket(ctx context.Context, bucket string, uat
res, err := c.raw.UpdateBucket(ctx, req, s.gax...)
battrs = newBucketFromProto(res)
return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))

return battrs, err
}
Expand All @@ -354,7 +354,7 @@ func (c *grpcStorageClient) LockBucketRetentionPolicy(ctx context.Context, bucke
return run(ctx, func() error {
_, err := c.raw.LockBucketRetentionPolicy(ctx, req, s.gax...)
return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))

}
func (c *grpcStorageClient) ListObjects(ctx context.Context, bucket string, q *Query, opts ...storageOption) *ObjectIterator {
Expand Down Expand Up @@ -384,7 +384,7 @@ func (c *grpcStorageClient) ListObjects(ctx context.Context, bucket string, q *Q
err = run(it.ctx, func() error {
objects, token, err = gitr.InternalFetch(pageSize, pageToken)
return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))
if err != nil {
if st, ok := status.FromError(err); ok && st.Code() == codes.NotFound {
err = ErrBucketNotExist
Expand Down Expand Up @@ -547,7 +547,7 @@ func (c *grpcStorageClient) GetIamPolicy(ctx context.Context, resource string, v
var err error
rp, err = c.raw.GetIamPolicy(ctx, req, s.gax...)
return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))

return rp, err
}
Expand All @@ -564,7 +564,7 @@ func (c *grpcStorageClient) SetIamPolicy(ctx context.Context, resource string, p
return run(ctx, func() error {
_, err := c.raw.SetIamPolicy(ctx, req, s.gax...)
return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))
}

func (c *grpcStorageClient) TestIamPermissions(ctx context.Context, resource string, permissions []string, opts ...storageOption) ([]string, error) {
Expand All @@ -579,7 +579,7 @@ func (c *grpcStorageClient) TestIamPermissions(ctx context.Context, resource str
var err error
res, err = c.raw.TestIamPermissions(ctx, req, s.gax...)
return err
}, s.retry, s.idempotent)
}, s.retry, s.idempotent, setRetryHeaderGRPC(ctx))
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions storage/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (hkh *HMACKeyHandle) Get(ctx context.Context, opts ...HMACKeyOption) (*HMAC
err = run(ctx, func() error {
metadata, err = call.Context(ctx).Do()
return err
}, hkh.retry, true)
}, hkh.retry, true, setRetryHeaderHTTP(call))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func (hkh *HMACKeyHandle) Delete(ctx context.Context, opts ...HMACKeyOption) err

return run(ctx, func() error {
return delCall.Context(ctx).Do()
}, hkh.retry, true)
}, hkh.retry, true, setRetryHeaderHTTP(delCall))
}

func pbHmacKeyToHMACKey(pb *raw.HmacKey, updatedTimeCanBeNil bool) (*HMACKey, error) {
Expand Down Expand Up @@ -221,7 +221,7 @@ func (c *Client) CreateHMACKey(ctx context.Context, projectID, serviceAccountEma
h, err := call.Context(ctx).Do()
hkPb = h
return err
}, c.retry, false); err != nil {
}, c.retry, false, setRetryHeaderHTTP(call)); err != nil {
return nil, err
}

Expand Down Expand Up @@ -267,7 +267,7 @@ func (h *HMACKeyHandle) Update(ctx context.Context, au HMACKeyAttrsToUpdate, opt
err = run(ctx, func() error {
metadata, err = call.Context(ctx).Do()
return err
}, h.retry, isIdempotent)
}, h.retry, isIdempotent, setRetryHeaderHTTP(call))

if err != nil {
return nil, err
Expand Down Expand Up @@ -373,7 +373,7 @@ func (it *HMACKeysIterator) fetch(pageSize int, pageToken string) (token string,
err = run(it.ctx, func() error {
resp, err = call.Context(ctx).Do()
return err
}, it.retry, true)
}, it.retry, true, setRetryHeaderHTTP(call))
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit 20e9b2a

Please sign in to comment.