-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configurable disk alert threshold (#1318)
* feat: configurable disk alert threshold * fix flag name * improve alerting heuristic and rename config option
- Loading branch information
1 parent
f9f7e33
commit 7281bd8
Showing
9 changed files
with
188 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,57 @@ | ||
package health | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/pyroscope-io/pyroscope/pkg/util/bytesize" | ||
"github.com/pyroscope-io/pyroscope/pkg/util/disk" | ||
) | ||
|
||
var ( | ||
errZeroTotalSize = errors.New("total disk size is zero") | ||
errTotalLessThanAvailable = errors.New("total disk size is less than available space") | ||
) | ||
|
||
const minAvailSpace = bytesize.GB | ||
|
||
type DiskPressure struct { | ||
Threshold bytesize.ByteSize | ||
Threshold float64 | ||
Path string | ||
} | ||
|
||
func (d DiskPressure) Probe() (StatusMessage, error) { | ||
var m StatusMessage | ||
available, err := disk.FreeSpace(d.Path) | ||
if d.Threshold == 0 { | ||
return StatusMessage{Status: Healthy}, nil | ||
} | ||
u, err := disk.Usage(d.Path) | ||
if err != nil { | ||
return m, err | ||
return StatusMessage{}, err | ||
} | ||
if available < d.Threshold { | ||
return d.makeProbe(u) | ||
} | ||
|
||
func (d DiskPressure) makeProbe(u disk.UsageStats) (StatusMessage, error) { | ||
var m StatusMessage | ||
if u.Total == 0 { | ||
return m, errZeroTotalSize | ||
} | ||
if u.Available > u.Total { | ||
return m, errTotalLessThanAvailable | ||
} | ||
m.Status = Healthy | ||
if u.Available < d.minRequired(u) { | ||
availPercent := 100 * float64(u.Available) / float64(u.Total) | ||
m.Message = fmt.Sprintf("Disk space is running low: %v available (%.1f%%)", u.Available, availPercent) | ||
m.Status = Critical | ||
} else { | ||
m.Status = Healthy | ||
} | ||
m.Message = fmt.Sprintf("Disk space is running low: %v available", available) | ||
return m, nil | ||
} | ||
|
||
func (d DiskPressure) minRequired(u disk.UsageStats) bytesize.ByteSize { | ||
t := bytesize.ByteSize(float64(u.Total) / 100 * d.Threshold) | ||
if t > minAvailSpace { | ||
return t | ||
} | ||
return minAvailSpace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package health | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/pyroscope-io/pyroscope/pkg/util/bytesize" | ||
"github.com/pyroscope-io/pyroscope/pkg/util/disk" | ||
) | ||
|
||
var _ = Describe("DiskPressure", func() { | ||
It("does not fire if threshold is zero", func() { | ||
var d DiskPressure | ||
m, err := d.Probe() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m.Status).To(Equal(Healthy)) | ||
Expect(m.Message).To(BeEmpty()) | ||
}) | ||
|
||
It("does not fire if available space is greater than the configured threshold", func() { | ||
d := DiskPressure{ | ||
Threshold: 5, | ||
} | ||
m, err := d.makeProbe(disk.UsageStats{ | ||
Total: 10 * bytesize.GB, | ||
Available: 1 * bytesize.GB, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m.Status).To(Equal(Healthy)) | ||
Expect(m.Message).To(BeEmpty()) | ||
}) | ||
|
||
It("fires if less than the configured threshold is available", func() { | ||
d := DiskPressure{ | ||
Threshold: 5, | ||
} | ||
m, err := d.makeProbe(disk.UsageStats{ | ||
Total: 100 * bytesize.GB, | ||
Available: 4 * bytesize.GB, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m.Status).To(Equal(Critical)) | ||
Expect(m.Message).To(Equal("Disk space is running low: 4.00 GB available (4.0%)")) | ||
}) | ||
|
||
It("fires if less than 1GB is available", func() { | ||
d := DiskPressure{ | ||
Threshold: 5, | ||
} | ||
m, err := d.makeProbe(disk.UsageStats{ | ||
Total: 5 * bytesize.GB, | ||
Available: bytesize.GB - 1, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m.Status).To(Equal(Critical)) | ||
Expect(m.Message).To(Equal("Disk space is running low: 1024.00 MB available (20.0%)")) | ||
}) | ||
|
||
It("fires if available is less than the configured threshold", func() { | ||
d := DiskPressure{ | ||
Threshold: 5, | ||
} | ||
m, err := d.makeProbe(disk.UsageStats{ | ||
Total: 1 * bytesize.GB, | ||
Available: 1 * bytesize.MB, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m.Status).To(Equal(Critical)) | ||
Expect(m.Message).To(Equal("Disk space is running low: 1.00 MB available (0.1%)")) | ||
}) | ||
|
||
It("fires if no space available", func() { | ||
d := DiskPressure{ | ||
Threshold: 5, | ||
} | ||
m, err := d.makeProbe(disk.UsageStats{ | ||
Total: 100 * bytesize.MB, | ||
Available: 0, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m.Status).To(Equal(Critical)) | ||
Expect(m.Message).To(Equal("Disk space is running low: 0 bytes available (0.0%)")) | ||
}) | ||
|
||
It("fails if Available > Total", func() { | ||
var d DiskPressure | ||
_, err := d.makeProbe(disk.UsageStats{ | ||
Total: 1 * bytesize.GB, | ||
Available: 2 * bytesize.GB, | ||
}) | ||
Expect(err).To(MatchError(errTotalLessThanAvailable)) | ||
}) | ||
|
||
It("fails if Total is zero", func() { | ||
var d DiskPressure | ||
_, err := d.makeProbe(disk.UsageStats{ | ||
Total: 0, | ||
Available: 2 * bytesize.GB, | ||
}) | ||
Expect(err).To(MatchError(errZeroTotalSize)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package disk | ||
|
||
import ( | ||
"github.com/pyroscope-io/pyroscope/pkg/util/bytesize" | ||
) | ||
|
||
type UsageStats struct { | ||
Total bytesize.ByteSize | ||
Available bytesize.ByteSize | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters