Skip to content

Commit

Permalink
hotfix: graylog not working (#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyujin-cho committed Apr 5, 2024
1 parent e286913 commit 89856b8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/1999.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix Graylog log adapter not working after upgrading to Python 3.12
56 changes: 51 additions & 5 deletions src/ai/backend/common/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import Any, Mapping, MutableMapping, Optional

import coloredlogs
import graypy
import trafaret as t
import yarl
import zmq
Expand Down Expand Up @@ -198,11 +199,56 @@ def emit(self, record):
self._sock.sendall(json.dumps(log).encode("utf-8"))


class GELFTLSHandler(graypy.GELFTLSHandler):
ssl_ctx: ssl.SSLContext

def __init__(
self,
host,
port=12204,
validate=False,
ca_certs=None,
**kwargs
):
"""Initialize the GELFTLSHandler
:param host: GELF TLS input host.
:type host: str
:param port: GELF TLS input port.
:type port: int
:param validate: If :obj:`True`, validate the Graylog server's
certificate. In this case specifying ``ca_certs`` is also
required.
:type validate: bool
:param ca_certs: Path to CA bundle file.
:type ca_certs: str
"""

super().__init__(host, port=port, validate=validate, **kwargs)
self.ssl_ctx = ssl.create_default_context(capath=ca_certs)
if not validate:
self.ssl_ctx.check_hostname = False
self.ssl_ctx.verify_mode = ssl.CERT_NONE

def makeSocket(self, timeout=1):
"""Create a TLS wrapped socket"""
plain_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

if hasattr(plain_socket, "settimeout"):
plain_socket.settimeout(timeout)

wrapped_socket = self.ssl_ctx.wrap_socket(
plain_socket, server_hostname=self.host,
)
wrapped_socket.connect((self.host, self.port))

return wrapped_socket


def setup_graylog_handler(config: Mapping[str, Any]) -> Optional[logging.Handler]:
try:
import graypy
except ImportError:
return None
drv_config = config["graylog"]
graylog_params = {
"host": drv_config["host"],
Expand All @@ -217,7 +263,7 @@ def setup_graylog_handler(config: Mapping[str, Any]) -> Optional[logging.Handler
else:
graylog_params["fqdn"] = drv_config["fqdn"]

graylog_handler = graypy.GELFTLSHandler(**graylog_params)
graylog_handler = GELFTLSHandler(**graylog_params)
graylog_handler.setLevel(config["level"])
return graylog_handler

Expand Down

0 comments on commit 89856b8

Please sign in to comment.