Skip to content

Commit

Permalink
Merge pull request #50 from BeryJu/object-storage
Browse files Browse the repository at this point in the history
Add Collector for Object Storage buckets
  • Loading branch information
DazWilkin committed Apr 16, 2024
2 parents 773f97c + 0ce6b33 commit d90e6a1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ The full list is below.
| `linode_nodebalancer_transfer_total_bytes` | Gauge |
| `linode_nodebalancer_transfer_out_bytes` | Gauge |
| `linode_nodebalancer_transfer_in_bytes` | Gauge |
| `linode_objectstorage_objects_count` | Gauge |
| `linode_objectstorage_size_bytes` | Gauge |
| `linode_volume_up` | Counter |
| `linode_tickets_count` | Gauge |

Expand Down
91 changes: 91 additions & 0 deletions collector/objectstorage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package collector

import (
"context"
"log"
"sync"

"github.com/linode/linodego"
"github.com/prometheus/client_golang/prometheus"
)

// ObjectStorageCollector represents a Linode object storage bucket
type ObjectStorageCollector struct {
client linodego.Client

Size *prometheus.Desc
ObjectsCount *prometheus.Desc
}

// NewObjectStorageCollector creates a ObjectStorageCollector
func NewObjectStorageCollector(client linodego.Client) *ObjectStorageCollector {
log.Println("[NewObjectStorageCollector] Entered")
subsystem := "objectstorage"
labelKeys := []string{"label", "region"}
return &ObjectStorageCollector{
client: client,

Size: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "size_bytes"),
"Size of a bucket (in bytes)",
labelKeys,
nil,
),
ObjectsCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "objects_count"),
"Count of objects in a bucket",
labelKeys,
nil,
),
}
}

// Collect implements Collector interface and is called by Prometheus to collect metrics
func (c *ObjectStorageCollector) Collect(ch chan<- prometheus.Metric) {
log.Println("[ObjectStorageCollector:Collect] Entered")
ctx := context.Background()

buckets, err := c.client.ListObjectStorageBuckets(ctx, nil)
if err != nil {
log.Println(err)
return
}
log.Printf("[ObjectStorageCollector:Collect] len(buckets)=%d", len(buckets))

var wg sync.WaitGroup
for _, bucket := range buckets {
log.Printf("[ObjectStorageCollector:Collect] Bucket ID (%s)", bucket.Label)

wg.Add(1)
go func(bucket linodego.ObjectStorageBucket) {
defer wg.Done()
log.Printf("[ObjectStorageCollector:Collect:go] Bucket ID (%s)", bucket.Label)
labelValues := []string{
bucket.Label,
bucket.Cluster,
}

ch <- prometheus.MustNewConstMetric(
c.Size,
prometheus.GaugeValue,
float64(bucket.Size),
labelValues...,
)
ch <- prometheus.MustNewConstMetric(
c.ObjectsCount,
prometheus.GaugeValue,
float64(bucket.Objects),
labelValues...,
)
}(bucket)
}
wg.Wait()
log.Println("[ObjectStorageCollector:Collect] Completes")
}

// Describe implements Collector interface and is called by Prometheus to describe metrics
func (c *ObjectStorageCollector) Describe(ch chan<- *prometheus.Desc) {
log.Println("[ObjectStorageCollector:Describe] Entered")
ch <- c.Size
log.Println("[ObjectStorageCollector:Describe] Completes")
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func main() {
registry.MustRegister(collector.NewNodeBalancerCollector(client))
registry.MustRegister(collector.NewTicketCollector(client))
registry.MustRegister(collector.NewVolumeCollector(client))
registry.MustRegister(collector.NewObjectStorageCollector(client))

mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(rootHandler))
Expand Down

0 comments on commit d90e6a1

Please sign in to comment.