Skip to content

Commit 236599f

Browse files
authored
Merge pull request #24 from redhat-aqe/actions
Add github actions
2 parents 0ba343e + 92cf39e commit 236599f

File tree

14 files changed

+277
-215
lines changed

14 files changed

+277
-215
lines changed

.github/workflows/main.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: CI/CD release pipeline
2+
3+
# Run this workflow every time a new commit pushed to your repository
4+
on: push
5+
6+
jobs:
7+
tox-test:
8+
name: Tox test
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v2
13+
- name: Run Tox tests
14+
uses: fedora-python/tox-github-action@master
15+
with:
16+
tox_env: ${{ matrix.tox_env }}
17+
strategy:
18+
matrix:
19+
tox_env: [py3, black, pylint, mypy]
20+
21+
build-and-release:
22+
name: Build package and release to PyPi
23+
runs-on: ubuntu-latest
24+
needs: [tox-test]
25+
steps:
26+
- uses: actions/checkout@master
27+
- name: Set up Python 3.x
28+
uses: actions/setup-python@v1
29+
with:
30+
python-version: 3.x
31+
- name: Install dependencies
32+
run: |
33+
python -m pip install --upgrade pip
34+
pip install setuptools wheel
35+
- name: Build
36+
run: |
37+
python setup.py sdist bdist_wheel
38+
- name: Publish distribution 📦 to Test PyPI
39+
uses: pypa/gh-action-pypi-publish@master
40+
with:
41+
password: ${{ secrets.TEST_PYPI_TOKEN }}
42+
repository_url: https://test.pypi.org/legacy/
43+
skip_existing: true
44+
- name: Publish distribution 📦 to PyPI
45+
if: startsWith(github.ref, 'refs/tags')
46+
uses: pypa/gh-action-pypi-publish@master
47+
with:
48+
password: ${{ secrets.PYPI_TOKEN }}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Kafka Logging Handler
22
=====================
33
[![Downloads](https://pepy.tech/badge/kafka-logging-handler)](https://pepy.tech/project/kafka-logging-handler)
4+
![Workflow](https://github.com/redhat-aqe/kafka-logging-handler/workflows/release-pipeline/badge.svg)
5+
46

57
The following library simplifies the process of forwarding logs to a Kafka consumer.
68

build_requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
flake8
1+
black
22
pypandoc
3+
pytest

examples/mt_mp.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Usage of kafka-logging-handler with multiprocessing and multithreading."""
2-
2+
# pylint: disable=global-statement
33
from concurrent.futures import ThreadPoolExecutor
44
import logging
55
import multiprocessing
@@ -10,7 +10,7 @@
1010

1111
from kafka_logger.handlers import KafkaLoggingHandler
1212

13-
REQUIRED_ENV_VARS = ['KAFKA_SERVER', 'KAFKA_CERT', 'KAFKA_TOPIC']
13+
REQUIRED_ENV_VARS = ["KAFKA_SERVER", "KAFKA_CERT", "KAFKA_TOPIC"]
1414

1515
MAIN_PROCESS_THREADS = 2
1616
CHILD_PROCESSES = 2
@@ -25,8 +25,7 @@ def get_process_thread():
2525
# you can get PID and thread ID as well:
2626
# os.getpid(), threading.current_thread().ident
2727
return "(process: {}, thread: {})".format(
28-
multiprocessing.current_process().name,
29-
threading.current_thread().name
28+
multiprocessing.current_process().name, threading.current_thread().name
3029
)
3130

3231

@@ -48,9 +47,7 @@ def grandchild_process():
4847
def child_process_with_grandchild():
4948
"""Spawn grandchild process."""
5049
LOGGER.info("I'm going to spawn another child %s", get_process_thread())
51-
subprocess = Process(
52-
target=grandchild_process,
53-
name="Grandchild process")
50+
subprocess = Process(target=grandchild_process, name="Grandchild process")
5451
subprocess.start()
5552
subprocess.join()
5653

@@ -59,7 +56,9 @@ def thread_worker(index):
5956
"""Log a message."""
6057
LOGGER.info(
6158
"Hi, I'm a thread worker #%d in the child process thread pool %s",
62-
index, get_process_thread())
59+
index,
60+
get_process_thread(),
61+
)
6362

6463

6564
def child_process_with_threads():
@@ -72,15 +71,18 @@ def child_process_with_threads():
7271

7372
def main_process_thread(index):
7473
"""Log a message."""
75-
LOGGER.info("Hi, I'm a thread #%d in the main process %s",
76-
index, get_process_thread())
74+
LOGGER.info(
75+
"Hi, I'm a thread #%d in the main process %s", index, get_process_thread()
76+
)
7777

7878

7979
def child_process_with_exception():
8080
"""Raise an exception after start."""
81-
LOGGER.info("Hi, I'm child process that is going to raise exception %s",
82-
get_process_thread())
83-
raise Exception('This exception will not occur in Kafka!')
81+
LOGGER.info(
82+
"Hi, I'm child process that is going to raise exception %s",
83+
get_process_thread(),
84+
)
85+
raise Exception("This exception will not occur in Kafka!")
8486

8587

8688
def main():
@@ -95,8 +97,8 @@ def main():
9597
log_level = logging.DEBUG
9698

9799
log_format = logging.Formatter(
98-
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
99-
'%Y-%m-%dT%H:%M:%S')
100+
"%(asctime)s %(name)-12s %(levelname)-8s %(message)s", "%Y-%m-%dT%H:%M:%S"
101+
)
100102

101103
# create handler to show logs at stdout
102104
stdout_handler = logging.StreamHandler(sys.stdout)
@@ -106,14 +108,12 @@ def main():
106108

107109
# create Kafka logging handler
108110
kafka_handler = KafkaLoggingHandler(
109-
os.environ['KAFKA_SERVER'],
110-
os.environ['KAFKA_TOPIC'],
111-
security_protocol='SSL',
112-
ssl_cafile=os.environ['KAFKA_CERT'],
111+
os.environ["KAFKA_SERVER"],
112+
os.environ["KAFKA_TOPIC"],
113+
security_protocol="SSL",
114+
ssl_cafile=os.environ["KAFKA_CERT"],
113115
unhandled_exception_logger=LOGGER,
114-
additional_fields={
115-
"service": "test_service"
116-
}
116+
additional_fields={"service": "test_service"},
117117
)
118118
kafka_handler.setFormatter(log_format)
119119
LOGGER.addHandler(kafka_handler)
@@ -126,9 +126,8 @@ def main():
126126
child_processes = []
127127
for idx in range(CHILD_PROCESSES):
128128
child = Process(
129-
target=child_process,
130-
name="Child process #{}".format(idx),
131-
args=(idx,))
129+
target=child_process, name="Child process #{}".format(idx), args=(idx,)
130+
)
132131
child_processes.append(child)
133132
child.start()
134133

@@ -138,7 +137,8 @@ def main():
138137
thread = threading.Thread(
139138
target=main_process_thread,
140139
name="Thread of the main process #{}".format(idx),
141-
args=(idx, ))
140+
args=(idx,),
141+
)
142142
threads.append(thread)
143143
thread.start()
144144
# wait for threads to finish
@@ -153,27 +153,28 @@ def main():
153153
# test if a child of a child process logs correctly
154154
child_with_subprocess = Process(
155155
target=child_process_with_grandchild,
156-
name="Child process that spawns another child")
156+
name="Child process that spawns another child",
157+
)
157158
child_with_subprocess.start()
158159
child_with_subprocess.join()
159160

160161
# test threads in a child process
161162
child_with_threads = Process(
162-
target=child_process_with_threads,
163-
name="Child process that has a thread pool")
163+
target=child_process_with_threads, name="Child process that has a thread pool"
164+
)
164165
child_with_threads.start()
165166
child_with_threads.join()
166167

167168
# test unhandled exception in a child process
168169
child_exception = Process(
169-
target=child_process_with_exception,
170-
name="Child process with an exception")
170+
target=child_process_with_exception, name="Child process with an exception"
171+
)
171172
child_exception.start()
172173
child_exception.join()
173174

174175
# top-level exception works only in the main process
175-
raise Exception('Testing top-level exception!')
176+
raise Exception("Testing top-level exception!")
176177

177178

178-
if __name__ == '__main__':
179+
if __name__ == "__main__":
179180
main()

examples/preprocess.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import logging
44
import os
5+
import random
56
import sys
67
import time
7-
import random
88

99
from kafka_logger.handlers import KafkaLoggingHandler
1010

11-
REQUIRED_ENV_VARS = ['KAFKA_SERVER', 'KAFKA_CERT', 'KAFKA_TOPIC']
12-
PASSWORDS = ['qwerty', '12345', 'password']
11+
REQUIRED_ENV_VARS = ["KAFKA_SERVER", "KAFKA_CERT", "KAFKA_TOPIC"]
12+
PASSWORDS = ["qwerty", "12345", "password"]
1313

1414

1515
def main():
@@ -22,8 +22,8 @@ def main():
2222
log_level = logging.DEBUG
2323

2424
log_format = logging.Formatter(
25-
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
26-
'%Y-%m-%dT%H:%M:%S')
25+
"%(asctime)s %(name)-12s %(levelname)-8s %(message)s", "%Y-%m-%dT%H:%M:%S"
26+
)
2727

2828
# create handler to show logs at stdout
2929
stdout_handler = logging.StreamHandler(sys.stdout)
@@ -33,40 +33,35 @@ def main():
3333

3434
def remove_lineno(log):
3535
"""Example preprocessor, includes lineno to message field."""
36-
log['message'] += ' (at line {})'.format(log['lineno'])
37-
del log['lineno']
38-
log['custom_field'] = 42
36+
log["message"] += " (at line {})".format(log["lineno"])
37+
del log["lineno"]
38+
log["custom_field"] = 42
3939
return log
4040

4141
def hide_passwords(log):
4242
"""Example preprocessor, hides passwords."""
4343
for password in PASSWORDS:
44-
hidden_password = \
45-
password[0] + \
46-
'*' * (len(password) - 2) + \
47-
password[-1]
48-
log['message'] = log['message'].replace(password, hidden_password)
44+
hidden_password = password[0] + "*" * (len(password) - 2) + password[-1]
45+
log["message"] = log["message"].replace(password, hidden_password)
4946
return log
5047

5148
# create Kafka logging handler
5249
kafka_handler = KafkaLoggingHandler(
53-
os.environ['KAFKA_SERVER'],
54-
os.environ['KAFKA_TOPIC'],
55-
security_protocol='SSL',
56-
ssl_cafile=os.environ['KAFKA_CERT'],
50+
os.environ["KAFKA_SERVER"],
51+
os.environ["KAFKA_TOPIC"],
52+
security_protocol="SSL",
53+
ssl_cafile=os.environ["KAFKA_CERT"],
5754
# you can configure how often logger will send logs to Kafka
5855
# flush_buffer_size=3, # uncomment to see that it works slower
5956
# flush_interval=3.0, # interval in seconds
6057
unhandled_exception_logger=logger,
6158
kafka_producer_args={
62-
'api_version_auto_timeout_ms': 1000000,
63-
'request_timeout_ms': 1000000,
59+
"api_version_auto_timeout_ms": 1000000,
60+
"request_timeout_ms": 1000000,
6461
},
6562
# you can include arbitrary fields to all produced logs
66-
additional_fields={
67-
"service": "test_service"
68-
},
69-
log_preprocess=[remove_lineno, hide_passwords]
63+
additional_fields={"service": "test_service"},
64+
log_preprocess=[remove_lineno, hide_passwords],
7065
)
7166
kafka_handler.setFormatter(log_format)
7267
logger.addHandler(kafka_handler)
@@ -80,5 +75,5 @@ def hide_passwords(log):
8075
time.sleep(0.5)
8176

8277

83-
if __name__ == '__main__':
78+
if __name__ == "__main__":
8479
main()

0 commit comments

Comments
 (0)