Example #1
0
    def wrapper(*args, **kwargs):
        background = False
        if not kwargs.get("db_session"):
            if not kwargs.get("organization_slug"):
                raise Exception("If not db_session is supplied organization slug must be provided.")

            schema_engine = engine.execution_options(
                schema_translate_map={
                    None: f"dispatch_organization_{kwargs['organization_slug']}",
                }
            )
            db_session = sessionmaker(bind=schema_engine)

            background = True
            kwargs["db_session"] = db_session()
        try:
            metrics_provider.counter("function.call.counter", tags={"function": fullname(func)})
            start = time.perf_counter()
            result = func(*args, **kwargs)
            elapsed_time = time.perf_counter() - start
            metrics_provider.timer(
                "function.elapsed.time", value=elapsed_time, tags={"function": fullname(func)}
            )
            return result
        except Exception as e:
            log.exception(e)
        finally:
            if background:
                kwargs["db_session"].close()
Example #2
0
 def wrapper(*args, **kwargs):
     start = time.perf_counter()
     result = func(*args, **kwargs)
     elapsed_time = time.perf_counter() - start
     metrics_provider.timer("function.elapsed.time",
                            value=elapsed_time,
                            tags={"function": fullname(func)})
     return result
Example #3
0
    def wrapper(*args, **kwargs):
        background = False

        if not kwargs.get("db_session"):
            db_session = SessionLocal()
            background = True
            kwargs["db_session"] = db_session

        if not kwargs.get("slack_client"):
            slack_client = dispatch_slack_service.create_slack_client()
            kwargs["slack_client"] = slack_client

        try:
            metrics_provider.counter("function.call.counter",
                                     tags={
                                         "function": fullname(func),
                                         "slack": True
                                     })
            start = time.perf_counter()
            result = func(*args, **kwargs)
            elapsed_time = time.perf_counter() - start
            metrics_provider.timer(
                "function.elapsed.time",
                value=elapsed_time,
                tags={
                    "function": fullname(func),
                    "slack": True
                },
            )
            return result
        except Exception as e:
            # we generate our own guid for now, maybe slack provides us something we can use?
            slack_interaction_guid = str(uuid.uuid4())
            log.exception(
                e, extra=dict(slack_interaction_guid=slack_interaction_guid))

            # notify the user the interaction failed
            user_id = args[0]
            channel_id = args[2]
            message = f"Sorry, we've run into an unexpected error. For help, please reach out to the incident commander and provide them with the following token: {slack_interaction_guid}."
            dispatch_slack_service.send_ephemeral_message(
                kwargs["slack_client"], channel_id, user_id, message)

        finally:
            if background:
                kwargs["db_session"].close()
Example #4
0
    def wrapper(*args, **kwargs):
        db_session = SessionLocal()
        kwargs["db_session"] = db_session
        try:
            metrics_provider.counter(f"function.call.counter", tags={"function": fullname(func)})
            start = time.perf_counter()
            result = func(*args, **kwargs)
            elapsed_time = time.perf_counter() - start
            metrics_provider.timer(
                f"function.elapsed.time", value=elapsed_time, tags={"function": fullname(func)}
            )
            return result
        except Exception as e:
            import traceback

            configure_extensions()
            print(traceback.format_exc())
            sentry_sdk.capture_exception(e)
        finally:
            db_session.close()
Example #5
0
 def wrapper(*args, **kwargs):
     background = False
     if not kwargs.get("db_session"):
         db_session = SessionLocal()
         background = True
         kwargs["db_session"] = db_session
     try:
         metrics_provider.counter("function.call.counter",
                                  tags={"function": fullname(func)})
         start = time.perf_counter()
         result = func(*args, **kwargs)
         elapsed_time = time.perf_counter() - start
         metrics_provider.timer("function.elapsed.time",
                                value=elapsed_time,
                                tags={"function": fullname(func)})
         return result
     except Exception as e:
         log.exception(e)
     finally:
         if background:
             kwargs["db_session"].close()
Example #6
0
    def wrapper(*args, **kwargs):
        db_session = SessionLocal()
        metrics_provider.counter("function.call.counter", tags={"function": fullname(func)})
        start = time.perf_counter()

        # iterate for all schema
        for organization in organization_service.get_all(db_session=db_session):
            schema_engine = engine.execution_options(
                schema_translate_map={None: f"dispatch_organization_{organization.slug}"}
            )
            schema_session = sessionmaker(bind=schema_engine)()
            kwargs["db_session"] = schema_session
            for project in project_service.get_all(db_session=schema_session):
                kwargs["project"] = project
                try:
                    func(*args, **kwargs)
                except Exception as e:
                    log.exception(e)
            schema_session.close()
        elapsed_time = time.perf_counter() - start
        metrics_provider.timer(
            "function.elapsed.time", value=elapsed_time, tags={"function": fullname(func)}
        )
        db_session.close()
Example #7
0
    def wrapper(*args, **kwargs):
        background = False

        metrics_provider.counter("function.call.counter",
                                 tags={
                                     "function": fullname(func),
                                     "slack": True
                                 })

        channel_id = kwargs["channel_id"]
        user_id = kwargs["user_id"]
        if not kwargs.get("db_session"):

            # slug passed directly is prefered over just having a channel_id
            organization_slug = kwargs.pop("organization_slug", None)
            if not organization_slug:
                scoped_db_session = get_organization_scope_from_channel_id(
                    channel_id=channel_id)
                if not scoped_db_session:
                    scoped_db_session = get_default_organization_scope()
            else:
                scoped_db_session = get_organization_scope_from_slug(
                    organization_slug)

            background = True
            kwargs["db_session"] = scoped_db_session

        if not kwargs.get("slack_client"):
            slack_client = dispatch_slack_service.create_slack_client(
                config=kwargs["config"])
            kwargs["slack_client"] = slack_client

        try:
            start = time.perf_counter()
            result = func(*args, **kwargs)
            elapsed_time = time.perf_counter() - start
            metrics_provider.timer(
                "function.elapsed.time",
                value=elapsed_time,
                tags={
                    "function": fullname(func),
                    "slack": True
                },
            )
            return result
        except ValidationError as e:
            log.exception(e)
            message = f"Command Error: {e.errors()[0]['msg']}"

            dispatch_slack_service.send_ephemeral_message(
                client=kwargs["slack_client"],
                conversation_id=channel_id,
                user_id=user_id,
                text=message,
            )

        except Exception as e:
            # we generate our own guid for now, maybe slack provides us something we can use?
            slack_interaction_guid = str(uuid.uuid4())
            log.exception(
                e, extra=dict(slack_interaction_guid=slack_interaction_guid))

            # we notify the user that the interaction failed
            message = f"""Sorry, we've run into an unexpected error. For help please reach out to your Dispatch admins \
and provide them with the following token: '{slack_interaction_guid}'."""

            dispatch_slack_service.send_ephemeral_message(
                client=kwargs["slack_client"],
                conversation_id=channel_id,
                user_id=user_id,
                text=message,
            )
        finally:
            if background:
                kwargs["db_session"].close()
Example #8
0
    def wrapper(*args, **kwargs):
        background = False

        if not kwargs.get("db_session"):
            channel_id = args[2]

            # slug passed directly is prefered over just having a channel_id
            organization_slug = kwargs.pop("organization_slug", None)
            if not organization_slug:
                scoped_db_session = get_organization_from_channel_id(
                    channel_id=channel_id)
                if not scoped_db_session:
                    raise Exception(
                        f"Could not find an organization associated with channel_id. ChannelId: {channel_id}"
                    )
            else:
                schema_engine = engine.execution_options(
                    schema_translate_map={
                        None: f"dispatch_organization_{organization_slug}",
                    })

                scoped_db_session = sessionmaker(bind=schema_engine)()

            background = True
            kwargs["db_session"] = scoped_db_session

        if not kwargs.get("slack_client"):
            slack_client = dispatch_slack_service.create_slack_client()
            kwargs["slack_client"] = slack_client

        try:
            metrics_provider.counter("function.call.counter",
                                     tags={
                                         "function": fullname(func),
                                         "slack": True
                                     })
            start = time.perf_counter()
            result = func(*args, **kwargs)
            elapsed_time = time.perf_counter() - start
            metrics_provider.timer(
                "function.elapsed.time",
                value=elapsed_time,
                tags={
                    "function": fullname(func),
                    "slack": True
                },
            )
            return result
        except Exception as e:
            # we generate our own guid for now, maybe slack provides us something we can use?
            slack_interaction_guid = str(uuid.uuid4())
            log.exception(
                e, extra=dict(slack_interaction_guid=slack_interaction_guid))

            user_id = args[0]
            channel_id = args[2]

            conversation = conversation_service.get_by_channel_id_ignoring_channel_type(
                db_session=kwargs["db_session"], channel_id=channel_id)

            # we notify the user that the interaction failed
            message = (
                f"Sorry, we've run into an unexpected error. For help, please reach out to {conversation.incident.commander.individual.name}",
                f" and provide them with the following token: {slack_interaction_guid}.",
            )
            if conversation.incident.status != IncidentStatus.closed:
                dispatch_slack_service.send_ephemeral_message(
                    kwargs["slack_client"], channel_id, user_id, message)
            else:
                dispatch_slack_service.send_message(
                    client=kwargs["slack_client"],
                    conversation_id=user_id,
                    text=message,
                )
        finally:
            if background:
                kwargs["db_session"].close()