Skip to content

Handle the error in SpanLogger.Log if log level is Error (#3016) #3995

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## master / unreleased

* [CHANGE] Querier / ruler: deprecated `-store.query-chunk-limit` CLI flag (and its respective YAML config option `max_chunks_per_query`) in favour of `-querier.max-fetched-chunks-per-query` (and its respective YAML config option `max_fetched_chunks_per_query`). The new limit specifies the maximum number of chunks that can be fetched in a single query from ingesters and long-term storage: the total number of actual fetched chunks could be 2x the limit, being independently applied when querying ingesters and long-term storage. #4125
* [ENHANCEMENT] Tracing: flag a span with error if its log level is set to Error. #3995

## 1.9.0 in progress

Expand Down
1 change: 0 additions & 1 deletion pkg/chunk/cache/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func (c *Memcached) fetch(ctx context.Context, keys []string) (found []string, b

// Memcached returns partial results even on error.
if err != nil {
log.Error(err)
level.Error(log).Log("msg", "Failed to get keys from memcached", "err", err)
}
return err
Expand Down
1 change: 0 additions & 1 deletion pkg/querier/queryrange/results_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ func (s resultsCache) get(ctx context.Context, key string) ([]Extent, bool) {

if err := proto.Unmarshal(bufs[0], &resp); err != nil {
level.Error(log).Log("msg", "error unmarshalling cached value", "err", err)
log.Error(err)
return nil, false
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/testexporter/correctness/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ func verifySamples(log *spanlogger.SpanLogger, tc Case, pairs []model.SamplePair
} else {
expectedNumSamples := int(duration / cfg.ScrapeInterval)
if math.Abs(float64(expectedNumSamples-len(pairs))) > 2 {
level.Error(log).Log("msg", "wrong number of samples", "expected", expectedNumSamples, "actual", len(pairs))
log.Error(fmt.Errorf("wrong number of samples"))
level.Error(log).Log("msg", "wrong number of samples", "expected", expectedNumSamples, "actual", len(pairs), "err", fmt.Errorf("wrong number of samples"))
return false
}
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/util/spanlogger/spanlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ func FromContextWithFallback(ctx context.Context, fallback log.Logger) *SpanLogg
// also puts the on the spans.
func (s *SpanLogger) Log(kvps ...interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it doable adding some valuable unit tests on the new logic you've added? Otherwise we'll need to manually test it before merging, cause the logic is a bit convoluted and there may be subtle bugs slipping it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I could not see a way to retrieve the field of SpanLogger, it's unlikely to test by comparing the value of the fields. If we refactor the new logic into a private function, I can write unit tests for it. However unit testing a private function doesn't sound like a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pracucci Do you have any further suggestion based on my previous comment? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using https://github.com/opentracing/opentracing-go/blob/master/mocktracer/mocktracer.go ?

(We unit-test private functions all the time, but I agree it's better if you can avoid)

s.Logger.Log(kvps...)

var logAsError = false
errorIndex := -1
for i := 0; i < len(kvps)-1; i += 2 {
// Find out whether to log as error
if kvps[i] == level.Key() {
logAsError = kvps[i+1] == level.ErrorValue()
if !logAsError {
break
}
ext.Error.Set(s.Span, true)
} else if errorIndex == -1 {
// Check if this is the error we want to log
if _, ok := kvps[i+1].(error); ok && (kvps[i] == "err" || kvps[i] == "error") {
errorIndex = i
}
}
if logAsError && errorIndex != -1 {
s.Span.LogFields(otlog.Error(kvps[i+1].(error)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to use errorIndex instead of i, otherwise you're assuming that the level is always before the error in kvps ordering.

// Remove the already logged error
kvps = append(kvps[:i], kvps[i+2:]...)
break
}
}

fields, err := otlog.InterleavedKVToFields(kvps...)
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/spanlogger/spanlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ func TestSpanLogger_Log(t *testing.T) {
_ = span.Log("foo")
newSpan := FromContext(ctx)
require.Equal(t, span.Span, newSpan.Span)
_ = newSpan.Log("bar")
require.Error(t, newSpan.Log("bar", "err", errors.New("err"), "metric2", 2))
noSpan := FromContext(context.Background())
_ = noSpan.Log("foo")
require.Error(t, noSpan.Error(errors.New("err")))
require.NoError(t, noSpan.Error(nil))
require.NoError(t, noSpan.Log("metric1", 1, "err", errors.New("err"), "metric2", 2))
}

func TestSpanLogger_CustomLogger(t *testing.T) {
Expand Down