|
| 1 | +# Superstream Client for Python (`superclient`) |
| 2 | + |
| 3 | +Superclient is a zero-code optimisation agent for Python applications that use Apache Kafka. |
| 4 | +It transparently intercepts producer creation in the popular client libraries and tunes |
| 5 | +configuration parameters (compression, batching, etc.) based on recommendations |
| 6 | +provided by the Superstream platform. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Why use Superclient? |
| 11 | + |
| 12 | +• **No code changes** – simply install the package and run your program. |
| 13 | +• **Dynamic configuration** – adapts to cluster-specific and topic-specific insights |
| 14 | + coming from `superstream.metadata_v1`. |
| 15 | +• **Safe by design** – any internal failure falls back to your original |
| 16 | + configuration; the application never crashes because of the agent. |
| 17 | +• **Minimal overhead** – uses a single lightweight background thread (or an async |
| 18 | + coroutine when running with `aiokafka`). |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Supported Kafka libraries |
| 23 | + |
| 24 | +| Library | Producer class | Status | |
| 25 | +|---------|----------------|--------| |
| 26 | +| kafka-python | `kafka.KafkaProducer` | ✓ implemented | |
| 27 | +| aiokafka | `aiokafka.AIOKafkaProducer` | ✓ implemented | |
| 28 | +| confluent-kafka | `confluent_kafka.Producer` | ✓ implemented | |
| 29 | + |
| 30 | +Other libraries/frameworks that wrap these producers (e.g. Faust, FastAPI event |
| 31 | +publishers, Celery Kafka back-ends) inherit the benefits automatically. |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Installation |
| 36 | + |
| 37 | +```bash |
| 38 | +pip install superclient |
| 39 | +``` |
| 40 | + |
| 41 | +The package ships with a `sitecustomize.py` entry-point, therefore Python |
| 42 | +imports the agent automatically **before your application's code starts**. |
| 43 | +If `sitecustomize` is disabled in your environment you can initialise manually: |
| 44 | + |
| 45 | +```python |
| 46 | +import superclient # side-effects automatically enable the agent |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Environment variables |
| 52 | + |
| 53 | +| Variable | Description | |
| 54 | +|----------|-------------| |
| 55 | +| `SUPERSTREAM_DISABLED` | `true` disables all functionality | |
| 56 | +| `SUPERSTREAM_DEBUG` | `true` prints verbose debug logs | |
| 57 | +| `SUPERSTREAM_TOPICS_LIST` | Comma-separated list of topics your application *may* write to | |
| 58 | +| `SUPERSTREAM_LATENCY_SENSITIVE` | `true` prevents the agent from lowering `linger.ms` | |
| 59 | + |
| 60 | +At start-up the agent logs the set of variables detected: |
| 61 | + |
| 62 | +``` |
| 63 | +[superstream] INFO agent: Superstream Agent initialized with environment variables: {'SUPERSTREAM_DEBUG': 'true', ...} |
| 64 | +``` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## How it works |
| 69 | + |
| 70 | +1. An **import hook** patches the producer classes once their modules are |
| 71 | + imported. |
| 72 | +2. When your code creates a producer the agent: |
| 73 | + a. Skips internal Superstream clients (their `client_id` starts with |
| 74 | + `superstreamlib-`). |
| 75 | + b. Fetches the latest optimisation metadata from |
| 76 | + `superstream.metadata_v1`. |
| 77 | + c. Computes an optimal configuration for the most impactful topic (or falls |
| 78 | + back to sensible defaults) while respecting the |
| 79 | + latency-sensitive flag. |
| 80 | + d. Overrides producer kwargs/in-dict values before the original constructor |
| 81 | + executes. |
| 82 | + e. Sends a *client_info* message to `superstream.clients` that contains both |
| 83 | + original and optimised configurations. |
| 84 | +3. A single background heartbeat thread (or async task for `aiokafka`) emits |
| 85 | + *client_stats* messages every `report_interval_ms` (default 5 minutes). |
| 86 | +4. When the application closes the producer the agent stops tracking it and |
| 87 | + ceases heart-beats. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Logging |
| 92 | + |
| 93 | +Log lines are printed to `stdout`/`stderr` and start with the `[superstream]` |
| 94 | +prefix so they integrate with existing log pipelines. Set |
| 95 | +`SUPERSTREAM_DEBUG=true` for additional diagnostic messages. |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## Security & compatibility |
| 100 | + |
| 101 | +• Authentication/SSL/SASL/DNS settings are **copied from your original |
| 102 | + configuration** to every short-lived internal client. |
| 103 | +• The agent only relies on the Kafka library already present in your |
| 104 | + environment, therefore **no dependency conflicts** are introduced. |
| 105 | +• All exceptions are caught internally; your application will **never crash or |
| 106 | + hang** because of Superclient. |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +Apache 2.0 |
0 commit comments