Skip to content

Commit

Permalink
feat: add more metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Sharov <kvendingoldo@yandex.ru>
  • Loading branch information
kvendingoldo committed Jan 25, 2023
1 parent 4f29f31 commit bb0faee
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 13 deletions.
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
44 changes: 44 additions & 0 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",
"ECS service metadata.",
nil, nil)

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

svcMemLimitDesc = prometheus.NewDesc(
"ecs_svc_memory_limit",
"Total CPU Limit.",
nil, nil)

cpuTotalDesc = prometheus.NewDesc(
"ecs_cpu_seconds_total",
"Total CPU usage in seconds.",
Expand Down Expand Up @@ -142,6 +157,35 @@ 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),
)

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

stats, err := c.client.RetrieveTaskStats(ctx)
if err != nil {
log.Printf("Failed to retrieve container stats: %v", err)
Expand Down
69 changes: 56 additions & 13 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,16 @@ type ContainerStats struct {
Name string `json:"name"`
ID string `json:"id"`
NumProcs float64 `json:"num_procs"`
Read string `json:"read"`
PreRead string `json:"preread"`

PidsStats []struct{} `json:"pids_stats"`
StorageStats []struct{} `json:"storage_stats"`

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 +121,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 +147,38 @@ type TaskMetadata struct {
Labels map[string]string `json:"Labels"`
DesiredStatus string `json:"DesiredStatus"`
KnownStatus string `json:"KnownStatus"`
Type string `json:"Type"`
ContainerARN string `json:"ContainerARN"`
Limits []struct {
CPU float64 `json:"CPU"`
Memory float64 `json:"Memory"`
} `json:"Limits"`
CreatedAt string `json:"CreatedAt"`
StartedAt string `json:"StartedAt"`
Type string `json:"Type"`
Networks []struct {
NetworkMore 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"`
LogOptions []struct {
AwslogsCreateGroup string `json:"awslogs-create-group"`
AwslogsGroup string `json:"awslogs-group"`
AwslogsRegion string `json:"awslogs-region"`
AwslogsStream string `json:"awslogs-stream"`
} `json:"LogOptions"`
LogDriver string `json:"LogDriver"`
} `json:"Containers"`
}

0 comments on commit bb0faee

Please sign in to comment.