-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENG-264 add storage metrics api (#768)
- Loading branch information
1 parent
f579d84
commit a2e1f2c
Showing
21 changed files
with
881 additions
and
198 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
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,47 @@ | ||
from collections.abc import AsyncIterator, Iterator | ||
from contextlib import asynccontextmanager, contextmanager | ||
from typing import Any | ||
|
||
import aiobotocore | ||
import aiobotocore.client | ||
import aiobotocore.session | ||
import botocore | ||
import botocore.client | ||
import botocore.session | ||
|
||
from .config import AWSConfig | ||
|
||
|
||
@contextmanager | ||
def create_s3_client( | ||
session: botocore.session.Session, config: AWSConfig | ||
) -> Iterator[botocore.client.BaseClient]: | ||
client = session.create_client("s3", **_create_s3_client_kwargs(config)) | ||
yield client | ||
client.close() | ||
|
||
|
||
@asynccontextmanager | ||
async def create_async_s3_client( | ||
session: aiobotocore.session.AioSession, config: AWSConfig | ||
) -> AsyncIterator[aiobotocore.client.AioBaseClient]: | ||
async with session.create_client( | ||
"s3", **_create_s3_client_kwargs(config) | ||
) as client: | ||
yield client | ||
|
||
|
||
def _create_s3_client_kwargs(config: AWSConfig) -> dict[str, Any]: | ||
kwargs: dict[str, Any] = { | ||
"region_name": config.region, | ||
"config": botocore.config.Config( | ||
retries={"mode": "standard"}, # 3 retries by default | ||
), | ||
} | ||
if config.access_key_id: | ||
kwargs["aws_access_key_id"] = config.access_key_id | ||
if config.secret_access_key: | ||
kwargs["aws_secret_access_key"] = config.secret_access_key | ||
if config.s3_endpoint_url: | ||
kwargs["endpoint_url"] = config.s3_endpoint_url | ||
return kwargs |
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,63 @@ | ||
import logging | ||
from collections.abc import Iterator | ||
from contextlib import ExitStack, contextmanager | ||
|
||
import botocore | ||
import botocore.client | ||
import botocore.config | ||
import botocore.session | ||
from fastapi import FastAPI | ||
from neuro_logging import init_logging | ||
from prometheus_client import CollectorRegistry, make_asgi_app | ||
|
||
from .aws import create_s3_client | ||
from .config import EnvironConfigFactory, MetricsConfig | ||
from .s3_storage import StorageMetricsS3Storage | ||
from .storage_usage import StorageUsageCollector | ||
|
||
|
||
@contextmanager | ||
def create_app(config: MetricsConfig) -> Iterator[FastAPI]: | ||
with ExitStack() as exit_stack: | ||
session = botocore.session.get_session() | ||
s3_client = exit_stack.enter_context( | ||
create_s3_client(session=session, config=config.aws) | ||
) | ||
|
||
storage_metrics_s3_storage = StorageMetricsS3Storage( | ||
s3_client, config.aws.metrics_s3_bucket_name | ||
) | ||
|
||
collector = StorageUsageCollector( | ||
config=config.aws, storage_metrics_s3_storage=storage_metrics_s3_storage | ||
) | ||
registry = CollectorRegistry() | ||
registry.register(collector) | ||
|
||
metrics_app = make_asgi_app(registry=registry) | ||
app = FastAPI(debug=False) | ||
|
||
app.mount("/metrics", metrics_app) | ||
|
||
yield app | ||
|
||
|
||
def main() -> None: | ||
import uvicorn | ||
|
||
init_logging() | ||
|
||
config = EnvironConfigFactory().create_metrics() | ||
logging.info("Loaded config: %r", config) | ||
with create_app(config) as app: | ||
uvicorn.run( | ||
app, | ||
host=config.server.host, | ||
port=config.server.port, | ||
proxy_headers=True, | ||
log_config=None, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.