-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
138 lines (120 loc) · 3.52 KB
/
main.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
/*
Basic Prometheus exporter for a Docker Swarm. Exposes a HTTP endpoint for
Prometheus to scrape which just has the basic info on what services are
running and how many tasks are in what state for each service.
*/
package main
import (
"context"
"net/http"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
coll := DockerServices{Client: dockerClient}
if err := prometheus.Register(&coll); err != nil {
panic(err)
}
// Get rid of the stupid golang metrics
prometheus.Unregister(collectors.NewGoCollector())
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":9675", nil); err != nil {
panic(err)
}
}
// DockerServices implements the Collector interface.
type DockerServices struct {
*client.Client
}
var _ prometheus.Collector = (*DockerServices)(nil)
var (
replicaCount = prometheus.NewDesc(
"swarm_service_desired_replicas",
"Number of replicas requested for this service",
[]string{"service_name"}, nil,
)
taskCount = prometheus.NewDesc(
"swarm_service_tasks",
"Number of docker tasks",
[]string{"service_name", "state"}, nil,
)
imageVersion = prometheus.NewDesc(
"swarm_service_info",
"Information about each service",
[]string{"service_name", "image"}, nil,
)
lastChangeTime = prometheus.NewDesc(
"swarm_service_change_time",
"Time when a task state last changed",
[]string{"service_name"}, nil,
)
)
func (c DockerServices) Describe(ch chan<- *prometheus.Desc) {
ch <- replicaCount
ch <- taskCount
ch <- imageVersion
ch <- lastChangeTime
}
// Collect scrapes the container information from Docker.
func (c DockerServices) Collect(ch chan<- prometheus.Metric) {
services, err := c.Client.ServiceList(context.Background(), types.ServiceListOptions{})
if err != nil {
panic(err)
}
tasks, err := c.Client.TaskList(context.Background(), types.TaskListOptions{})
if err != nil {
panic(err)
}
for _, service := range services {
if service.Spec.Mode.Replicated != nil {
ch <- prometheus.MustNewConstMetric(
replicaCount,
prometheus.GaugeValue,
float64(*service.Spec.Mode.Replicated.Replicas),
service.Spec.Annotations.Name,
)
}
taskStates := make(map[string]int)
taskStates["running"] = 0 // Should really do this for all potential states (https://github.com/moby/moby/blob/v1.13.1/api/types/swarm/task.go)
var lastTaskStatusChange time.Time
for _, task := range tasks {
if task.ServiceID == service.ID {
taskStates[string(task.Status.State)] += 1
if task.Status.Timestamp.After(lastTaskStatusChange) {
lastTaskStatusChange = task.Status.Timestamp
}
}
}
for state, count := range taskStates {
ch <- prometheus.MustNewConstMetric(
taskCount,
prometheus.GaugeValue,
float64(count),
service.Spec.Annotations.Name,
string(state),
)
}
// See https://www.robustperception.io/exposing-the-software-version-to-prometheus
ch <- prometheus.MustNewConstMetric(
imageVersion,
prometheus.GaugeValue,
1,
service.Spec.Annotations.Name,
string(service.Spec.TaskTemplate.ContainerSpec.Image),
)
ch <- prometheus.MustNewConstMetric(
lastChangeTime,
prometheus.GaugeValue,
float64(lastTaskStatusChange.Unix()),
service.Spec.Annotations.Name,
)
}
}