Merge pull request #285 from grafana/dummy_insight_logs

Test view for the insight logs
This commit is contained in:
Innokentii Konstantinov 2022-07-25 13:41:34 +04:00 committed by GitHub
commit 0e8e6b2eb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View file

@ -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 += [

View file

@ -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)

View file

@ -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",