3
3
import sys
4
4
import os
5
5
import base64
6
- import json
7
6
from datetime import datetime
8
7
from datetime import timezone
9
- from urllib .parse import urlparse
10
8
import requests
11
- from requests .adapters import HTTPAdapter
12
9
13
10
14
11
def get_timestamp ():
@@ -49,14 +46,15 @@ def get_basic_auth():
49
46
return {}
50
47
51
48
52
- def validate_endpoint (endpoint , graphql_endpoint = False , max_time = 1 ):
49
+ def validate_endpoint (endpoint , graphql_endpoint = False , connection_timeout = 5 , read_timeout = 5 ):
53
50
"""
54
51
Validate an endpoint by making HTTP request and checking status code.
55
52
56
53
Args:
57
54
endpoint (str): The endpoint URL to validate
58
55
graphql_endpoint (bool): Whether this is a GraphQL endpoint
59
- max_time (int): Maximum time for request in seconds
56
+ connection_timeout (int): Connection timeout in seconds
57
+ read_timeout (int): Read timeout in seconds
60
58
"""
61
59
process_name = "endpoint.checks"
62
60
session = create_session ()
@@ -75,22 +73,23 @@ def validate_endpoint(endpoint, graphql_endpoint=False, max_time=1):
75
73
endpoint ,
76
74
headers = headers ,
77
75
json = data ,
78
- timeout = max_time ,
76
+ timeout = ( connection_timeout , read_timeout ) ,
79
77
verify = False # Equivalent to curl's -k flag
80
78
)
81
79
else :
82
80
# Regular endpoint check
83
81
response = session .get (
84
82
endpoint ,
85
83
headers = headers ,
86
- timeout = max_time ,
84
+ timeout = ( connection_timeout , read_timeout ) ,
87
85
verify = False # Equivalent to curl's -k flag
88
86
)
89
87
90
88
status_code = response .status_code
91
89
92
90
except requests .exceptions .Timeout :
93
- print (f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } timed out after { max_time } seconds" )
91
+ print (
92
+ f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } timed out (connection: { connection_timeout } s, read: { read_timeout } s)" )
94
93
return False
95
94
except requests .exceptions .ConnectionError :
96
95
print (f"{ get_timestamp ()} [{ process_name } ] - Failed to connect to endpoint { endpoint } " )
@@ -104,12 +103,14 @@ def validate_endpoint(endpoint, graphql_endpoint=False, max_time=1):
104
103
print (f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } is not found - status code: { status_code } " )
105
104
return False
106
105
elif status_code == 401 :
107
- print (f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } requires authentication - status code: { status_code } . Please provide valid credentials via SE_ROUTER_USERNAME and SE_ROUTER_PASSWORD environment variables." )
106
+ print (
107
+ f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } requires authentication - status code: { status_code } . Please provide valid credentials via SE_ROUTER_USERNAME and SE_ROUTER_PASSWORD environment variables." )
108
108
return False
109
109
elif status_code != 200 :
110
110
print (f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } is not available - status code: { status_code } " )
111
111
return False
112
112
113
+ print (f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } is reachable - status code: { status_code } " )
113
114
return True
114
115
115
116
@@ -123,10 +124,10 @@ def main():
123
124
124
125
endpoint = sys .argv [1 ]
125
126
graphql_endpoint = len (sys .argv ) > 2 and sys .argv [2 ].lower () == 'true'
126
- max_time = int (os .environ .get ('SE_ENDPOINT_CHECK_TIMEOUT' , 1 ))
127
+ max_time = int (os .environ .get ('SE_ENDPOINT_CHECK_TIMEOUT' , 5 ))
127
128
128
129
# Validate the endpoint
129
- success = validate_endpoint (endpoint , graphql_endpoint , max_time )
130
+ success = validate_endpoint (endpoint , graphql_endpoint , max_time , max_time )
130
131
131
132
# Exit with appropriate code
132
133
sys .exit (0 if success else 1 )
0 commit comments