Skip to content

Commit

Permalink
Update exporter boilerplate (#77)
Browse files Browse the repository at this point in the history
Use Prometheus common / exporter-toolkit boilerplate.
* Fixup linter errors.

Signed-off-by: SuperQ <superq@gmail.com>
  • Loading branch information
SuperQ authored Oct 13, 2024
1 parent a08fd9d commit 0fff1f8
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 60 deletions.
24 changes: 24 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
linters:
enable:
- misspell
- revive
- sloglint

issues:
exclude-rules:
- path: _test.go
linters:
- errcheck

linters-settings:
errcheck:
exclude-functions:
# Used in HTTP handlers, any error is handled by the server itself.
- (net/http.ResponseWriter).Write
revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
- name: unused-parameter
severity: warning
disabled: true
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ ecs_memory_limit_bytes{container="ecs-metadata-proxy"} 9.223372036854772e+18
# HELP ecs_memory_max_bytes Maximum memory usage in bytes.
# TYPE ecs_memory_max_bytes gauge
ecs_memory_max_bytes{container="ecs-metadata-proxy"} 9.023488e+06
# HELP ecs_network_receive_bytes_total Network recieved in bytes.
# HELP ecs_network_receive_bytes_total Network received in bytes.
# TYPE ecs_network_receive_bytes_total counter
ecs_network_receive_bytes_total{container="ecs-metadata-proxy",device="eth1"} 4.2851757e+07
# HELP ecs_network_receive_dropped_total Network packets dropped in recieving.
# HELP ecs_network_receive_dropped_total Network packets dropped in receiving.
# TYPE ecs_network_receive_dropped_total counter
ecs_network_receive_dropped_total{container="ecs-metadata-proxy",device="eth1"} 0
# HELP ecs_network_receive_errors_total Network errors in recieving.
# HELP ecs_network_receive_errors_total Network errors in receiving.
# TYPE ecs_network_receive_errors_total counter
ecs_network_receive_errors_total{container="ecs-metadata-proxy",device="eth1"} 0
# HELP ecs_network_receive_packets_total Network packets recieved.
# HELP ecs_network_receive_packets_total Network packets received.
# TYPE ecs_network_receive_packets_total counter
ecs_network_receive_packets_total{container="ecs-metadata-proxy",device="eth1"} 516239
# HELP ecs_network_transmit_bytes_total Network transmitted in bytes.
Expand Down
25 changes: 13 additions & 12 deletions ecscollector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package ecscollector
import (
"context"
"fmt"
"log"
"log/slog"
"time"

"github.com/prometheus-community/ecs_exporter/ecsmetadata"
Expand All @@ -35,7 +35,7 @@ var (
"ECS service metadata.",
metadataLabels, nil)

svcCpuLimitDesc = prometheus.NewDesc(
svcCPULimitDesc = prometheus.NewDesc(
"ecs_svc_cpu_limit",
"Total CPU Limit.",
svcLabels, nil)
Expand Down Expand Up @@ -67,22 +67,22 @@ var (

networkRxBytesDesc = prometheus.NewDesc(
"ecs_network_receive_bytes_total",
"Network recieved in bytes.",
"Network received in bytes.",
networkLabels, nil)

networkRxPacketsDesc = prometheus.NewDesc(
"ecs_network_receive_packets_total",
"Network packets recieved.",
"Network packets received.",
networkLabels, nil)

networkRxDroppedDesc = prometheus.NewDesc(
"ecs_network_receive_dropped_total",
"Network packets dropped in recieving.",
"Network packets dropped in receiving.",
networkLabels, nil)

networkRxErrorsDesc = prometheus.NewDesc(
"ecs_network_receive_errors_total",
"Network errors in recieving.",
"Network errors in receiving.",
networkLabels, nil)

networkTxBytesDesc = prometheus.NewDesc(
Expand Down Expand Up @@ -139,12 +139,13 @@ var networkLabels = append(

// NewCollector returns a new Collector that queries ECS metadata server
// for ECS task and container metrics.
func NewCollector(client *ecsmetadata.Client) prometheus.Collector {
return &collector{client: client}
func NewCollector(client *ecsmetadata.Client, logger *slog.Logger) prometheus.Collector {
return &collector{client: client, logger: logger}
}

type collector struct {
client *ecsmetadata.Client
logger *slog.Logger
}

func (c *collector) Describe(ch chan<- *prometheus.Desc) {
Expand All @@ -166,7 +167,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
ctx := context.Background()
metadata, err := c.client.RetrieveTaskMetadata(ctx)
if err != nil {
log.Printf("Failed to retrieve metadata: %v", err)
c.logger.Debug("Failed to retrieve metadata", "error", err)
return
}

Expand All @@ -191,7 +192,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
if metadata.Limits != nil {
if metadata.Limits.CPU != nil {
ch <- prometheus.MustNewConstMetric(
svcCpuLimitDesc,
svcCPULimitDesc,
prometheus.GaugeValue,
*metadata.Limits.CPU,
metadata.TaskARN,
Expand All @@ -209,13 +210,13 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {

stats, err := c.client.RetrieveTaskStats(ctx)
if err != nil {
log.Printf("Failed to retrieve container stats: %v", err)
c.logger.Debug("Failed to retrieve container stats", "error", err)
return
}
for _, container := range metadata.Containers {
s := stats[container.ID]
if s == nil {
log.Printf("Couldn't find container with ID %q in stats", container.ID)
c.logger.Debug("Couldn't find container with ID in stats", "id", container.ID)
continue
}

Expand Down
31 changes: 23 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,45 @@ module github.com/prometheus-community/ecs_exporter
go 1.22

require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/aws/amazon-ecs-agent/ecs-agent v0.0.0-20240920192628-cf8c7a6b6504
github.com/prometheus/client_golang v1.15.1
github.com/prometheus/client_golang v1.20.4
github.com/prometheus/common v0.60.0
github.com/prometheus/exporter-toolkit v0.13.0
)

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/aws/aws-sdk-go v1.51.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/docker/docker v24.0.9+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/mdlayher/vsock v1.2.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 0fff1f8

Please sign in to comment.