A library for instrumenting Python code at runtime.
Project description
Diagnose
A library for instrumenting Python code at runtime.
Probes
Structured logs and metrics and observability are great, but almost always require you to alter your code, which interrupts the flow when reading code. It also typically requires a build cycle to alter; it's no fun making a ticket, getting review, waiting for a build and deploy, and then doing it all again to back out your temporary additions. This is doubly true when doing research, where you might perform a dozen small experiments to measure your live code.
This library allows you to dynamically add probes at runtime instead. Probes are:
- reliable: errors will never affect your production code
- ephemeral: set a "lifespan" (in minutes), after which point the probe detaches
- comprehensive: all references to the target function are instrumented
- fast: measure most functions with fast local lookups; uses hunter (in Cython) for more invasive internal probes.
Individual probes can be created directly with the FunctionProbe class:
>>> from path.to.module import myclass
>>> myclass().add13(arg=5)
18
>>> p = diagnose.FunctionProbe("path.to.module.myclass.add13")
>>> p.instruments["foo"] = diagnose.LogInstrument("foo", "arg", internal=False)
>>> p.start()
>>> myclass().add13(arg=5)
Probe (foo) = 5
18
Managers
In a running system, we want to add, remove, start, and stop probes without having to code at an interactive prompt or restart the system; we do this with a ProbeManager. Start by configuring the global diagnose.manager:
>>> diagnose.manager.instrument_classes = {
"log": LogInstrument,
"hist": MyHistogramInstrument,
"incr": MyIncrementInstrument,
}
>>> diagnose.manager.global_namespace.update({"foo": foo})
Later, you can define probes:
>>> diagnose.manager.specs["probe-1"] = {
"target": "myapp.module.file.class.method",
"instrument": {
"type": "log",
"name": "myapp.method",
"value": "result",
"internal": False,
"custom": {},
},
"lifespan": 10,
"lastmodified": datetime.datetime.utcnow(),
"applied": {},
}
Then call diagnose.manager.apply()
, either when you add a probe, or on a
schedule if your store is in MongoDB and the process defining probes is not
the target process.
The applied
dictionary will be filled with information about which processes
have applied the probe, and whether they encountered any errors.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file diagnose-1.0.1.tar.gz
.
File metadata
- Download URL: diagnose-1.0.1.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.12.4 setuptools/27.2.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9114ed184ce68d90afe668213160de33256c83c240d098c475226c459d600c9 |
|
MD5 | 987165daa5e85391a159bc87bc4170d2 |
|
BLAKE2b-256 | 1c35945edd1571d2d4165a198f6032e396e0c718d397bdb74d165797c2f38d7e |
File details
Details for the file diagnose-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: diagnose-1.0.1-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.12.4 setuptools/27.2.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0af412e40dd9f8b292cc0501e3c2ee99e67aa58901842c1f81232b6b1fe52321 |
|
MD5 | 36cb7190b0b903ee492f3bcb415edb7a |
|
BLAKE2b-256 | 25397f595b1dfd9539fa3b27fd7d7b66ea5ba9b7a18fdcadb6fa46fc708525c9 |
File details
Details for the file diagnose-1.0.1-py2-none-any.whl
.
File metadata
- Download URL: diagnose-1.0.1-py2-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/2.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f03eea4a9c27a934d9959b0052bd0d0447e1175af5ea3029b920c3a4979030ce |
|
MD5 | 615443ea04a1be67500d7bbbc7903aa9 |
|
BLAKE2b-256 | ad6b664caef620cff3bc7e1e1944e7453a37a05aa669c5743492f9b8b5bce0f7 |