PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/appengine/appengine_config.py

https://code.google.com/p/pageforest/
Python | 227 lines | 55 code | 47 blank | 125 comment | 8 complexity | 5fb87639f2ca24a61fbf856984cdf17e MD5 | raw file
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2007 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """Sample Appstats Configuration.
  18. There are four sections:
  19. 0) WSGI middleware declaration.
  20. 1) Django version declaration.
  21. 2) Configuration constants.
  22. 3) Configuration functions.
  23. Also a section at the end for the remote_api handler.
  24. """
  25. import logging
  26. import random
  27. import re
  28. # 0) WSGI middleware declaration.
  29. # Only use this if you're not Django; with Django, it's easier to add
  30. # 'google.appengine.ext.appstats.recording.AppstatsDjangoMiddleware',
  31. # to your Django settings.py file.
  32. # # def webapp_add_wsgi_middleware(app):
  33. # # from google.appengine.ext.appstats import recording
  34. # # app = recording.appstats_wsgi_middleware(app)
  35. # # return app
  36. # 1) Django version declaration.
  37. # If your application uses Django and requires a specific version of
  38. # Django, uncomment the following block of three lines. Currently
  39. # supported values for the Django version are '0.96' (the default),
  40. # '1.0', and '1.1'.
  41. # # from google.appengine.dist import use_library
  42. # # use_library('django', '1.0')
  43. # # import django
  44. # 2) Configuration constants.
  45. # DEBUG: True of False. When True, verbose messages are logged at the
  46. # DEBUG level. Also, this flag is causes tracebacks to be shown in
  47. # the web UI when an exception occurs. (Tracebacks are always logged
  48. # at the ERROR level as well.)
  49. appstats_DEBUG = False
  50. # DUMP_LEVEL: -1, 0, 1 or 2. Controls how much debug output is
  51. # written to the logs by the internal dump() function during event
  52. # recording. -1 dumps nothing; 0 dumps one line of information; 1
  53. # dumps more informat and 2 dumps the maximum amount of information.
  54. # You would only need to change this if you were debugging the
  55. # recording implementation.
  56. appstats_DUMP_LEVEL = -1
  57. # The following constants control the resolution and range of the
  58. # memcache keys used to record information about individual requests.
  59. # Two requests that are closer than KEY_DISTANCE milliseconds will be
  60. # mapped to the same key (thus losing all information about the
  61. # earlier of the two requests). Up to KEY_MODULUS distinct keys are
  62. # generated; after KEY_DISTANCE * KEY_MODULUS milliseconds the key
  63. # values roll over. Increasing KEY_MODULUS causes a proportional
  64. # increase of the amount of data saved in memcache. Increasing
  65. # KEY_DISTANCE causes a requests during a larger timespan to be
  66. # recorded, at the cost of increasing risk of assigning the same key
  67. # to two adjacent requests.
  68. appstats_KEY_DISTANCE = 100
  69. appstats_KEY_MODULUS = 1000
  70. # The following constants control the namespace and key values used to
  71. # store information in memcache. You can safely leave this alone.
  72. appstats_KEY_NAMESPACE = '__appstats__'
  73. appstats_KEY_PREFIX = '__appstats__'
  74. appstats_KEY_TEMPLATE = ':%06d'
  75. appstats_PART_SUFFIX = ':part'
  76. appstats_FULL_SUFFIX = ':full'
  77. appstats_LOCK_SUFFIX = '<lock>'
  78. # Numerical limits on how much information is saved for each event.
  79. # MAX_STACK limits the number of stack frames saved; MAX_LOCALS limits
  80. # the number of local variables saved per stack frame. MAX_REPR
  81. # limits the length of the string representation of each variable
  82. # saved; MAX_DEPTH limits the nesting depth used when computing the
  83. # string representation of structured variables (e.g. lists of lists).
  84. appstats_MAX_STACK = 10
  85. appstats_MAX_LOCALS = 10
  86. appstats_MAX_REPR = 100
  87. appstats_MAX_DEPTH = 10
  88. # Regular expressions. These are matched against the 'code key' of a
  89. # stack frame, which is a string of the form
  90. # '<filename>:<function>:<lineno>'. If the code key of a stack frame
  91. # matches RE_STACK_BOTTOM, it and all remaining stack frames are
  92. # skipped. If the code key matches RE_STACK_SKIP, that frame is not
  93. # saved but subsequent frames may be saved.
  94. appstats_RE_STACK_BOTTOM = r'dev_appserver\.py'
  95. appstats_RE_STACK_SKIP = r'recording\.py|apiproxy_stub_map\.py'
  96. # Timeout for memcache lock management, in seconds.
  97. appstats_LOCK_TIMEOUT = 1
  98. # Timezone offset. This is used to convert recorded times (which are
  99. # all in UTC) to local time. The default is US/Pacific winter time.
  100. appstats_TZOFFSET = 8 * 3600
  101. # URL path (sans host) leading to the stats UI. Should match app.yaml.
  102. # If "builtins: - appstats: on" is used, the path should be /_ah/stats.
  103. appstats_stats_url = '/_ah/stats'
  104. # Fraction of requests to record. Set this to a float between 0.0
  105. # and 1.0 to record that fraction of all requests.
  106. appstats_RECORD_FRACTION = 1.0
  107. # List of dicts mapping env vars to regular expressions. Each dict
  108. # specifies a set of filters to be 'and'ed together. The keys are
  109. # environment variables, the values are *match* regular expressions.
  110. # A request is recorded if it matches all filters of at least one
  111. # dict. If the FILTER_LIST variable is empty, all requests are
  112. # recorded. Missing environment variables are considered to have
  113. # the empty string as value. If a regular expression starts with
  114. # '!', the sense of the match is negated (the value should *not*
  115. # match the expression).
  116. appstats_FILTER_LIST = []
  117. # 3) Configuration functions.
  118. # should_record() can be used to record a random percentage of calls.
  119. # The argument is the CGI or WSGI environment dict. The default
  120. # implementation returns True iff the request matches FILTER_LIST (see
  121. # above) *and* random.random() < RECORD_FRACTION.
  122. def appstats_should_record(env):
  123. if appstats_FILTER_LIST:
  124. logging.debug('FILTER_LIST: %r', appstats_FILTER_LIST)
  125. for filter_dict in appstats_FILTER_LIST:
  126. for key, regex in filter_dict.iteritems():
  127. negated = isinstance(regex, str) and regex.startswith('!')
  128. if negated:
  129. regex = regex[1:]
  130. value = env.get(key, '')
  131. if bool(re.match(regex, value)) == negated:
  132. logging.debug('No match on %r for %s=%r', regex, key, value)
  133. break
  134. else:
  135. logging.debug('Match on %r', filter_dict)
  136. break
  137. else:
  138. logging.debug('Non-empty FILTER_LIST, but no filter matches')
  139. return False
  140. if appstats_RECORD_FRACTION >= 1.0:
  141. return True
  142. return random.random() < appstats_RECORD_FRACTION
  143. # The following functions are called by the UI code only; they don't
  144. # affect the recorded information.
  145. # normalize_path() takes a path and returns an 'path key'. The path
  146. # key is used by the UI to compute statistics for similar URLs. If
  147. # your application has a large or infinite URL space (e.g. each issue
  148. # in an issue tracker might have its own numeric URL), this function
  149. # can be used to produce more meaningful statistics.
  150. def appstats_normalize_path(path):
  151. return path
  152. # extract_key() is a lower-level function with the same purpose as
  153. # normalize_key(). It can be used to lump different request methods
  154. # (e.g. GET and POST) together, or conversely to use other information
  155. # on the request object (mostly the query string) to produce a more
  156. # fine-grained path key. The argument is a StatsProto object; this is
  157. # a class defined in recording.py. Useful methods are:
  158. # # - http_method()
  159. # - http_path()
  160. # - http_query()
  161. # - http_status()
  162. # # Note that the StatsProto argument is loaded only with summary
  163. # information; this means you cannot access the request headers.
  164. def appstats_extract_key(request):
  165. key = appstats_normalize_path(request.http_path())
  166. if request.http_method() != 'GET':
  167. key = '%s %s' % (request.http_method(), key)
  168. return key
  169. # ########################################
  170. # Remote_API Authentication configuration.
  171. # # See google/appengine/ext/remote_api/handler.py for more information.
  172. # In most cases, you will not want to configure this.
  173. # # remoteapi_CUSTOM_ENVIRONMENT_AUTHENTICATION = (
  174. # 'HTTP_X_APPENGINE_INBOUND_APPID', ['a trusted appid here'])
  175. remoteapi_CUSTOM_ENVIRONMENT_AUTHENTICATION = ('HTTP_X_APPENGINE_INBOUND_APPID', ['pageforest'])