Skip to main content

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) for each instrument
  • 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 by calling attach_to(target):

>>> from path.to.module import myclass
>>> myclass().add13(arg=5)
18
>>> p = diagnose.probes.attach_to("path.to.module.myclass.add13")
>>> p.instruments["foo"] = diagnose.LogInstrument("foo", "arg")
>>> p.start()
>>> myclass().add13(arg=5)
Probe (foo) = 5
18

Instruments aren't limited to recording devices! Use probes to fire off any kind of event handler. Instruments are free to maintain state themselves, or read it from somewhere else, to control their own behavior or even implement feedback mechanisms. A truly evil instrument could even alter the args/kwargs passed to a function on the fly, or call arbitrary Python code to do any number of crazy things. Consequently, it's up to you to govern what instruments are added to your environment.

Managers

In a running system, we want to add, remove, start, and stop probes and instruments without having to code at an interactive prompt or restart the system; we do this with an InstrumentManager. 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 instruments:

>>> diagnose.manager.specs["instr-1"] = {
    "target": "myapp.module.file.class.method",
    "instrument": {
        "type": "log",
        "name": "myapp.method",
        "value": "result",
        "event": "return",
        "custom": {},
    },
    "lifespan": 10,
    "lastmodified": datetime.datetime.utcnow(),
    "applied": {},
}

Then call diagnose.manager.apply(), either when you add an instrument, 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diagnose-2.1.0.tar.gz (13.3 kB view details)

Uploaded Source

Built Distributions

diagnose-2.1.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

diagnose-2.1.0-py2-none-any.whl (14.4 kB view details)

Uploaded Python 2

File details

Details for the file diagnose-2.1.0.tar.gz.

File metadata

  • Download URL: diagnose-2.1.0.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • 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

Hashes for diagnose-2.1.0.tar.gz
Algorithm Hash digest
SHA256 6b53ee310767088fb36383040bc5dfb74ca39f9f018388d0a2256305e8f9eabe
MD5 58300ad579819d67f88c7f605f5d327f
BLAKE2b-256 24d22466462e6f11f266a94806d725e8dc641fbd6f0e8ea3ab436203f89c6c15

See more details on using hashes here.

File details

Details for the file diagnose-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: diagnose-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • 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

Hashes for diagnose-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81085b3e8f86031b7484e0f2d07a722fa3342dc1178f628548a29fc4796477e5
MD5 78cbe8b3d50c0a11ddb72ba9f6ea89da
BLAKE2b-256 9de007e675c8596d8b6269a2e836063186c5cb1368588df042d37bf74fb41bbc

See more details on using hashes here.

File details

Details for the file diagnose-2.1.0-py2-none-any.whl.

File metadata

  • Download URL: diagnose-2.1.0-py2-none-any.whl
  • Upload date:
  • Size: 14.4 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

Hashes for diagnose-2.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 9e3e237d82e9ceb8722eb0d0da23eb70ffdcbb333149e68a5ca0f2e8c2e3b733
MD5 7f9004c316ad7d0b14fa2726ce4764c7
BLAKE2b-256 270c00ccf98bf8fdac5677bc4a7f1a5d1027fd4c8a4d3fd00b6fe81b3fea348c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page