Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add last modified object size metric #6

Merged
merged 1 commit into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Flags can also be set as environment variables, prefixed by `S3_EXPORTER_`. For
| ------ | ------- | ------ |
| s3_biggest_object_size_bytes | The size of the largest object. | bucket, prefix |
| s3_last_modified_object_date | The modification date of the most recently modified object. | bucket, prefix |
| s3_last_modified_object_size_bytes | The size of the object that was modified most recently. | bucket, prefix |
| s3_list_success | Did the ListObjects operation complete successfully? | bucket, prefix |
| s3_objects_size_sum_bytes | The sum of the size of all the objects. | bucket, prefix |
| s3_objects_total | The total number of objects. | bucket, prefix |
Expand Down Expand Up @@ -86,4 +87,4 @@ scrape_configs:
Return series where the last modified object date is more than 24 hours ago:
```
(time() - s3_last_modified_object_date) / 3600 > 24
```
```
11 changes: 11 additions & 0 deletions s3_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ var (
"The last modified date of the object that was modified most recently",
[]string{"bucket", "prefix"}, nil,
)
s3LastModifiedObjectSize = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "last_modified_object_size_bytes"),
"The size of the object that was modified most recently",
[]string{"bucket", "prefix"}, nil,
)
s3ObjectTotal = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "objects_total"),
"The total number of objects for the bucket/prefix combination",
Expand Down Expand Up @@ -60,6 +65,7 @@ type Exporter struct {
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- s3ListSuccess
ch <- s3LastModifiedObjectDate
ch <- s3LastModifiedObjectSize
ch <- s3ObjectTotal
ch <- s3SumSize
ch <- s3BiggestSize
Expand All @@ -71,6 +77,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
var numberOfObjects float64
var totalSize int64
var biggestObjectSize int64
var lastObjectSize int64

query := &s3.ListObjectsV2Input{
Bucket: &e.bucket,
Expand All @@ -93,6 +100,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
totalSize = totalSize + *item.Size
if item.LastModified.After(lastModified) {
lastModified = *item.LastModified
lastObjectSize = *item.Size
}
if *item.Size > biggestObjectSize {
biggestObjectSize = *item.Size
Expand All @@ -108,6 +116,9 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
s3LastModifiedObjectDate, prometheus.GaugeValue, float64(lastModified.UnixNano()/1e9), e.bucket, e.prefix,
)
ch <- prometheus.MustNewConstMetric(
s3LastModifiedObjectSize, prometheus.GaugeValue, float64(lastObjectSize), e.bucket, e.prefix,
)
ch <- prometheus.MustNewConstMetric(
s3ObjectTotal, prometheus.GaugeValue, numberOfObjects, e.bucket, e.prefix,
)
Expand Down
3 changes: 3 additions & 0 deletions s3_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
ExpectedOutputLines: []string{
"s3_list_success{bucket=\"mock\",prefix=\"one\"} 1",
"s3_last_modified_object_date{bucket=\"mock\",prefix=\"one\"} 1.5604596e+09",
"s3_last_modified_object_size_bytes{bucket=\"mock\",prefix=\"one\"} 1234",
"s3_biggest_object_size_bytes{bucket=\"mock\",prefix=\"one\"} 1234",
"s3_objects_size_sum_bytes{bucket=\"mock\",prefix=\"one\"} 1234",
"s3_objects_total{bucket=\"mock\",prefix=\"one\"} 1",
Expand Down Expand Up @@ -50,6 +51,7 @@ var (
ExpectedOutputLines: []string{
"s3_biggest_object_size_bytes{bucket=\"mock\",prefix=\"none\"} 0",
"s3_last_modified_object_date{bucket=\"mock\",prefix=\"none\"} -6.795364578e+09",
"s3_last_modified_object_size_bytes{bucket=\"mock\",prefix=\"none\"} 0",
"s3_list_success{bucket=\"mock\",prefix=\"none\"} 1",
"s3_objects_size_sum_bytes{bucket=\"mock\",prefix=\"none\"} 0",
"s3_objects_total{bucket=\"mock\",prefix=\"none\"} 0",
Expand All @@ -71,6 +73,7 @@ var (
ExpectedOutputLines: []string{
"s3_biggest_object_size_bytes{bucket=\"mock\",prefix=\"multiple\"} 4567",
"s3_last_modified_object_date{bucket=\"mock\",prefix=\"multiple\"} 1.568592e+09",
"s3_last_modified_object_size_bytes{bucket=\"mock\",prefix=\"multiple\"} 4567",
"s3_list_success{bucket=\"mock\",prefix=\"multiple\"} 1",
"s3_objects_size_sum_bytes{bucket=\"mock\",prefix=\"multiple\"} 11602",
"s3_objects_total{bucket=\"mock\",prefix=\"multiple\"} 4",
Expand Down