My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# 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.
#




"""A logging handler that records information about unique exceptions.

'Unique' in this case is defined as a given (exception class, location) tuple.
Unique exceptions are logged to the datastore with an example stacktrace and an
approximate count of occurrences, grouped by day and application version.

A cron handler, in google.appengine.ext.ereporter.report_generator, constructs
and emails a report based on the previous day's exceptions.

Example usage:

In your handler script(s), add:

import logging
from google.appengine.ext import ereporter

ereporter.register_logger()

In your app.yaml, add:

handlers:
- url: /_ereporter/.*
script: $PYTHON_LIB/google/appengine/ext/ereporter/report_generator.py
login: admin

In your cron.yaml, add:

cron:
- description: Daily exception report
url: /_ereporter?sender=you@yourdomain.com
schedule: every day 00:00

This will cause a daily exception report to be generated and emailed to all
admins, with exception traces grouped by minor version. If you only want to
get exception information for the most recent minor version, add the
'versions=latest' argument to the query string. For other valid query string
arguments, see report_generator.py.

If you anticipate a lot of exception traces (for example, if you're deploying
many minor versions, each of which may have its own set of exceptions), you
can ensure that the traces from the newest minor versions get included by adding
this to your index.yaml:

indexes:
- kind: ExceptionRecord
properties:
- name: date
- name: major_version
- name: minor_version
direction: desc
"""









import datetime
import logging
import os
import sha
import traceback
import urllib

from google.appengine.api import memcache
from google.appengine.api import namespace_manager
from google.appengine.ext import db
from google.appengine.ext import webapp



MAX_SIGNATURE_LENGTH = 256


class ExceptionRecord(db.Model):
"""Datastore model for a record of a unique exception."""

signature = db.StringProperty(required=True)
major_version = db.StringProperty(required=True)
minor_version = db.IntegerProperty(required=True)
date = db.DateProperty(required=True)
count = db.IntegerProperty(required=True, default=0)


stacktrace = db.TextProperty(required=True)
http_method = db.TextProperty(required=True)
url = db.TextProperty(required=True)
handler = db.TextProperty(required=True)

@classmethod
def get_key_name(cls, signature, version, date=None):
"""Generates a key name for an exception record.

Args:
signature: A signature representing the exception and its site.
version: The major/minor version of the app the exception occurred in.
date: The date the exception occurred.

Returns:
The unique key name for this exception record.
"""
if not date:
date = datetime.date.today()
return '%s@%s:%s' % (signature, date, version)


class ExceptionRecordingHandler(logging.Handler):
"""A handler that records exception data to the App Engine datastore."""

def __init__(self, log_interval=10):
"""Constructs a new ExceptionRecordingHandler.

Args:
log_interval: The minimum interval at which we will log an individual
exception. This is a per-exception timeout, so doesn't affect the
aggregate rate of exception logging, only the rate at which we record
ocurrences of a single exception, to prevent datastore contention.
"""
self.log_interval = log_interval
logging.Handler.__init__(self)

@classmethod
def __RelativePath(cls, path):
"""Rewrites a path to be relative to the app's root directory.

Args:
path: The path to rewrite.

Returns:
The path with the prefix removed, if that prefix matches the app's
root directory.
"""
cwd = os.getcwd()
if path.startswith(cwd):
path = path[len(cwd)+1:]
return path

@classmethod
def __GetSignature(cls, exc_info):
"""Returns a unique signature string for an exception.

Args:
exc_info: The exc_info object for an exception.

Returns:
A unique signature string for the exception, consisting of fully
qualified exception name and call site.
"""
ex_type, unused_value, trace = exc_info
frames = traceback.extract_tb(trace)

fulltype = '%s.%s' % (ex_type.__module__, ex_type.__name__)
path, line_no = frames[-1][:2]
path = cls.__RelativePath(path)
site = '%s:%d' % (path, line_no)
signature = '%s@%s' % (fulltype, site)
if len(signature) > MAX_SIGNATURE_LENGTH:
signature = 'hash:%s' % sha.new(signature).hexdigest()

return signature

@classmethod
def __GetURL(cls):
"""Returns the URL of the page currently being served.

Returns:
The full URL of the page currently being served.
"""
if os.environ['SERVER_PORT'] == '80':
scheme = 'http://'
else:
scheme = 'https://'
host = os.environ['SERVER_NAME']
script_name = urllib.quote(os.environ['SCRIPT_NAME'])
path_info = urllib.quote(os.environ['PATH_INFO'])
qs = os.environ.get('QUERY_STRING', '')
if qs:
qs = '?' + qs
return scheme + host + script_name + path_info + qs

def __GetFormatter(self):
"""Returns the log formatter for this handler.

Returns:
The log formatter to use.
"""
if self.formatter:
return self.formatter
else:
return logging._defaultFormatter

def emit(self, record):
"""Log an error to the datastore, if applicable.

Args:
The logging.LogRecord object.
See http://docs.python.org/library/logging.html#logging.LogRecord
"""
try:
if not record.exc_info:

return

signature = self.__GetSignature(record.exc_info)

old_namespace = namespace_manager.get_namespace()
try:
namespace_manager.set_namespace('')


if not memcache.add(signature, None, self.log_interval):
return


db.run_in_transaction_custom_retries(1, self.__EmitTx, signature,
record.exc_info)
finally:
namespace_manager.set_namespace(old_namespace)
except Exception:
self.handleError(record)

def __EmitTx(self, signature, exc_info):
"""Run in a transaction to insert or update the record for this transaction.

Args:
signature: The signature for this exception.
exc_info: The exception info record.
"""
today = datetime.date.today()
version = os.environ['CURRENT_VERSION_ID']
major_ver, minor_ver = version.rsplit('.', 1)
minor_ver = int(minor_ver)
key_name = ExceptionRecord.get_key_name(signature, version)

exrecord = ExceptionRecord.get_by_key_name(key_name)
if not exrecord:
exrecord = ExceptionRecord(
key_name=key_name,
signature=signature,
major_version=major_ver,
minor_version=minor_ver,
date=today,
stacktrace=self.__GetFormatter().formatException(exc_info),
http_method=os.environ['REQUEST_METHOD'],
url=self.__GetURL(),
handler=self.__RelativePath(os.environ['PATH_TRANSLATED']))

exrecord.count += 1
exrecord.put()


def register_logger(logger=None):
if not logger:
logger = logging.getLogger()
handler = ExceptionRecordingHandler()
logger.addHandler(handler)
return handler

Change log

r327 by pro...@google.com on Mar 26, 2013   Diff
App Engine Python SDK version $VERSION
Go to: 

Older revisions

All revisions of this file

File info

Size: 8028 bytes, 280 lines
Powered by Google Project Hosting