Skip to content

Commit 03165e8

Browse files
cstyanpracucci
andauthored
Be consistent with span logging, set error=true and use otlog.Error (#2970)
* Make use of spanlogger when functions that generate spans result in an error, this is so we consistently have the error=true tag set. Signed-off-by: Callum Styan <[email protected]> * Add spanlogger Error to errcheck exclude Signed-off-by: Callum Styan <[email protected]> * Review cleanup. Signed-off-by: Callum Styan <[email protected]> * Address more review feedback. Signed-off-by: Callum Styan <[email protected]> * We need nolint comments on these lines if we want to assign the ctx in attempt to avoid use of the incorrect ctx in the future. Signed-off-by: Callum Styan <[email protected]> * Globally exclude SpanLogger.Error() errcheck Signed-off-by: Marco Pracucci <[email protected]> Co-authored-by: Marco Pracucci <[email protected]>
1 parent f308855 commit 03165e8

File tree

8 files changed

+34
-33
lines changed

8 files changed

+34
-33
lines changed

.errcheck-exclude

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ io/ioutil.WriteFile
22
io/ioutil.ReadFile
33
(github.com/go-kit/kit/log.Logger).Log
44
io.Copy
5-
(github.com/opentracing/opentracing-go.Tracer).Inject
5+
(github.com/opentracing/opentracing-go.Tracer).Inject
6+
(*github.com/cortexproject/cortex/pkg/util/spanlogger.SpanLogger).Error

pkg/chunk/cache/memcached.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
"github.com/bradfitz/gomemcache/memcache"
1212
"github.com/go-kit/kit/log"
1313
"github.com/go-kit/kit/log/level"
14-
opentracing "github.com/opentracing/opentracing-go"
1514
otlog "github.com/opentracing/opentracing-go/log"
1615
"github.com/prometheus/client_golang/prometheus"
1716
"github.com/prometheus/client_golang/prometheus/promauto"
1817
instr "github.com/weaveworks/common/instrument"
1918

2019
"github.com/cortexproject/cortex/pkg/util"
20+
"github.com/cortexproject/cortex/pkg/util/spanlogger"
2121
)
2222

2323
type observableVecCollector struct {
@@ -146,19 +146,20 @@ func (c *Memcached) Fetch(ctx context.Context, keys []string) (found []string, b
146146

147147
func (c *Memcached) fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missed []string) {
148148
var items map[string]*memcache.Item
149-
err := instr.CollectedRequest(ctx, "Memcache.GetMulti", c.requestDuration, memcacheStatusCode, func(innerCtx context.Context) error {
150-
sp := opentracing.SpanFromContext(innerCtx)
151-
sp.LogFields(otlog.Int("keys requested", len(keys)))
149+
const method = "Memcache.GetMulti"
150+
err := instr.CollectedRequest(ctx, method, c.requestDuration, memcacheStatusCode, func(innerCtx context.Context) error {
151+
log, _ := spanlogger.New(innerCtx, method)
152+
log.LogFields(otlog.Int("keys requested", len(keys)))
152153

153154
var err error
154155
items, err = c.memcache.GetMulti(keys)
155156

156-
sp.LogFields(otlog.Int("keys found", len(items)))
157+
log.LogFields(otlog.Int("keys found", len(items)))
157158

158159
// Memcached returns partial results even on error.
159160
if err != nil {
160-
sp.LogFields(otlog.Error(err))
161-
level.Error(c.logger).Log("msg", "Failed to get keys from memcached", "err", err)
161+
log.Error(err)
162+
level.Error(log).Log("msg", "Failed to get keys from memcached", "err", err)
162163
}
163164
return err
164165
})

pkg/chunk/gcp/bigtable_index_client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
"cloud.google.com/go/bigtable"
1414
"github.com/go-kit/kit/log"
1515
ot "github.com/opentracing/opentracing-go"
16-
otlog "github.com/opentracing/opentracing-go/log"
1716
"github.com/pkg/errors"
1817

1918
"github.com/cortexproject/cortex/pkg/chunk"
2019
chunk_util "github.com/cortexproject/cortex/pkg/chunk/util"
2120
"github.com/cortexproject/cortex/pkg/util"
2221
"github.com/cortexproject/cortex/pkg/util/grpcclient"
22+
"github.com/cortexproject/cortex/pkg/util/spanlogger"
2323
)
2424

2525
const (
@@ -324,8 +324,8 @@ func (s *storageClientV1) QueryPages(ctx context.Context, queries []chunk.IndexQ
324324
func (s *storageClientV1) query(ctx context.Context, query chunk.IndexQuery, callback chunk_util.Callback) error {
325325
const null = string('\xff')
326326

327-
sp, ctx := ot.StartSpanFromContext(ctx, "QueryPages", ot.Tag{Key: "tableName", Value: query.TableName}, ot.Tag{Key: "hashValue", Value: query.HashValue})
328-
defer sp.Finish()
327+
log, ctx := spanlogger.New(ctx, "QueryPages", ot.Tag{Key: "tableName", Value: query.TableName}, ot.Tag{Key: "hashValue", Value: query.HashValue})
328+
defer log.Finish()
329329

330330
table := s.client.Open(query.TableName)
331331

@@ -358,7 +358,7 @@ func (s *storageClientV1) query(ctx context.Context, query chunk.IndexQuery, cal
358358
return true
359359
})
360360
if err != nil {
361-
sp.LogFields(otlog.String("error", err.Error()))
361+
log.Error(err)
362362
return errors.WithStack(err)
363363
}
364364
return nil

pkg/chunk/util/parallel_chunk_fetch.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"context"
55
"sync"
66

7-
ot "github.com/opentracing/opentracing-go"
87
otlog "github.com/opentracing/opentracing-go/log"
98

109
"github.com/cortexproject/cortex/pkg/chunk"
10+
"github.com/cortexproject/cortex/pkg/util/spanlogger"
1111
)
1212

1313
const maxParallel = 1000
@@ -20,9 +20,9 @@ var decodeContextPool = sync.Pool{
2020

2121
// GetParallelChunks fetches chunks in parallel (up to maxParallel).
2222
func GetParallelChunks(ctx context.Context, chunks []chunk.Chunk, f func(context.Context, *chunk.DecodeContext, chunk.Chunk) (chunk.Chunk, error)) ([]chunk.Chunk, error) {
23-
sp, ctx := ot.StartSpanFromContext(ctx, "GetParallelChunks")
24-
defer sp.Finish()
25-
sp.LogFields(otlog.Int("chunks requested", len(chunks)))
23+
log, ctx := spanlogger.New(ctx, "GetParallelChunks")
24+
defer log.Finish()
25+
log.LogFields(otlog.Int("chunks requested", len(chunks)))
2626

2727
queuedChunks := make(chan chunk.Chunk)
2828

@@ -62,9 +62,9 @@ func GetParallelChunks(ctx context.Context, chunks []chunk.Chunk, f func(context
6262
}
6363
}
6464

65-
sp.LogFields(otlog.Int("chunks fetched", len(result)))
65+
log.LogFields(otlog.Int("chunks fetched", len(result)))
6666
if lastErr != nil {
67-
sp.LogFields(otlog.Error(lastErr))
67+
log.Error(lastErr)
6868
}
6969

7070
// Return any chunks we did receive: a partial result may be useful

pkg/querier/queryrange/query_range.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/cortexproject/cortex/pkg/ingester/client"
2323
"github.com/cortexproject/cortex/pkg/util"
24+
"github.com/cortexproject/cortex/pkg/util/spanlogger"
2425
)
2526

2627
// StatusSuccess Prometheus success result.
@@ -238,17 +239,16 @@ func (prometheusCodec) DecodeResponse(ctx context.Context, r *http.Response, _ R
238239
body, _ := ioutil.ReadAll(r.Body)
239240
return nil, httpgrpc.Errorf(r.StatusCode, string(body))
240241
}
241-
242-
sp, _ := opentracing.StartSpanFromContext(ctx, "ParseQueryRangeResponse")
243-
defer sp.Finish()
242+
log, ctx := spanlogger.New(ctx, "ParseQueryRangeResponse") //nolint:ineffassign,staticcheck
243+
defer log.Finish()
244244

245245
buf, err := ioutil.ReadAll(r.Body)
246246
if err != nil {
247-
sp.LogFields(otlog.Error(err))
247+
log.Error(err)
248248
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error decoding response: %v", err)
249249
}
250250

251-
sp.LogFields(otlog.Int("bytes", len(buf)))
251+
log.LogFields(otlog.Int("bytes", len(buf)))
252252

253253
var resp PrometheusResponse
254254
if err := json.Unmarshal(buf, &resp); err != nil {

pkg/querier/queryrange/results_cache.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,14 @@ func (s resultsCache) get(ctx context.Context, key string) ([]Extent, bool) {
450450
}
451451

452452
var resp CachedResponse
453-
sp, _ := opentracing.StartSpanFromContext(ctx, "unmarshal-extent")
454-
defer sp.Finish()
453+
log, ctx := spanlogger.New(ctx, "unmarshal-extent") //nolint:ineffassign,staticcheck
454+
defer log.Finish()
455455

456-
sp.LogFields(otlog.Int("bytes", len(bufs[0])))
456+
log.LogFields(otlog.Int("bytes", len(bufs[0])))
457457

458458
if err := proto.Unmarshal(bufs[0], &resp); err != nil {
459-
level.Error(s.logger).Log("msg", "error unmarshalling cached value", "err", err)
460-
sp.LogFields(otlog.Error(err))
459+
level.Error(log).Log("msg", "error unmarshalling cached value", "err", err)
460+
log.Error(err)
461461
return nil, false
462462
}
463463

pkg/testexporter/correctness/simple.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
"github.com/go-kit/kit/log/level"
12-
otlog "github.com/opentracing/opentracing-go/log"
1312
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
1413
"github.com/prometheus/client_golang/prometheus"
1514
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -161,7 +160,7 @@ func verifySamples(log *spanlogger.SpanLogger, tc Case, pairs []model.SamplePair
161160
} else {
162161
sampleResult.WithLabelValues(tc.Name(), fail).Inc()
163162
level.Error(log).Log("msg", "wrong value", "at", pair.Timestamp, "expected", tc.ExpectedValueAt(pair.Timestamp.Time()), "actual", pair.Value)
164-
log.LogFields(otlog.Error(fmt.Errorf("wrong value")))
163+
log.Error(fmt.Errorf("wrong value"))
165164
return false
166165
}
167166
}
@@ -171,14 +170,14 @@ func verifySamples(log *spanlogger.SpanLogger, tc Case, pairs []model.SamplePair
171170
expectedNumSamples := int(duration / cfg.ScrapeInterval)
172171
if !epsilonCorrect(float64(len(pairs)), float64(expectedNumSamples), cfg.samplesEpsilon) {
173172
level.Error(log).Log("msg", "wrong number of samples", "expected", expectedNumSamples, "actual", len(pairs))
174-
log.LogFields(otlog.Error(fmt.Errorf("wrong number of samples")))
173+
log.Error(fmt.Errorf("wrong number of samples"))
175174
return false
176175
}
177176
} else {
178177
expectedNumSamples := int(duration / cfg.ScrapeInterval)
179178
if math.Abs(float64(expectedNumSamples-len(pairs))) > 2 {
180179
level.Error(log).Log("msg", "wrong number of samples", "expected", expectedNumSamples, "actual", len(pairs))
181-
log.LogFields(otlog.Error(fmt.Errorf("wrong number of samples")))
180+
log.Error(fmt.Errorf("wrong number of samples"))
182181
return false
183182
}
184183
}

pkg/util/spanlogger/spanlogger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (s *SpanLogger) Log(kvps ...interface{}) error {
5959
return nil
6060
}
6161

62-
// Error sets error flag and logs the error, if non-nil. Returns the err passed in.
62+
// Error sets error flag and logs the error on the span, if non-nil. Returns the err passed in.
6363
func (s *SpanLogger) Error(err error) error {
6464
if err == nil {
6565
return nil

0 commit comments

Comments
 (0)