Skip to content

Commit d8b3258

Browse files
authored
chore: improve logs on Tokens API (#1216)
1 parent 6699db5 commit d8b3258

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

functions-python/tokens/.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[run]
22
omit =
33
*/test*/*
4+
*/helpers/*
5+
*/shared/*
46

57
[report]
68
exclude_lines =

functions-python/tokens/function_config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"timeout": 20,
77
"memory": "128Mi",
88
"trigger_http": true,
9-
"include_folders": [],
9+
"include_folders": ["helpers"],
10+
"include_api_folders": ["common"],
1011
"environment_variables": [],
1112
"secret_environment_variables": [
1213
{
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
# Common packages
12
functions-framework==3.*
3+
google-cloud-logging
4+
psycopg2-binary==2.9.6
5+
aiohttp~=3.10.5
6+
asyncio~=3.4.3
7+
urllib3~=2.2.2
8+
requests~=2.32.3
9+
attrs~=23.1.0
10+
pluggy~=1.3.0
11+
certifi~=2024.7.4
12+
13+
# Flask dependencies for OpenAPI implementation
214
flask
3-
requests
4-
PyJWT
5-
werkzeug
15+
werkzeug
16+
17+
# JWT library for JWT token verification
18+
PyJWT

functions-python/tokens/src/main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import json
1818
import os
1919
import jwt
20+
import logging
2021
import requests
2122
import flask
2223
import functions_framework
@@ -25,6 +26,7 @@
2526
from datetime import datetime
2627
from datetime import timezone
2728
from werkzeug.exceptions import UnsupportedMediaType, BadRequest
29+
from shared.helpers.logger import init_logger
2830

2931
IDP_TOKEN_URL: Final[str] = "https://securetoken.googleapis.com/v1/token"
3032
HEADERS: Final[dict[str, str]] = {
@@ -33,6 +35,9 @@
3335
}
3436

3537

38+
init_logger()
39+
40+
3641
class TokenPostResponse:
3742
def __init__(
3843
self, access_token: str, expiration_datetime_utc: str, token_type: str
@@ -117,7 +122,7 @@ def extract_refresh_token(request):
117122
except UnsupportedMediaType as e:
118123
raise e
119124
except Exception as e:
120-
print(f"Error extracting refresh token : {e}")
125+
logging.error("Error extracting refresh token : %s", e)
121126
raise BadRequest()
122127

123128

@@ -154,7 +159,7 @@ def tokens_post(request: flask.Request) -> Response:
154159
idp_response = get_idp_response(request.get_json().get("refresh_token"))
155160

156161
if idp_response.status_code != 200:
157-
print(f"Error retrieving refresh token : {idp_response.json()}")
162+
logging.error("Error retrieving refresh token : %s", idp_response.json())
158163
return Response(
159164
status=500,
160165
mimetype="application/json",
@@ -166,7 +171,7 @@ def tokens_post(request: flask.Request) -> Response:
166171

167172
return create_response_from_idp(idp_response)
168173
except UnsupportedMediaType as e:
169-
print(f"Error creating response from idp : {e}")
174+
logging.error("Error creating response from idp : %s", e)
170175
return Response(
171176
status=e.code,
172177
mimetype="application/json",
@@ -183,7 +188,7 @@ def tokens_post(request: flask.Request) -> Response:
183188
response=json.dumps(TokenPostResponseError("Bad Request.").__dict__),
184189
)
185190
except Exception as e:
186-
print(f"Error creating response from idp : {e}")
191+
logging.error("Error creating response from idp : %s", e)
187192
return Response(
188193
status=500,
189194
mimetype="application/json",

0 commit comments

Comments
 (0)