-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Instrument using custom attributes + add Worker tests (#18) * Add tests to the Job class (#19) * Add support for reporting extra fields if configured (#20) * Add Reporter class for Newrelic abstraction and job utility (#21) * Fix integration bugs + add report_raw and flatten dicts (#23) * Patch GH action to work on python 2.7 only * Downgrade pytest, PyYAML, psycopg2-binary to work with python 2.7 * Fix tests and job to be python 2.7 compatible
- Loading branch information
Showing
17 changed files
with
1,077 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Run tests | ||
|
||
on: | ||
push: | ||
branches: [ python2.7 ] | ||
pull_request: | ||
branches: [ python2.7 ] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-20.04 | ||
container: | ||
image: python:2.7.18-buster | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Install dependencies | ||
run: pip install -r requirements-test.txt | ||
- name: Run tests | ||
run: pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
*.pyc | ||
/dist | ||
/build | ||
*.egg-info | ||
*.egg-info | ||
.venv | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
FROM python:2.7-jessie | ||
FROM python:2.7.18-buster | ||
|
||
LABEL maintainer="Hossam Hammady <[email protected]>" | ||
LABEL maintainer="Hossam Hammady <[email protected]>" | ||
|
||
WORKDIR /home | ||
|
||
COPY / /home/ | ||
# install deps first | ||
RUN pip install --upgrade pip | ||
COPY requirements-test.txt /home/ | ||
RUN pip install -r requirements-test.txt | ||
|
||
RUN pip install --upgrade pip && \ | ||
pip install twine && \ | ||
python setup.py sdist bdist_wheel | ||
# copy rest of files | ||
COPY / /home/ | ||
|
||
CMD ["twine", "upload", "dist/*"] | ||
CMD ["pytest"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import json | ||
from contextlib import contextmanager | ||
import newrelic.agent | ||
|
||
|
||
class Reporter(object): | ||
|
||
def __init__(self, attribute_prefix='', logger=None): | ||
self._prefix = attribute_prefix | ||
self._logger = logger | ||
if self._logger: | ||
self._logger.info('Reporter: initializing NewRelic') | ||
newrelic.agent.initialize() | ||
self._newrelic_app = newrelic.agent.register_application() | ||
|
||
def report(self, **attributes): | ||
# flatten attributes | ||
attributes = self._flatten_attributes(attributes) | ||
# format attributes | ||
attributes = self._format_attributes(attributes) | ||
self.report_raw(**attributes) | ||
|
||
def report_raw(self, **attributes): | ||
# report to NewRelic | ||
self._report_newrelic(attributes) | ||
|
||
@contextmanager | ||
def recorder(self, name): | ||
with newrelic.agent.BackgroundTask( | ||
application=self._newrelic_app, | ||
name=name, | ||
group='DelayedJob') as task: | ||
yield task | ||
|
||
def shutdown(self): | ||
newrelic.agent.shutdown_agent() | ||
|
||
def record_exception(self, exc_info): | ||
newrelic.agent.notice_error(error=exc_info) | ||
|
||
def _flatten_attributes(self, attributes): | ||
# flatten nested dict attributes | ||
flattened_attributes = {} | ||
for key, value in attributes.items(): | ||
if type(value) == dict: | ||
for nested_key, nested_value in value.items(): | ||
flattened_attributes[nested_key] = nested_value | ||
else: | ||
flattened_attributes[key] = value | ||
return flattened_attributes | ||
|
||
def _format_attributes(self, attributes): | ||
# prefix then convert all attribute keys to camelCase | ||
# ensure values types are supported or json dump them | ||
return { | ||
self._prefix + self._to_camel_case(key): self._convert_value(value) | ||
for key, value in attributes.items() | ||
if key is not None and value is not None | ||
} | ||
|
||
@staticmethod | ||
def _to_camel_case(string): | ||
return string[0]+string.title()[1:].replace("-","").replace("_","").replace(" ","") | ||
|
||
@staticmethod | ||
def _convert_value(value): | ||
if type(value) not in [str, int, float, bool]: | ||
return json.dumps(value) | ||
return value | ||
|
||
def _report_newrelic(self, attributes): | ||
if self._logger: | ||
self._logger.debug('Reporter: reporting to NewRelic: %s' % attributes) | ||
# convert attributes dict to list of tuples | ||
attributes = list(attributes.items()) | ||
newrelic.agent.add_custom_attributes(attributes) |
Oops, something went wrong.