Esempio n. 1
0
def get_metrics_reporting_registry(
    process_registry: CollectorRegistry,
) -> CollectorRegistry:
    """
    Get the metrics registry for reporting metrics.

    If we're running under gunicorn, then each worker has its own process and
    its own process collector. For reporting, we need a fresh registry with a
    multiprocess collector that points to the metrics folder (created empty at
    startup, with a database file for each process and metric type). It will
    use the databases in this folder to generate combined metrics across the
    processes, and will not double-count the reporting process's metrics.

    If we're not running under gunicorn, then return the passed per-process
    registry, which is the only metrics registry. In the single-process case,
    We could use the default prometheus_client.REGISTRY, but it makes tests
    easier to write if it is possible to replace the registry with a fresh one.
    """
    try:
        settings = config.Settings()
        prometheus_multiproc_dir = settings.prometheus_multiproc_dir
    except ValidationError:
        prometheus_multiproc_dir = None

    if prometheus_multiproc_dir:
        registry = CollectorRegistry()
        MultiProcessCollector(registry, path=prometheus_multiproc_dir)
        return registry
    return process_registry
Esempio n. 2
0
def init_sentry():
    """
    Initialize Sentry integrations for capturing exceptions.

    Because FastAPI uses threads to integrate async and sync code, this needs
    to be called at module import.

    sentry_sdk.init needs a data source name (DSN) URL, which it reads from the
    environment variable SENTRY_DSN.
    """
    try:
        settings = config.Settings()
        sentry_debug = settings.sentry_debug
    except ValidationError:
        sentry_debug = False

    # pylint: disable=abstract-class-instantiated
    sentry_sdk.init(
        release=get_version().get("commit", None),
        debug=sentry_debug,
        send_default_pii=False,
    )
    ignore_logger("uvicorn.error")
    ignore_logger("ctms.web")
Esempio n. 3
0
def ingest_object(db_session, obj):
    """Ingest a Stripe object."""

    try:
        ingest_stripe_object(db_session, obj)
    except StripeIngestUnknownObjectError:
        logger.info("Skipping %s %s", obj["object"], obj["id"])
    else:
        logger.info("Ingested %s %s", obj["object"], obj["id"])
        db_session.commit()


def get_parser():
    the_parser = argparse.ArgumentParser(
        description="Load Stripe data from JSON")
    the_parser.add_argument("filenames",
                            metavar="data.json",
                            nargs="+",
                            help="Stripe data to load")
    return the_parser


if __name__ == "__main__":
    config_settings = config.Settings()
    engine, session_factory = get_db_engine(config_settings)
    session = session_factory()
    parser = get_parser()
    args = parser.parse_args()
    logging.basicConfig(level=logging.INFO)
    main(session, args.filenames)