Skip to content

Commit 64a4b85

Browse files
committed
pr-feedback
1 parent d94c4d1 commit 64a4b85

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

.ci/metrics/metrics.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,17 @@ def buildkite_fetch_page_build_list(
8787
buildkite_token: str, after_cursor: str = None
8888
) -> list[dict[str, str]]:
8989
"""Fetches a page of the build list using the GraphQL BuildKite API.
90+
9091
Returns the BUILDKITE_GRAPHQL_BUILDS_PER_PAGE last running/queued builds,
9192
or the BUILDKITE_GRAPHQL_BUILDS_PER_PAGE running/queued builds
9293
older than the one pointer by |after_cursor| if provided.
9394
The |after_cursor| value is taken from the previous page returned by the
9495
API.
96+
9597
Args:
9698
buildkite_token: the secret token to authenticate GraphQL requests.
9799
after_cursor: cursor after which to start the page fetch.
100+
98101
Returns:
99102
The most recent builds after cursor (if set) with the following format:
100103
[
@@ -130,7 +133,7 @@ def buildkite_fetch_page_build_list(
130133
AFTER="null" if after_cursor is None else '"{}"'.format(after_cursor),
131134
)
132135
data = data.replace("\n", "").replace('"', '\\"')
133-
data = '{ "query": "' + data + '" }'
136+
data = json.dumps({"query": data}) #'{ "query": "' + data + '" }'
134137
url = "https://graphql.buildkite.com/v1"
135138
headers = {
136139
"Authorization": "Bearer " + buildkite_token,
@@ -151,12 +154,15 @@ def buildkite_fetch_page_build_list(
151154

152155
def buildkite_get_build_info(build_number: str) -> dict:
153156
"""Returns all the info associated with the provided build number.
157+
154158
Note: for unknown reasons, graphql returns no jobs for a given build,
155159
while this endpoint does, hence why this uses this API instead of graphql.
156-
Args:
157-
build_number: which build number to fetch info for.
158-
Returns:
159-
The info for the target build, a JSON dictionnary.
160+
161+
Args:
162+
build_number: which build number to fetch info for.
163+
164+
Returns:
165+
The info for the target build, a JSON dictionnary.
160166
"""
161167

162168
URL = "https://buildkite.com/llvm-project/github-pull-requests/builds/{}.json"
@@ -165,6 +171,7 @@ def buildkite_get_build_info(build_number: str) -> dict:
165171

166172
def buildkite_get_incomplete_tasks(buildkite_token: str) -> list:
167173
"""Returns all the running/pending BuildKite builds.
174+
168175
Args:
169176
buildkite_token: the secret token to authenticate GraphQL requests.
170177
last_cursor: the cursor to stop at if set. If None, a full page is fetched.
@@ -184,13 +191,14 @@ def buildkite_get_metrics(
184191
buildkite_token: str, previously_incomplete: set[int]
185192
) -> (list[JobMetrics], set[int]):
186193
"""Returns a tuple with:
194+
187195
- the metrics recorded for newly completed workflow jobs.
188196
- the set of workflow still running now.
189197
190198
Args:
191-
buildkite_token: the secret token to authenticate GraphQL requests.
192-
previously_incomplete: the set of running workflows the last time this
193-
function was called.
199+
buildkite_token: the secret token to authenticate GraphQL requests.
200+
previously_incomplete: the set of running workflows the last time this
201+
function was called.
194202
"""
195203

196204
running_builds = buildkite_get_incomplete_tasks(buildkite_token)
@@ -204,14 +212,31 @@ def buildkite_get_metrics(
204212
info = buildkite_get_build_info(build_id)
205213
metric_timestamp = dateutil.parser.isoparse(info["finished_at"])
206214
for job in info["jobs"]:
207-
# Skip this job.
215+
# This workflow is not interesting to us.
208216
if job["name"] not in BUILDKITE_WORKFLOW_TO_TRACK:
209217
continue
210218

219+
# Note: BuildKite API can return empty dates for some fields
220+
# depending on the completion scenario. Example, a job cancelled
221+
# before even starting will get an None date for 'started_at'.
222+
# For this reason, if a timestamp is missing, we consider it
223+
# skipped and keep the last event value.
211224
created_at = dateutil.parser.isoparse(job["created_at"])
212-
scheduled_at = dateutil.parser.isoparse(job["scheduled_at"])
213-
started_at = dateutil.parser.isoparse(job["started_at"])
214-
finished_at = dateutil.parser.isoparse(job["finished_at"])
225+
scheduled_at = (
226+
dateutil.parser.isoparse(job["scheduled_at"])
227+
if "scheduled_at" in job
228+
else created_at
229+
)
230+
started_at = (
231+
dateutil.parser.isoparse(job["started_at"])
232+
if "started_at" in job
233+
else scheduled_at
234+
)
235+
finished_at = (
236+
dateutil.parser.isoparse(job["finished_at"])
237+
if "finished_at" in job
238+
else started_at
239+
)
215240

216241
job_name = BUILDKITE_WORKFLOW_TO_TRACK[job["name"]]
217242
queue_time = (started_at - scheduled_at).seconds
@@ -224,7 +249,7 @@ def buildkite_get_metrics(
224249
datetime.datetime.now(datetime.timezone.utc) - metric_timestamp
225250
).total_seconds() / 60
226251
if metric_age_mn > GRAFANA_METRIC_MAX_AGE_MN:
227-
logging.info(
252+
logging.warning(
228253
f"Job {job['name']} from workflow {build_id} dropped due"
229254
+ f" to staleness: {metric_age_mn}mn old."
230255
)
@@ -372,7 +397,7 @@ def github_get_metrics(
372397
datetime.datetime.now(datetime.timezone.utc) - completed_at
373398
).total_seconds() / 60
374399
if metric_age_mn > GRAFANA_METRIC_MAX_AGE_MN:
375-
logging.info(
400+
logging.warning(
376401
f"Job {job.id} from workflow {task.id} dropped due"
377402
+ f" to staleness: {metric_age_mn}mn old."
378403
)

0 commit comments

Comments
 (0)