Skip to main content

Snuba SDK for generating SnQL queries.

Project description

See here for the full documentation.



Snuba SDK is a tool that allows requests to Snuba to be built programatically. A Request consists of a Query, the dataset the Query is targeting, the AppID of the Request, and any flags for the Request. A Query object is a code representation of a SnQL query, and has a number of attributes corresponding to different parts of the query.

Requests and Queries can be created directly:

request = Request(
    dataset = "discover",
    app_id = "myappid",
    query = Query(
            Function("uniq", [Column("event_id")], "uniq_events"),
            Condition(Column("timestamp"), Op.GT, datetime.datetime(2021, 1, 1)),
            Condition(Column("project_id"), Op.IN, Function("tuple", [1, 2, 3])),
    flags = Flags(debug=True)

Queries can also be built incrementally:

query = (
    Query("discover", Entity("events"))
        [Column("title"), Function("uniq", [Column("event_id")], "uniq_events")]
            Condition(Column("timestamp"), Op.GT, datetime.datetime.(2021, 1, 1)),
            Condition(Column("project_id"), Op.IN, Function("tuple", [1, 2, 3])),

Once the request is built, it can be translated into a Snuba request that can be sent to Snuba.

# Outputs a formatted Snuba request

It can also be printed in a more human readable format.

# Outputs a formatted Snuba request

This outputs:

    "dataset": "discover",
    "app_id": "myappid",
    "query": "MATCH (events) SELECT title, uniq(event_id) AS uniq_events BY title WHERE timestamp > toDateTime('2021-01-01T00:00:00.000000') AND project_id IN tuple(1, 2, 3) LIMIT 10 OFFSET 0 GRANULARITY 3600",
    "debug": true

If an expression in the query is invalid (e.g. Column(1)) then an InvalidExpressionError exception will be thrown. If there is a problem with a query, it will throw an InvalidQueryError exception when .validate() or .translate() is called. If there is a problem with the Request or the Flags, an InvalidRequestError or InvalidFlagError will be thrown respectively.

Contributing to the SDK

Please refer to CONTRIBUTING.rst.


Licensed under MIT, see LICENSE.

Project details

Download files

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

Built Distribution

snuba_sdk-1.0.1-py2.py3-none-any.whl (28.8 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page