Skip to main content

No project description provided

Project description

Initial version of the sentry rest api client, with reporting generation. This software is not production ready, and may greatly change in the future.

Features

  • Built with asyncio and aiohttp, we get sentry events concurrently.
  • Used backoff package to retry failed requests.

Limitations

Right now we don't support many sentry API endpoints. And this package right now focused on Issues & Events API.

Quick start

My main usage (current one) of this package is to generate reports for groups of specific errors, here is an example how to use it to group proxy-related issues and events by specific time range (14 days by default).

To get auth token check this documentation: Auth Tokens. Don't forget to allow at least read access for Issues & Events.

Save code below into some file, set environment variables and try to run it, if you have issues with setting up environment variables you can use plain information.

import asyncio
from datetime import timedelta
import logging
from os import environ
from typing import Any

from sentry_rest import EventErrorsReport, SentryClient
from collections import defaultdict

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

if __name__ == "__main__":
    sentry_client = SentryClient(
        auth_token=environ["SENTRY_AUTH_TOKEN"],
        auth_organization=environ["SENTRY_ORGANIZATION"],
        auth_project=environ["SENTRY_PROJECT"],
        search_query="Proxy error detected",
        tracing_enabled=False,  # toggle aiohttp requests information
    )

    # Get all events for issues with specific rearch query
    events = asyncio.run(sentry_client.extract())

    # Group event messages by proxy and date
    report = EventErrorsReport(events)
    report_data = report.group_by_pattern(pattern=r"[0-9]+(?:\.[0-9]+){3}")

    # -------------------------------
    # "Simple Vizualization"
    # --------------------------------

    # Step 1. Group by proxy
    grouped_data: defaultdict[str, defaultdict[str, Any]] = defaultdict(
        lambda: defaultdict(list)
    )
    for grouping_value, event in report_data:
        grouped_data[grouping_value][event["dateCreatedObject"]].append(
            {
                "message": f"{event["dateCreatedTime"]} >> "
                f"{event["message"].split("\n")[0].split("|")[1][:80]}",
                "eventCreatedTimestamp": event["eventCreatedTimestamp"],
            }
        )

    # Step 2. Visualize grouped data
    print("Proxy checking report (2 weeks), timezone is UTC")
    print("=" * 40)
    for grouping_value, events in grouped_data.items():
        print(
            f"[Proxy: {grouping_value}. "
            f"Total erors: {sum(len(e) for e in events.values())}]"
        )
        for date, errors in events.items():
            print(f"\t{date}: {len(errors)} errors")
            prev_timestamp = None
            for error in sorted(errors, key=lambda e: e["eventCreatedTimestamp"]):
                if not prev_timestamp:
                    prev_timestamp = error["eventCreatedTimestamp"]

                if (
                    error["eventCreatedTimestamp"] - prev_timestamp
                ).total_seconds() > timedelta(hours=1).total_seconds():
                    print("\t\t" + "-" * 8 + "🕐" + "-" * 8)
                    prev_timestamp = error["eventCreatedTimestamp"]

                print(f"\t\t{error["message"]}")

        print("=" * 40)

Example of output:

Proxy checking report (2 weeks), timezone is UTC
========================================
[Proxy: 161.77.124.98. Total erors: 5]
        2024-09-04: 3 errors
                02:18:20 >> ProxyCheckError
                --------🕐--------
                06:18:20 >> ProxyCheckError
                --------🕐--------
                10:18:20 >> ProxyCheckError
        2024-09-03: 2 errors
                18:18:20 >> ProxyCheckError
                --------🕐--------
                22:18:20 >> ProxyCheckError
========================================
[Proxy: 89.38.228.198. Total erors: 39]
        2024-09-04: 3 errors
                02:18:20 >> ProxyCheckError
                --------🕐--------
                06:18:20 >> ProxyCheckError
                --------🕐--------
                10:18:20 >> ProxyCheckError
        2024-09-03: 36 errors
                14:36:32 >> APIError_fetch_data
                14:36:33 >> APIError_fetch_data
                14:36:34 >> APIError_fetch_data
                14:36:36 >> APIError_fetch_data
                14:36:37 >> APIError_fetch_data
                14:36:38 >> APIError_fetch_data
                14:36:52 >> initialize_api
                --------🕐--------
                18:13:12 >> APIError_fetch_data
                18:13:13 >> APIError_fetch_data
                18:13:14 >> APIError_fetch_data
                18:13:15 >> APIError_fetch_data
                18:13:18 >> APIError_fetch_data
                18:13:32 >> APIError_fetch_data
                18:13:32 >> initialize_api
                18:18:20 >> ProxyCheckError
                18:58:11 >> APIError_fetch_data
                18:58:12 >> APIError_fetch_data
                18:58:13 >> APIError_fetch_data
                18:58:15 >> APIError_fetch_data
                18:58:20 >> APIError_fetch_data
                18:58:31 >> initialize_api
                --------🕐--------
                19:56:58 >> APIError_fetch_data
                19:56:58 >> APIError_fetch_data
                19:56:59 >> APIError_fetch_data
                19:57:02 >> APIError_fetch_data
                19:57:11 >> APIError_fetch_data
                19:57:17 >> APIError_fetch_data
                19:57:18 >> initialize_api
                20:01:57 >> APIError_fetch_data
                20:01:58 >> APIError_fetch_data
                20:01:59 >> APIError_fetch_data
                20:02:00 >> APIError_fetch_data
                20:02:04 >> APIError_fetch_data
                20:02:06 >> APIError_fetch_data
                20:02:17 >> initialize_api
                --------🕐--------
                22:18:20 >> ProxyCheckError
========================================
[Proxy: 89.42.81.232. Total erors: 5]
        2024-09-04: 3 errors
                01:31:38 >> APIError_fetch_data
                --------🕐--------
                09:01:08 >> APIError_fetch_data
                09:38:20 >> APIError_fetch_data
        2024-09-03: 2 errors
                22:03:20 >> APIError_fetch_data
                22:03:39 >> APIError_fetch_data
========================================

If you don't want to save code you are able to use this command in package root directory:

PYTHONPATH=. python examples/proxy_errors.py

Dependencies

  • aiohttp
  • backoff

To-do

  • verify and fix types
  • add tests

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

sentry_rest-0.0.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

sentry_rest-0.0.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file sentry_rest-0.0.3.tar.gz.

File metadata

  • Download URL: sentry_rest-0.0.3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.10.2

File hashes

Hashes for sentry_rest-0.0.3.tar.gz
Algorithm Hash digest
SHA256 6fd1593bfc78deb553c5e813ed5390539004290f8b6fc8d1c890545c88bf8858
MD5 036312effe1937d2a4eb60992e7469d2
BLAKE2b-256 0fbcbf665ba8f1d4b0fc846cb4b7024d1992eea7c66fda912d72037a3ca8847e

See more details on using hashes here.

File details

Details for the file sentry_rest-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: sentry_rest-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.10.2

File hashes

Hashes for sentry_rest-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d54e836ad0f7abd05f3d1f7dcb5be52334ca7904d2217bc6e1e3d614c5165cad
MD5 87525d2806767c72824cd73cbb4edecb
BLAKE2b-256 0dc1265eee1e2a1c8fa75da11c8b878ea341cd0439f5d826130aad7154dfd8b6

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