Skip to content

Commit 7a15d5e

Browse files
authored
Merge pull request #91 from grafana/paula/add-disk-size-bytes-metric
Add metric to track the size and labels of individual disks
2 parents 12ebb39 + 1dff10c commit 7a15d5e

File tree

7 files changed

+38
-2
lines changed

7 files changed

+38
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ It exposes the following metrics:
4747
|-|-|
4848
| `unused_disks_count` | How many unused disks are in this provider |
4949
| `unused_disks_size_gb` | Total size of unused disks in this provider in GB |
50+
| `unused_disk_size_bytes` | Size of each disk in bytes |
5051
| `unused_disks_last_used_at` | Last timestamp (unix ms) when this disk was used. GCP only! |
5152
| `unused_provider_duration_ms` | How long in milliseconds took to fetch this provider information |
5253
| `unused_provider_info` | CSP information |

aws/disk.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/grafana/unused"
88
)
99

10+
const GiBbytes = 1073741824
11+
1012
var _ unused.Disk = &Disk{}
1113

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

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

51+
// SizeBytes returns the size of this AWS EC2 volume in bytes.
52+
func (d *Disk) SizeBytes() float64 { return float64(*d.Volume.Size) * GiBbytes }
53+
4954
// LastUsedAt returns a zero [time.Time] value, as AWS does not
5055
// provide this information.
5156
func (d *Disk) LastUsedAt() time.Time { return time.Time{} }

azure/disk.go

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func (d *Disk) Name() string { return *d.Disk.Name }
2929
// SizeGB returns the size of this Azure compute disk in GB.
3030
func (d *Disk) SizeGB() int { return int(*d.Disk.DiskSizeGB) }
3131

32+
// SizeBytes returns the size of this Azure compute disk in bytes.
33+
func (d *Disk) SizeBytes() float64 { return float64(*d.Disk.DiskSizeBytes) }
34+
3235
// CreatedAt returns the time when this Azure compute disk was
3336
// created.
3437
func (d *Disk) CreatedAt() time.Time { return d.Disk.TimeCreated.ToTime() }

cmd/unused-exporter/exporter.go

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type exporter struct {
3232

3333
info *prometheus.Desc
3434
count *prometheus.Desc
35+
ds *prometheus.Desc
3536
size *prometheus.Desc
3637
dur *prometheus.Desc
3738
suc *prometheus.Desc
@@ -63,6 +64,11 @@ func registerExporter(ctx context.Context, providers []unused.Provider, cfg conf
6364
"How many unused disks are in this provider",
6465
append(labels, "k8s_namespace"),
6566
nil),
67+
ds: prometheus.NewDesc(
68+
prometheus.BuildFQName(namespace, "disk", "size_bytes"),
69+
"Disk size in bytes",
70+
append(labels, []string{"disk", "k8s_namespace", "type", "region", "zone"}...),
71+
nil),
6672

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

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

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

278285
return float64(lastUsed.UnixMilli())
279286
}
287+
288+
func getRegionFromZone(p unused.Provider, z string) string {
289+
if strings.ToLower(p.Name()) == "azure" {
290+
return z
291+
}
292+
293+
// Drop the last character to get the region from the zone for GCP and AWS
294+
return z[:len(z)-1]
295+
}

disk.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ type Disk interface {
1515
// Name returns the disk name.
1616
Name() string
1717

18-
// SizeGB returns the disk size in GB.
18+
// SizeGB returns the disk size in GB (Azure/GCP) and GiB for AWS.
1919
SizeGB() int
2020

21+
// SizeBytes returns the disk size in bytes.
22+
SizeBytes() float64
23+
2124
// CreatedAt returns the time when the disk was created.
2225
CreatedAt() time.Time
2326

gcp/disk.go

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
compute "google.golang.org/api/compute/v1"
1010
)
1111

12+
const GBbytes = 1_000_000_000
13+
1214
// ensure we are properly defining the interface
1315
var _ unused.Disk = &Disk{}
1416

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

56+
// SizeBytes returns the size of the GCP compute disk in bytes.
57+
func (d *Disk) SizeBytes() float64 { return float64(d.Disk.SizeGb) * GBbytes }
58+
5459
// DiskType Type returns the type of the GCP compute disk.
5560
func (d *Disk) DiskType() unused.DiskType {
5661
splitDiskType := strings.Split(d.Disk.Type, "/")

unusedtest/disk.go

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
var _ unused.Disk = Disk{}
1010

11+
const GBbytes = 1_000_000_000
12+
1113
// Disk implements [unused.Disk] for testing purposes.
1214
type Disk struct {
1315
id, name string
@@ -30,4 +32,5 @@ func (d Disk) CreatedAt() time.Time { return d.createdAt }
3032
func (d Disk) Meta() unused.Meta { return d.meta }
3133
func (d Disk) LastUsedAt() time.Time { return d.createdAt.Add(1 * time.Minute) }
3234
func (d Disk) SizeGB() int { return d.size }
35+
func (d Disk) SizeBytes() float64 { return float64(d.size) * GBbytes }
3336
func (d Disk) DiskType() unused.DiskType { return d.diskType }

0 commit comments

Comments
 (0)