feat(usage): rename metrics → tokens_embedded/pages_embedded + export token cost to Prometheus
Billing product model finalized (Deck #281): bill pages externally, record tokens internally. Rename the data-plane metric literals to match the now- canonical contract (Deck #284) — the control plane's METRIC_EVENT_NAMES is already renamed, so the old names would be unmapped and never sync to Stripe. Rename (values unchanged): - embeddings_queries → tokens_embedded (value = real token count, already emitted by this PR; the unit upstream providers bill on). - pages_chunks → pages_embedded (value kept as len(chunk_texts) interim; TODO(#282): real normalized "pages indexed" count — real pages for paginated types, chars/tokens-per-page constant otherwise — is deferred to the instrumentation card, this only lands the name/contract). - All literals, log strings, docstrings, comments, the migration comment, and tests renamed; grep confirms zero old strings remain. Observability (new): export embedding token cost to Prometheus as astrolabe_embedding_tokens_total{provider,operation} (operation = index|query) so the billed cost unit is visible in Grafana, not just the per-tenant billing DB. Dedicated counter (doesn't inflate the existing chunk/request metrics) and always-on (independent of USAGE_METERING_ENABLED, so OSS/self-host gets it). Wired on both the indexing batch embed and the search query embed (query inside the per-request cache-miss branch, so reused embeddings aren't double-counted). Note: the rename orphans any pre-existing embeddings_queries/pages_chunks rows in tenant app DBs (CP no longer maps them) — acceptable; pipeline is inert with throwaway dev/sandbox data. Deck #284 (folded into PR #875). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
ddefb03701
commit
973f80e7b9
@@ -1,6 +1,6 @@
|
||||
"""Token-usage surfacing: the Provider ABC estimate default + SimpleProvider.
|
||||
|
||||
The usage-metering hooks (Deck #67) bill ``embeddings_queries`` by tokens. Real
|
||||
The usage-metering hooks (Deck #67) bill ``tokens_embedded`` by tokens. Real
|
||||
providers report exact counts from their API response; providers without a token
|
||||
field (Simple, and the ABC default) fall back to a char-based estimate so the
|
||||
billable value stays non-zero and monotone with input size.
|
||||
|
||||
@@ -85,6 +85,7 @@ def patched_search(monkeypatch):
|
||||
|
||||
settings = MagicMock()
|
||||
settings.get_collection_name.return_value = "test_collection"
|
||||
settings.get_embedding_provider_family.return_value = "mistral"
|
||||
monkeypatch.setattr(
|
||||
"nextcloud_mcp_server.search.bm25_hybrid.get_settings", lambda: settings
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Unit tests for the search-path usage-metering helper (Deck #67).
|
||||
|
||||
``record_search_usage`` records the billable ``embeddings_queries`` event for a
|
||||
``record_search_usage`` records the billable ``tokens_embedded`` event for a
|
||||
semantic search. These pin the value mapping (query token count), the flag-off
|
||||
no-op, the doc_types metadata bounding, and the best-effort failure path —
|
||||
covering the server-tool metering wiring without standing up the full
|
||||
@@ -38,7 +38,7 @@ async def test_records_query_token_count(store_spy):
|
||||
|
||||
store_spy.record_usage_event.assert_awaited_once()
|
||||
kwargs = store_spy.record_usage_event.await_args.kwargs
|
||||
assert kwargs["metric"] == "embeddings_queries"
|
||||
assert kwargs["metric"] == "tokens_embedded"
|
||||
assert kwargs["value"] == 42
|
||||
assert kwargs["enabled"] is True
|
||||
assert kwargs["metadata"]["user_id"] == "alice"
|
||||
|
||||
@@ -12,7 +12,10 @@ from __future__ import annotations
|
||||
import pytest
|
||||
|
||||
from nextcloud_mcp_server.config import Settings
|
||||
from nextcloud_mcp_server.observability.metrics import record_embedding
|
||||
from nextcloud_mcp_server.observability.metrics import (
|
||||
record_embedding,
|
||||
record_embedding_tokens,
|
||||
)
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
@@ -120,3 +123,31 @@ class TestRecordEmbedding:
|
||||
assert metric_sample(
|
||||
"astrolabe_embedding_requests_total", {**labels, "status": "error"}
|
||||
) == pytest.approx(1.0)
|
||||
|
||||
|
||||
class TestRecordEmbeddingTokens:
|
||||
"""astrolabe_embedding_tokens_total — token cost split by index/query."""
|
||||
|
||||
def test_index_increments_by_token_count(self, metric_sample):
|
||||
labels = {"provider": "tok-prov", "operation": "index"}
|
||||
before = metric_sample("astrolabe_embedding_tokens_total", labels)
|
||||
record_embedding_tokens("tok-prov", "index", 4242)
|
||||
assert metric_sample(
|
||||
"astrolabe_embedding_tokens_total", labels
|
||||
) == pytest.approx(before + 4242)
|
||||
|
||||
def test_query_operation_is_separate_series(self, metric_sample):
|
||||
labels = {"provider": "tok-prov", "operation": "query"}
|
||||
before = metric_sample("astrolabe_embedding_tokens_total", labels)
|
||||
record_embedding_tokens("tok-prov", "query", 7)
|
||||
assert metric_sample(
|
||||
"astrolabe_embedding_tokens_total", labels
|
||||
) == pytest.approx(before + 7)
|
||||
|
||||
def test_zero_or_negative_is_noop(self, metric_sample):
|
||||
labels = {"provider": "tok-noop", "operation": "index"}
|
||||
record_embedding_tokens("tok-noop", "index", 0)
|
||||
record_embedding_tokens("tok-noop", "index", -3)
|
||||
assert metric_sample(
|
||||
"astrolabe_embedding_tokens_total", labels
|
||||
) == pytest.approx(0.0)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Unit tests for the indexing-path usage-metering helper (Deck #67).
|
||||
|
||||
``record_indexing_usage`` records the two billable events (``pages_chunks`` +
|
||||
``embeddings_queries``) after a document's chunks are embedded. These cover the
|
||||
``record_indexing_usage`` records the two billable events (``pages_embedded`` +
|
||||
``tokens_embedded``) after a document's chunks are embedded. These cover the
|
||||
value mapping, the flag/zero-chunk no-ops, and the best-effort failure path
|
||||
without standing up the full document pipeline.
|
||||
"""
|
||||
@@ -25,8 +25,8 @@ def store_spy(monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_records_pages_chunks_and_token_count(store_spy):
|
||||
"""Both events fire: pages_chunks = chunk count, embeddings_queries = tokens."""
|
||||
async def test_records_pages_embedded_and_token_count(store_spy):
|
||||
"""Both events fire: pages_embedded = chunk count, tokens_embedded = tokens."""
|
||||
await processor.record_indexing_usage(
|
||||
enabled=True,
|
||||
provider="mistral",
|
||||
@@ -40,7 +40,7 @@ async def test_records_pages_chunks_and_token_count(store_spy):
|
||||
|
||||
calls = store_spy.record_usage_event.await_args_list
|
||||
by_metric = {c.kwargs["metric"]: c.kwargs["value"] for c in calls}
|
||||
assert by_metric == {"pages_chunks": 110, "embeddings_queries": 4242}
|
||||
assert by_metric == {"pages_embedded": 110, "tokens_embedded": 4242}
|
||||
for c in calls:
|
||||
# Hot-path fast-gate + tenant-local attribution metadata.
|
||||
assert c.kwargs["enabled"] is True
|
||||
|
||||
@@ -95,7 +95,7 @@ async def test_flag_off_is_noop(storage, monkeypatch):
|
||||
"""With metering disabled, nothing is written (zero DB work)."""
|
||||
_set_metering(monkeypatch, False)
|
||||
store = UsageEventStore(storage)
|
||||
await store.record_usage_event(metric="pages_chunks", value=5)
|
||||
await store.record_usage_event(metric="pages_embedded", value=5)
|
||||
assert await _count(storage) == 0
|
||||
|
||||
|
||||
@@ -115,12 +115,12 @@ async def test_enabled_param_short_circuits_without_reading_settings(
|
||||
monkeypatch.setattr(store_module, "get_settings", _boom)
|
||||
store = UsageEventStore(storage)
|
||||
|
||||
await store.record_usage_event(metric="pages_chunks", value=1, enabled=False)
|
||||
await store.record_usage_event(metric="pages_embedded", value=1, enabled=False)
|
||||
assert await _count(storage) == 0
|
||||
|
||||
eid = str(uuid.uuid4())
|
||||
await store.record_usage_event(
|
||||
metric="pages_chunks", value=1, event_id=eid, enabled=True
|
||||
metric="pages_embedded", value=1, event_id=eid, enabled=True
|
||||
)
|
||||
assert await _count(storage) == 1
|
||||
|
||||
@@ -131,7 +131,7 @@ async def test_insert_roundtrip(storage, monkeypatch):
|
||||
store = UsageEventStore(storage)
|
||||
eid = str(uuid.uuid4())
|
||||
await store.record_usage_event(
|
||||
metric="pages_chunks",
|
||||
metric="pages_embedded",
|
||||
value=7,
|
||||
event_id=eid,
|
||||
metadata={"provider": "gateway"},
|
||||
@@ -140,7 +140,7 @@ async def test_insert_roundtrip(storage, monkeypatch):
|
||||
assert row is not None
|
||||
# Postgres returns event_id as a uuid.UUID; normalize to str for compare.
|
||||
assert str(row[0]) == eid
|
||||
assert row[2] == "pages_chunks"
|
||||
assert row[2] == "pages_embedded"
|
||||
assert row[3] == 7
|
||||
|
||||
|
||||
@@ -149,11 +149,11 @@ async def test_on_conflict_dedup(storage, monkeypatch):
|
||||
_set_metering(monkeypatch, True)
|
||||
store = UsageEventStore(storage)
|
||||
eid = str(uuid.uuid4())
|
||||
await store.record_usage_event(metric="pages_chunks", value=1, event_id=eid)
|
||||
await store.record_usage_event(metric="embeddings_queries", value=99, event_id=eid)
|
||||
await store.record_usage_event(metric="pages_embedded", value=1, event_id=eid)
|
||||
await store.record_usage_event(metric="tokens_embedded", value=99, event_id=eid)
|
||||
assert await _count(storage) == 1
|
||||
row = await _fetch(storage, eid)
|
||||
assert row[2] == "pages_chunks" # DO NOTHING, not DO UPDATE
|
||||
assert row[2] == "pages_embedded" # DO NOTHING, not DO UPDATE
|
||||
assert row[3] == 1
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ async def test_metadata_json_roundtrip(storage, monkeypatch):
|
||||
eid = str(uuid.uuid4())
|
||||
meta = {"provider": "gateway", "model": "titan", "nested": {"chunks": 3}}
|
||||
await store.record_usage_event(
|
||||
metric="pages_chunks", value=3, event_id=eid, metadata=meta
|
||||
metric="pages_embedded", value=3, event_id=eid, metadata=meta
|
||||
)
|
||||
row = await _fetch(storage, eid)
|
||||
raw = row[4]
|
||||
@@ -187,7 +187,7 @@ async def test_occurred_at_roundtrip(storage, monkeypatch):
|
||||
eid = str(uuid.uuid4())
|
||||
when = datetime(2026, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
|
||||
await store.record_usage_event(
|
||||
metric="pages_chunks", value=1, event_id=eid, occurred_at=when
|
||||
metric="pages_embedded", value=1, event_id=eid, occurred_at=when
|
||||
)
|
||||
row = await _fetch(storage, eid)
|
||||
stored = row[1]
|
||||
@@ -207,7 +207,7 @@ async def test_metadata_none_is_null(storage, monkeypatch):
|
||||
store = UsageEventStore(storage)
|
||||
eid = str(uuid.uuid4())
|
||||
await store.record_usage_event(
|
||||
metric="embeddings_queries", value=1, event_id=eid, metadata=None
|
||||
metric="tokens_embedded", value=1, event_id=eid, metadata=None
|
||||
)
|
||||
row = await _fetch(storage, eid)
|
||||
assert row[4] is None
|
||||
@@ -233,7 +233,7 @@ async def test_best_effort_swallows_db_errors(storage, monkeypatch, caplog):
|
||||
|
||||
# Must not raise.
|
||||
with caplog.at_level(logging.WARNING, logger="nextcloud_mcp_server.usage.store"):
|
||||
await store.record_usage_event(metric="pages_chunks", value=1)
|
||||
await store.record_usage_event(metric="pages_embedded", value=1)
|
||||
|
||||
assert recorded, "record_db_operation should be called on the error path"
|
||||
assert recorded[-1][3] == "error"
|
||||
@@ -262,7 +262,7 @@ async def test_best_effort_swallows_unserializable_metadata(
|
||||
# Must not raise.
|
||||
with caplog.at_level(logging.WARNING, logger="nextcloud_mcp_server.usage.store"):
|
||||
await store.record_usage_event(
|
||||
metric="pages_chunks", value=1, metadata=bad_metadata
|
||||
metric="pages_embedded", value=1, metadata=bad_metadata
|
||||
)
|
||||
|
||||
# Nothing was written — the encode failed before the insert.
|
||||
|
||||
Reference in New Issue
Block a user