Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COS integration improvements #170

Merged
merged 14 commits into from
Jan 4, 2024
2 changes: 1 addition & 1 deletion src-docs/github_client.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ get_job_info(
path: GithubRepo,
workflow_run_id: str,
runner_name: str
) → GithubJobStats
) → JobStats
```

Get information about a job for a specific workflow run.
Expand Down
14 changes: 7 additions & 7 deletions src-docs/github_metrics.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ Functions to calculate metrics from data retrieved from GitHub.

---

<a href="../src/github_metrics.py#L11"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/github_metrics.py#L13"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `job_queue_duration`
## <kbd>function</kbd> `job`

```python
job_queue_duration(
job(
github_client: GithubClient,
pre_job_metrics: PreJobMetrics,
runner_name: str
) → float
) → GithubJobMetrics
```

Calculate the job queue duration.
Calculate the job metrics for a runner.

The Github API is accessed to retrieve the job data for the runner, which includes the time the job was created and the time the job was started.
The Github API is accessed to retrieve the job data for the runner.



Expand All @@ -35,6 +35,6 @@ The Github API is accessed to retrieve the job data for the runner, which includ


**Returns:**
The time in seconds the job took before the runner picked it up.
The job metrics.


11 changes: 10 additions & 1 deletion src-docs/github_type.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ Status of runner on GitHub.

---

## <kbd>class</kbd> `GithubJobStats`
## <kbd>class</kbd> `JobConclusion`
Conclusion of a job on GitHub.





---

## <kbd>class</kbd> `JobStats`
Stats for a job on GitHub.


Expand Down
36 changes: 19 additions & 17 deletions src-docs/metrics.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Models and functions for the metric events.

---

<a href="../src/metrics.py#L153"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/metrics.py#L156"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `issue_event`

Expand All @@ -39,7 +39,7 @@ The metric event is logged to the metrics log.

---

<a href="../src/metrics.py#L204"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/metrics.py#L207"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `setup_logrotate`

Expand All @@ -56,6 +56,23 @@ Configure logrotate for the metrics log.
- <b>`LogrotateSetupError`</b>: If the logrotate.timer cannot be enabled.


---

## <kbd>class</kbd> `CodeInformation`
Information about a status code.

This could e.g. be an exit code or a http status code.



**Attributes:**

- <b>`code`</b>: The status code.





---

## <kbd>class</kbd> `Event`
Expand Down Expand Up @@ -89,21 +106,6 @@ Initialize the event.



---

## <kbd>class</kbd> `ExitCodeInformation`
Information about the exit code of a runner.



**Attributes:**

- <b>`code`</b>: The exit code of the runner.





---

## <kbd>class</kbd> `Reconciliation`
Expand Down
25 changes: 25 additions & 0 deletions src-docs/metrics_type.py.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- markdownlint-disable -->

<a href="../src/metrics_type.py#L0"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

# <kbd>module</kbd> `metrics_type.py`
Data types used by modules handling metrics.



---

## <kbd>class</kbd> `GithubJobMetrics`
Metrics about a job.



**Attributes:**

- <b>`queue_duration`</b>: The time in seconds the job took before the runner picked it up.
- <b>`conclusion`</b>: The conclusion of the job.





10 changes: 3 additions & 7 deletions src-docs/runner.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ The `Runner` class stores the information on the runners and manages the lifecyc

The `RunnerManager` class from `runner_manager.py` creates and manages a collection of `Runner` instances.

**Global Variables**
---------------
- **YQ_BIN_URL_AMD64**
- **YQ_BIN_URL_ARM64**


---
Expand All @@ -39,7 +35,7 @@ The configuration values for creating a single runner instance.
## <kbd>class</kbd> `Runner`
Single instance of GitHub self-hosted runner.

<a href="../src/runner.py#L102"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L98"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `__init__`

Expand Down Expand Up @@ -67,7 +63,7 @@ Construct the runner instance.

---

<a href="../src/runner.py#L132"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L128"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `create`

Expand All @@ -91,7 +87,7 @@ Create the runner instance on LXD and register it on GitHub.

---

<a href="../src/runner.py#L168"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L164"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `remove`

Expand Down
14 changes: 7 additions & 7 deletions src-docs/runner_metrics.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Classes and function to extract the metrics from a shared filesystem.

---

<a href="../src/runner_metrics.py#L228"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner_metrics.py#L229"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `extract`

Expand Down Expand Up @@ -44,15 +44,15 @@ In order to avoid DoS attacks, the file size is also checked.

---

<a href="../src/runner_metrics.py#L255"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner_metrics.py#L256"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

## <kbd>function</kbd> `issue_events`

```python
issue_events(
runner_metrics: RunnerMetrics,
flavor: str,
queue_duration: Optional[float]
job_metrics: Optional[GithubJobMetrics]
) → set[Type[Event]]
```

Expand All @@ -64,7 +64,7 @@ Issue the metrics events for a runner.

- <b>`runner_metrics`</b>: The metrics for the runner.
- <b>`flavor`</b>: The flavor of the runner.
- <b>`queue_duration`</b>: The job queue duration of the job the runner executed.
- <b>`job_metrics`</b>: The metrics about the job run by the runner.



Expand All @@ -74,14 +74,14 @@ Issue the metrics events for a runner.

---

## <kbd>class</kbd> `ExitCodeInformation`
Information about the exit code of a runner.
## <kbd>class</kbd> `CodeInformation`
Information about a status code.



**Attributes:**

- <b>`code`</b>: The exit code of the runner.
- <b>`code`</b>: The status code.



Expand Down
14 changes: 9 additions & 5 deletions src/github_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import errors
from github_type import (
GithubJobStats,
JobStats,
RegistrationToken,
RemoveToken,
RunnerApplicationList,
Expand Down Expand Up @@ -167,9 +167,7 @@ def delete_runner(self, path: GithubPath, runner_id: int) -> None:
runner_id=runner_id,
)

def get_job_info(
self, path: GithubRepo, workflow_run_id: str, runner_name: str
) -> GithubJobStats:
def get_job_info(self, path: GithubRepo, workflow_run_id: str, runner_name: str) -> JobStats:
"""Get information about a job for a specific workflow run.

Args:
Expand Down Expand Up @@ -201,7 +199,13 @@ def get_job_info(
started_at = datetime.fromisoformat(
job["started_at"].replace("Z", "+00:00")
)
return GithubJobStats(created_at=created_at, started_at=started_at)
# conclusion could be null per api schema, so we need to handle that
# though we would assume that it should always be present,
# as the job should be finished
conclusion = job.get("conclusion", None)
return JobStats(
created_at=created_at, started_at=started_at, conclusion=conclusion
)

except HTTPError as exc:
raise errors.JobNotFoundError(
Expand Down
19 changes: 10 additions & 9 deletions src/github_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@
# See LICENSE file for licensing details.

"""Functions to calculate metrics from data retrieved from GitHub."""

import errors
from github_client import GithubClient
from metrics_type import GithubJobMetrics
from runner_metrics import PreJobMetrics
from runner_type import GithubRepo


def job_queue_duration(
def job(
github_client: GithubClient, pre_job_metrics: PreJobMetrics, runner_name: str
) -> float:
"""Calculate the job queue duration.
) -> GithubJobMetrics:
"""Calculate the job metrics for a runner.

The Github API is accessed to retrieve the job data for the runner, which includes
the time the job was created and the time the job was started.
The Github API is accessed to retrieve the job data for the runner.

Args:
github_client: The GitHub API client.
pre_job_metrics: The pre-job metrics.
runner_name: The name of the runner.

Returns:
The time in seconds the job took before the runner picked it up.
The job metrics.
"""
owner, repo = pre_job_metrics.repository.split("/", maxsplit=1)

try:
job = github_client.get_job_info(
job_info = github_client.get_job_info(
path=GithubRepo(owner=owner, repo=repo),
workflow_run_id=pre_job_metrics.workflow_run_id,
runner_name=runner_name,
)
except errors.JobNotFoundError as exc:
raise errors.GithubMetricsError from exc
duration = (job.started_at - job.created_at).total_seconds()
queue_duration = (job_info.started_at - job_info.created_at).total_seconds()

return duration
return GithubJobMetrics(queue_duration=queue_duration, conclusion=job_info.conclusion)
arturo-seijas marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 15 additions & 2 deletions src/github_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from datetime import datetime
from enum import Enum
from typing import List, TypedDict
from typing import List, Optional, TypedDict

from pydantic import BaseModel
from typing_extensions import NotRequired
Expand Down Expand Up @@ -115,7 +115,19 @@ class RemoveToken(TypedDict):
expires_at: str


class GithubJobStats(BaseModel):
class JobConclusion(str, Enum):
"""Conclusion of a job on GitHub."""

SUCCESS = "success"
FAILURE = "failure"
NEUTRAL = "neutral"
CANCELLED = "cancelled"
SKIPPED = "skipped"
TIMED_OUT = "timed_out"
ACTION_REQUIRED = "action_required"


class JobStats(BaseModel):
"""Stats for a job on GitHub.

Attributes:
Expand All @@ -125,3 +137,4 @@ class GithubJobStats(BaseModel):

created_at: datetime
started_at: datetime
conclusion: Optional[JobConclusion]
Loading
Loading