def test_initiate_apm_client_env_not_present(self, monkeypatch): monkeypatch.setitem(Utility.environment["elasticsearch"], 'enable', True) monkeypatch.setitem(Utility.environment["elasticsearch"], 'env_type', None) assert Utility.initiate_apm_client() is None
def test_initiate_apm_client_service_url_not_present(self, monkeypatch): monkeypatch.setitem(Utility.environment["elasticsearch"], 'enable', True) monkeypatch.setitem(Utility.environment["elasticsearch"], 'apm_server_url', None) monkeypatch.setitem(Utility.environment["elasticsearch"], 'service_name', None) assert not Utility.initiate_apm_client()
def start_training(bot: str, user: str, token: str = None, reload=True): """ prevents training of the bot, if the training session is in progress otherwise start training :param reload: whether to reload model in the cache :param bot: bot id :param token: JWT token for remote model reload :param user: user id :return: model path """ exception = None model_file = None training_status = None if Utility.environment.get('model') and Utility.environment['model'][ 'train'].get('event_url'): Utility.train_model_event(bot, user, token) else: try: apm_client = Utility.initiate_apm_client() if apm_client: elasticapm.instrument() apm_client.begin_transaction(transaction_type="script") model_file = train_model_for_bot(bot) training_status = MODEL_TRAINING_STATUS.DONE.value agent_url = Utility.environment['model']['train'].get('agent_url') if agent_url: if token: Utility.http_request( 'get', urljoin(agent_url, "/api/bot/model/reload"), token, user) else: if reload: AgentProcessor.reload(bot) except Exception as e: logging.exception(e) training_status = MODEL_TRAINING_STATUS.FAIL.value exception = str(e) finally: if apm_client: apm_client.end_transaction(name=__name__, result="success") ModelProcessor.set_training_status( bot=bot, user=user, status=training_status, model_path=model_file, exception=exception, ) return model_file
def test_initiate_apm_client_with_url_present(self, monkeypatch): monkeypatch.setitem(Utility.environment["elasticsearch"], 'enable', True) monkeypatch.setitem(Utility.environment["elasticsearch"], 'service_name', "kairon") monkeypatch.setitem(Utility.environment["elasticsearch"], 'apm_server_url', "http://localhost:8082") client = Utility.initiate_apm_client() config = client.config._config assert config.server_url == "http://localhost:8082" assert config.service_name == "kairon" assert config.environment == "development" assert config.secret_token is None monkeypatch.setitem(Utility.environment["elasticsearch"], 'secret_token', "12345") client = Utility.initiate_apm_client() config = client.config._config assert config.server_url == "http://localhost:8082" assert config.service_name == "kairon" assert config.environment == "development" assert config.secret_token == "12345"
from kairon.api.app.routers import auth, bot, augment, history, user, account from kairon.utils import Utility logging.basicConfig(level="DEBUG") secure_headers = SecureHeaders() app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], expose_headers=["content-disposition"], ) apm_client = Utility.initiate_apm_client() if apm_client: app.add_middleware(ElasticAPM, client=apm_client) @app.middleware("http") async def add_secure_headers(request: Request, call_next): """add security headers""" response = await call_next(request) secure_headers.starlette(response) return response @app.middleware("http") async def log_requests(request: Request, call_next): """logging request calls"""
def test_initiate_apm_client_enabled(self, monkeypatch): monkeypatch.setitem(Utility.environment["elasticsearch"], 'enable', True) assert not Utility.initiate_apm_client()
def test_initiate_apm_client_disabled(self): assert not Utility.initiate_apm_client()