Skip to content

Commit 4cdff68

Browse files
committedJul 11, 2017
Log significant events when a user lands a revision
1 parent a2f6db5 commit 4cdff68

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-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 landoapi.models.landing import (
@@ -14,23 +15,46 @@
1415
RevisionNotFoundException,
1516
)
1617

18+
logger = logging.getLogger(__name__)
19+
1720

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

‎landoapi/models/landing.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# This Source Code Form is subject to the terms of the Mozilla Public
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
import logging
5+
46
from landoapi.hgexportbuilder import build_patch_for_revision
57
from landoapi.models.storage import db
68
from landoapi.phabricator_client import PhabricatorClient
79
from landoapi.transplant_client import TransplantClient
810

11+
logger = logging.getLogger(__name__)
12+
913
TRANSPLANT_JOB_STARTED = 'started'
1014
TRANSPLANT_JOB_FINISHED = 'finished'
1115

@@ -48,6 +52,7 @@ def create(cls, revision_id, phabricator_api_key=None, save=True):
4852
request_id = trans.land(
4953
'ldap_username@example.com', hgpatch, repo['uri']
5054
)
55+
5156
if not request_id:
5257
raise LandingNotCreatedException
5358

@@ -59,6 +64,14 @@ def create(cls, revision_id, phabricator_api_key=None, save=True):
5964
if save:
6065
landing.save(create=True)
6166

67+
logger.info(
68+
{
69+
'revision': revision_id,
70+
'landing': landing.id,
71+
'msg': 'landing created for revision'
72+
}, 'landing.success'
73+
)
74+
6275
return landing
6376

6477
@classmethod

‎landoapi/transplant_client.py

+37-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,29 @@ def land(self, ldap_username, hgpatch, tree, 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':
70+
'transplant',
71+
'username':
72+
ldap_username,
73+
'msg':
74+
'received an empty response from the transplant service',
75+
}, 'transplant.failure'
76+
)
77+
return None
5378

5479
def _request(self, url, data=None, params=None, method='GET'):
5580
data = data if data else {}
@@ -64,6 +89,16 @@ def _request(self, url, data=None, params=None, method='GET'):
6489
status_code = response.status_code
6590
response = response.json()
6691

92+
logger.info(
93+
{
94+
'code': status_code,
95+
'method': method,
96+
'service': 'transplant',
97+
'url': self.api_url,
98+
'path': url,
99+
}, 'request.summary'
100+
)
101+
67102
if 'error' in response:
68103
exp = TransplantAPIException()
69104
exp.error_code = status_code

0 commit comments

Comments
 (0)
Failed to load comments.