mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-26 06:04:05 -08:00
Fix some dead code, missing error checks, shadowings.
I applied https://medium.com/@jgautheron/quality-pipeline-for-go-projects-497e34d6567 and was greeted with a deluge of warnings, most of which were not applicable or really fixable realistically. These are some of the first ones I decided to fix.
This commit is contained in:
parent
896928ad00
commit
af513468eb
|
@ -304,5 +304,7 @@ func usage() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Execute(os.Stdout, groups)
|
if err := t.Execute(os.Stdout, groups); err != nil {
|
||||||
|
panic(fmt.Errorf("error executing usage template: %s", err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,6 @@ var (
|
||||||
const (
|
const (
|
||||||
testStartTime = model.Time(0)
|
testStartTime = model.Time(0)
|
||||||
epsilon = 0.000001 // Relative error allowed for sample values.
|
epsilon = 0.000001 // Relative error allowed for sample values.
|
||||||
maxErrorCount = 10
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Test is a sequence of read and write commands that are run
|
// Test is a sequence of read and write commands that are run
|
||||||
|
|
|
@ -179,7 +179,9 @@ func (fd *FileDiscovery) stop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
fd.watcher.Close()
|
if err := fd.watcher.Close(); err != nil {
|
||||||
|
log.Errorf("Error closing file watcher for %s: %s", fd.paths, err)
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("File discovery for %s stopped.", fd.paths)
|
log.Debugf("File discovery for %s stopped.", fd.paths)
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,9 @@ func (p *persistence) recoverFromCrash(fingerprintToSeries map[model.Fingerprint
|
||||||
|
|
||||||
// Delete the fingerprint mapping file as it might be stale or
|
// Delete the fingerprint mapping file as it might be stale or
|
||||||
// corrupt. We'll rebuild the mappings as we go.
|
// corrupt. We'll rebuild the mappings as we go.
|
||||||
os.Remove(p.mappingsFileName())
|
if err := os.RemoveAll(p.mappingsFileName()); err != nil {
|
||||||
|
return fmt.Errorf("couldn't remove old fingerprint mapping file %s: %s", p.mappingsFileName(), err)
|
||||||
|
}
|
||||||
// The mappings to rebuild.
|
// The mappings to rebuild.
|
||||||
fpm := fpMappings{}
|
fpm := fpMappings{}
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,9 @@ func (i *FingerprintMetricIndex) IndexBatch(mapping FingerprintMetricMapping) er
|
||||||
b := i.NewBatch()
|
b := i.NewBatch()
|
||||||
|
|
||||||
for fp, m := range mapping {
|
for fp, m := range mapping {
|
||||||
b.Put(codable.Fingerprint(fp), codable.Metric(m))
|
if err := b.Put(codable.Fingerprint(fp), codable.Metric(m)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.Commit(b)
|
return i.Commit(b)
|
||||||
|
@ -72,7 +74,9 @@ func (i *FingerprintMetricIndex) UnindexBatch(mapping FingerprintMetricMapping)
|
||||||
b := i.NewBatch()
|
b := i.NewBatch()
|
||||||
|
|
||||||
for fp := range mapping {
|
for fp := range mapping {
|
||||||
b.Delete(codable.Fingerprint(fp))
|
if err := b.Delete(codable.Fingerprint(fp)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.Commit(b)
|
return i.Commit(b)
|
||||||
|
@ -196,14 +200,18 @@ type LabelPairFingerprintIndex struct {
|
||||||
//
|
//
|
||||||
// While this method is fundamentally goroutine-safe, note that the order of
|
// While this method is fundamentally goroutine-safe, note that the order of
|
||||||
// execution for multiple batches executed concurrently is undefined.
|
// execution for multiple batches executed concurrently is undefined.
|
||||||
func (i *LabelPairFingerprintIndex) IndexBatch(m LabelPairFingerprintsMapping) error {
|
func (i *LabelPairFingerprintIndex) IndexBatch(m LabelPairFingerprintsMapping) (err error) {
|
||||||
batch := i.NewBatch()
|
batch := i.NewBatch()
|
||||||
|
|
||||||
for pair, fps := range m {
|
for pair, fps := range m {
|
||||||
if len(fps) == 0 {
|
if len(fps) == 0 {
|
||||||
batch.Delete(codable.LabelPair(pair))
|
err = batch.Delete(codable.LabelPair(pair))
|
||||||
} else {
|
} else {
|
||||||
batch.Put(codable.LabelPair(pair), fps)
|
err = batch.Put(codable.LabelPair(pair), fps)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1123,7 +1123,7 @@ func (p *persistence) fingerprintsModifiedBefore(beforeTime model.Time) ([]model
|
||||||
var fp codable.Fingerprint
|
var fp codable.Fingerprint
|
||||||
var tr codable.TimeRange
|
var tr codable.TimeRange
|
||||||
fps := []model.Fingerprint{}
|
fps := []model.Fingerprint{}
|
||||||
p.archivedFingerprintToTimeRange.ForEach(func(kv index.KeyValueAccessor) error {
|
err := p.archivedFingerprintToTimeRange.ForEach(func(kv index.KeyValueAccessor) error {
|
||||||
if err := kv.Value(&tr); err != nil {
|
if err := kv.Value(&tr); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1135,7 +1135,7 @@ func (p *persistence) fingerprintsModifiedBefore(beforeTime model.Time) ([]model
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return fps, nil
|
return fps, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// archivedMetric retrieves the archived metric with the given fingerprint. This
|
// archivedMetric retrieves the archived metric with the given fingerprint. This
|
||||||
|
@ -1438,11 +1438,15 @@ func (p *persistence) checkpointFPMappings(fpm fpMappings) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
f.Sync()
|
syncErr := f.Sync()
|
||||||
closeErr := f.Close()
|
closeErr := f.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
err = syncErr
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
err = closeErr
|
err = closeErr
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
@ -1111,7 +1111,9 @@ func (s *memorySeriesStorage) maintainArchivedSeries(fp model.Fingerprint, befor
|
||||||
s.seriesOps.WithLabelValues(archivePurge).Inc()
|
s.seriesOps.WithLabelValues(archivePurge).Inc()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.persistence.updateArchivedTimeRange(fp, newFirstTime, lastTime)
|
if err := s.persistence.updateArchivedTimeRange(fp, newFirstTime, lastTime); err != nil {
|
||||||
|
log.Errorf("Error updating archived time range for fingerprint %v: %s", fp, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See persistence.loadChunks for detailed explanation.
|
// See persistence.loadChunks for detailed explanation.
|
||||||
|
|
|
@ -30,7 +30,9 @@ type testStorageCloser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testStorageCloser) Close() {
|
func (t *testStorageCloser) Close() {
|
||||||
t.storage.Stop()
|
if err := t.storage.Stop(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
t.directory.Close()
|
t.directory.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -272,11 +272,11 @@ func (te Expander) Expand() (result string, resultErr error) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
|
||||||
tmpl, err := text_template.New(te.name).Funcs(te.funcMap).Parse(te.text)
|
tmpl, err := text_template.New(te.name).Funcs(te.funcMap).Parse(te.text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error parsing template %v: %v", te.name, err)
|
return "", fmt.Errorf("error parsing template %v: %v", te.name, err)
|
||||||
}
|
}
|
||||||
|
var buffer bytes.Buffer
|
||||||
err = tmpl.Execute(&buffer, te.data)
|
err = tmpl.Execute(&buffer, te.data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error executing template %v: %v", te.name, err)
|
return "", fmt.Errorf("error executing template %v: %v", te.name, err)
|
||||||
|
@ -296,7 +296,6 @@ func (te Expander) ExpandHTML(templateFiles []string) (result string, resultErr
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
|
||||||
tmpl := html_template.New(te.name).Funcs(html_template.FuncMap(te.funcMap))
|
tmpl := html_template.New(te.name).Funcs(html_template.FuncMap(te.funcMap))
|
||||||
tmpl.Funcs(html_template.FuncMap{
|
tmpl.Funcs(html_template.FuncMap{
|
||||||
"tmpl": func(name string, data interface{}) (html_template.HTML, error) {
|
"tmpl": func(name string, data interface{}) (html_template.HTML, error) {
|
||||||
|
@ -315,6 +314,7 @@ func (te Expander) ExpandHTML(templateFiles []string) (result string, resultErr
|
||||||
return "", fmt.Errorf("error parsing template files for %v: %v", te.name, err)
|
return "", fmt.Errorf("error parsing template files for %v: %v", te.name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var buffer bytes.Buffer
|
||||||
err = tmpl.Execute(&buffer, te.data)
|
err = tmpl.Execute(&buffer, te.data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error executing template %v: %v", te.name, err)
|
return "", fmt.Errorf("error executing template %v: %v", te.name, err)
|
||||||
|
|
|
@ -156,13 +156,16 @@ func BasicHelp(app *App, ts string) func() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
t.Execute(&buf, struct {
|
err := t.Execute(&buf, struct {
|
||||||
Name string
|
Name string
|
||||||
Commands []command
|
Commands []command
|
||||||
}{
|
}{
|
||||||
Name: app.Name,
|
Name: app.Name,
|
||||||
Commands: cmds,
|
Commands: cmds,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("error executing help template: %s", err))
|
||||||
|
}
|
||||||
return strings.TrimSpace(buf.String())
|
return strings.TrimSpace(buf.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ func TestLocking(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// File must now exist.
|
// File must now exist.
|
||||||
if _, err := os.Stat(fileName); err != nil {
|
if _, err = os.Stat(fileName); err != nil {
|
||||||
t.Errorf("Could not stat file %q expected to exist: %s", fileName, err)
|
t.Errorf("Could not stat file %q expected to exist: %s", fileName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func TestLocking(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// File must still exist.
|
// File must still exist.
|
||||||
if _, err := os.Stat(fileName); err != nil {
|
if _, err = os.Stat(fileName); err != nil {
|
||||||
t.Errorf("Could not stat file %q expected to exist: %s", fileName, err)
|
t.Errorf("Could not stat file %q expected to exist: %s", fileName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,12 +48,16 @@ func NewDeadlineRoundTripper(timeout time.Duration, proxyURL *url.URL) http.Roun
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
c, err = net.DialTimeout(netw, addr, timeout)
|
c, err = net.DialTimeout(netw, addr, timeout)
|
||||||
|
if err != nil {
|
||||||
if err == nil {
|
return nil, err
|
||||||
c.SetDeadline(start.Add(timeout))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
if err = c.SetDeadline(start.Add(timeout)); err != nil {
|
||||||
|
c.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue