-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add metrics endpoint for load balancers and servers (#331)
Adds the missing metrics endpoint for the load balancers and servers resources. - https://docs.hetzner.cloud/#load-balancers-get-metrics-for-a-loadbalancer - https://docs.hetzner.cloud/#servers-get-metrics-for-a-server
- Loading branch information
Showing
13 changed files
with
391 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from datetime import datetime, timedelta | ||
from os import environ | ||
|
||
from hcloud import Client | ||
from hcloud.images import Image | ||
from hcloud.server_types import ServerType | ||
|
||
assert ( | ||
"HCLOUD_TOKEN" in environ | ||
), "Please export your API token in the HCLOUD_TOKEN environment variable" | ||
token = environ["HCLOUD_TOKEN"] | ||
|
||
client = Client(token=token) | ||
|
||
server = client.servers.get_by_name("my-server") | ||
if server is None: | ||
response = client.servers.create( | ||
name="my-server", | ||
server_type=ServerType("cx11"), | ||
image=Image(name="ubuntu-22.04"), | ||
) | ||
server = response.server | ||
|
||
end = datetime.now() | ||
start = end - timedelta(hours=1) | ||
|
||
response = server.get_metrics( | ||
type=["cpu", "network"], | ||
start=start, | ||
end=end, | ||
) | ||
|
||
print(json.dumps(response.metrics)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from __future__ import annotations | ||
|
||
from .domain import Metrics, TimeSeries # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
from typing import Dict, List, Literal, Tuple | ||
|
||
from dateutil.parser import isoparse | ||
|
||
from ..core import BaseDomain | ||
|
||
TimeSeries = Dict[str, Dict[Literal["values"], List[Tuple[float, str]]]] | ||
|
||
|
||
class Metrics(BaseDomain): | ||
"""Metrics Domain | ||
:param start: Start of period of metrics reported. | ||
:param end: End of period of metrics reported. | ||
:param step: Resolution of results in seconds. | ||
:param time_series: Dict with time series data, using the name of the time series as | ||
key. The metrics timestamps and values are stored in a list of tuples | ||
``[(timestamp, value), ...]``. | ||
""" | ||
|
||
start: datetime | ||
end: datetime | ||
step: float | ||
time_series: TimeSeries | ||
|
||
__slots__ = ( | ||
"start", | ||
"end", | ||
"step", | ||
"time_series", | ||
) | ||
|
||
def __init__( | ||
self, | ||
start: str, | ||
end: str, | ||
step: float, | ||
time_series: TimeSeries, | ||
): | ||
self.start = isoparse(start) | ||
self.end = isoparse(end) | ||
self.step = step | ||
self.time_series = time_series |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.