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

Use mozlog for application logging #21

Merged
merged 3 commits into from
Jul 11, 2017
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Log significant events when a user lands a revision
  • Loading branch information
mars-f committed Jul 11, 2017
commit 73475d0131c872979a1e441a9feafd0a58b442ce
26 changes: 25 additions & 1 deletion landoapi/api/landings.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
Transplant API
See the OpenAPI Specification for this API in the spec/swagger.yml file.
"""
import logging
from connexion import problem
from flask import request
from sqlalchemy.orm.exc import NoResultFound
@@ -13,23 +14,46 @@
TRANSPLANT_JOB_FAILED, TRANSPLANT_JOB_LANDED
)

logger = logging.getLogger(__name__)


def land(data, api_key=None):
""" API endpoint at /revisions/{id}/transplants to land revision. """
# get revision_id from body
revision_id = data['revision_id']
logger.info(
{
'path': request.path,
'method': request.method,
'data': data,
'msg': 'landing requested by user'
}, 'landing.invoke'
)
try:
landing = Landing.create(revision_id, api_key)
except RevisionNotFoundException:
# We could not find a matching revision.
logger.info(
{
'revision': revision_id,
'msg': 'revision not found'
}, 'landing.failure'
)
return problem(
404,
'Revision not found',
'The requested revision does not exist',
type='https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404'
)
except LandingNotCreatedException:
except LandingNotCreatedException as exc:
# We could not find a matching revision.
logger.info(
{
'revision': revision_id,
'exc': exc,
'msg': 'error creating landing',
}, 'landing.error'
)
return problem(
502,
'Landing not created',
12 changes: 12 additions & 0 deletions landoapi/models/landing.py
Original file line number Diff line number Diff line change
@@ -3,11 +3,15 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os

import logging

from landoapi.hgexportbuilder import build_patch_for_revision
from landoapi.models.storage import db
from landoapi.phabricator_client import PhabricatorClient
from landoapi.transplant_client import TransplantClient

logger = logging.getLogger(__name__)

TRANSPLANT_JOB_STARTING = 'pending'
TRANSPLANT_JOB_STARTED = 'started'
TRANSPLANT_JOB_LANDED = 'landed'
@@ -72,6 +76,14 @@ def create(cls, revision_id, phabricator_api_key=None):
landing.status = TRANSPLANT_JOB_STARTED
landing.save()

logger.info(
{
'revision': revision_id,
'landing': landing.id,
'msg': 'landing created for revision'
}, 'landing.success'
)

return landing

def save(self):
36 changes: 34 additions & 2 deletions landoapi/transplant_client.py
Original file line number Diff line number Diff line change
@@ -3,12 +3,16 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os

import logging
import requests
import requests_mock

from sqlalchemy import text
from landoapi.models.storage import db

logger = logging.getLogger(__name__)


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

# Transplant API is responding with a created request_id of the job
return result.get('request_id') if result else None
if result:
logger.info(
{
'service': 'transplant',
'username': ldap_username,
'msg': 'patch sent to transplant service',
}, 'transplant.success'
)
return result.get('request_id')
else:
# Transplant API responded with no data, indicating an error of
# some sort.
logger.info(
{
'service': 'transplant',
'username': ldap_username,
'msg': 'received an empty response from the transplant service',
}, 'transplant.failure'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at the end of this line you can add a comment like: # yapf: disable to prevent the linter from formating this hash. I don't think this is good style just because the linter says so, it's clearly a bug and we know better :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

) # yapf: disable
return None

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

logger.info(
{
'code': status_code,
'method': method,
'service': 'transplant',
'url': self.api_url,
'path': url,
}, 'request.summary'
)

if 'error' in response:
exp = TransplantAPIException()
exp.error_code = status_code