Skip to content

Commit

Permalink
Add a host parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
chelnak committed Feb 25, 2024
1 parent 9d59171 commit e8d8667
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ http://status-cake-exporter.default.svc:8000

## Usage

| Setting | Required | Default |
|--------------------------------------|----------|---------|
| API_KEY | Yes | Null |
| TAGS | No | Null |
| LOG_LEVEL | No | info |
| PORT | No | 8000 |
| Setting | Required | Default |
|--------------------------------------|----------|--------- |
| HOST | No | https://api.statuscake.com/v1 |
| API_KEY | Yes | Null |
| TAGS | No | Null |
| LOG_LEVEL | No | info |
| PORT | No | 8000 |
| ITEMS_PER_PAGE | No | 25 |

### Docker

Expand All @@ -45,14 +47,16 @@ Otherwise, you can use the Helm Chart provided in [chart/status-cake-exporter](c

### Grafana

To get up and running quickly, use [examples/grafana-example.json](examples/grafana-example.json) as an example.
To get up and running quickly, use [examples/grafana-example.json](examples/grafana-example.json) as an example.

### Terminal

```sh
Usage: status-cake-exporter [OPTIONS]

Options:
--host TEXT The host of the statuscake api. [env var: HOST;
default: https://api.statuscake.com/v1]
--api-key TEXT API Key for the account. [env var: API_KEY;
required]
--tags TEXT A comma separated list of tags used to filter tests
Expand All @@ -73,6 +77,7 @@ Options:
|-----|------|-------------|
| status_cake_test_info | Gauge |A basic listing of the tests under the current account. |
| status_cake_test_uptime_percent | Gauge | Tests and their uptime percentage |
| status_cake_test_performance | Gauge | Tests and their performance percentage |

## Prometheus

Expand Down Expand Up @@ -107,6 +112,8 @@ status_cake_test_info * on(test_id) group_right(test_name) status_cake_test_upti

## Development

### Tilt

This repository uses [Tilt](https://tilt.dev) for rapid development on Kubernetes.

To use this, run:
Expand All @@ -119,3 +126,13 @@ tilt up
Tilt will reload your environment when it detects changes to your code.

Note: You will need to provide valid credentials for StatusCake in your `Tiltfile` for this to work.

### Docker compose

You can use the provided `docker-compose.yml` to run the exporter locally.

```sh
docker-compose watch
```

This will start the exporter along with a mocked statuscake api server.
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
exporter:
build: .
environment:
- HOST=http://prism:4010
- API_KEY=123
- LOG_LEVEL=debug
ports:
- "8000:8000"
develop:
watch:
- action: rebuild
path: .
prism:
image: stoplight/prism:4
command: 'mock -h 0.0.0.0 https://developers.statuscake.com/redocusaurus/plugin-redoc-0.yaml'
ports:
- '8080:4010'
7 changes: 5 additions & 2 deletions status_cake_exporter/_status_cake.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from time import sleep
from typing import Any

from statuscake import ApiClient
from statuscake import ApiClient, Configuration
from statuscake.apis import MaintenanceWindowsApi, UptimeApi
from statuscake.exceptions import ApiValueError, ForbiddenException
from typing_extensions import NotRequired, TypedDict
Expand Down Expand Up @@ -44,12 +44,14 @@ class StatusCake:
A wrapper class for the StatusCake API client.
"""

def __init__(self, api_key: str, per_page: int) -> None:
def __init__(self, host: str, api_key: str, per_page: int) -> None:
"""
Args:
host: [str] The host of the StatusCake API
api_key: [str] The StatusCake API key
per_page: [int] The number of results to return per page
"""
self.host: str = host
self.api_key: str = api_key
self.per_page: int = int(per_page)

Expand All @@ -61,6 +63,7 @@ def __get_api_client(self) -> ApiClient:
ApiClient
"""
return ApiClient(
Configuration(host=self.host),
header_name="Authorization",
header_value=f"Bearer {self.api_key}",
)
Expand Down
6 changes: 4 additions & 2 deletions status_cake_exporter/_test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ def transform(
class TestCollector(Collector):
"""The collector subclass responsible for gathering test metrics from the StatusCake API."""

def __init__(self, api_key: str, per_page: int, tags: str):
def __init__(self, host: str, api_key: str, per_page: int, tags: str):
"""
Args:
host: [str] The host of the StatusCake API
api_key: [str] The StatusCake API key
per_page: [int] The number of tests to return per page
tags: [str] The tags to filter the tests by
"""
self.host: str = host
self.api_key: str = api_key
self.per_page: int = per_page
self.tags: str = tags
Expand All @@ -121,7 +123,7 @@ def collect(self):
Yields:
[Generator] The generator that yields the metrics to the Prometheus client.
"""
statuscake = StatusCake(self.api_key, self.per_page)
statuscake = StatusCake(self.host, self.api_key, self.per_page)

logger.info("Collector started.")

Expand Down
7 changes: 6 additions & 1 deletion status_cake_exporter/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@


def exporter(
host=typer.Option(
"https://api.statuscake.com/v1",
help="The host of the StatusCake API.",
envvar="HOST",
),
api_key: str = typer.Option(..., help="API Key for the account.", envvar="API_KEY"),
tags: str = typer.Option(
"",
Expand Down Expand Up @@ -49,7 +54,7 @@ def exporter(
start_http_server(port)

logger.info("Registering collectors.")
test_collector = TestCollector(api_key, items_per_page, tags)
test_collector = TestCollector(host, api_key, items_per_page, tags)
REGISTRY.register(test_collector)

while True:
Expand Down

0 comments on commit e8d8667

Please sign in to comment.