Skip to main content

Snuba SDK for generating SnQL queries.

Project description

https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png

See here for the full documentation.

Status

https://img.shields.io/pypi/v/snuba-sdk.svg https://github.com/getsentry/snuba-sdk/workflows/tests/badge.svg https://img.shields.io/discord/621778831602221064

Examples

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",
    tenant_ids = {"referrer": "my_referrer", "organization_id": 1234}
    query = Query(
        match=Entity("events"),
        select=[
            Column("title"),
            Function("uniq", [Column("event_id")], "uniq_events"),
        ],
        groupby=[Column("title")],
        where=[
            Condition(Column("timestamp"), Op.GT, datetime.datetime(2021, 1, 1)),
            Condition(Column("project_id"), Op.IN, Function("tuple", [1, 2, 3])),
        ],
        limit=Limit(10),
        offset=Offset(0),
        granularity=Granularity(3600),
    ),
    flags = Flags(debug=True)
)

Queries can also be built incrementally:

query = (
    Query("discover", Entity("events"))
    .set_select(
        [Column("title"), Function("uniq", [Column("event_id")], "uniq_events")]
    )
    .set_groupby([Column("title")])
    .set_where(
        [
            Condition(Column("timestamp"), Op.GT, datetime.datetime.(2021, 1, 1)),
            Condition(Column("project_id"), Op.IN, Function("tuple", [1, 2, 3])),
        ]
    )
    .set_limit(10)
    .set_offset(0)
    .set_granularity(3600)
)

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

# Outputs a formatted Snuba request
request.serialize()

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

# Outputs a formatted Snuba request
print(request.print())

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.

License

Licensed under FSL-1.0-Apache-2.0, see LICENSE.md.

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

snuba_sdk-2.0.11-py2.py3-none-any.whl (48.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file snuba_sdk-2.0.11-py2.py3-none-any.whl.

File metadata

  • Download URL: snuba_sdk-2.0.11-py2.py3-none-any.whl
  • Upload date:
  • Size: 48.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for snuba_sdk-2.0.11-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bffee86470cf16dd842942f4ee37310767217290fdab060380e50cdcf74a9e21
MD5 7fb63e367d839684e0ae05e491d47e57
BLAKE2b-256 b3b406d14664a994682c64b95cc537d7747c4dd81f3c51b022e90056782f3eed

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