Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App logs deep link #1329

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ help: Makefile
@echo

build-static:
rm -rf static/
make build-css
make build-js
python3 manage.py collectstatic
Expand Down
14 changes: 14 additions & 0 deletions controlpanel/api/models/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db import models
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.urls import reverse
from django_extensions.db.fields import AutoSlugField
from django_extensions.db.models import TimeStampedModel
Expand Down Expand Up @@ -309,6 +310,19 @@ def auth_settings(self):
def get_absolute_url(self):
return reverse("manage-app", kwargs={"pk": self.pk})

def get_logs_url(self, container="webapp", env="dev"):
"""
Builds a deep link to Kibana logs for the given container and environment.
"""
return render_to_string(
"includes/app-kibana-logs-url.html",
context={
"namespace": self.namespace,
"container_name": container,
"env": env,
},
)


class AddCustomerError(Exception):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

{% macro app_deployment_settings(app, env_name, app_domain, deployment_setting, request, csrf_input) %}
{% set app_full_url = "https://" + app.app_url_name(env_name)+ "." + app_domain %}
<h3 class="govuk-heading-s">Namespace</h2>
<p class="govuk-body">{{ app.namespace }}-{{ env_name }}</p>
<p><b>If the app has been successfully deployed onto Cloud Platform, it can be accessed here:</b><a href="{{ app_full_url }}">{{ app_full_url }}</a></p>
<hr style="height:5px;border:none;color:#333;background-color:#333;"/>
<section class="{{ env_name }}-settings-panel">
Expand Down Expand Up @@ -127,4 +129,4 @@ <h3 class="govuk-heading-s" >App environment variables</h3>
</table>
</section>

{% endmacro %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://kibana.cloud-platform.service.justice.gov.uk/_plugin/kibana/app/discover#/?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-24h,to:now))&_a=(columns:!(log),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'167701b0-f8c0-11ec-b95c-1d65c3682287',key:kubernetes.namespace_name,negate:!f,params:(query:{{ namespace }}-{{ env }}),type:phrase),query:(match_phrase:(kubernetes.namespace_name:{{ namespace }}-{{ env }}))),('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'167701b0-f8c0-11ec-b95c-1d65c3682287',key:kubernetes.container_name,negate:!f,params:(query:{{ container_name }}),type:phrase),query:(match_phrase:(kubernetes.container_name:{{ container_name }})))),index:'167701b0-f8c0-11ec-b95c-1d65c3682287',interval:auto,query:(language:kuery,query:''),sort:!())
5 changes: 4 additions & 1 deletion controlpanel/frontend/jinja2/webapp-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ <h2 class="govuk-heading-m">Source Code Repository</h2>

<h2 class="govuk-heading-m">App logs</h2>
<p class="govuk-body">
<a href="{{ settings.KIBANA_BASE_URL }}">{{ settings.KIBANA_BASE_URL }}</a>
Logs are viewable in Kibana. Click the link below to view application logs for each environment:
{% for env, url in app_log_urls.items() %}
<p><a href="{{ url }}">{{ env.title() }}</a>
{% endfor %}
</p>

<h2 class="govuk-heading-m">App resources usage dashboard</h2>
Expand Down
4 changes: 4 additions & 0 deletions controlpanel/frontend/views/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def get_context_data(self, **kwargs):
context["repo_access_error_msg"] = access_repo_error_msg
context["github_settings_access_error_msg"] = github_settings_access_error_msg

context["app_log_urls"] = {}
for env_name in context["deployments_settings"].keys():
context["app_log_urls"].update({env_name: app.get_logs_url(env=env_name)})

return context


Expand Down
18 changes: 18 additions & 0 deletions tests/api/models/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,21 @@ def test_iam_role_arn():
def test_app_url_name(namespace, env, expected):
app = App(namespace=namespace)
assert app.app_url_name(env_name=env) == expected


@pytest.mark.parametrize("env", ["dev", "prod"], ids=["dev", "prod"])
def test_get_logs_url(env):
expected = (
"https://kibana.cloud-platform.service.justice.gov.uk/_plugin/kibana/app/discover#/?_g=("
"filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-24h,to:now))&_a=(columns:!"
"(log),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:'"
"167701b0-f8c0-11ec-b95c-1d65c3682287',key:kubernetes.namespace_name,negate:!f,params:("
f"query:example-namespace-{env}),type:phrase),query:(match_phrase:(kubernetes."
f"namespace_name:example-namespace-{env}))),('$state':(store:appState),meta:(alias:!n"
",disabled:!f,index:'167701b0-f8c0-11ec-b95c-1d65c3682287',key:kubernetes.container_name,"
"negate:!f,params:(query:webapp),type:phrase),query:(match_phrase:(kubernetes."
"container_name:webapp)))),index:'167701b0-f8c0-11ec-b95c-1d65c3682287',interval:auto,query"
":(language:kuery,query:''),sort:!())"
)
app = App(namespace="example-namespace")
assert app.get_logs_url(env=env) == expected
Loading