openmeter icon indicating copy to clipboard operation
openmeter copied to clipboard

feat(streaming): lookup usage by customer key and subjects

Open hekike opened this issue 3 months ago • 1 comments
trafficstars

Lookup usage by customer Key and subjects in usage attribution.

  • achieved by changing the default query methods both in streaming and customer/adapter

⚠️ Before merging this or otherwise deploying this change, check if conflicting subject-customer keys are present in the DB ⚠️

# e.g. scenarios like
customer1:
  key: key-1
  subject:
    key: key-2

customer2:
  key: key-2
  subject:
    key: key-1
-- aggregating query
select
  cs.namespace, count(*)
from customer_subjects cs
inner join customers c on cs.customer_id = c.id
left join customers c2 on cs.subject_key = c2.key and c2.namespace = cs.namespace and c2.deleted_at is null
where c2.id != c.id and c2.id is not null and cs.deleted_at is null
group by cs.namespace;

The effect for these conflicts after the change would be that the wrong customer / subject / usage data would be returned for a given query

Summary by CodeRabbit

  • New Features

    • Customers can be looked up by either usage-attribution key or subject key for more reliable retrieval.
  • Improvements

    • More accurate customer filtering in analytics queries, including cases where only a customer key is present.
    • Query generation now uses a dictionary-style mapping for subject→customer resolution, improving mapping robustness.
  • Refactor

    • Internal query helper streamlined and an external dependency removed.
  • Tests

    • Added descriptive subtest names and expanded tests to cover key- and subject-based lookups.

hekike avatar Aug 01 '25 12:08 hekike