Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Ley
synapse
Commits
8460e48d
Commit
8460e48d
authored
6 years ago
by
Richard van der Hoff
Browse files
Options
Downloads
Patches
Plain Diff
Move request_id management into SynapseRequest
parent
18e144fe
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
synapse/http/server.py
+16
-15
16 additions, 15 deletions
synapse/http/server.py
synapse/http/site.py
+9
-0
9 additions, 0 deletions
synapse/http/site.py
with
25 additions
and
15 deletions
synapse/http/server.py
+
16
−
15
View file @
8460e48d
...
@@ -46,38 +46,40 @@ import simplejson
...
@@ -46,38 +46,40 @@ import simplejson
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
_next_request_id
=
0
def
request_handler
(
include_metrics
=
False
):
def
request_handler
(
include_metrics
=
False
):
"""
Decorator for ``wrap_request_handler``
"""
"""
Decorator for ``wrap_request_handler``
"""
return
lambda
request_handler
:
wrap_request_handler
(
request_handler
,
include_metrics
)
return
lambda
request_handler
:
wrap_request_handler
(
request_handler
,
include_metrics
)
def
wrap_request_handler
(
request_handler
,
include_metrics
=
False
):
def
wrap_request_handler
(
request_handler
,
include_metrics
=
False
):
"""
Wraps a
method that acts as a
request handler with the necessary logging
"""
Wraps a request handler
method
with the necessary logging
and exception
and exception
handling.
handling.
The method must have a signature of
"
handle_foo(self, request)
"
. The
The
handler
method must have a signature of
"
handle_foo(self, request)
"
,
argument
"
self
"
must have
"
version_string
"
and
"
clock
"
attributes
. The
where
"
self
"
must have
"
version_string
"
and
"
clock
"
attributes
(and
argument
"
request
"
must be a
twisted HTTP r
equest.
"
request
"
must be a
SynapseR
equest
)
.
The
method
must return a deferred. If the deferred succeeds we assume that
The
handler
must return a deferred. If the deferred succeeds we assume that
a response has been sent. If the deferred fails with a SynapseError we use
a response has been sent. If the deferred fails with a SynapseError we use
it to send a JSON response with the appropriate HTTP reponse code. If the
it to send a JSON response with the appropriate HTTP reponse code. If the
deferred fails with any other type of error we send a 500 reponse.
deferred fails with any other type of error we send a 500 reponse.
We insert a unique request-id into the logging context for this request and
As well as calling `request.processing` (which will log the response and
log the response and duration for this request.
duration for this request), the wrapped request handler will insert the
request id into the logging context.
"""
"""
@defer.inlineCallbacks
@defer.inlineCallbacks
def
wrapped_request_handler
(
self
,
request
):
def
wrapped_request_handler
(
self
,
request
):
global
_next_request_id
"""
request_id
=
"
%s-%s
"
%
(
request
.
method
,
_next_request_id
)
Args:
_next_request_id
+=
1
self:
request (synapse.http.site.SynapseRequest):
"""
request_id
=
request
.
get_request_id
()
with
LoggingContext
(
request_id
)
as
request_context
:
with
LoggingContext
(
request_id
)
as
request_context
:
request_context
.
request
=
request_id
with
Measure
(
self
.
clock
,
"
wrapped_request_handler
"
):
with
Measure
(
self
.
clock
,
"
wrapped_request_handler
"
):
request_metrics
=
RequestMetrics
()
request_metrics
=
RequestMetrics
()
# we start the request metrics timer here with an initial stab
# we start the request metrics timer here with an initial stab
...
@@ -87,7 +89,6 @@ def wrap_request_handler(request_handler, include_metrics=False):
...
@@ -87,7 +89,6 @@ def wrap_request_handler(request_handler, include_metrics=False):
servlet_name
=
self
.
__class__
.
__name__
servlet_name
=
self
.
__class__
.
__name__
request_metrics
.
start
(
self
.
clock
,
name
=
servlet_name
)
request_metrics
.
start
(
self
.
clock
,
name
=
servlet_name
)
request_context
.
request
=
request_id
with
request
.
processing
():
with
request
.
processing
():
try
:
try
:
with
PreserveLoggingContext
(
request_context
):
with
PreserveLoggingContext
(
request_context
):
...
...
This diff is collapsed.
Click to expand it.
synapse/http/site.py
+
9
−
0
View file @
8460e48d
...
@@ -22,6 +22,8 @@ import time
...
@@ -22,6 +22,8 @@ import time
ACCESS_TOKEN_RE
=
re
.
compile
(
br
'
(\?.*access(_|%5[Ff])token=)[^&]*(.*)$
'
)
ACCESS_TOKEN_RE
=
re
.
compile
(
br
'
(\?.*access(_|%5[Ff])token=)[^&]*(.*)$
'
)
_next_request_seq
=
0
class
SynapseRequest
(
Request
):
class
SynapseRequest
(
Request
):
def
__init__
(
self
,
site
,
*
args
,
**
kw
):
def
__init__
(
self
,
site
,
*
args
,
**
kw
):
...
@@ -30,6 +32,10 @@ class SynapseRequest(Request):
...
@@ -30,6 +32,10 @@ class SynapseRequest(Request):
self
.
authenticated_entity
=
None
self
.
authenticated_entity
=
None
self
.
start_time
=
0
self
.
start_time
=
0
global
_next_request_seq
self
.
request_seq
=
_next_request_seq
_next_request_seq
+=
1
def
__repr__
(
self
):
def
__repr__
(
self
):
# We overwrite this so that we don't log ``access_token``
# We overwrite this so that we don't log ``access_token``
return
'
<%s at 0x%x method=%s uri=%s clientproto=%s site=%s>
'
%
(
return
'
<%s at 0x%x method=%s uri=%s clientproto=%s site=%s>
'
%
(
...
@@ -41,6 +47,9 @@ class SynapseRequest(Request):
...
@@ -41,6 +47,9 @@ class SynapseRequest(Request):
self
.
site
.
site_tag
,
self
.
site
.
site_tag
,
)
)
def
get_request_id
(
self
):
return
"
%s-%i
"
%
(
self
.
method
,
self
.
request_seq
)
def
get_redacted_uri
(
self
):
def
get_redacted_uri
(
self
):
return
ACCESS_TOKEN_RE
.
sub
(
return
ACCESS_TOKEN_RE
.
sub
(
br
'
\1<redacted>\3
'
,
br
'
\1<redacted>\3
'
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment