Skip to content

Commit

Permalink
Merge pull request #4 from malinkinsa/error_processing_and_tcp_tls
Browse files Browse the repository at this point in the history
Error processing and tcp tls
  • Loading branch information
malinkinsa authored Apr 12, 2022
2 parents e610d4e + e137445 commit 189bf32
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Async python logging handlers that send messages in the Graylog Extended Log For
- [Available params](#available-params)

## List of ready to run GELF handlers
- TCP (without TLS);
- HTTP;
- TCP (with and without TLS);
- HTTP (with and without TLS);

## Get AsyncGELF
```python
Expand Down Expand Up @@ -62,5 +62,6 @@ asyncio.run(main(message))
- ```gelf_version``` Optional | GELF spec version (default: 1.1)
- ```level``` Optional | The level equal to the standard syslog levels (default: 1);
- ```scheme``` Optional | HTTP Scheme <i>for GELF HTTP input only</i> (default: http);
- ```tls``` Path to custom (self-signed) certificate in pem format <i>for GELF HTTP input only</i> (default: None)
- ```compress``` Optional | Compress message before sending it to the server or not <i>for GELF HTTP input only</i> (default: false)
- ```tls``` Optional | Path to custom (self-signed) certificate in pem format (default: None)
- ```compress``` Optional | Compress message before sending it to the server or not <i>for GELF HTTP input only</i> (default: False)
- ```debug``` Optional | Additional information in error log (default: False)
82 changes: 67 additions & 15 deletions asyncgelf/asyncgelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(
scheme: Optional[str] = 'http',
tls: Optional = None,
compress: Optional = False,
debug: Optional = False
):
"""
:param host: graylog server address
Expand All @@ -26,6 +27,7 @@ def __init__(
:param scheme: HTTP Scheme for GELF HTTP input only
:param tls: Path to custom (self-signed) certificate in pem format
:param compress: compress message before sending it to the server or not
:param debug: additional information in error log
"""

self.host = host
Expand All @@ -36,6 +38,7 @@ def __init__(
self.scheme = scheme
self.compress = compress
self.tls = tls
self.debug = debug

def make(self, message):
"""
Expand Down Expand Up @@ -63,13 +66,46 @@ async def tcp_handler(self, massage):
""" Transforming GELF dictionary into bytes """
bytes_msg = json.dumps(gelf_message).encode('utf-8')

stream_reader, stream_writer = await asyncio.open_connection(
self.host, self.port
)
if self.tls:
ssl_contex = ssl.create_default_context()
ssl_contex.load_verify_locations(cafile=self.tls)

try:
stream_reader, stream_writer = await asyncio.open_connection(
self.host,
self.port,
ssl=ssl_contex,
)

"""
if you send the message over tcp, it should always be null terminated or the input will reject it
"""
stream_writer.write(bytes_msg + b'\x00')
stream_writer.close()

except Exception as e:
if self.debug:
return f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}"

return getattr(e, 'message', repr(e))

try:
stream_reader, stream_writer = await asyncio.open_connection(
self.host,
self.port,
)

"""
if you send the message over tcp, it should always be null terminated or the input will reject it
"""
stream_writer.write(bytes_msg + b'\x00')
stream_writer.close()

""" if you send the message over tcp, it should always be null terminated or the input will reject it """
stream_writer.write(bytes_msg + b'\x00')
stream_writer.close()
except Exception as e:
if self.debug:
return f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}"

return getattr(e, 'message', repr(e))


class GelfHttp(GelfBase):
Expand All @@ -94,20 +130,36 @@ async def http_handler(self, message):

gelf_endpoint = f'https://{self.host}:{self.port}/gelf'

async with httpx.AsyncClient(verify=ssl_contex) as client:
try:
async with httpx.AsyncClient(verify=ssl_contex) as client:
response = await client.post(
gelf_endpoint,
headers=header,
data=json.dumps(gelf_message),
)

return response.status_code

except Exception as e:
if self.debug:
return f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}"

return getattr(e, 'message', repr(e))

gelf_endpoint = f'{self.scheme}://{self.host}:{self.port}/gelf'

try:
async with httpx.AsyncClient() as client:
response = await client.post(
gelf_endpoint,
headers=header,
data=json.dumps(gelf_message),
)

return response.status_code

gelf_endpoint = f'{self.scheme}://{self.host}:{self.port}/gelf'
except Exception as e:
if self.debug:
return f"{type(e).__name__} at line {e.__traceback__.tb_lineno} of {__file__}: {e}"

async with httpx.AsyncClient() as client:
response = await client.post(
gelf_endpoint,
headers=header,
data=json.dumps(gelf_message),
)
return response.status_code
return getattr(e, 'message', repr(e))
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

setup(
name='asyncgelf',
version='0.1.2',
version='0.1.3',
author='Sergey Malinkin',
author_email='[email protected]',
url='https://github.com/malinkinsa/asyncgelf',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/0.1.2.tar.gz',
download_url='https://github.com/malinkinsa/asyncgelf/archive/refs/tags/0.1.3.tar.gz',
description='Async python logging handlers that send messages in the Graylog Extended Log Format (GELF).',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 189bf32

Please sign in to comment.