Skip to content

Commit

Permalink
api: allow clients to send their identification
Browse files Browse the repository at this point in the history
This allows to track the clients used to request updates. Ideally this help to
identify what versions are mostly used and drop support for outdated version.

A client can now send the "client" field in the format "name - version".

Signed-off-by: Paul Spooren <mail@aparcar.org>
  • Loading branch information
aparcar committed Jul 29, 2022
1 parent c7c6103 commit 87ad689
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions asu/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ def api_v1_build_post():
result_ttl = "7d"
failure_ttl = "12h"

if "client" in req:
get_redis().hincrby("stats:clients", req["client"])
else:
if request.headers.get("user-agent").startswith("auc"):
get_redis().hincrby(
"stats:clients",
request.headers.get("user-agent").replace(" (", "/").replace(")", ""),
)
else:
get_redis().hincrby("stats:clients", "unknown/0")

if job is None:
get_redis().incr("stats:cache-miss")
response, status = validate_request(req)
Expand Down
10 changes: 10 additions & 0 deletions asu/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def collect(self):

yield stats_builds

stats_clients = CounterMetricFamily(
"clients",
"Clients requesting images",
labels=["name", "version"],
)
for client, count in self.connection.hgetall("stats:clients").items():
stats_clients.add_metric(client.decode().split("/"), count)

yield stats_clients

hits = self.connection.get("stats:cache-hit")
if hits:
hits = int(hits.decode())
Expand Down
5 changes: 5 additions & 0 deletions asu/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ components:
type: object
additionalProperties: false
properties:
client:
type: string
example: luci - git-22.073.39928-701ea94
description: |
Client name and version that requests the image,
distro:
type: string
example: openwrt
Expand Down
75 changes: 75 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,78 @@ def test_stats_cache(client, upstream):
response = client.get("/metrics")
print(response.get_data(as_text=True))
assert "cache_hits 1.0" in response.get_data(as_text=True)


def test_stats_clients_luci(client, upstream):
response = client.post(
"/api/v1/build",
json=dict(
version="TESTVERSION",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
client="luci/git-22.073.39928-701ea94",
),
)

response = client.get("/metrics")
print(response.get_data(as_text=True))
assert (
'clients_total{name="luci",version="git-22.073.39928-701ea94"} 1.0'
in response.get_data(as_text=True)
)


def test_stats_clients_unknown(client, upstream):
response = client.post(
"/api/v1/build",
json=dict(
version="TESTVERSION",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
)

response = client.get("/metrics")
print(response.get_data(as_text=True))
assert 'clients_total{name="unknown",version="0"} 1.0' in response.get_data(
as_text=True
)


def test_stats_clients_auc(client, upstream):
response = client.post(
"/api/v1/build",
json=dict(
version="TESTVERSION",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
headers={"User-Agent": "auc (0.3.2)"},
)

response = client.get("/metrics")
print(response.get_data(as_text=True))
assert 'clients_total{name="auc",version="0.3.2"} 1.0' in response.get_data(
as_text=True
)

def test_stats_clients_auc_possible_new_format(client, upstream):
response = client.post(
"/api/v1/build",
json=dict(
version="TESTVERSION",
target="testtarget/testsubtarget",
profile="testprofile",
packages=["test1", "test2"],
),
headers={"User-Agent": "auc/0.3.2"},
)

response = client.get("/metrics")
print(response.get_data(as_text=True))
assert 'clients_total{name="auc",version="0.3.2"} 1.0' in response.get_data(
as_text=True
)

0 comments on commit 87ad689

Please sign in to comment.