Skip to main content

Running code use instrumentation aid

Project description

Python code wake lib/service (pycodewake)

test codecov

Concept

Code Wake is a code instrumentation aid, recording to a database for analysis.

With configuration so that Code Wake can find a data store, usage can be as simple as this, create a process:

import code_wake

cwproc = code_wake.Process()
cwproc.log()

Record events:

cwproc.log({"event": "order", "type": "special", "customer": customer})

Or log an exception:

cwproc.log({"event": "fault", "doing": "create_order", "customer": customer}, exc)

Here exc is an exception, and by default it's stack trace will be recorded along with the event. Stack traces may also be recorded for non exception events as well, either by specifying inc_st=True, or by changing the configuration such that non exception events always have them.

The event will be logged asynchronously by default if the store supports this, and thus log will return None. If you specify the kward sync=True then the event created will be returned.

Configuration

By default, if /etc/code_wake.conf exists, or ./etc/code_wake.conf, then they will be taken as configuration. If the CODE_WAKE_CONFIG environment variable is set then this will override the config location.

What you put in the configuration depends a lot on if it is a global config used my many different apps, or just one, and having a configuration at all is entirely optional.

A complete, canonical configuration might look like this:

store:
    adapter: "code_wake_sql14_store,Sql14Store"
    config: "sqlite:////tmp/some_file.sqlite"
environment:
    name: "production"
app:
    name: "my_app"
    vsn: "1.2.3"
stacktraces:
    include:
        from_exceptions: true
        for_non_exceptions: false

This might be appropriate for a simple app, as it configures the app name and version. A more general config for use by many apps would certainly omit those.

An environment is optional, app versions are optional, the stack trace configuration will be defaulted, the app name will default to the executable path, so the only really essential thing is the store adapter and configuration. If you don't want to use a config you must provide at least that when you instantiate your process.

Use without config

If you do not use a config, you should instantiate a store adapter yourself and pass it to the Process constructor:

import code_wake
from code_wake_sql14_store import Sql14Store

cwproc = code_wake.Process(store=Sql14Store("sqlite:////tmp/some_file.sqlite"))

The process is a singleton, so subsequently when you want to log an event you can simply do:

import code_wake

cwproc = code_wake.Process()
cwproc.log({"what": "ever"})

Other process options

When you instantiate the Process for the first time, there are other options which would be good to use (if there isn't a configurartion to provide good values):

import code_wake
from code_wake_sql14_store import Sql14Store

cwproc = code_wake.Process(
    app_name="my_app",
    app_vsn="1.2.3",
    env_name="production",
    store=Sql14Store("sqlite:////tmp/some_file.sqlite"),
)

Also st_for_non_exceptions will override the recording of stack traces for non exception events and st_from_exceptions for exception events.

Queue store

A "queue store" which can be mixed in with another store, can be used to turn any store into one which always queues, instead of immediately adding the event. This means that a process may be protected from being adversely affected by high latency or unreliable stores. The events are processed by another thread, which commits them to whatever actual storage you are using.

For example:

from code_wake import QueueStore
from code_wake_sql14_store import Sql14Store

class Sql14QueueStore(QueueStore, Sql14Store):
    pass

store = Sql14QueueStore("sqlite:////tmp/some_file.sqlite")

The store adapter used with the queue store must be thread safe and cross thread operable. Sqlite for example, does not work cross threads when used with memory backing!

Environment variables

Apart from CODE_WAKE_CONFIG to set the config, the following are supported and will override any configuration file settings if set:

| Environment variable | Description | +---------------------------------+-------------| | CODE_WAKE_STORE_ADAPTER | set the store adapter (module,class) | | CODE_WAKE_STORE_CONFIG | comma separated adapter params | | CODE_WAKE_ENVIRONMENT | name of the environment | | CODE_WAKE_APP_NAME | name of the application | | CODE_WAKE_APP_VSN | version of the application | | CODE_WAKE_ST_FROM_EXCEPTIONS | "true" or "false to turn exception stack traces on/off | | CODE_WAKE_ST_FOR_NON_EXCEPTIONS | "true" or "false to turn other event stack traces on/off |

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

code-wake-0.2.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code_wake-0.2.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file code-wake-0.2.0.tar.gz.

File metadata

  • Download URL: code-wake-0.2.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for code-wake-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bc359d6c826b210a36a81ca6e6cf2de8a433db49b297d22f96a9e6dc31b2264b
MD5 955358c702f603e408dd8720e8b1ccad
BLAKE2b-256 8d0bb337e4e507b782e5f0e37c4f9d37eb6282945da4af27f1467ab3b870fcf2

See more details on using hashes here.

File details

Details for the file code_wake-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: code_wake-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for code_wake-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60b4ae6b6f3f5dcec2ab83acb336f7615cfbdbb1f3eab7adab94adcabe42ad79
MD5 625c5b8b8b9d974191906063eeea1de1
BLAKE2b-256 6200ca82325c140fb44aef9eb2e61d40aaf25becd178c78f6cab01b65dcbcb04

See more details on using hashes here.

Supported by

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