Skip to content

Commit

Permalink
feat: add metrics for scan and push timestamps (#5)
Browse files Browse the repository at this point in the history
* feat: add metrics for scan and push timestamps

* chore: update README with timestamp metrics
  • Loading branch information
js-timbirkett authored Jul 13, 2021
1 parent 48f128c commit 184a515
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ aws_ecr_image_size_in_bytes{digest="sha256:9f47a709e9bea292ce1906f216df5f0804934
....
```

#### `aws_ecr_image_pushed_at_timestamp_seconds`
- **Type:** Gauge
- **Description:** The unix timestamp that this image was pushed at
- **Example:**
```
# HELP aws_ecr_image_pushed_at_timestamp_seconds The unix timestamp that this image was pushed at
# TYPE aws_ecr_image_pushed_at_timestamp_seconds gauge
aws_ecr_image_pushed_at_timestamp_seconds{digest="sha256:046c3c95cfd4ab660947885571130d34fef6fd5ddabb3ef84ac7fd7b79e4b8f1",name="chimunk",registry_id="486979256902",tag="1df508a3"} 1.601994911e+09
aws_ecr_image_pushed_at_timestamp_seconds{digest="sha256:10bcbc280f1bc017e767a2fc1ecb37085979dd0807fe312411ee9d3abc78f0b6",name="savage-lands",registry_id="486979256902",tag="v1.0.41"} 1.593518011e+09
aws_ecr_image_pushed_at_timestamp_seconds{digest="sha256:b869d1ffa62b8aba6ac3e26056acacf276425287513bcc77317fa9d2b607c054",name="luna-tuna",registry_id="486979256902",tag="8fd066ee"} 1.596207388e+09
....
....
```

#### `aws_ecr_image_scan_severity_count`
- **Type:** Gauge
- **Description:** Scan result counts per image/tag/ by severity
Expand All @@ -94,6 +108,20 @@ aws_ecr_image_scan_severity_count{digest="sha256:f340879c042e88e08d7540c7ec26fb0
....
```

#### `aws_ecr_image_scan_completed_at_timestamp_seconds`
- **Type:** Gauge
- **Description:** The unix timestamp when the scan was completed
- **Example:**
```
# HELP aws_ecr_image_scan_completed_at_timestamp_seconds The unix timestamp when the scan was completed
# TYPE aws_ecr_image_scan_completed_at_timestamp_seconds gauge
aws_ecr_image_scan_completed_at_timestamp_seconds{digest="sha256:0b26628c113374546c4790e01bce65c3f4642db063286f16fe13e256923b2689",name="moose-juice",registry_id="486979256902",tag="5a35d50d"} 1.617208126e+09
aws_ecr_image_scan_completed_at_timestamp_seconds{digest="sha256:a910ed7e15cb5fc7e5f0f2294f8028b56689be563bd1d352a4254197739dfa8e",name="super-goggles",registry_id="486979256902",tag="2faa6445"} 1.618313952e+09
aws_ecr_image_scan_completed_at_timestamp_seconds{digest="sha256:981a9c17106eee1099d815f82dfb45f4e8d016a63816fec92f290f1af0117c37",name="foot-massage",registry_id="486979256902",tag="227c8031"} 1.622629411e+09
....
....
```

It should be possible to join metrics to show things like whether or not your repository is set
to scan on push, the number of images in your repository, the size and scan result summaries for currently
running images.
Expand Down
41 changes: 41 additions & 0 deletions ecr_exporter/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from prometheus_client.core import InfoMetricFamily, GaugeMetricFamily
from cachetools import TTLCache
from datetime import timezone


def _ecr_client():
Expand Down Expand Up @@ -67,12 +68,24 @@ def collect(self):
labels=["name", "tag", "digest", "registry_id"],
)

image_push_timestamp_metrics = GaugeMetricFamily(
"aws_ecr_image_pushed_at_timestamp_seconds",
"The unix timestamp that this image was pushed at",
labels=["name", "tag", "digest", "registry_id"],
)

image_scan_metrics = GaugeMetricFamily(
"aws_ecr_image_scan_severity_count",
"ECR image scan summary results",
labels=["name", "tag", "digest", "registry_id", "severity"],
)

image_scan_timestamp_metrics = GaugeMetricFamily(
"aws_ecr_image_scan_completed_at_timestamp_seconds",
"The unix timestamp when the scan was completed",
labels=["name", "tag", "digest", "registry_id"],
)

for repo in repositories:
images = self.imagecache.get(repo["repositoryName"])

Expand All @@ -95,6 +108,19 @@ def collect(self):
],
int(image["imageSizeInBytes"]),
)
image_push_timestamp_metrics.add_metric(
[
repo["repositoryName"],
tag,
image["imageDigest"],
self.registry_id,
],
int(
image["imagePushedAt"]
.replace(tzinfo=timezone.utc)
.timestamp()
),
)

scan_summary = image.get("imageScanFindingsSummary")
if scan_summary and scan_summary.get("findingSeverityCounts"):
Expand All @@ -110,12 +136,27 @@ def collect(self):
],
int(severity_counts[severity]),
)
image_scan_timestamp_metrics.add_metric(
[
repo["repositoryName"],
tag,
image["imageDigest"],
self.registry_id,
],
int(
scan_summary["imageScanCompletedAt"]
.replace(tzinfo=timezone.utc)
.timestamp()
),
)

return [
repository_count_metric,
repository_info_metrics,
image_size_metrics,
image_push_timestamp_metrics,
image_scan_metrics,
image_scan_timestamp_metrics,
]

def refresh_repository_cache(self):
Expand Down

0 comments on commit 184a515

Please sign in to comment.