Skip to content

Commit

Permalink
Merge pull request #91 from grafana/paula/add-disk-size-bytes-metric
Browse files Browse the repository at this point in the history
Add metric to track the size and labels of individual disks
  • Loading branch information
paulajulve authored Apr 10, 2024
2 parents 12ebb39 + 1dff10c commit 7a15d5e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ It exposes the following metrics:
|-|-|
| `unused_disks_count` | How many unused disks are in this provider |
| `unused_disks_size_gb` | Total size of unused disks in this provider in GB |
| `unused_disk_size_bytes` | Size of each disk in bytes |
| `unused_disks_last_used_at` | Last timestamp (unix ms) when this disk was used. GCP only! |
| `unused_provider_duration_ms` | How long in milliseconds took to fetch this provider information |
| `unused_provider_info` | CSP information |
Expand Down
7 changes: 6 additions & 1 deletion aws/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/grafana/unused"
)

const GiBbytes = 1073741824

var _ unused.Disk = &Disk{}

// Disk holds information about an AWS EC2 volume.
Expand Down Expand Up @@ -43,9 +45,12 @@ func (d *Disk) CreatedAt() time.Time { return *d.Volume.CreateTime }
// Meta returns the disk metadata.
func (d *Disk) Meta() unused.Meta { return d.meta }

// SizeGB returns the size of this AWS EC2 volume in GB.
// SizeGB returns the size of this AWS EC2 volume in GiB.
func (d *Disk) SizeGB() int { return int(*d.Volume.Size) }

// SizeBytes returns the size of this AWS EC2 volume in bytes.
func (d *Disk) SizeBytes() float64 { return float64(*d.Volume.Size) * GiBbytes }

// LastUsedAt returns a zero [time.Time] value, as AWS does not
// provide this information.
func (d *Disk) LastUsedAt() time.Time { return time.Time{} }
Expand Down
3 changes: 3 additions & 0 deletions azure/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (d *Disk) Name() string { return *d.Disk.Name }
// SizeGB returns the size of this Azure compute disk in GB.
func (d *Disk) SizeGB() int { return int(*d.Disk.DiskSizeGB) }

// SizeBytes returns the size of this Azure compute disk in bytes.
func (d *Disk) SizeBytes() float64 { return float64(*d.Disk.DiskSizeBytes) }

// CreatedAt returns the time when this Azure compute disk was
// created.
func (d *Disk) CreatedAt() time.Time { return d.Disk.TimeCreated.ToTime() }
Expand Down
16 changes: 16 additions & 0 deletions cmd/unused-exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type exporter struct {

info *prometheus.Desc
count *prometheus.Desc
ds *prometheus.Desc
size *prometheus.Desc
dur *prometheus.Desc
suc *prometheus.Desc
Expand Down Expand Up @@ -63,6 +64,11 @@ func registerExporter(ctx context.Context, providers []unused.Provider, cfg conf
"How many unused disks are in this provider",
append(labels, "k8s_namespace"),
nil),
ds: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "disk", "size_bytes"),
"Disk size in bytes",
append(labels, []string{"disk", "k8s_namespace", "type", "region", "zone"}...),
nil),

size: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "disks", "size_gb"),
Expand Down Expand Up @@ -177,6 +183,7 @@ func (e *exporter) pollProvider(p unused.Provider) {
}

addMetric(&ms, p, e.dlu, lastUsedTS(d), d.ID(), m.CreatedForPV(), m.CreatedForPVC(), m.Zone())
addMetric(&ms, p, e.ds, d.SizeBytes(), d.ID(), ns, string(d.DiskType()), getRegionFromZone(p, m.Zone()), m.Zone())
}

addMetric(&ms, p, e.info, 1)
Expand Down Expand Up @@ -277,3 +284,12 @@ func lastUsedTS(d unused.Disk) float64 {

return float64(lastUsed.UnixMilli())
}

func getRegionFromZone(p unused.Provider, z string) string {
if strings.ToLower(p.Name()) == "azure" {
return z
}

// Drop the last character to get the region from the zone for GCP and AWS
return z[:len(z)-1]
}
5 changes: 4 additions & 1 deletion disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ type Disk interface {
// Name returns the disk name.
Name() string

// SizeGB returns the disk size in GB.
// SizeGB returns the disk size in GB (Azure/GCP) and GiB for AWS.
SizeGB() int

// SizeBytes returns the disk size in bytes.
SizeBytes() float64

// CreatedAt returns the time when the disk was created.
CreatedAt() time.Time

Expand Down
5 changes: 5 additions & 0 deletions gcp/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
compute "google.golang.org/api/compute/v1"
)

const GBbytes = 1_000_000_000

// ensure we are properly defining the interface
var _ unused.Disk = &Disk{}

Expand Down Expand Up @@ -51,6 +53,9 @@ func (d *Disk) LastUsedAt() time.Time {
// SizeGB returns the size of the GCP compute disk in GB.
func (d *Disk) SizeGB() int { return int(d.Disk.SizeGb) }

// SizeBytes returns the size of the GCP compute disk in bytes.
func (d *Disk) SizeBytes() float64 { return float64(d.Disk.SizeGb) * GBbytes }

// DiskType Type returns the type of the GCP compute disk.
func (d *Disk) DiskType() unused.DiskType {
splitDiskType := strings.Split(d.Disk.Type, "/")
Expand Down
3 changes: 3 additions & 0 deletions unusedtest/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

var _ unused.Disk = Disk{}

const GBbytes = 1_000_000_000

// Disk implements [unused.Disk] for testing purposes.
type Disk struct {
id, name string
Expand All @@ -30,4 +32,5 @@ func (d Disk) CreatedAt() time.Time { return d.createdAt }
func (d Disk) Meta() unused.Meta { return d.meta }
func (d Disk) LastUsedAt() time.Time { return d.createdAt.Add(1 * time.Minute) }
func (d Disk) SizeGB() int { return d.size }
func (d Disk) SizeBytes() float64 { return float64(d.size) * GBbytes }
func (d Disk) DiskType() unused.DiskType { return d.diskType }

0 comments on commit 7a15d5e

Please sign in to comment.