2020-10-21 12:59:54 +02:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
import logging
|
2020-10-21 12:59:54 +02:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
from collections import deque
|
|
|
|
from ipaddress import IPv4Address, IPv6Address, ip_address
|
|
|
|
from math import floor
|
2023-08-15 14:11:20 +02:00
|
|
|
from typing import Callable, Deque, Optional
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
import attr
|
|
|
|
from zope.interface import implementer
|
|
|
|
|
|
|
|
from twisted.application.internet import ClientService
|
2020-10-29 17:53:57 +01:00
|
|
|
from twisted.internet.defer import CancelledError, Deferred
|
2020-10-21 12:59:54 +02:00
|
|
|
from twisted.internet.endpoints import (
|
|
|
|
HostnameEndpoint,
|
|
|
|
TCP4ClientEndpoint,
|
|
|
|
TCP6ClientEndpoint,
|
|
|
|
)
|
2022-05-13 13:35:31 +02:00
|
|
|
from twisted.internet.interfaces import (
|
|
|
|
IPushProducer,
|
|
|
|
IReactorTCP,
|
|
|
|
IStreamClientEndpoint,
|
|
|
|
)
|
2020-10-21 12:59:54 +02:00
|
|
|
from twisted.internet.protocol import Factory, Protocol
|
2021-03-12 17:37:57 +01:00
|
|
|
from twisted.internet.tcp import Connection
|
2020-10-29 17:53:57 +01:00
|
|
|
from twisted.python.failure import Failure
|
2020-10-29 12:27:37 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
|
2022-01-13 14:49:28 +01:00
|
|
|
@attr.s(slots=True, auto_attribs=True)
|
2020-10-21 12:59:54 +02:00
|
|
|
@implementer(IPushProducer)
|
|
|
|
class LogProducer:
|
|
|
|
"""
|
|
|
|
An IPushProducer that writes logs from its buffer to its transport when it
|
|
|
|
is resumed.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
buffer: Log buffer to read logs from.
|
|
|
|
transport: Transport to write to.
|
2020-10-29 12:27:37 +01:00
|
|
|
format: A callable to format the log record to a string.
|
2020-10-21 12:59:54 +02:00
|
|
|
"""
|
|
|
|
|
2021-03-12 17:37:57 +01:00
|
|
|
# This is essentially ITCPTransport, but that is missing certain fields
|
|
|
|
# (connected and registerProducer) which are part of the implementation.
|
2022-01-13 14:49:28 +01:00
|
|
|
transport: Connection
|
|
|
|
_format: Callable[[logging.LogRecord], str]
|
|
|
|
_buffer: Deque[logging.LogRecord]
|
|
|
|
_paused: bool = attr.ib(default=False, init=False)
|
2020-10-21 12:59:54 +02:00
|
|
|
|
2022-05-13 13:35:31 +02:00
|
|
|
def pauseProducing(self) -> None:
|
2020-10-21 12:59:54 +02:00
|
|
|
self._paused = True
|
|
|
|
|
2022-05-13 13:35:31 +02:00
|
|
|
def stopProducing(self) -> None:
|
2020-10-21 12:59:54 +02:00
|
|
|
self._paused = True
|
|
|
|
self._buffer = deque()
|
|
|
|
|
2022-05-13 13:35:31 +02:00
|
|
|
def resumeProducing(self) -> None:
|
2020-10-29 12:27:37 +01:00
|
|
|
# If we're already producing, nothing to do.
|
2020-10-21 12:59:54 +02:00
|
|
|
self._paused = False
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
# Loop until paused.
|
2020-10-21 12:59:54 +02:00
|
|
|
while self._paused is False and (self._buffer and self.transport.connected):
|
|
|
|
try:
|
2020-10-29 12:27:37 +01:00
|
|
|
# Request the next record and format it.
|
|
|
|
record = self._buffer.popleft()
|
|
|
|
msg = self._format(record)
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
# Send it as a new line over the transport.
|
|
|
|
self.transport.write(msg.encode("utf8"))
|
2020-10-29 12:27:37 +01:00
|
|
|
self.transport.write(b"\n")
|
2020-10-21 12:59:54 +02:00
|
|
|
except Exception:
|
|
|
|
# Something has gone wrong writing to the transport -- log it
|
|
|
|
# and break out of the while.
|
|
|
|
traceback.print_exc(file=sys.__stderr__)
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
class RemoteHandler(logging.Handler):
|
2020-10-21 12:59:54 +02:00
|
|
|
"""
|
2020-10-29 12:27:37 +01:00
|
|
|
An logging handler that writes logs to a TCP target.
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
host: The host of the logging target.
|
|
|
|
port: The logging target's port.
|
|
|
|
maximum_buffer: The maximum buffer size.
|
|
|
|
"""
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host: str,
|
|
|
|
port: int,
|
|
|
|
maximum_buffer: int = 1000,
|
2022-05-13 13:35:31 +02:00
|
|
|
level: int = logging.NOTSET,
|
|
|
|
_reactor: Optional[IReactorTCP] = None,
|
2020-10-29 12:27:37 +01:00
|
|
|
):
|
|
|
|
super().__init__(level=level)
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.maximum_buffer = maximum_buffer
|
|
|
|
|
2021-07-15 12:02:43 +02:00
|
|
|
self._buffer: Deque[logging.LogRecord] = deque()
|
|
|
|
self._connection_waiter: Optional[Deferred] = None
|
|
|
|
self._producer: Optional[LogProducer] = None
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
# Connect without DNS lookups if it's a direct IP.
|
2020-10-29 12:27:37 +01:00
|
|
|
if _reactor is None:
|
|
|
|
from twisted.internet import reactor
|
|
|
|
|
2022-05-13 13:35:31 +02:00
|
|
|
_reactor = reactor # type: ignore[assignment]
|
2020-10-29 12:27:37 +01:00
|
|
|
|
2020-10-21 12:59:54 +02:00
|
|
|
try:
|
|
|
|
ip = ip_address(self.host)
|
|
|
|
if isinstance(ip, IPv4Address):
|
2021-07-15 12:02:43 +02:00
|
|
|
endpoint: IStreamClientEndpoint = TCP4ClientEndpoint(
|
2021-03-03 21:47:38 +01:00
|
|
|
_reactor, self.host, self.port
|
2021-07-15 12:02:43 +02:00
|
|
|
)
|
2020-10-21 12:59:54 +02:00
|
|
|
elif isinstance(ip, IPv6Address):
|
2020-10-29 12:27:37 +01:00
|
|
|
endpoint = TCP6ClientEndpoint(_reactor, self.host, self.port)
|
2020-10-21 12:59:54 +02:00
|
|
|
else:
|
|
|
|
raise ValueError("Unknown IP address provided: %s" % (self.host,))
|
|
|
|
except ValueError:
|
2020-10-29 12:27:37 +01:00
|
|
|
endpoint = HostnameEndpoint(_reactor, self.host, self.port)
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
factory = Factory.forProtocol(Protocol)
|
2020-10-29 12:27:37 +01:00
|
|
|
self._service = ClientService(endpoint, factory, clock=_reactor)
|
2020-10-21 12:59:54 +02:00
|
|
|
self._service.startService()
|
2020-10-29 17:53:57 +01:00
|
|
|
self._stopping = False
|
2020-10-21 12:59:54 +02:00
|
|
|
self._connect()
|
|
|
|
|
2022-05-13 13:35:31 +02:00
|
|
|
def close(self) -> None:
|
2020-10-29 17:53:57 +01:00
|
|
|
self._stopping = True
|
2020-10-21 12:59:54 +02:00
|
|
|
self._service.stopService()
|
|
|
|
|
|
|
|
def _connect(self) -> None:
|
|
|
|
"""
|
|
|
|
Triggers an attempt to connect then write to the remote if not already writing.
|
|
|
|
"""
|
2020-10-29 12:27:37 +01:00
|
|
|
# Do not attempt to open multiple connections.
|
2020-10-21 12:59:54 +02:00
|
|
|
if self._connection_waiter:
|
|
|
|
return
|
|
|
|
|
2020-10-29 17:53:57 +01:00
|
|
|
def fail(failure: Failure) -> None:
|
|
|
|
# If the Deferred was cancelled (e.g. during shutdown) do not try to
|
|
|
|
# reconnect (this will cause an infinite loop of errors).
|
|
|
|
if failure.check(CancelledError) and self._stopping:
|
|
|
|
return
|
|
|
|
|
|
|
|
# For a different error, print the traceback and re-connect.
|
|
|
|
failure.printTraceback(file=sys.__stderr__)
|
2020-10-21 12:59:54 +02:00
|
|
|
self._connection_waiter = None
|
|
|
|
self._connect()
|
|
|
|
|
2020-10-29 17:53:57 +01:00
|
|
|
def writer(result: Protocol) -> None:
|
2021-03-12 17:37:57 +01:00
|
|
|
# Force recognising transport as a Connection and not the more
|
|
|
|
# generic ITransport.
|
2021-07-15 12:02:43 +02:00
|
|
|
transport: Connection = result.transport # type: ignore
|
2021-03-12 17:37:57 +01:00
|
|
|
|
2020-10-21 12:59:54 +02:00
|
|
|
# We have a connection. If we already have a producer, and its
|
|
|
|
# transport is the same, just trigger a resumeProducing.
|
2021-03-12 17:37:57 +01:00
|
|
|
if self._producer and transport is self._producer.transport:
|
2020-10-21 12:59:54 +02:00
|
|
|
self._producer.resumeProducing()
|
|
|
|
self._connection_waiter = None
|
|
|
|
return
|
|
|
|
|
|
|
|
# If the producer is still producing, stop it.
|
|
|
|
if self._producer:
|
|
|
|
self._producer.stopProducing()
|
|
|
|
|
|
|
|
# Make a new producer and start it.
|
|
|
|
self._producer = LogProducer(
|
2021-02-16 23:32:34 +01:00
|
|
|
buffer=self._buffer,
|
2021-03-12 17:37:57 +01:00
|
|
|
transport=transport,
|
2021-02-16 23:32:34 +01:00
|
|
|
format=self.format,
|
2020-10-21 12:59:54 +02:00
|
|
|
)
|
2021-03-12 17:37:57 +01:00
|
|
|
transport.registerProducer(self._producer, True)
|
2020-10-21 12:59:54 +02:00
|
|
|
self._producer.resumeProducing()
|
|
|
|
self._connection_waiter = None
|
|
|
|
|
2021-07-15 12:02:43 +02:00
|
|
|
deferred: Deferred = self._service.whenConnected(failAfterFailures=1)
|
2021-03-12 17:37:57 +01:00
|
|
|
deferred.addCallbacks(writer, fail)
|
|
|
|
self._connection_waiter = deferred
|
2020-10-29 17:53:57 +01:00
|
|
|
|
2020-10-21 12:59:54 +02:00
|
|
|
def _handle_pressure(self) -> None:
|
|
|
|
"""
|
2020-10-29 12:27:37 +01:00
|
|
|
Handle backpressure by shedding records.
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
The buffer will, in this order, until the buffer is below the maximum:
|
2020-10-29 12:27:37 +01:00
|
|
|
- Shed DEBUG records.
|
|
|
|
- Shed INFO records.
|
|
|
|
- Shed the middle 50% of the records.
|
2020-10-21 12:59:54 +02:00
|
|
|
"""
|
|
|
|
if len(self._buffer) <= self.maximum_buffer:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Strip out DEBUGs
|
|
|
|
self._buffer = deque(
|
2020-10-29 12:27:37 +01:00
|
|
|
filter(lambda record: record.levelno > logging.DEBUG, self._buffer)
|
2020-10-21 12:59:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if len(self._buffer) <= self.maximum_buffer:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Strip out INFOs
|
|
|
|
self._buffer = deque(
|
2020-10-29 12:27:37 +01:00
|
|
|
filter(lambda record: record.levelno > logging.INFO, self._buffer)
|
2020-10-21 12:59:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if len(self._buffer) <= self.maximum_buffer:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Cut the middle entries out
|
|
|
|
buffer_split = floor(self.maximum_buffer / 2)
|
|
|
|
|
|
|
|
old_buffer = self._buffer
|
|
|
|
self._buffer = deque()
|
|
|
|
|
2021-04-20 12:50:49 +02:00
|
|
|
for _ in range(buffer_split):
|
2020-10-21 12:59:54 +02:00
|
|
|
self._buffer.append(old_buffer.popleft())
|
|
|
|
|
|
|
|
end_buffer = []
|
2021-04-20 12:50:49 +02:00
|
|
|
for _ in range(buffer_split):
|
2020-10-21 12:59:54 +02:00
|
|
|
end_buffer.append(old_buffer.pop())
|
|
|
|
|
|
|
|
self._buffer.extend(reversed(end_buffer))
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
def emit(self, record: logging.LogRecord) -> None:
|
|
|
|
self._buffer.append(record)
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
# Handle backpressure, if it exists.
|
|
|
|
try:
|
|
|
|
self._handle_pressure()
|
|
|
|
except Exception:
|
2020-10-29 12:27:37 +01:00
|
|
|
# If handling backpressure fails, clear the buffer and log the
|
2020-10-21 12:59:54 +02:00
|
|
|
# exception.
|
|
|
|
self._buffer.clear()
|
2020-10-29 12:27:37 +01:00
|
|
|
logger.warning("Failed clearing backpressure")
|
2020-10-21 12:59:54 +02:00
|
|
|
|
|
|
|
# Try and write immediately.
|
|
|
|
self._connect()
|