Skip to main content

nuclia-metering

Client library and Protobuf contract (usage.v1) used by emitting services to send usage to the Nuclia usage pipeline.

  • Validates events where they are created, with a stable idempotency key and a frozen UTC timestamp.
  • Publishes them to the zone's usage-events JetStream stream and only treats them as sent once acknowledged.
  • Retries inside the stream's duplicate window, buffers events during short outages, and exposes metrics hooks.
  • Services send raw values only (bytes, seconds, requests…), never prices or credits.

Install

pip install nuclia-metering
# or
uv add nuclia-metering

Requires Python 3.12+.

Quick start

import asyncio

from nuclia_metering import UsageClient, UsageClientConfig


async def main() -> None:
    config = UsageClientConfig(service="nucliadb")
    async with UsageClient(config=config, servers="nats://localhost:4222") as client:
        await client.emit(
            entity_id="kb-123",
            meter_slug="nucliadb.search.requests",
            value=1,
            properties={"entity_type": "kb"},
        )


asyncio.run(main())

Read the integration guide before sending usage from a new service, and see examples/simple_service for a runnable example.

Library API arguments are keyword-only: use UsageClient(config=config) and client.emit(entity_id="kb-123", meter_slug="nucliadb.search.requests", value=1). Positional arguments raise TypeError, preventing accidental argument-order mistakes.

Contract

The Protobuf definitions live in proto/usage/v1/usage.proto. The Python modules are generated when the package is built and ship as nuclia_metering.proto.usage.v1.usage_pb2.

Properties allow at most 8 keys, each at most 32 UTF-8 bytes, with values at most 64 UTF-8 bytes. Events are validated automatically when created. The regional usage consumer applies the same validation, defined in nuclia_metering.validation.

USAGE_KEY_NAMESPACE is 1334a099-f030-4232-bbd6-b958cba95ebd. A business idempotency key is sent as UUIDv5(USAGE_KEY_NAMESPACE, key), so every language derives the same id.

Development

Prerequisites

uv installs Python 3.12+ and every Python tool (ruff, ty, pytest, protoc via grpcio-tools). The other tools are only needed for some tasks:

Tool Needed for Install
uv everything Installing uv
buf changing .proto files Install the Buf CLI
Docker (or Rancher Desktop) integration tests, the TruffleHog pre-commit hook and the example service Get Docker or Install Rancher Desktop

CI runs all of these checks, so a missing optional tool only means you find out on the PR.

Commands

Run make to list every target.

make install           # uv sync (generates the protobuf modules into src/) and pre-commit hooks
make proto             # regenerate the protobuf modules after changing a .proto file
make format            # ruff format/fix and buf format
make lint              # ruff, ty, buf lint and buf format check
make breaking          # buf breaking against main
make test              # all tests; also test-unit and test-integration
make check             # definition of done: lint, buf breaking and all tests
make api-snapshot      # regenerate tests/snapshots/public_api.txt after an intended public API change
make pre-commit        # every pre-commit hook on the whole repo
make build             # sdist and wheel into dist/
make example           # run the example service (NATS_PORT=14222 if 4222 is taken); make example-down to stop

The pre-commit hooks block direct commits to main and run TruffleHog on staged changes to catch leaked secrets.

Security scanning

Every repository in a production workload must run these scans. They use organization-level secrets, so they only run in GitHub Actions:

Workflow Tool Runs on
trufflehog.yml TruffleHog secrets detection on the full history every PR and push to main
polaris.yml Polaris SAST (rapid on PRs, full on main) PRs to and pushes to main
bd_sca_scanner.yml Black Duck SCA of the runtime dependencies changes to uv.lock or pyproject.toml

If TruffleHog finds a secret, revoke and rotate it immediately. Removing it from the code is not enough.

Integration tests start NATS (nats:2.15-alpine, override with NATS_IMAGE) with testcontainers; they are skipped when Docker is not available. With Rancher Desktop or Colima, set DOCKER_HOST if /var/run/docker.sock does not exist.

Working with AI agents

This repo is set up for GitHub Copilot (VS Code, CLI and code review). Agents and humans follow the same contract in AGENTS.md, and run make check before finishing. Procedures for common changes live in .github/skills. The one-time admin setup is in docs/ai-setup.md.

Releasing

Releases are cut by release-please from Conventional Commits and published to PyPI with trusted publishing. Each release attests build provenance and a CycloneDX SBOM of the runtime dependencies for the wheel and sdist. To verify a downloaded artifact:

gh attestation verify nuclia_metering-<version>-py3-none-any.whl --repo nuclia/nuclia-metering
gh attestation verify nuclia_metering-<version>-py3-none-any.whl --repo nuclia/nuclia-metering \
  --predicate-type https://cyclonedx.org/bom

On every push to main, release-please opens or updates a release PR with the next version, the changelog and the version bumps in pyproject.toml, uv.lock and .release-please-manifest.json. Merging it tags the release and publishes the package. Never edit the version by hand.

The commit type decides the next version. Use it in the PR title when squash-merging:

Commit Example Release
fix:, perf:, revert:, deps: fix: retry on ServerError responses patch (1.2.0 → 1.2.1)
feat: feat: add MAX meter helper minor (1.2.0 → 1.3.0)
! or BREAKING CHANGE: footer feat!: rename UsageClient.emit major (1.2.0 → 2.0.0)
minor before 1.0 (0.1.0 → 0.2.0)
chore:, docs:, test:, ci:, refactor:, build: docs: explain LAST meters no release

To force a specific version (for example the first stable release), add a Release-As: 1.0.0 footer to the commit message.

Any change to proto/usage/v1 that buf breaking rejects is a breaking change: add a new package version (usage.v2) instead.

Metadata

Release files for nuclia-metering 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nuclia-metering 0.1.0
File Size Uploaded Provenance Actions
nuclia_metering-0.1.0.tar.gz 36.1 kB With provenance Details for nuclia_metering-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nuclia-metering 0.1.0
File Interpreter ABI Platform Actions
nuclia_metering-0.1.0-py3-none-any.whl Python 3 none any Details for nuclia_metering-0.1.0-py3-none-any.whl

Total release size: 62.3 kB

Release files / nuclia_metering-0.1.0.tar.gz

Download URL nuclia_metering-0.1.0.tar.gz
Size 36.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7abe0e38414401bed129eaaf70d1febf111bcf95a5f69a99b893d254284d00ad
BLAKE2b-256 checksum
How to use checksums
3873a611c49ef84703560d2c9ee963310a5830e4d2457b885f39578664e5a9fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / nuclia_metering-0.1.0-py3-none-any.whl

Download URL nuclia_metering-0.1.0-py3-none-any.whl
Size 26.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8472ccbfcc0dfeb2f9fca3f0e1c4a9cefc53b44c0d54c616c3b35a07fcbce44
BLAKE2b-256 checksum
How to use checksums
f94890409b25dc0e951dd898721b535ebcb2a292f92bd81deabf0a07cba0757a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for nuclia-metering 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for nuclia_metering-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page