@@ -21,12 +21,9 @@ async def test_start_stop_streaming(self, tmp_path, component_tests_config):
2121 print ("test_start_stop_streaming" )
2222 config = component_tests_config (cfd_mode = CfdModes .NAMED , run_proxy_dns = False , provide_ingress = False )
2323 LOGGER .debug (config )
24- headers = {}
25- headers ["Content-Type" ] = "application/json"
2624 config_path = write_config (tmp_path , config .full_config )
2725 with start_cloudflared (tmp_path , config , cfd_args = ["run" , "--hello-world" ], new_process = True ):
28- wait_tunnel_ready (tunnel_url = config .get_url (),
29- require_min_connections = 1 )
26+ wait_tunnel_ready (tunnel_url = config .get_url (), require_min_connections = 1 )
3027 cfd_cli = CloudflaredCli (config , config_path , LOGGER )
3128 url = cfd_cli .get_management_wsurl ("logs" , config , config_path )
3229 async with connect (url , open_timeout = 5 , close_timeout = 3 ) as websocket :
@@ -43,19 +40,16 @@ async def test_streaming_logs(self, tmp_path, component_tests_config):
4340 print ("test_streaming_logs" )
4441 config = component_tests_config (cfd_mode = CfdModes .NAMED , run_proxy_dns = False , provide_ingress = False )
4542 LOGGER .debug (config )
46- headers = {}
47- headers ["Content-Type" ] = "application/json"
4843 config_path = write_config (tmp_path , config .full_config )
4944 with start_cloudflared (tmp_path , config , cfd_args = ["run" , "--hello-world" ], new_process = True ):
50- wait_tunnel_ready (tunnel_url = config .get_url (),
51- require_min_connections = 1 )
45+ wait_tunnel_ready (tunnel_url = config .get_url (), require_min_connections = 1 )
5246 cfd_cli = CloudflaredCli (config , config_path , LOGGER )
5347 url = cfd_cli .get_management_wsurl ("logs" , config , config_path )
5448 async with connect (url , open_timeout = 5 , close_timeout = 5 ) as websocket :
5549 # send start_streaming
5650 await websocket .send ('{"type": "start_streaming"}' )
5751 # send some http requests to the tunnel to trigger some logs
58- await asyncio . wait_for ( generate_and_validate_log_event ( websocket , config .get_url () ), 10 )
52+ await generate_and_validate_http_events ( websocket , config .get_url (), 10 )
5953 # send stop_streaming
6054 await websocket .send ('{"type": "stop_streaming"}' )
6155
@@ -68,26 +62,51 @@ async def test_streaming_logs_filters(self, tmp_path, component_tests_config):
6862 print ("test_streaming_logs_filters" )
6963 config = component_tests_config (cfd_mode = CfdModes .NAMED , run_proxy_dns = False , provide_ingress = False )
7064 LOGGER .debug (config )
71- headers = {}
72- headers ["Content-Type" ] = "application/json"
7365 config_path = write_config (tmp_path , config .full_config )
7466 with start_cloudflared (tmp_path , config , cfd_args = ["run" , "--hello-world" ], new_process = True ):
75- wait_tunnel_ready (tunnel_url = config .get_url (),
76- require_min_connections = 1 )
67+ wait_tunnel_ready (tunnel_url = config .get_url (), require_min_connections = 1 )
7768 cfd_cli = CloudflaredCli (config , config_path , LOGGER )
7869 url = cfd_cli .get_management_wsurl ("logs" , config , config_path )
7970 async with connect (url , open_timeout = 5 , close_timeout = 5 ) as websocket :
80- # send start_streaming with info logs only
71+ # send start_streaming with tcp logs only
8172 await websocket .send (json .dumps ({
8273 "type" : "start_streaming" ,
8374 "filters" : {
84- "events" : ["tcp" ]
75+ "events" : ["tcp" ],
76+ "level" : "debug"
8577 }
8678 }))
8779 # don't expect any http logs
8880 await generate_and_validate_no_log_event (websocket , config .get_url ())
8981 # send stop_streaming
9082 await websocket .send ('{"type": "stop_streaming"}' )
83+
84+ @pytest .mark .asyncio
85+ async def test_streaming_logs_sampling (self , tmp_path , component_tests_config ):
86+ """
87+ Validates that a streaming logs connection will stream logs with sampling.
88+ """
89+ print ("test_streaming_logs_sampling" )
90+ config = component_tests_config (cfd_mode = CfdModes .NAMED , run_proxy_dns = False , provide_ingress = False )
91+ LOGGER .debug (config )
92+ config_path = write_config (tmp_path , config .full_config )
93+ with start_cloudflared (tmp_path , config , cfd_args = ["run" , "--hello-world" ], new_process = True ):
94+ wait_tunnel_ready (tunnel_url = config .get_url (), require_min_connections = 1 )
95+ cfd_cli = CloudflaredCli (config , config_path , LOGGER )
96+ url = cfd_cli .get_management_wsurl ("logs" , config , config_path )
97+ async with connect (url , open_timeout = 5 , close_timeout = 5 ) as websocket :
98+ # send start_streaming with info logs only
99+ await websocket .send (json .dumps ({
100+ "type" : "start_streaming" ,
101+ "filters" : {
102+ "sampling" : 0.5
103+ }
104+ }))
105+ # don't expect any http logs
106+ count = await generate_and_validate_http_events (websocket , config .get_url (), 10 )
107+ assert count < (10 * 2 ) # There are typically always two log lines for http requests (request and response)
108+ # send stop_streaming
109+ await websocket .send ('{"type": "stop_streaming"}' )
91110
92111 @pytest .mark .asyncio
93112 async def test_streaming_logs_actor_override (self , tmp_path , component_tests_config ):
@@ -97,12 +116,9 @@ async def test_streaming_logs_actor_override(self, tmp_path, component_tests_con
97116 print ("test_streaming_logs_actor_override" )
98117 config = component_tests_config (cfd_mode = CfdModes .NAMED , run_proxy_dns = False , provide_ingress = False )
99118 LOGGER .debug (config )
100- headers = {}
101- headers ["Content-Type" ] = "application/json"
102119 config_path = write_config (tmp_path , config .full_config )
103120 with start_cloudflared (tmp_path , config , cfd_args = ["run" , "--hello-world" ], new_process = True ):
104- wait_tunnel_ready (tunnel_url = config .get_url (),
105- require_min_connections = 1 )
121+ wait_tunnel_ready (tunnel_url = config .get_url (), require_min_connections = 1 )
106122 cfd_cli = CloudflaredCli (config , config_path , LOGGER )
107123 url = cfd_cli .get_management_wsurl ("logs" , config , config_path )
108124 task = asyncio .ensure_future (start_streaming_to_be_remotely_closed (url ))
@@ -131,16 +147,22 @@ async def start_streaming_override(url):
131147 await asyncio .sleep (1 )
132148
133149# Every http request has two log lines sent
134- async def generate_and_validate_log_event (websocket : WebSocketClientProtocol , url : str ):
135- send_request (url )
136- req_line = await websocket .recv ()
137- log_line = json .loads (req_line )
138- assert log_line ["type" ] == "logs"
139- assert log_line ["logs" ][0 ]["event" ] == "http"
140- req_line = await websocket .recv ()
141- log_line = json .loads (req_line )
142- assert log_line ["type" ] == "logs"
143- assert log_line ["logs" ][0 ]["event" ] == "http"
150+ async def generate_and_validate_http_events (websocket : WebSocketClientProtocol , url : str , count_send : int ):
151+ for i in range (count_send ):
152+ send_request (url )
153+ # There are typically always two log lines for http requests (request and response)
154+ count = 0
155+ while True :
156+ try :
157+ req_line = await asyncio .wait_for (websocket .recv (), 2 )
158+ log_line = json .loads (req_line )
159+ assert log_line ["type" ] == "logs"
160+ assert log_line ["logs" ][0 ]["event" ] == "http"
161+ count += 1
162+ except asyncio .TimeoutError :
163+ # ignore timeout from waiting for recv
164+ break
165+ return count
144166
145167# Every http request has two log lines sent
146168async def generate_and_validate_no_log_event (websocket : WebSocketClientProtocol , url : str ):
0 commit comments