Skip to content

Commit 3dcb5bf

Browse files
authored
Merge pull request #1 from ustudio/initial-implementation
Make standard kubernetes logging configuration a library
2 parents de1fd77 + 6d4595f commit 3dcb5bf

File tree

8 files changed

+165
-0
lines changed

8 files changed

+165
-0
lines changed

circle.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
machine:
2+
post:
3+
- pyenv global 2.7.11 3.5.2
4+
dependencies:
5+
override:
6+
- pip install tox
7+
test:
8+
override:
9+
- tox
10+
deployment:
11+
release:
12+
tag: /v[0-9]+(\.[0-9]+)*/
13+
owner: ustudio
14+
commands:
15+
- ./publish_to_pypi.sh

publish_to_pypi.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
is_package_installed() {
6+
python -c "import $1";
7+
}
8+
9+
die() {
10+
echo "$1. Deployment failed.";
11+
12+
exit 2;
13+
}
14+
15+
16+
[ -n "$PYPI_USERNAME" ] || die '$PYPI_USERNAME is required';
17+
18+
[ -n "$PYPI_PASSWORD" ] || die '$PYPI_PASSWORD is required';
19+
20+
if ! is_package_installed twine; then
21+
echo 'Installing twine';
22+
pip install twine || die 'Twine failed to install';
23+
fi
24+
25+
python setup.py sdist || die 'setup.py sdist failed'
26+
27+
twine upload --username $PYPI_USERNAME --password $PYPI_PASSWORD dist/* || die 'Twine upload failed';

setup.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
try:
2+
from setuptools import setup
3+
except ImportError:
4+
from distutils.core import setup
5+
6+
7+
install_requires = [
8+
"datadog-logger",
9+
"kubernetes-downward-api",
10+
"datadog"
11+
]
12+
13+
setup(name="ustack-logging",
14+
version="0.1.0",
15+
description="Default logging configuration for uStack style Python applications.",
16+
url="https://github.com/ustudio/ustack-logging",
17+
packages=["ustack_logging"],
18+
install_requires=install_requires)

tests/__init__.py

Whitespace-only changes.

tests/test_ustack_logging.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import logging
2+
import unittest
3+
4+
try:
5+
from unittest import mock
6+
except ImportError:
7+
import mock
8+
9+
from ustack_logging.logging_configuration import configure_logging
10+
11+
12+
class TestLogging(unittest.TestCase):
13+
@mock.patch("ustack_logging.logging_configuration.log_error_events", autospec=True)
14+
@mock.patch("kubernetes_downward_api.parse", autospec=True)
15+
@mock.patch("datadog.initialize", autospec=True)
16+
@mock.patch("logging.basicConfig", autospec=True)
17+
def test_configures_logging_format_and_logs_errors_to_datadog(
18+
self, mock_log_config, mock_dd_init, mock_k8s_parse, mock_log_errors):
19+
mock_k8s_parse.return_value = {
20+
"namespace": "dev",
21+
"labels": {
22+
"app": "my_app",
23+
"role": "app"
24+
}
25+
}
26+
27+
configure_logging({
28+
"DATADOG_API_KEY": "dd-api-key",
29+
"DATADOG_APP_KEY": "dd-app-key"
30+
})
31+
32+
mock_log_config.assert_called_once_with(
33+
format="%(asctime)s %(levelname)s:%(module)s:%(message)s",
34+
datefmt="%Y-%m-%d %H:%M:%S%z", level=logging.INFO)
35+
36+
mock_dd_init.assert_called_once_with(api_key="dd-api-key", app_key="dd-app-key")
37+
38+
mock_k8s_parse.assert_called_once_with(["/etc/podinfo"])
39+
mock_log_errors.assert_called_once_with(
40+
tags=["environment:dev", "service:my_app", "role:app"])
41+
42+
@mock.patch("ustack_logging.logging_configuration.log_error_events", autospec=True)
43+
@mock.patch("kubernetes_downward_api.parse", autospec=True)
44+
@mock.patch("datadog.initialize", autospec=True)
45+
@mock.patch("logging.basicConfig", autospec=True)
46+
def test_ignores_errors_from_datadog_initialization(
47+
self, mock_log_config, mock_dd_init, mock_k8s_parse, mock_log_errors):
48+
mock_k8s_parse.return_value = {
49+
"namespace": "dev",
50+
"labels": {
51+
"app": "my_app",
52+
"role": "app"
53+
}
54+
}
55+
56+
mock_dd_init.side_effect = RuntimeError
57+
58+
configure_logging({
59+
"DATADOG_API_KEY": "dd-api-key",
60+
"DATADOG_APP_KEY": "dd-app-key"
61+
})
62+
63+
mock_log_config.assert_called_once_with(
64+
format="%(asctime)s %(levelname)s:%(module)s:%(message)s",
65+
datefmt="%Y-%m-%d %H:%M:%S%z", level=logging.INFO)

tox.ini

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tox (http://tox.testrun.org/) is a tool for running tests
2+
# in multiple virtualenvs. This configuration file will run the
3+
# test suite on all supported python versions. To use it, "pip install tox"
4+
# and then run "tox" from this directory.
5+
6+
[tox]
7+
envlist = py27, py35
8+
9+
[testenv]
10+
commands = nosetests
11+
deps =
12+
nose
13+
14+
[testenv:py27]
15+
deps =
16+
nose
17+
mock

ustack_logging/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import datadog
2+
from datadog_logger import log_error_events
3+
import kubernetes_downward_api
4+
import logging
5+
6+
7+
def configure_logging(environ):
8+
logging.basicConfig(
9+
format="%(asctime)s %(levelname)s:%(module)s:%(message)s",
10+
datefmt="%Y-%m-%d %H:%M:%S%z", level=logging.INFO)
11+
12+
try:
13+
datadog.initialize(api_key=environ["DATADOG_API_KEY"], app_key=environ["DATADOG_APP_KEY"])
14+
15+
podinfo = kubernetes_downward_api.parse(["/etc/podinfo"])
16+
17+
log_error_events(tags=[
18+
"environment:{0}".format(podinfo["namespace"]),
19+
"service:{0}".format(podinfo["labels"]["app"]),
20+
"role:{0}".format(podinfo["labels"]["role"])
21+
])
22+
except:
23+
logging.warning("Could not initialize DataDog error logging, with error:", exc_info=True)

0 commit comments

Comments
 (0)