39
39
DEFAULT_HOST_LABELS = ["name" , "playbook" , "updated" ]
40
40
41
41
42
- # TODO: This method should be more flexible and live in a library
42
+ # TODO: This could be made more flexible and live in a library
43
43
def get_search_results (client , kind , limit , created_after ):
44
44
"""
45
45
kind: string, one of ["playbooks", "hosts", "tasks"]
@@ -72,31 +72,22 @@ def __init__(self, client, log, limit, labels=DEFAULT_PLAYBOOK_LABELS):
72
72
self .labels = labels
73
73
74
74
self .metrics = {
75
- "completed" : Gauge ("ara_playbooks_completed" , "Completed Ansible playbooks" , labels ),
76
- "expired" : Gauge ("ara_playbooks_expired" , "Expired Ansible playbooks" , labels ),
77
- "failed" : Gauge ("ara_playbooks_failed" , "Failed Ansible playbooks" , labels ),
78
75
"range" : Gauge ("ara_playbooks_range" , "Limit metric collection to the N most recent playbooks" ),
79
- "running" : Gauge ("ara_playbooks_running" , "Running Ansible playbooks" , labels ),
80
76
"total" : Gauge ("ara_playbooks_total" , "Total number of playbooks recorded by ara" ),
81
- "duration" : Summary ("ara_playbooks_duration" , "Duration (in seconds) of playbooks recorded by ara" , labels ),
77
+ "playbooks" : Summary (
78
+ "ara_playbooks" , "Labels and duration (in seconds) of playbooks recorded by ara" , labels
79
+ ),
82
80
}
81
+ self .metrics ["range" ].set (self .limit )
83
82
84
- def collect_metrics (self , created_after = None , limit = 1000 ):
85
- self .metrics ["range" ].set (limit )
86
-
83
+ def collect_metrics (self , created_after = None ):
87
84
playbooks = get_search_results (self .client , "playbooks" , self .limit , created_after )
88
85
# Save the most recent timestamp so we only scrape beyond it next time
89
86
if playbooks :
90
87
created_after = cli_utils .increment_timestamp (playbooks [0 ]["created" ])
91
88
self .log .info (f"updating metrics for { len (playbooks )} playbooks..." )
92
89
93
90
for playbook in playbooks :
94
- self .metrics ["total" ].inc ()
95
-
96
- # Gather the values of each label so we can attach them to our metrics
97
- labels = {label : playbook [label ] for label in self .labels }
98
- self .metrics [playbook ["status" ]].labels (** labels ).inc ()
99
-
100
91
# The API returns a duration in string format, convert it back to seconds
101
92
# so we can use it as a value for the metric.
102
93
if playbook ["duration" ] is not None :
@@ -108,7 +99,12 @@ def collect_metrics(self, created_after=None, limit=1000):
108
99
seconds = 0
109
100
else :
110
101
seconds = 0
111
- self .metrics ["duration" ].labels (** labels ).observe (seconds )
102
+
103
+ # Gather the values of each label so we can attach them to our metrics
104
+ labels = {label : playbook [label ] for label in self .labels }
105
+
106
+ self .metrics ["playbooks" ].labels (** labels ).observe (seconds )
107
+ self .metrics ["total" ].inc ()
112
108
113
109
return created_after
114
110
@@ -121,33 +117,20 @@ def __init__(self, client, log, limit, labels=DEFAULT_TASK_LABELS):
121
117
self .labels = labels
122
118
123
119
self .metrics = {
124
- "completed" : Gauge ("ara_tasks_completed" , "Completed Ansible tasks" , labels ),
125
- "expired" : Gauge ("ara_tasks_expired" , "Expired Ansible tasks" , labels ),
126
- "failed" : Gauge ("ara_tasks_failed" , "Failed Ansible tasks" , labels ),
127
120
"range" : Gauge ("ara_tasks_range" , "Limit metric collection to the N most recent tasks" ),
128
- "running" : Gauge ("ara_tasks_running" , "Running Ansible tasks" , labels ),
129
121
"total" : Gauge ("ara_tasks_total" , "Number of tasks recorded by ara in prometheus" ),
130
- "duration" : Summary (
131
- "ara_tasks_duration" , "Duration, in seconds, of playbook tasks recorded by ara" , labels
132
- ),
122
+ "tasks" : Summary ("ara_tasks" , "Labels and duration, in seconds, of playbook tasks recorded by ara" , labels ),
133
123
}
134
-
135
- def collect_metrics (self , created_after = None ):
136
124
self .metrics ["range" ].set (self .limit )
137
125
126
+ def collect_metrics (self , created_after = None ):
138
127
tasks = get_search_results (self .client , "tasks" , self .limit , created_after )
139
128
# Save the most recent timestamp so we only scrape beyond it next time
140
129
if tasks :
141
130
created_after = cli_utils .increment_timestamp (tasks [0 ]["created" ])
142
131
self .log .info (f"updating metrics for { len (tasks )} tasks..." )
143
132
144
133
for task in tasks :
145
- self .metrics ["total" ].inc ()
146
-
147
- # Gather the values of each label so we can attach them to our metrics
148
- labels = {label : task [label ] for label in self .labels }
149
- self .metrics [task ["status" ]].labels (** labels ).inc ()
150
-
151
134
# The API returns a duration in string format, convert it back to seconds
152
135
# so we can use it as a value for the metric.
153
136
if task ["duration" ] is not None :
@@ -159,7 +142,12 @@ def collect_metrics(self, created_after=None):
159
142
seconds = 0
160
143
else :
161
144
seconds = 0
162
- self .metrics ["duration" ].labels (** labels ).observe (seconds )
145
+
146
+ # Gather the values of each label so we can attach them to our metrics
147
+ labels = {label : task [label ] for label in self .labels }
148
+
149
+ self .metrics ["tasks" ].labels (** labels ).observe (seconds )
150
+ self .metrics ["total" ].inc ()
163
151
164
152
return created_after
165
153
@@ -180,10 +168,9 @@ def __init__(self, client, log, limit, labels=DEFAULT_HOST_LABELS):
180
168
"total" : Gauge ("ara_hosts_total" , "Hosts recorded by ara" ),
181
169
"unreachable" : Gauge ("ara_hosts_unreachable" , "Number of unreachable errors on a host" , labels ),
182
170
}
183
-
184
- def collect_metrics (self , created_after = None ):
185
171
self .metrics ["range" ].set (self .limit )
186
172
173
+ def collect_metrics (self , created_after = None ):
187
174
hosts = get_search_results (self .client , "hosts" , self .limit , created_after )
188
175
# Save the most recent timestamp so we only scrape beyond it next time
189
176
if hosts :
0 commit comments