summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/test/qa-tests/buildscripts/resmokelib/logging/handlers.py
blob: b688a1da68aa7e944dd04b7cbc2695f611c0bfcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Additional handlers that are used as the base classes of the buildlogger
handler.
"""

from __future__ import absolute_import

import json
import logging
import threading
import urllib2

from .. import utils
from ..utils import timer

_TIMEOUT_SECS = 10

class BufferedHandler(logging.Handler):
    """
    A handler class that buffers logging records in memory. Whenever
    each record is added to the buffer, a check is made to see if the
    buffer should be flushed. If it should, then flush() is expected to
    do what's needed.
    """

    def __init__(self, capacity, interval_secs):
        """
        Initializes the handler with the buffer size and timeout after
        which the buffer is flushed regardless.
        """

        logging.Handler.__init__(self)

        if not isinstance(capacity, int):
            raise TypeError("capacity must be an integer")
        elif capacity <= 0:
            raise ValueError("capacity must be a positive integer")

        if not isinstance(interval_secs, (int, float)):
            raise TypeError("interval_secs must be a number")
        elif interval_secs <= 0.0:
            raise ValueError("interval_secs must be a positive number")

        self.capacity = capacity
        self.interval_secs = interval_secs
        self.buffer = []

        self._lock = threading.Lock()
        self._timer = None  # Defer creation until actually begin to log messages.

    def _new_timer(self):
        """
        Returns a new timer.AlarmClock instance that will call the
        flush() method after 'interval_secs' seconds.
        """

        return timer.AlarmClock(self.interval_secs, self.flush, args=[self])

    def process_record(self, record):
        """
        Applies a transformation to the record before it gets added to
        the buffer.

        The default implementation returns 'record' unmodified.
        """

        return record

    def emit(self, record):
        """
        Emits a record.

        Append the record to the buffer after it has been transformed by
        process_record(). If the length of the buffer is greater than or
        equal to its capacity, then flush() is called to process the
        buffer.

        After flushing the buffer, the timer is restarted so that it
        will expire after another 'interval_secs' seconds.
        """

        with self._lock:
            self.buffer.append(self.process_record(record))
            if len(self.buffer) >= self.capacity:
                if self._timer is not None:
                    self._timer.snooze()
                self.flush_with_lock(False)
                if self._timer is not None:
                    self._timer.reset()

        if self._timer is None:
            self._timer = self._new_timer()
            self._timer.start()

    def flush(self, close_called=False):
        """
        Ensures all logging output has been flushed.
        """

        with self._lock:
            if self.buffer:
                self.flush_with_lock(close_called)

    def flush_with_lock(self, close_called):
        """
        Ensures all logging output has been flushed.

        This version resets the buffers back to an empty list and is
        intended to be overridden by subclasses.
        """

        self.buffer = []

    def close(self):
        """
        Tidies up any resources used by the handler.

        Stops the timer and flushes the buffer.
        """

        if self._timer is not None:
            self._timer.dismiss()
        self.flush(close_called=True)

        logging.Handler.close(self)


class HTTPHandler(object):
    """
    A class which sends data to a web server using POST requests.
    """

    def __init__(self, realm, url_root, username, password):
        """
        Initializes the handler with the necessary authenticaton
        credentials.
        """

        digest_handler = urllib2.HTTPDigestAuthHandler()
        digest_handler.add_password(
            realm=realm,
            uri=url_root,
            user=username,
            passwd=password)

        self.url_root = url_root
        self.url_opener = urllib2.build_opener(digest_handler, urllib2.HTTPErrorProcessor())

    def _make_url(self, endpoint):
        return "%s/%s/" % (self.url_root.rstrip("/"), endpoint.strip("/"))

    def post(self, endpoint, data=None, headers=None, timeout_secs=_TIMEOUT_SECS):
        """
        Sends a POST request to the specified endpoint with the supplied
        data.

        Returns the response, either as a string or a JSON object based
        on the content type.
        """

        data = utils.default_if_none(data, [])
        data = json.dumps(data, encoding="utf-8")

        headers = utils.default_if_none(headers, {})
        headers["Content-Type"] = "application/json; charset=utf-8"

        url = self._make_url(endpoint)
        request = urllib2.Request(url=url, data=data, headers=headers)

        response = self.url_opener.open(request, timeout=timeout_secs)
        headers = response.info()

        content_type = headers.gettype()
        if content_type == "application/json":
            encoding = headers.getparam("charset") or "utf-8"
            return json.load(response, encoding=encoding)

        return response.read()