This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
util.py
167 lines (134 loc) · 5.62 KB
/
util.py
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
# ***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
# ***** END LICENSE BLOCK *****
import json
import time
import socket
import urllib
import logging
import urlparse
import traceback
from datetime import datetime
from decimal import Decimal, InvalidOperation
from pyramid.util import DottedNameResolver
def round_time(value=None, precision=2):
"""Transforms a timestamp into a two digits Decimal.
Arg:
value: timestamps representation - float or str.
If None, uses time.time()
precision: number of digits to keep. defaults to 2.
Return:
A Decimal two-digits instance.
"""
if value is None:
value = time.time()
if not isinstance(value, str):
value = str(value)
try:
digits = '0' * precision
return Decimal(value).quantize(Decimal('1.' + digits))
except InvalidOperation:
raise ValueError(value)
def resolve_name(name, package=None):
"""Resolve dotted name into a python object.
This function resolves a dotted name as a reference to a python object,
returning whatever object happens to live at that path. It's a simple
convenience wrapper around pyramid's DottedNameResolver.
The optional argument 'package' specifies the package name for relative
imports. If not specified, only absolute paths will be supported.
"""
return DottedNameResolver(package).resolve(name)
def maybe_resolve_name(name_or_object, package=None):
"""Resolve dotted name or object into a python object.
This function resolves a dotted name as a reference to a python object,
returning whatever object happens to live at that path. If the given
name is not a string, it is returned unchanged.
The optional argument 'package' specifies the package name for relative
imports. If not specified, only absolute paths will be supported.
"""
return DottedNameResolver(package).maybe_resolve(name_or_object)
def dnslookup(url):
"""Replaces a hostname by its IP in an url.
Uses gethostbyname to do a DNS lookup, so the nscd cache is used.
If gevent has patched the standard library, makes sure it uses the
original version because gevent uses its own mechanism based on
the async libevent's evdns_resolve_ipv4, which does not use
glibc's resolver.
"""
try:
from gevent.socket import _socket
gethostbyname = _socket.gethostbyname
except ImportError:
import socket
gethostbyname = socket.gethostbyname
# parsing
parsed_url = urlparse.urlparse(url)
host, port = urllib.splitport(parsed_url.netloc)
user, host = urllib.splituser(host)
# resolving the host
host = gethostbyname(host)
# recomposing
if port is not None:
host = '%s:%s' % (host, port)
if user is not None:
host = '%s@%s' % (user, host)
parts = [parsed_url[0]] + [host] + list(parsed_url[2:])
return urlparse.urlunparse(parts)
class JsonLogFormatter(logging.Formatter):
"""Log formatter that outputs machine-readable json.
This log formatter outputs JSON format messages that are compatible with
Mozilla's standard heka-based log aggregation infrastructure. It ignores
any user-specific message and instead outouts a JSON dict of all relevant
log-record attributes.
"""
DEFAULT_LOGRECORD_ATTRS = set((
'args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename',
'funcName', 'levelname', 'levelno', 'lineno', 'module', 'msecs',
'message', 'msg', 'name', 'pathname', 'process', 'processName',
'relativeCreated', 'thread', 'threadName'
))
DEFAULT_DETAILS = {
"v": 1,
"hostname": socket.gethostname(),
}
def format(self, record):
# Take default values from the record and the environment.
details = self.DEFAULT_DETAILS.copy()
details.update({
"op": record.name,
"name": record.name,
"time": datetime.utcfromtimestamp(record.created).isoformat()+"Z",
"pid": record.process,
})
# Include any custom attributes set on the record.
# These would usually be collected metrics data.
for key, value in record.__dict__.iteritems():
if key not in self.DEFAULT_LOGRECORD_ATTRS:
details[key] = value
# Only include the 'message' key if it has useful content
# and is not already a JSON blob.
message = record.getMessage()
if message:
if not message.startswith("{") and not message.endswith("}"):
details["message"] = message
# If there is an error, format it for nice output.
if record.exc_info is not None:
details["error"] = repr(record.exc_info[1])
details["traceback"] = safer_format_traceback(*record.exc_info)
return json.dumps(details)
def safer_format_traceback(exc_typ, exc_val, exc_tb):
"""Format an exception traceback into safer string.
We don't want to let users write arbitrary data into our logfiles,
which could happen if they e.g. managed to trigger a ValueError with
a carefully-crafted payload. This function formats the traceback
using "%r" for the actual exception data, which passes it through repr()
so that any special chars are safely escaped.
"""
lines = ["Uncaught exception:\n"]
lines.extend(traceback.format_tb(exc_tb))
lines.append("%r\n" % (exc_typ,))
lines.append("%r\n" % (exc_val,))
return "".join(lines)