Skip to main content

amberflo-metering-python

CI Status PyPI

Amberflo is the simplest way to integrate metering into your application.

This is the official Python 3 client that wraps the Amberflo REST API.

:heavy_check_mark: Features

  • Add and update customers
  • Assign and update product plans to customers
  • List invoices of a customer
  • Get a new customer portal session for a customer
  • Add and list prepaid orders to customers
  • Send meter events
    • In asynchronous batches for high throughput (with optional flush on demand)
    • Or synchronously
    • Using the Amberflo API or the Amberflo supplied AWS S3 bucket
  • Query usage
  • Fine grained logging control

:rocket: Quick Start

  1. Sign up for free and get an API key.

  2. Install the SDK

pip install amberflo-metering-python
  1. Create a customer
import os
from metering.customer import CustomerApiClient, create_customer_payload

client = CustomerApiClient(os.environ.get("API_KEY"))

message = create_customer_payload(
    customer_id="sample-customer-123",
    customer_email="customer-123@sample.com",
    customer_name="Sample Customer",
    traits={
        "region": "us-east-1",
    },
)
customer = client.add_or_update(message)
  1. Ingest meter events
import os
from time import time
from metering.ingest import create_ingest_client

client = create_ingest_client(api_key=os.environ["API_KEY"])

dimensions = {"region": "us-east-1"}
customer_id = "sample-customer-123"

client.meter(
    meter_api_name="sample-meter",
    meter_value=5,
    meter_time_in_millis=int(time() * 1000),
    customer_id=customer_id,
    dimensions=dimensions,
)
  1. Query usage
import os
from time import time
from metering.usage import (AggregationType, Take, TimeGroupingInterval,
                            TimeRange, UsageApiClient, create_usage_query)

client = UsageApiClient(os.environ.get("API_KEY"))

since_two_days_ago = TimeRange(int(time()) - 60 * 60 * 24 * 2)

query = create_usage_query(
    meter_api_name="my_meter",
    aggregation=AggregationType.SUM,
    time_grouping_interval=TimeGroupingInterval.DAY,
    time_range=since_two_days_ago,
    group_by=["customerId"],
    usage_filter={"customerId": ["some-customer-321", "sample-customer-123"]},
    take=Take(limit=10, is_ascending=False),
)
report = client.get(query)

:zap: High throughput ingestion

Amberflo.io libraries are built to support high throughput environments. That means you can safely send hundreds of meter records per second. For example, you can chose to deploy it on a web server that is serving hundreds of requests per second.

However, every call does not result in a HTTP request, but is queued in memory instead. Messages are batched and flushed in the background, allowing for much faster operation. The size of batch and rate of flush can be customized.

Flush on demand: For example, at the end of your program, you'll want to flush to make sure there's nothing left in the queue. Calling this method will block the calling thread until there are no messages left in the queue. So, you'll want to use it as part of your cleanup scripts and avoid using it as part of the request lifecycle.

Error handling: The SDK allows you to set up a on_error callback function for handling errors when trying to send a batch.

Here is a complete example, showing the default values of all options:

def on_error_callback(error, batch):
    ...

client = create_ingest_client(
    api_key=API_KEY,
    max_queue_size=100000,  # max number of items in the queue before rejecting new items
    threads=2,  # number of worker threads doing the sending
    retries=2,  # max number of retries after failures
    batch_size=100,  # max number of meter records in a batch
    send_interval_in_secs=0.5,  # wait time before sending an incomplete batch
    sleep_interval_in_secs=0.1,  # wait time after failure to send or queue empty
    on_error=on_error_callback,  # handle failures to send a batch
)

...

client.meter(...)

client.flush()  # block and make sure all messages are sent

What happens if there are just too many messages?

If the module detects that it can't flush faster than it's receiving messages, it'll simply stop accepting new messages. This allows your program to continually run without ever crashing due to a backed up metering queue.

Ingesting through the S3 bucket

The SDK provides a metering.ingest.IngestS3Client so you can send your meter records to us via the S3 bucket.

Use of this feature is enabled if you install the library with the s3 option:

pip install amberflo-metering-python[s3]

Just pass the S3 bucket credentials to the factory function:

client = create_ingest_client(
    bucket_name=os.environ.get("BUCKET_NAME"),
    access_key=os.environ.get("ACCESS_KEY"),
    secret_key=os.environ.get("SECRET_KEY"),
)

:book: Documentation

General documentation on how to use Amberflo is available at Product Walkthrough.

The full REST API documentation is available at API Reference.

:scroll: Samples

Code samples covering different scenarios are available in the ./samples folder.

:construction_worker: Contributing

Feel free to open issues and send a pull request.

Also, check out CONTRIBUTING.md.

:bookmark_tabs: Reference

API Clients

Ingest

from metering.ingest import (
    create_ingest_payload,
    create_ingest_client,
)

Customer

from metering.customer import (
    CustomerApiClient,
    create_customer_payload,
)

Usage

from metering.usage import (
    AggregationType,
    Take,
    TimeGroupingInterval,
    TimeRange,
    UsageApiClient,
    create_usage_query,
    create_all_usage_query,
)

Customer Portal Session

from metering.customer_portal_session import (
    CustomerPortalSessionApiClient,
    create_customer_portal_session_payload,
)

Customer Prepaid Order

from metering.customer_prepaid_order import (
    BillingPeriod,
    BillingPeriodUnit,
    CustomerPrepaidOrderApiClient,
    create_customer_prepaid_order_payload,
)

Customer Product Invoice

from metering.customer_product_invoice import (
    CustomerProductInvoiceApiClient,
    create_all_invoices_query,
    create_latest_invoice_query,
    create_invoice_query,
)

Customer Product Plan

from metering.customer_product_plan import (
    CustomerProductPlanApiClient,
    create_customer_product_plan_payload,
)

Exceptions

from metering.exceptions import ApiError

Logging

amberflo-metering-python uses the standard Python logging framework. By default, logging is and set at the WARNING level.

The following loggers are used:

  • metering.ingest.producer
  • metering.ingest.s3_client
  • metering.ingest.consumer
  • metering.session.ingest_session
  • metering.session.api_session

Metadata

Release files for amberflo-metering-python 3.3.1

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

Source distribution (sdist)

Source distribution for amberflo-metering-python 3.3.1
File Size Uploaded Provenance
amberflo-metering-python-3.3.1.tar.gz 19.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for amberflo-metering-python 3.3.1
File Interpreter ABI Platform
amberflo_metering_python-3.3.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 44.0 kB

Release files / amberflo-metering-python-3.3.1.tar.gz

Download URL amberflo-metering-python-3.3.1.tar.gz
Size 19.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7742051968cebf14f4d692e8d66a3c6b08ee5640b0aa62f180b1ba623e9e1a6a
BLAKE2b-256 checksum
How to use checksums
ad04d2ff270284271c8a3e7d1636a4d77625490db888b70e5610440ebc13878f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.18

Release files / amberflo_metering_python-3.3.1-py2.py3-none-any.whl

Download URL amberflo_metering_python-3.3.1-py2.py3-none-any.whl
Size 24.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
325f3255b12e51fb997c0a76ab86ce6dfcb00510cf3be6420fde22abd05a9b7e
BLAKE2b-256 checksum
How to use checksums
15962b9b48f013f12bcc8d3f921ef46018992b88170b677630f7693641e0d070
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.18

Security for amberflo-metering-python 3.3.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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