forked from atr0phy/aws_batch_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
155 lines (136 loc) · 4.01 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package awsBatchExporter
import (
"context"
"log"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/aws/aws-sdk-go/service/batch/batchiface"
"github.com/prometheus/client_golang/prometheus"
)
type Collector struct {
client batchiface.BatchAPI
region string
timeout time.Duration
}
const (
namespace = "aws_batch"
timeout = 10 * time.Second
)
var (
jobStatus = []string{
batch.JobStatusSubmitted,
batch.JobStatusPending,
batch.JobStatusRunnable,
batch.JobStatusStarting,
batch.JobStatusRunning,
batch.JobStatusFailed,
batch.JobStatusSucceeded,
}
jobSubmitted = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "submitted_job"),
"Job in the queue that are in the SUBMITTED state",
[]string{"region", "id", "queue", "name"}, nil,
)
jobPending = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "pending_job"),
"Job in the queue that are in the PENDING state",
[]string{"region", "id", "queue", "name"}, nil,
)
jobRunnable = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "runnable_job"),
"Job in the queue that are in the RUNNABLE state",
[]string{"region", "id", "queue", "name"}, nil,
)
jobStarting = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "starting_job"),
"Job in the queue that are in the STARTING state",
[]string{"region", "id", "queue", "name"}, nil,
)
jobRunning = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "running_job"),
"Job in the queue that are in the RUNNING state",
[]string{"region", "id", "queue", "name"}, nil,
)
jobFailed = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "failed_job"),
"Job in the queue that are in the FAILED state",
[]string{"region", "id", "queue", "name"}, nil,
)
jobSucceeded = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "succeeded_job"),
"Job in the queue that are in the SUCCEEDED state",
[]string{"region", "id", "queue", "name"}, nil,
)
jobDescMap = map[string]*prometheus.Desc{
batch.JobStatusSubmitted: jobSubmitted,
batch.JobStatusPending: jobPending,
batch.JobStatusRunnable: jobRunnable,
batch.JobStatusStarting: jobStarting,
batch.JobStatusRunning: jobRunning,
batch.JobStatusFailed: jobFailed,
batch.JobStatusSucceeded: jobSucceeded,
}
)
type JobResult struct {
id string
queue string
name string
status string
}
func New(region string) (*Collector, error) {
s, err := session.NewSession(&aws.Config{Region: aws.String(region)})
if err != nil {
return nil, err
}
return &Collector{
client: batch.New(s),
region: region,
timeout: timeout,
}, nil
}
func (*Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- jobSubmitted
ch <- jobPending
ch <- jobRunnable
ch <- jobStarting
ch <- jobRunning
ch <- jobFailed
ch <- jobSucceeded
}
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r, err := c.client.DescribeJobQueuesWithContext(ctx, &batch.DescribeJobQueuesInput{})
if err != nil {
log.Printf("Error collecting metrics: %v\n", err)
return
}
var wg sync.WaitGroup
for _, d := range r.JobQueues {
wg.Add(1)
go func(d batch.JobQueueDetail) {
defer wg.Done()
var results []JobResult
for _, s := range jobStatus {
r, err := c.client.ListJobsWithContext(ctx, &batch.ListJobsInput{JobQueue: d.JobQueueName, JobStatus: &s})
if err != nil {
log.Printf("Error collecting job status metrics: %v\n", err)
continue
}
for _, j := range r.JobSummaryList {
results = append(results, JobResult{id: *j.JobId, queue: *d.JobQueueName, name: *j.JobName, status: *j.Status})
}
}
c.collectJobDetailStatus(ch, results)
}(*d)
}
wg.Wait()
}
func (c *Collector) collectJobDetailStatus(ch chan<- prometheus.Metric, results []JobResult) {
for _, r := range results {
ch <- prometheus.MustNewConstMetric(jobDescMap[r.status], prometheus.GaugeValue, 1, c.region, r.id, r.queue, r.name)
}
}