forked from thanos-io/objstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracing.go
159 lines (129 loc) · 4.01 KB
/
tracing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package objstore
import (
"context"
"io"
"github.com/opentracing/opentracing-go"
"github.com/thanos-io/objstore/tracing"
)
// TracingBucket includes bucket operations in the traces.
type TracingBucket struct {
bkt Bucket
}
func NewTracingBucket(bkt Bucket) InstrumentedBucket {
return TracingBucket{bkt: bkt}
}
func (t TracingBucket) Iter(ctx context.Context, dir string, f func(string) error, options ...IterOption) (err error) {
tracing.DoWithSpan(ctx, "bucket_iter", func(spanCtx context.Context, span opentracing.Span) {
span.LogKV("dir", dir)
err = t.bkt.Iter(spanCtx, dir, f, options...)
})
return
}
func (t TracingBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
span, spanCtx := tracing.StartSpan(ctx, "bucket_get")
span.LogKV("name", name)
r, err := t.bkt.Get(spanCtx, name)
if err != nil {
span.LogKV("err", err)
span.Finish()
return nil, err
}
return newTracingReadCloser(r, span), nil
}
func (t TracingBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
span, spanCtx := tracing.StartSpan(ctx, "bucket_getrange")
span.LogKV("name", name, "offset", off, "length", length)
r, err := t.bkt.GetRange(spanCtx, name, off, length)
if err != nil {
span.LogKV("err", err)
span.Finish()
return nil, err
}
return newTracingReadCloser(r, span), nil
}
func (t TracingBucket) Exists(ctx context.Context, name string) (exists bool, err error) {
tracing.DoWithSpan(ctx, "bucket_exists", func(spanCtx context.Context, span opentracing.Span) {
span.LogKV("name", name)
exists, err = t.bkt.Exists(spanCtx, name)
})
return
}
func (t TracingBucket) Attributes(ctx context.Context, name string) (attrs ObjectAttributes, err error) {
tracing.DoWithSpan(ctx, "bucket_attributes", func(spanCtx context.Context, span opentracing.Span) {
span.LogKV("name", name)
attrs, err = t.bkt.Attributes(spanCtx, name)
})
return
}
func (t TracingBucket) Upload(ctx context.Context, name string, r io.Reader) (err error) {
tracing.DoWithSpan(ctx, "bucket_upload", func(spanCtx context.Context, span opentracing.Span) {
span.LogKV("name", name)
err = t.bkt.Upload(spanCtx, name, r)
})
return
}
func (t TracingBucket) Delete(ctx context.Context, name string) (err error) {
tracing.DoWithSpan(ctx, "bucket_delete", func(spanCtx context.Context, span opentracing.Span) {
span.LogKV("name", name)
err = t.bkt.Delete(spanCtx, name)
})
return
}
func (t TracingBucket) Name() string {
return "tracing: " + t.bkt.Name()
}
func (t TracingBucket) Close() error {
return t.bkt.Close()
}
func (t TracingBucket) IsObjNotFoundErr(err error) bool {
return t.bkt.IsObjNotFoundErr(err)
}
func (t TracingBucket) WithExpectedErrs(expectedFunc IsOpFailureExpectedFunc) Bucket {
if ib, ok := t.bkt.(InstrumentedBucket); ok {
return TracingBucket{bkt: ib.WithExpectedErrs(expectedFunc)}
}
return t
}
func (t TracingBucket) ReaderWithExpectedErrs(expectedFunc IsOpFailureExpectedFunc) BucketReader {
return t.WithExpectedErrs(expectedFunc)
}
type tracingReadCloser struct {
r io.ReadCloser
s opentracing.Span
objSize int64
objSizeErr error
read int
}
func newTracingReadCloser(r io.ReadCloser, span opentracing.Span) io.ReadCloser {
// Since TryToGetSize can only reliably return size before doing any read calls,
// we call during "construction" and remember the results.
objSize, objSizeErr := TryToGetSize(r)
return &tracingReadCloser{r: r, s: span, objSize: objSize, objSizeErr: objSizeErr}
}
func (t *tracingReadCloser) ObjectSize() (int64, error) {
return t.objSize, t.objSizeErr
}
func (t *tracingReadCloser) Read(p []byte) (int, error) {
n, err := t.r.Read(p)
if n > 0 {
t.read += n
}
if err != nil && err != io.EOF && t.s != nil {
t.s.LogKV("err", err)
}
return n, err
}
func (t *tracingReadCloser) Close() error {
err := t.r.Close()
if t.s != nil {
t.s.LogKV("read", t.read)
if err != nil {
t.s.LogKV("close err", err)
}
t.s.Finish()
t.s = nil
}
return err
}