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

feat: add more metrics #53

Merged
merged 5 commits into from
Feb 27, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ dependencies-stamp
.deps
*.tar.gz
/vendor
.idea
72 changes: 64 additions & 8 deletions ecscollector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ import (
const nanoSeconds = 1.0e9

var (
metadataDesc = prometheus.NewDesc(
"ecs_metadata_info",
"ECS service metadata.",
metadataLabels, nil)

svcCpuLimitDesc = prometheus.NewDesc(
"ecs_svc_cpu_limit",
"Total CPU Limit.",
svcLabels, nil)

svcMemLimitDesc = prometheus.NewDesc(
"ecs_svc_memory_limit_bytes",
"Total MEM Limit in bytes.",
svcLabels, nil)

cpuTotalDesc = prometheus.NewDesc(
"ecs_cpu_seconds_total",
"Total CPU usage in seconds.",
Expand All @@ -39,11 +54,6 @@ var (
"Memory usage in bytes.",
labels, nil)

memMaxUsageDesc = prometheus.NewDesc(
"ecs_memory_max_bytes",
"Maximum memory usage in bytes.",
labels, nil)

memLimitDesc = prometheus.NewDesc(
"ecs_memory_limit_bytes",
"Memory limit in bytes.",
Expand Down Expand Up @@ -99,6 +109,23 @@ var labels = []string{
"container",
}

var svcLabels = []string{
"task_arn",
}

var metadataLabels = []string{
"cluster",
"task_arn",
"family",
"revision",
"desired_status",
"known_status",
"pull_started_at",
"pull_stopped_at",
"availability_zone",
"launch_type",
}

var cpuLabels = append(
labels,
"cpu",
Expand All @@ -122,7 +149,6 @@ type collector struct {
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- cpuTotalDesc
ch <- memUsageDesc
ch <- memMaxUsageDesc
ch <- memLimitDesc
ch <- memCacheUsageDesc
ch <- networkRxBytesDesc
Expand All @@ -142,6 +168,37 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
log.Printf("Failed to retrieve metadata: %v", err)
return
}

ch <- prometheus.MustNewConstMetric(
metadataDesc,
prometheus.GaugeValue,
1.0,
metadata.Cluster,
metadata.TaskARN,
metadata.Family,
metadata.Revision,
metadata.DesiredStatus,
metadata.KnownStatus,
metadata.PullStartedAt,
metadata.PullStoppedAt,
metadata.AvailabilityZone,
metadata.LaunchType,
)

ch <- prometheus.MustNewConstMetric(
svcCpuLimitDesc,
prometheus.GaugeValue,
float64(metadata.Limits.CPU),
svcLabels...,
)

ch <- prometheus.MustNewConstMetric(
svcMemLimitDesc,
prometheus.GaugeValue,
float64(metadata.Limits.Memory),
svcLabels...,
)

stats, err := c.client.RetrieveTaskStats(ctx)
if err != nil {
log.Printf("Failed to retrieve container stats: %v", err)
Expand Down Expand Up @@ -175,7 +232,6 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {

for desc, value := range map[*prometheus.Desc]float64{
memUsageDesc: float64(s.MemoryStats.Usage),
memMaxUsageDesc: float64(s.MemoryStats.MaxUsage),
memLimitDesc: float64(s.MemoryStats.Limit),
memCacheUsageDesc: cacheValue,
} {
Expand All @@ -187,7 +243,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
)
}

// Network metrics per inteface.
// Network metrics per interface.
for iface, netStats := range s.Networks {
networkLabelVals := append(labelVals, iface)

Expand Down
54 changes: 42 additions & 12 deletions ecsmetadata/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -82,7 +82,7 @@ func (c *Client) request(ctx context.Context, uri string, out interface{}) error
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand All @@ -93,10 +93,13 @@ type ContainerStats struct {
Name string `json:"name"`
ID string `json:"id"`
NumProcs float64 `json:"num_procs"`
Read string `json:"read"`
PreRead string `json:"preread"`

CPUStats dockertypes.CPUStats `json:"cpu_stats"`
PreCPUStats dockertypes.CPUStats `json:"precpu_stats"`
MemoryStats dockertypes.MemoryStats `json:"memory_stats"`
BlkioStats dockertypes.BlkioStats `json:"blkio_stats"`

Networks map[string]struct {
RxBytes float64 `json:"rx_bytes"`
Expand All @@ -115,17 +118,23 @@ type ContainerStats struct {
} `json:"network_rate_stats"`
}

// TODO(jbd): Add storage stats.
type TaskMetadataLimits struct {
CPU float64 `json:"CPU"`
Memory float64 `json:"Memory"`
}

type TaskMetadata struct {
Cluster string `json:"Cluster"`
TaskARN string `json:"TaskARN"`
Family string `json:"Family"`
Revision string `json:"Revision"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
AvailabilityZone string `json:"AvailabilityZone"`
LaunchType string `json:"LaunchType"`
Cluster string `json:"Cluster"`
TaskARN string `json:"TaskARN"`
Family string `json:"Family"`
Revision string `json:"Revision"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
Limits TaskMetadataLimits `json:"Limits"`
PullStartedAt string `json:"PullStartedAt"`
PullStoppedAt string `json:"PullStoppedAt"`
AvailabilityZone string `json:"AvailabilityZone"`
LaunchType string `json:"LaunchType"`
Containers []struct {
DockerID string `json:"DockerId"`
Name string `json:"Name"`
Expand All @@ -135,7 +144,28 @@ type TaskMetadata struct {
Labels map[string]string `json:"Labels"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
CreatedAt string `json:"CreatedAt"`
StartedAt string `json:"StartedAt"`
Type string `json:"Type"`
ContainerARN string `json:"ContainerARN"`
Networks []struct {
NetworkMode string `json:"NetworkMode"`
IPv4Addresses []string `json:"IPv4Addresses"`
IPv6Addresses []string `json:"IPv6Addresses"`
AttachmentIndex float64 `json:"AttachmentIndex"`
MACAddress string `json:"MACAddress"`
IPv4SubnetCIDRBlock string `json:"IPv4SubnetCIDRBlock"`
IPv6SubnetCIDRBlock string `json:"IPv6SubnetCIDRBlock"`
DomainNameServers []string `json:"DomainNameServers"`
DomainNameSearchList []string `json:"DomainNameSearchList"`
PrivateDNSName string `json:"PrivateDNSName"`
SubnetGatewayIpv4Address string `json:"SubnetGatewayIpv4Address"`
} `json:"Networks"`
ClockDrift []struct {
ClockErrorBound float64 `json:"ClockErrorBound"`
ReferenceTimestamp string `json:"ReferenceTimestamp"`
ClockSynchronizationStatus string `json:"ClockSynchronizationStatus"`
} `json:"ClockDrift"`
ContainerARN string `json:"ContainerARN"`
LogDriver string `json:"LogDriver"`
} `json:"Containers"`
}