@@ -4,7 +4,6 @@ use graphql_client::{GraphQLQuery, Response};
44use opentelemetry_proto:: tonic:: metrics:: v1:: Metric ;
55use prometheus:: { CounterVec , GaugeVec , Opts , Registry } ;
66use crate :: metrics:: prometheus_registry_to_opentelemetry_metrics;
7- use web_time:: SystemTime ;
87use worker:: console_log;
98
109// The paths are relative to the directory where your `Cargo.toml` is located.
@@ -72,9 +71,11 @@ type float64 = f64;
7271#[ allow( non_camel_case_types) ]
7372type uint16 = u16 ;
7473
75- pub async fn do_get_workers_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_workers_analytics_query:: Variables ) -> Result < Vec < Metric > , Box < dyn Error > > {
74+ pub async fn do_get_workers_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_workers_analytics_query:: Variables , debug_logging : bool , fallback_timestamp_nanos : u64 ) -> Result < Vec < Metric > , Box < dyn Error > > {
7675 let request_body = GetWorkersAnalyticsQuery :: build_query ( variables) ;
77- console_log ! ( "[Workers] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
76+ if debug_logging {
77+ console_log ! ( "[Workers] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
78+ }
7879 let client = reqwest:: Client :: new ( ) ;
7980 let res = client. post ( cloudflare_api_url)
8081 . bearer_auth ( cloudflare_api_key)
@@ -86,7 +87,9 @@ pub async fn do_get_workers_analytics_query(cloudflare_api_url: &String, cloudfl
8687 }
8788
8889 let response_text = res. text ( ) . await ?;
89- console_log ! ( "[Workers] GraphQL response: {}" , response_text) ;
90+ if debug_logging {
91+ console_log ! ( "[Workers] GraphQL response: {}" , response_text) ;
92+ }
9093 let response_body: Response < get_workers_analytics_query:: ResponseData > = serde_json:: from_str ( & response_text) ?;
9194 if response_body. errors . is_some ( ) {
9295 console_log ! ( "[Workers] GraphQL query failed: {:?}" , response_body. errors) ;
@@ -111,6 +114,14 @@ pub async fn do_get_workers_analytics_query(cloudflare_api_url: &String, cloudfl
111114 let worker_duration = GaugeVec :: new ( worker_duration_opts, & [ "script_name" , "quantile" ] ) . unwrap ( ) ;
112115 registry. register ( Box :: new ( worker_duration. clone ( ) ) ) . unwrap ( ) ;
113116
117+ let worker_wall_time_opts = Opts :: new ( "cloudflare_worker_wall_time" , "Sum of wall time - microseconds" ) ;
118+ let worker_wall_time = CounterVec :: new ( worker_wall_time_opts, & [ "script_name" ] ) . unwrap ( ) ;
119+ registry. register ( Box :: new ( worker_wall_time. clone ( ) ) ) . unwrap ( ) ;
120+
121+ let worker_subrequests_opts = Opts :: new ( "cloudflare_worker_subrequests" , "Sum of subrequests" ) ;
122+ let worker_subrequests = CounterVec :: new ( worker_subrequests_opts, & [ "script_name" ] ) . unwrap ( ) ;
123+ registry. register ( Box :: new ( worker_subrequests. clone ( ) ) ) . unwrap ( ) ;
124+
114125 let mut last_datetime: Option < Time > = None ;
115126 for account in response_data. viewer . unwrap ( ) . accounts . iter ( ) {
116127 for worker in account. workers_invocations_adaptive . iter ( ) {
@@ -122,6 +133,8 @@ pub async fn do_get_workers_analytics_query(cloudflare_api_url: &String, cloudfl
122133
123134 worker_requests. with_label_values ( & [ script_name. as_str ( ) ] ) . inc_by ( sum. requests as f64 ) ;
124135 worker_errors. with_label_values ( & [ script_name. as_str ( ) ] ) . inc_by ( sum. errors as f64 ) ;
136+ worker_wall_time. with_label_values ( & [ script_name. as_str ( ) ] ) . inc_by ( sum. wall_time as f64 ) ;
137+ worker_subrequests. with_label_values ( & [ script_name. as_str ( ) ] ) . inc_by ( sum. subrequests as f64 ) ;
125138 worker_cpu_time. with_label_values ( & [ script_name. as_str ( ) , "P50" ] ) . set ( quantiles. cpu_time_p50 as f64 ) ;
126139 worker_cpu_time. with_label_values ( & [ script_name. as_str ( ) , "P75" ] ) . set ( quantiles. cpu_time_p75 as f64 ) ;
127140 worker_cpu_time. with_label_values ( & [ script_name. as_str ( ) , "P99" ] ) . set ( quantiles. cpu_time_p99 as f64 ) ;
@@ -136,16 +149,16 @@ pub async fn do_get_workers_analytics_query(cloudflare_api_url: &String, cloudfl
136149 let timestamp_nanos: u64 = last_datetime. map ( |datetime| {
137150 let datetime: NaiveDateTime = NaiveDateTime :: parse_from_str ( & datetime, "%+" ) . unwrap ( ) ;
138151 datetime. and_utc ( ) . timestamp_nanos_opt ( ) . unwrap_or ( 0 ) as u64
139- } ) . unwrap_or_else ( || {
140- systemtime_to_nanos ( SystemTime :: now ( ) )
141- } ) ;
152+ } ) . unwrap_or ( fallback_timestamp_nanos) ;
142153
143154 Ok ( prometheus_registry_to_opentelemetry_metrics ( registry, timestamp_nanos) )
144155}
145156
146- pub async fn do_get_d1_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_d1_analytics_query:: Variables ) -> Result < Vec < Metric > , Box < dyn Error > > {
157+ pub async fn do_get_d1_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_d1_analytics_query:: Variables , debug_logging : bool , fallback_timestamp_nanos : u64 ) -> Result < Vec < Metric > , Box < dyn Error > > {
147158 let request_body = GetD1AnalyticsQuery :: build_query ( variables) ;
148- console_log ! ( "[D1] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
159+ if debug_logging {
160+ console_log ! ( "[D1] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
161+ }
149162 let client = reqwest:: Client :: new ( ) ;
150163 let res = client. post ( cloudflare_api_url)
151164 . bearer_auth ( cloudflare_api_key)
@@ -157,7 +170,9 @@ pub async fn do_get_d1_analytics_query(cloudflare_api_url: &String, cloudflare_a
157170 }
158171
159172 let response_text = res. text ( ) . await ?;
160- console_log ! ( "[D1] GraphQL response: {}" , response_text) ;
173+ if debug_logging {
174+ console_log ! ( "[D1] GraphQL response: {}" , response_text) ;
175+ }
161176 let response_body: Response < get_d1_analytics_query:: ResponseData > = serde_json:: from_str ( & response_text) ?;
162177 if response_body. errors . is_some ( ) {
163178 console_log ! ( "[D1] GraphQL query failed: {:?}" , response_body. errors) ;
@@ -214,16 +229,16 @@ pub async fn do_get_d1_analytics_query(cloudflare_api_url: &String, cloudflare_a
214229 let timestamp_nanos: u64 = last_datetime. map ( |datetime| {
215230 let datetime: NaiveDateTime = NaiveDateTime :: parse_from_str ( & datetime, "%+" ) . unwrap ( ) ;
216231 datetime. and_utc ( ) . timestamp_nanos_opt ( ) . unwrap_or ( 0 ) as u64
217- } ) . unwrap_or_else ( || {
218- systemtime_to_nanos ( SystemTime :: now ( ) )
219- } ) ;
232+ } ) . unwrap_or ( fallback_timestamp_nanos) ;
220233
221234 Ok ( prometheus_registry_to_opentelemetry_metrics ( registry, timestamp_nanos) )
222235}
223236
224- pub async fn do_get_durableobjects_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_durable_objects_analytics_query:: Variables ) -> Result < Vec < Metric > , Box < dyn Error > > {
237+ pub async fn do_get_durableobjects_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_durable_objects_analytics_query:: Variables , debug_logging : bool , fallback_timestamp_nanos : u64 ) -> Result < Vec < Metric > , Box < dyn Error > > {
225238 let request_body = GetDurableObjectsAnalyticsQuery :: build_query ( variables) ;
226- console_log ! ( "[DurableObjects] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
239+ if debug_logging {
240+ console_log ! ( "[DurableObjects] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
241+ }
227242 let client = reqwest:: Client :: new ( ) ;
228243 let res = client. post ( cloudflare_api_url)
229244 . bearer_auth ( cloudflare_api_key)
@@ -235,7 +250,9 @@ pub async fn do_get_durableobjects_analytics_query(cloudflare_api_url: &String,
235250 }
236251
237252 let response_text = res. text ( ) . await ?;
238- console_log ! ( "[DurableObjects] GraphQL response: {}" , response_text) ;
253+ if debug_logging {
254+ console_log ! ( "[DurableObjects] GraphQL response: {}" , response_text) ;
255+ }
239256 let response_body: Response < get_durable_objects_analytics_query:: ResponseData > = serde_json:: from_str ( & response_text) ?;
240257 if response_body. errors . is_some ( ) {
241258 console_log ! ( "[DurableObjects] GraphQL query failed: {:?}" , response_body. errors) ;
@@ -291,16 +308,16 @@ pub async fn do_get_durableobjects_analytics_query(cloudflare_api_url: &String,
291308 let timestamp_nanos: u64 = last_datetime. map ( |datetime| {
292309 let datetime: NaiveDateTime = NaiveDateTime :: parse_from_str ( & datetime, "%+" ) . unwrap ( ) ;
293310 datetime. and_utc ( ) . timestamp_nanos_opt ( ) . unwrap_or ( 0 ) as u64
294- } ) . unwrap_or_else ( || {
295- systemtime_to_nanos ( SystemTime :: now ( ) )
296- } ) ;
311+ } ) . unwrap_or ( fallback_timestamp_nanos) ;
297312
298313 Ok ( prometheus_registry_to_opentelemetry_metrics ( registry, timestamp_nanos) )
299314}
300315
301- pub async fn do_get_queue_backlog_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_queue_backlog_analytics_query:: Variables ) -> Result < Vec < Metric > , Box < dyn Error > > {
316+ pub async fn do_get_queue_backlog_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_queue_backlog_analytics_query:: Variables , debug_logging : bool , fallback_timestamp_nanos : u64 ) -> Result < Vec < Metric > , Box < dyn Error > > {
302317 let request_body = GetQueueBacklogAnalyticsQuery :: build_query ( variables) ;
303- console_log ! ( "[QueueBacklog] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
318+ if debug_logging {
319+ console_log ! ( "[QueueBacklog] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
320+ }
304321 let client = reqwest:: Client :: new ( ) ;
305322 let res = client. post ( cloudflare_api_url)
306323 . bearer_auth ( cloudflare_api_key)
@@ -312,7 +329,9 @@ pub async fn do_get_queue_backlog_analytics_query(cloudflare_api_url: &String, c
312329 }
313330
314331 let response_text = res. text ( ) . await ?;
315- console_log ! ( "[QueueBacklog] GraphQL response: {}" , response_text) ;
332+ if debug_logging {
333+ console_log ! ( "[QueueBacklog] GraphQL response: {}" , response_text) ;
334+ }
316335 let response_body: Response < get_queue_backlog_analytics_query:: ResponseData > = serde_json:: from_str ( & response_text) ?;
317336 if response_body. errors . is_some ( ) {
318337 console_log ! ( "[QueueBacklog] GraphQL query failed: {:?}" , response_body. errors) ;
@@ -350,16 +369,16 @@ pub async fn do_get_queue_backlog_analytics_query(cloudflare_api_url: &String, c
350369 let timestamp_nanos: u64 = last_datetime. map ( |datetime| {
351370 let datetime: NaiveDateTime = NaiveDateTime :: parse_from_str ( & datetime, "%+" ) . unwrap ( ) ;
352371 datetime. and_utc ( ) . timestamp_nanos_opt ( ) . unwrap_or ( 0 ) as u64
353- } ) . unwrap_or_else ( || {
354- systemtime_to_nanos ( SystemTime :: now ( ) )
355- } ) ;
372+ } ) . unwrap_or ( fallback_timestamp_nanos) ;
356373
357374 Ok ( prometheus_registry_to_opentelemetry_metrics ( registry, timestamp_nanos) )
358375}
359376
360- pub async fn do_get_queue_operations_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_queue_operations_analytics_query:: Variables ) -> Result < Vec < Metric > , Box < dyn Error > > {
377+ pub async fn do_get_queue_operations_analytics_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_queue_operations_analytics_query:: Variables , debug_logging : bool , fallback_timestamp_nanos : u64 ) -> Result < Vec < Metric > , Box < dyn Error > > {
361378 let request_body = GetQueueOperationsAnalyticsQuery :: build_query ( variables) ;
362- console_log ! ( "[QueueOperations] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
379+ if debug_logging {
380+ console_log ! ( "[QueueOperations] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
381+ }
363382 let client = reqwest:: Client :: new ( ) ;
364383 let res = client. post ( cloudflare_api_url)
365384 . bearer_auth ( cloudflare_api_key)
@@ -371,7 +390,9 @@ pub async fn do_get_queue_operations_analytics_query(cloudflare_api_url: &String
371390 }
372391
373392 let response_text = res. text ( ) . await ?;
374- console_log ! ( "[QueueOperations] GraphQL response: {}" , response_text) ;
393+ if debug_logging {
394+ console_log ! ( "[QueueOperations] GraphQL response: {}" , response_text) ;
395+ }
375396 let response_body: Response < get_queue_operations_analytics_query:: ResponseData > = serde_json:: from_str ( & response_text) ?;
376397 if response_body. errors . is_some ( ) {
377398 console_log ! ( "[QueueOperations] GraphQL query failed: {:?}" , response_body. errors) ;
@@ -424,16 +445,16 @@ pub async fn do_get_queue_operations_analytics_query(cloudflare_api_url: &String
424445 let timestamp_nanos: u64 = last_datetime. map ( |datetime| {
425446 let datetime: NaiveDateTime = NaiveDateTime :: parse_from_str ( & datetime, "%+" ) . unwrap ( ) ;
426447 datetime. and_utc ( ) . timestamp_nanos_opt ( ) . unwrap_or ( 0 ) as u64
427- } ) . unwrap_or_else ( || {
428- systemtime_to_nanos ( SystemTime :: now ( ) )
429- } ) ;
448+ } ) . unwrap_or ( fallback_timestamp_nanos) ;
430449
431450 Ok ( prometheus_registry_to_opentelemetry_metrics ( registry, timestamp_nanos) )
432451}
433452
434- pub async fn do_get_zone_http_requests_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_zone_http_requests_query:: Variables ) -> Result < Vec < Metric > , Box < dyn Error > > {
453+ pub async fn do_get_zone_http_requests_query ( cloudflare_api_url : & String , cloudflare_api_key : & String , variables : get_zone_http_requests_query:: Variables , debug_logging : bool , fallback_timestamp_nanos : u64 ) -> Result < Vec < Metric > , Box < dyn Error > > {
435454 let request_body = GetZoneHttpRequestsQuery :: build_query ( variables) ;
436- console_log ! ( "[ZoneHttpRequests] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
455+ if debug_logging {
456+ console_log ! ( "[ZoneHttpRequests] GraphQL request: {}" , serde_json:: to_string_pretty( & request_body) . unwrap_or_default( ) ) ;
457+ }
437458 let client = reqwest:: Client :: new ( ) ;
438459 let res = client. post ( cloudflare_api_url)
439460 . bearer_auth ( cloudflare_api_key)
@@ -445,7 +466,9 @@ pub async fn do_get_zone_http_requests_query(cloudflare_api_url: &String, cloudf
445466 }
446467
447468 let response_text = res. text ( ) . await ?;
448- console_log ! ( "[ZoneHttpRequests] GraphQL response: {}" , response_text) ;
469+ if debug_logging {
470+ console_log ! ( "[ZoneHttpRequests] GraphQL response: {}" , response_text) ;
471+ }
449472 let response_body: Response < get_zone_http_requests_query:: ResponseData > = serde_json:: from_str ( & response_text) ?;
450473 if response_body. errors . is_some ( ) {
451474 console_log ! ( "[ZoneHttpRequests] GraphQL query failed: {:?}" , response_body. errors) ;
@@ -487,14 +510,8 @@ pub async fn do_get_zone_http_requests_query(cloudflare_api_url: &String, cloudf
487510 let timestamp_nanos: u64 = last_datetime. map ( |datetime| {
488511 let datetime: NaiveDateTime = NaiveDateTime :: parse_from_str ( & datetime, "%+" ) . unwrap ( ) ;
489512 datetime. and_utc ( ) . timestamp_nanos_opt ( ) . unwrap_or ( 0 ) as u64
490- } ) . unwrap_or_else ( || {
491- systemtime_to_nanos ( SystemTime :: now ( ) )
492- } ) ;
513+ } ) . unwrap_or ( fallback_timestamp_nanos) ;
493514
494515 Ok ( prometheus_registry_to_opentelemetry_metrics ( registry, timestamp_nanos) )
495516}
496517
497- fn systemtime_to_nanos ( time : web_time:: SystemTime ) -> u64 {
498- let duration = time. duration_since ( web_time:: SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
499- duration. as_nanos ( ) as u64
500- }
0 commit comments