diff --git a/engine/apps/api/urls.py b/engine/apps/api/urls.py index 1eb9adf2..30c3470b 100644 --- a/engine/apps/api/urls.py +++ b/engine/apps/api/urls.py @@ -39,6 +39,7 @@ from .views.slack_team_settings import ( from .views.subscription import SubscriptionView from .views.team import TeamViewSet from .views.telegram_channels import TelegramChannelViewSet +from .views.test_insight_logs import TestInsightLogsAPIView from .views.user import CurrentUserView, UserView from .views.user_group import UserGroupViewSet @@ -108,6 +109,7 @@ urlpatterns = [ "preview_template_options", PreviewTemplateOptionsView.as_view(), name="preview_template_options" ), optional_slash_path("route_regex_debugger", RouteRegexDebuggerView.as_view(), name="route_regex_debugger"), + optional_slash_path("insight_logs_test", TestInsightLogsAPIView.as_view(), name="insight-logs-test"), ] urlpatterns += [ diff --git a/engine/apps/api/views/test_insight_logs.py b/engine/apps/api/views/test_insight_logs.py new file mode 100644 index 00000000..da6b4d17 --- /dev/null +++ b/engine/apps/api/views/test_insight_logs.py @@ -0,0 +1,32 @@ +import logging + +from django.apps import apps +from rest_framework.response import Response +from rest_framework.views import APIView + +from apps.auth_token.auth import PluginAuthentication + +insight_logger = logging.getLogger("insight_logger") + + +class TestInsightLogsAPIView(APIView): + """ + TestInsightLogsAPIView is used to test insight-logs infra setup. + It will be removed once proper insight-logs will be instrumented. + """ + + authentication_classes = (PluginAuthentication,) + + def post(self, request): + DynamicSetting = apps.get_model("base", "DynamicSetting") + org_id_to_enable_insight_logs, _ = DynamicSetting.objects.get_or_create( + name="org_id_to_enable_insight_logs", + defaults={"json_value": []}, + ) + org = self.request.user.organization + insight_logs_enabled = org.id in org_id_to_enable_insight_logs.json_value + if insight_logs_enabled: + message = request.data.get("message", "hello world") + insight_logger.info(f"tenant_id={self.request.user.organization.stack_id} message={message}") + return Response() + return Response(status=418) diff --git a/engine/settings/base.py b/engine/settings/base.py index 515d1317..7e3ef38d 100644 --- a/engine/settings/base.py +++ b/engine/settings/base.py @@ -154,6 +154,7 @@ LOGGING = { "filters": {"request_id": {"()": "log_request_id.filters.RequestIDFilter"}}, "formatters": { "standard": {"format": "source=engine:app google_trace_id=%(request_id)s logger=%(name)s %(message)s"}, + "insight_logger": {"format": "insight_logs=true logger=%(name)s %(message)s"}, }, "handlers": { "console": { @@ -161,8 +162,17 @@ LOGGING = { "filters": ["request_id"], "formatter": "standard", }, + "insight_logger": { + "class": "logging.StreamHandler", + "formatter": "insight_logger", + }, }, "loggers": { + "insight_logger": { + "handlers": ["insight_logger"], + "level": "INFO", + "propagate": False, + }, "": { "handlers": ["console"], "level": "INFO",