33import sys
44import os
55import base64
6- import json
76from datetime import datetime
87from datetime import timezone
9- from urllib .parse import urlparse
108import requests
11- from requests .adapters import HTTPAdapter
129
1310
1411def get_timestamp ():
@@ -49,14 +46,15 @@ def get_basic_auth():
4946 return {}
5047
5148
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 ):
5350 """
5451 Validate an endpoint by making HTTP request and checking status code.
5552
5653 Args:
5754 endpoint (str): The endpoint URL to validate
5855 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
6058 """
6159 process_name = "endpoint.checks"
6260 session = create_session ()
@@ -75,22 +73,23 @@ def validate_endpoint(endpoint, graphql_endpoint=False, max_time=1):
7573 endpoint ,
7674 headers = headers ,
7775 json = data ,
78- timeout = max_time ,
76+ timeout = ( connection_timeout , read_timeout ) ,
7977 verify = False # Equivalent to curl's -k flag
8078 )
8179 else :
8280 # Regular endpoint check
8381 response = session .get (
8482 endpoint ,
8583 headers = headers ,
86- timeout = max_time ,
84+ timeout = ( connection_timeout , read_timeout ) ,
8785 verify = False # Equivalent to curl's -k flag
8886 )
8987
9088 status_code = response .status_code
9189
9290 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)" )
9493 return False
9594 except requests .exceptions .ConnectionError :
9695 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):
104103 print (f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } is not found - status code: { status_code } " )
105104 return False
106105 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." )
108108 return False
109109 elif status_code != 200 :
110110 print (f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } is not available - status code: { status_code } " )
111111 return False
112112
113+ print (f"{ get_timestamp ()} [{ process_name } ] - Endpoint { endpoint } is reachable - status code: { status_code } " )
113114 return True
114115
115116
@@ -123,10 +124,10 @@ def main():
123124
124125 endpoint = sys .argv [1 ]
125126 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 ))
127128
128129 # Validate the endpoint
129- success = validate_endpoint (endpoint , graphql_endpoint , max_time )
130+ success = validate_endpoint (endpoint , graphql_endpoint , max_time , max_time )
130131
131132 # Exit with appropriate code
132133 sys .exit (0 if success else 1 )
0 commit comments