Skip to content

Commit 33d9c62

Browse files
RyanGWU82erunion
andauthored
feat(python): documented way to skip logging a request (#271)
* feat: documented way to skip logging a request * Update packages/python/README.md Co-authored-by: Jon Ursenbach <[email protected]> * Update packages/python/README.md Co-authored-by: Jon Ursenbach <[email protected]> * Update packages/python/README.md Co-authored-by: Jon Ursenbach <[email protected]> Co-authored-by: Jon Ursenbach <[email protected]>
1 parent bb0d41d commit 33d9c62

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

packages/python/README.md

+31-15
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ First you'll need to write a "grouping function" to inform ReadMe of the user or
2424
```python
2525
def grouping_function(request):
2626
# You can lookup your user here, pull it off the request object, etc.
27-
return {
28-
"api_key": "unique api_key of the user",
29-
"label": "label for us to show for this user (ie email, project name, user name, etc)",
30-
"email": "email address for user"
31-
}
27+
# Your grouping function should return None if you don't want the request
28+
# to be logged, and otherwise return a structure like the one below.
29+
if user_is_authenticated:
30+
return {
31+
"api_key": "unique api_key of the user",
32+
"label": "label for us to show for this user (ie email, project name, user name, etc)",
33+
"email": "email address for user"
34+
}
35+
else:
36+
return None
3237
```
3338

3439
Second, once you have written a grouping function, add a `README_METRICS_CONFIG` setting using the `MetricsApiConfig` helper object:
@@ -69,11 +74,16 @@ First you'll need to write a "grouping function" to inform ReadMe of the user or
6974
```python
7075
def grouping_function(request):
7176
# You can lookup your user here, pull it off the request object, etc.
72-
return {
73-
"api_key": "unique api_key of the user",
74-
"label": "label for us to show for this user (ie email, project name, user name, etc)",
75-
"email": "email address for user"
76-
}
77+
# Your grouping function should return None if you don't want the request
78+
# to be logged, and otherwise return a structure like the one below.
79+
if user_is_authenticated:
80+
return {
81+
"api_key": "unique api_key of the user",
82+
"label": "label for us to show for this user (ie email, project name, user name, etc)",
83+
"email": "email address for user"
84+
}
85+
else:
86+
return None
7787
```
7888

7989
Second, once you have written a grouping function, set up the extension wherever you create your Flask app.
@@ -113,11 +123,17 @@ First you'll need to write a "grouping function" to inform ReadMe of the user or
113123

114124
```python
115125
def grouping_function(request):
116-
return {
117-
"api_key": "unique api_key of the user",
118-
"label": "label for us to show for this user (ie email, project name, user name, etc)",
119-
"email": "email address for user"
120-
}
126+
# You can lookup your user here, pull it off the request object, etc.
127+
# Your grouping function should return None if you don't want the request
128+
# to be logged, and otherwise return a structure like the one below.
129+
if user_is_authenticated:
130+
return {
131+
"api_key": "unique api_key of the user",
132+
"label": "label for us to show for this user (ie email, project name, user name, etc)",
133+
"email": "email address for user"
134+
}
135+
else:
136+
return None
121137
```
122138

123139
Then, wherever you initialize your WSGI app, you can wrap it with our middleware wrapper:

packages/python/readme_metrics/Metrics.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,16 @@ def process(self, request, response: ResponseInfoWrapper) -> None:
5353
)
5454
return
5555

56-
self.queue.put(self.payload_builder(request, response))
56+
payload = self.payload_builder(request, response)
57+
if payload is None:
58+
# PayloadBuilder returns None when the grouping function returns
59+
# None (an indication that the request should not be logged.)
60+
self.config.LOGGER.debug(
61+
f"Not enqueueing request, grouping function returned None"
62+
)
63+
return
64+
65+
self.queue.put(payload)
5766
if self.queue.qsize() >= self.config.BUFFER_LENGTH:
5867
args = (self.config, self.queue)
5968
if self.config.IS_BACKGROUND_MODE:

packages/python/readme_metrics/PayloadBuilder.py

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def __call__(self, request, response: ResponseInfoWrapper) -> dict:
5858
dict: Payload object (ready to be serialized and sent to ReadMe)
5959
"""
6060
group = self.grouping_function(request)
61+
if group is None:
62+
return None
63+
6164
if "api_key" in group:
6265
group["id"] = group["api_key"]
6366
del group["api_key"]

packages/python/readme_metrics/tests/PayloadBuilder_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ def testGroupingFunction(self):
207207
assert group["email"] == "[email protected]"
208208
assert group["label"] == "Spam Musubi"
209209

210+
def testGroupingFunctionNone(self):
211+
# PayloadBuilder should return None if the grouping_function returns
212+
# None (which means not to log the request).
213+
config = self.mockMiddlewareConfig(grouping_function=lambda req: None)
214+
215+
responseObjectString = "{ 'responseObject': 'value' }"
216+
environ = Environ.MockEnviron().getEnvironForRequest(b"", "POST")
217+
app = MockApplication(responseObjectString)
218+
metrics = MetricsCoreMock()
219+
middleware = MetricsMiddleware(app, config)
220+
middleware.metrics_core = metrics
221+
next(middleware(environ, app.mockStartResponse))
222+
payload_builder = self.createPayload(config)
223+
payload = payload_builder(metrics.req, metrics.res)
224+
assert payload is None
225+
210226
def testDeprecatedIDField(self):
211227
config = self.mockMiddlewareConfig(
212228
grouping_function=lambda req: {

0 commit comments

Comments
 (0)