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.

Breakpoints

Breakpoints allow you to perform tests that involve concurrency, or that must trigger specific actions at specific times, by setting specific breakpoints at which the execution must stop waiting for some conditions to happen.

        with Breakpoint(S3Archive, "unarchive") as bp:
            bp.start_thread(object.unarchive)  # Start something in the background that will invoke S3Archive.unarchive
            bp.wait()  # wait for S3Archive.unarchive to start

            # perform what has to be done once S3Archive.unarchive
            # has been started.
            # Note: you can get at the unarchive's stack frame using
            # bp.stackframe

            bp.release()  # let S3Archive.unarchive proceed.

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-4.0.0a1.tar.gz (18.3 kB view details)

Uploaded Source

Built Distributions

diagnose-4.0.0a1-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

diagnose-4.0.0a1-py2-none-any.whl (22.0 kB view details)

Uploaded Python 2

File details

Details for the file diagnose-4.0.0a1.tar.gz.

File metadata

  • Download URL: diagnose-4.0.0a1.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/2.7.18rc1

File hashes

Hashes for diagnose-4.0.0a1.tar.gz
Algorithm Hash digest
SHA256 ad694b2391bcd7d73ac7180a158822c7c9cd1bb11db1c3c7ba2b3df3a4583807
MD5 340c53a79c6204649fcdb447d2b49ed7
BLAKE2b-256 3f15a83be1a220198a94751773993c6df8bcab422b6ae191eea9674892b42c17

See more details on using hashes here.

File details

Details for the file diagnose-4.0.0a1-py3-none-any.whl.

File metadata

  • Download URL: diagnose-4.0.0a1-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/2.7.18rc1

File hashes

Hashes for diagnose-4.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 35e5278cfb186c393d4c425a5914f6455a0ffbfff7d8a699e7ac161fdb9b444a
MD5 928f0fb92798c3f73698e67ea7b7e804
BLAKE2b-256 6d67d0269a4c6f9ec0dd2b7ae9e61203ab5a06d2a9732c202c3f97c5d5291e64

See more details on using hashes here.

File details

Details for the file diagnose-4.0.0a1-py2-none-any.whl.

File metadata

  • Download URL: diagnose-4.0.0a1-py2-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/2.7.18rc1

File hashes

Hashes for diagnose-4.0.0a1-py2-none-any.whl
Algorithm Hash digest
SHA256 a1d90125f1b78286a1cad19cafdb70e8d82663d8abb136b33030681f9ecdafa0
MD5 31cb7792459290a7d8f0874e6d08eea4
BLAKE2b-256 6200551d4837b96e240eaf9337127f876316f9cb7dab2ddbeb7a84514b41ece8

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