mirror of https://github.com/grafana/loki
Queriers: Stop emitting spans for every objectstorage call (#9857)
**What this PR does / why we need it**: Stop emitting spans for every call to our object storages (like S3 or Azure). They are causing every chunk fetching to emit a span. Request metrics will still be reported just fine. **Which issue(s) this PR fixes**: N/Apull/9871/head
parent
9393f3f11a
commit
bc0069756e
@ -0,0 +1,24 @@ |
||||
package instrument |
||||
|
||||
import ( |
||||
"context" |
||||
"time" |
||||
|
||||
"github.com/weaveworks/common/instrument" |
||||
) |
||||
|
||||
// TimeRequest reports how much time was spent on the given function `f`.
|
||||
//
|
||||
// It is a thinner version of weaveworks/common/instrument.CollectedRequest that doesn't emit spans.
|
||||
func TimeRequest(ctx context.Context, method string, col instrument.Collector, toStatusCode func(error) string, f func(context.Context) error) error { |
||||
if toStatusCode == nil { |
||||
toStatusCode = instrument.ErrorCode |
||||
} |
||||
|
||||
start := time.Now() |
||||
col.Before(ctx, method, start) |
||||
err := f(ctx) |
||||
col.After(ctx, method, toStatusCode(err), start) |
||||
|
||||
return err |
||||
} |
@ -0,0 +1,70 @@ |
||||
package instrument |
||||
|
||||
// Source: https://github.com/weaveworks/common/blob/master/instrument/instrument_test.go
|
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/prometheus/client_golang/prometheus" |
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/require" |
||||
"github.com/weaveworks/common/instrument" |
||||
) |
||||
|
||||
func TestNewHistogramCollector(t *testing.T) { |
||||
m := prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
||||
Namespace: "test", |
||||
Subsystem: "instrumentation", |
||||
Name: "foo", |
||||
Help: "", |
||||
Buckets: prometheus.DefBuckets, |
||||
}, instrument.HistogramCollectorBuckets) |
||||
c := instrument.NewHistogramCollector(m) |
||||
assert.NotNil(t, c) |
||||
} |
||||
|
||||
type spyCollector struct { |
||||
before bool |
||||
after bool |
||||
afterCode string |
||||
} |
||||
|
||||
func (c *spyCollector) Register() { |
||||
} |
||||
|
||||
// Before collects for the upcoming request.
|
||||
func (c *spyCollector) Before(_ context.Context, _ string, _ time.Time) { |
||||
c.before = true |
||||
} |
||||
|
||||
// After collects when the request is done.
|
||||
func (c *spyCollector) After(_ context.Context, _, statusCode string, _ time.Time) { |
||||
c.after = true |
||||
c.afterCode = statusCode |
||||
} |
||||
|
||||
func TestCollectedRequest(t *testing.T) { |
||||
c := &spyCollector{} |
||||
fcalled := false |
||||
require.NoError(t, instrument.CollectedRequest(context.Background(), "test", c, nil, func(_ context.Context) error { |
||||
fcalled = true |
||||
return nil |
||||
})) |
||||
assert.True(t, fcalled) |
||||
assert.True(t, c.before) |
||||
assert.True(t, c.after) |
||||
assert.Equal(t, "200", c.afterCode) |
||||
} |
||||
|
||||
func TestCollectedRequest_Error(t *testing.T) { |
||||
c := &spyCollector{} |
||||
require.Error(t, instrument.CollectedRequest(context.Background(), "test", c, nil, func(_ context.Context) error { |
||||
return errors.New("boom") |
||||
})) |
||||
assert.True(t, c.before) |
||||
assert.True(t, c.after) |
||||
assert.Equal(t, "500", c.afterCode) |
||||
} |
Loading…
Reference in new issue