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(storage): trace span covers life of a Reader #8390

Merged
merged 3 commits into from Aug 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions storage/reader.go
Expand Up @@ -87,8 +87,9 @@ func (o *ObjectHandle) NewReader(ctx context.Context) (*Reader, error) {
// that file will be served back whole, regardless of the requested range as
// Google Cloud Storage dictates.
func (o *ObjectHandle) NewRangeReader(ctx context.Context, offset, length int64) (r *Reader, err error) {
ctx = trace.StartSpan(ctx, "cloud.google.com/go/storage.Object.NewRangeReader")
defer func() { trace.EndSpan(ctx, err) }()
// This span covers the life of the reader. It is closed via the context
// in Reader.Close.
ctx = trace.StartSpan(ctx, "cloud.google.com/go/storage.Object.Reader")

if err := o.validate(); err != nil {
return nil, err
Expand Down Expand Up @@ -117,6 +118,14 @@ func (o *ObjectHandle) NewRangeReader(ctx context.Context, offset, length int64)

r, err = o.c.tc.NewRangeReader(ctx, params, opts...)

// Pass the context so that the span can be closed in Reader.Close, or close the
// span now if there is an error.
if err == nil {
r.ctx = ctx
} else {
trace.EndSpan(ctx, err)
}

return r, err
}

Expand Down Expand Up @@ -204,11 +213,14 @@ type Reader struct {
gotCRC uint32 // running crc

reader io.ReadCloser
ctx context.Context
}

// Close closes the Reader. It must be called when done reading.
func (r *Reader) Close() error {
return r.reader.Close()
err := r.reader.Close()
trace.EndSpan(r.ctx, err)
return err
}

func (r *Reader) Read(p []byte) (int, error) {
Expand Down