Skip to content

Log HTTP errors responses of Jira client #503

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

Merged
merged 7 commits into from
May 16, 2023
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
26 changes: 24 additions & 2 deletions jbi/services/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from functools import lru_cache
from typing import TYPE_CHECKING, Any

import requests
from atlassian import Jira
from atlassian import errors as atlassian_errors
from requests import exceptions as requests_exceptions
Expand Down Expand Up @@ -39,10 +40,31 @@


class JiraClient(Jira):
"""Adapted Atlassian Jira client that wraps methods in our instrumentation
decorator.
"""Adapted Atlassian Jira client that logs errors and wraps methods
in our instrumentation decorator.
"""

def raise_for_status(self, *args, **kwargs):
"""Catch and log HTTP errors responses of the Jira client.

Without this the actual requests and responses are not exposed when an error
occurs, which makes troubleshooting tedious.
"""
try:
return super().raise_for_status(*args, **kwargs)
except requests.HTTPError as exc:
request = exc.request
response = exc.response
logger.error(
"HTTP: %s %s -> %s %s",
request.method,
request.path_url,
response.status_code,
response.reason,
extra={"body": response.text},
)
raise

get_server_info = instrumented_method(Jira.get_server_info)
get_permissions = instrumented_method(Jira.get_permissions)
get_project_components = instrumented_method(Jira.get_project_components)
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/services/test_jira.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import logging

import pytest
import requests
import responses
from requests.exceptions import ConnectionError

Expand Down Expand Up @@ -52,6 +54,39 @@ def test_jira_create_issue_is_instrumented(
mocked_statsd.timer.assert_called_with("jbi.jira.methods.create_issue.timer")


@pytest.mark.no_mocked_jira
def test_jira_calls_log_http_errors(mocked_responses, context_create_example, caplog):
url = f"{get_settings().jira_base_url}rest/api/2/project/{context_create_example.jira.project}/components"
mocked_responses.add(
responses.GET,
url,
status=404,
json={
"errorMessages": ["No project could be found with key 'X'."],
"errors": {},
},
)

with caplog.at_level(logging.ERROR):
with pytest.raises(requests.HTTPError):
jira.create_jira_issue(
context_create_example,
"Description",
sync_whiteboard_labels=False,
components=["Remote Settings"],
)

log_messages = [log.msg % log.args for log in caplog.records]
idx = log_messages.index(
"HTTP: GET /rest/api/2/project/JBI/components -> 404 Not Found"
)
log_record = caplog.records[idx]
assert (
log_record.body
== '{"errorMessages": ["No project could be found with key \'X\'."], "errors": {}}'
)


@pytest.mark.no_mocked_jira
def test_create_issue_with_components(mocked_responses, context_create_example):
url = f"{get_settings().jira_base_url}rest/api/2/project/{context_create_example.jira.project}/components"
Expand Down