def __init__(
        self,
        only_accept_local,
        port,
        run_state,
        buffer_size,
        max_request_size,
        max_connection_idle_time,
        logger,
    ):
        """Creates a new instance.

        @param only_accept_local: If true, only accept local connections.
        @param port: The port on which to accept connections.
        @param run_state: The run_state to use to control when this server should stop accepting connections and new
            requests. If 'run_state's 'stop' method is invoked, then 'run' will terminate.
        @param buffer_size: The maximum buffer size for buffering incoming requests per connection.
        @param max_request_size: The maximum size of an individual request. If this is exceeded, then the connection
            responsible is terminated.
        @param max_connection_idle_time: The maximum time to wait on a connection between requests before closing it.
        @param logger: The logger to use to record errors and metrics.
        """
        self.__logger = logger
        self.__parser = LineRequestParser(max_request_size)
        ServerProcessor.__init__(
            self,
            port,
            localhost_socket=only_accept_local,
            max_request_size=max_request_size,
            max_connection_idle_time=max_connection_idle_time,
            buffer_size=buffer_size,
            run_state=run_state,
        )
Example #2
0
class GraphiteTextServer(ServerProcessor):
    """Accepts connections on a server socket and handles them using Graphite's plaintext protocol format, emitting
    the received metrics to the log.
    """
    def __init__(self, only_accept_local, port, run_state, buffer_size,
                 max_request_size, max_connection_idle_time, logger):
        """Creates a new instance.

        @param only_accept_local: If true, only accept local connections.
        @param port: The port on which to accept connections.
        @param run_state: The run_state to use to control when this server should stop accepting connections and new
            requests. If 'run_state's 'stop' method is invoked, then 'run' will terminate.
        @param buffer_size: The maximum buffer size for buffering incoming requests per connection.
        @param max_request_size: The maximum size of an individual request. If this is exceeded, then the connection
            responsible is terminated.
        @param max_connection_idle_time: The maximum time to wait on a connection between requests before closing it.
        @param logger: The logger to use to record errors and metrics.
        """
        self.__logger = logger
        self.__parser = LineRequestParser(max_request_size)
        ServerProcessor.__init__(
            self,
            port,
            localhost_socket=only_accept_local,
            max_request_size=max_request_size,
            max_connection_idle_time=max_connection_idle_time,
            buffer_size=buffer_size,
            run_state=run_state)

    def execute_request(self, request):
        try:
            # This is how the carbon graphite server parses the line.  We could be more forgiving but if it works
            # for them, then we can do it as well.
            metric, value, orig_timestamp = request.strip().split()
            value = float(value)
            orig_timestamp = float(orig_timestamp)
            # Include the time that the original graphite request said to associate with the metric value.
            self.__logger.emit_value(
                metric, value, extra_fields={'orig_time': orig_timestamp})
        except ValueError:
            self.__logger.warn(
                'Could not parse incoming metric line from graphite plaintext server, ignoring',
                error_code='graphite_monitor/badPlainTextLine')

    def parse_request(self, request_input, num_available_bytes):
        return self.__parser.parse_request(request_input, num_available_bytes)

    def report_connection_problem(self, exception):
        self.__logger.exception(
            'Exception seen while processing Graphite connect on text port, '
            'closing connection: "%s"' % str(exception))
    def __init__(self, only_accept_local, port, run_state, buffer_size, max_request_size,
                 max_connection_idle_time, logger):
        """Creates a new instance.

        @param only_accept_local: If true, only accept local connections.
        @param port: The port on which to accept connections.
        @param run_state: The run_state to use to control when this server should stop accepting connections and new
            requests. If 'run_state's 'stop' method is invoked, then 'run' will terminate.
        @param buffer_size: The maximum buffer size for buffering incoming requests per connection.
        @param max_request_size: The maximum size of an individual request. If this is exceeded, then the connection
            responsible is terminated.
        @param max_connection_idle_time: The maximum time to wait on a connection between requests before closing it.
        @param logger: The logger to use to record errors and metrics.
        """
        self.__logger = logger
        self.__parser = LineRequestParser(max_request_size)
        ServerProcessor.__init__(self, port, localhost_socket=only_accept_local, max_request_size=max_request_size,
                                 max_connection_idle_time=max_connection_idle_time,
                                 buffer_size=buffer_size, run_state=run_state)
class GraphiteTextServer(ServerProcessor):
    """Accepts connections on a server socket and handles them using Graphite's plaintext protocol format, emitting
    the received metrics to the log.
    """
    def __init__(self, only_accept_local, port, run_state, buffer_size, max_request_size,
                 max_connection_idle_time, logger):
        """Creates a new instance.

        @param only_accept_local: If true, only accept local connections.
        @param port: The port on which to accept connections.
        @param run_state: The run_state to use to control when this server should stop accepting connections and new
            requests. If 'run_state's 'stop' method is invoked, then 'run' will terminate.
        @param buffer_size: The maximum buffer size for buffering incoming requests per connection.
        @param max_request_size: The maximum size of an individual request. If this is exceeded, then the connection
            responsible is terminated.
        @param max_connection_idle_time: The maximum time to wait on a connection between requests before closing it.
        @param logger: The logger to use to record errors and metrics.
        """
        self.__logger = logger
        self.__parser = LineRequestParser(max_request_size)
        ServerProcessor.__init__(self, port, localhost_socket=only_accept_local, max_request_size=max_request_size,
                                 max_connection_idle_time=max_connection_idle_time,
                                 buffer_size=buffer_size, run_state=run_state)

    def execute_request(self, request):
        try:
            # This is how the carbon graphite server parses the line.  We could be more forgiving but if it works
            # for them, then we can do it as well.
            metric, value, orig_timestamp = request.strip().split()
            value = float(value)
            orig_timestamp = float(orig_timestamp)
            # Include the time that the original graphite request said to associate with the metric value.
            self.__logger.emit_value(metric, value, extra_fields={'orig_time': orig_timestamp})
        except ValueError:
            self.__logger.warn('Could not parse incoming metric line from graphite plaintext server, ignoring',
                               error_code='graphite_monitor/badPlainTextLine')

    def parse_request(self, request_input, num_available_bytes):
        return self.__parser.parse_request(request_input, num_available_bytes)

    def report_connection_problem(self, exception):
        self.__logger.exception('Exception seen while processing Graphite connect on text port, '
                                'closing connection: "%s"' % str(exception))