Skip to content

Commit 9e809d2

Browse files
mars-fpurelogiq
authored andcommittedJul 11, 2017
Log significant events when a user lands a revision
1 parent 78d8120 commit 9e809d2

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed
 

‎landoapi/api/landings.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Transplant API
66
See the OpenAPI Specification for this API in the spec/swagger.yml file.
77
"""
8+
import logging
89
from connexion import problem
910
from flask import request
1011
from sqlalchemy.orm.exc import NoResultFound
@@ -13,23 +14,46 @@
1314
TRANSPLANT_JOB_FAILED, TRANSPLANT_JOB_LANDED
1415
)
1516

17+
logger = logging.getLogger(__name__)
18+
1619

1720
def land(data, api_key=None):
1821
""" API endpoint at /revisions/{id}/transplants to land revision. """
1922
# get revision_id from body
2023
revision_id = data['revision_id']
24+
logger.info(
25+
{
26+
'path': request.path,
27+
'method': request.method,
28+
'data': data,
29+
'msg': 'landing requested by user'
30+
}, 'landing.invoke'
31+
)
2132
try:
2233
landing = Landing.create(revision_id, api_key)
2334
except RevisionNotFoundException:
2435
# We could not find a matching revision.
36+
logger.info(
37+
{
38+
'revision': revision_id,
39+
'msg': 'revision not found'
40+
}, 'landing.failure'
41+
)
2542
return problem(
2643
404,
2744
'Revision not found',
2845
'The requested revision does not exist',
2946
type='https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404'
3047
)
31-
except LandingNotCreatedException:
48+
except LandingNotCreatedException as exc:
3249
# We could not find a matching revision.
50+
logger.info(
51+
{
52+
'revision': revision_id,
53+
'exc': exc,
54+
'msg': 'error creating landing',
55+
}, 'landing.error'
56+
)
3357
return problem(
3458
502,
3559
'Landing not created',

‎landoapi/models/landing.py

+12
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
import os
55

6+
import logging
7+
68
from landoapi.hgexportbuilder import build_patch_for_revision
79
from landoapi.models.storage import db
810
from landoapi.phabricator_client import PhabricatorClient
911
from landoapi.transplant_client import TransplantClient
1012

13+
logger = logging.getLogger(__name__)
14+
1115
TRANSPLANT_JOB_STARTING = 'pending'
1216
TRANSPLANT_JOB_STARTED = 'started'
1317
TRANSPLANT_JOB_LANDED = 'landed'
@@ -72,6 +76,14 @@ def create(cls, revision_id, phabricator_api_key=None):
7276
landing.status = TRANSPLANT_JOB_STARTED
7377
landing.save()
7478

79+
logger.info(
80+
{
81+
'revision': revision_id,
82+
'landing': landing.id,
83+
'msg': 'landing created for revision'
84+
}, 'landing.success'
85+
)
86+
7587
return landing
7688

7789
def save(self):

‎landoapi/transplant_client.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
import os
6+
7+
import logging
68
import requests
79
import requests_mock
810

911
from sqlalchemy import text
1012
from landoapi.models.storage import db
1113

14+
logger = logging.getLogger(__name__)
15+
1216

1317
class TransplantClient:
1418
""" A class to interface with Transplant's API. """
@@ -48,8 +52,26 @@ def land(self, ldap_username, hgpatch, tree, pingback, request):
4852
}
4953
)
5054

51-
# Transplant API is responding with a created request_id of the job
52-
return result.get('request_id') if result else None
55+
if result:
56+
logger.info(
57+
{
58+
'service': 'transplant',
59+
'username': ldap_username,
60+
'msg': 'patch sent to transplant service',
61+
}, 'transplant.success'
62+
)
63+
return result.get('request_id')
64+
else:
65+
# Transplant API responded with no data, indicating an error of
66+
# some sort.
67+
logger.info(
68+
{
69+
'service': 'transplant',
70+
'username': ldap_username,
71+
'msg': 'received an empty response from the transplant service',
72+
}, 'transplant.failure'
73+
) # yapf: disable
74+
return None
5375

5476
def _request(self, url, data=None, params=None, method='GET'):
5577
data = data if data else {}
@@ -64,6 +86,16 @@ def _request(self, url, data=None, params=None, method='GET'):
6486
status_code = response.status_code
6587
response = response.json()
6688

89+
logger.info(
90+
{
91+
'code': status_code,
92+
'method': method,
93+
'service': 'transplant',
94+
'url': self.api_url,
95+
'path': url,
96+
}, 'request.summary'
97+
)
98+
6799
if 'error' in response:
68100
exp = TransplantAPIException()
69101
exp.error_code = status_code

0 commit comments

Comments
 (0)
Failed to load comments.