44*/
55
66use std:: collections:: HashMap ;
7- use std:: sync:: Arc ;
87use std:: time:: Duration ;
98
109use crate :: client:: ad_response:: {
1110 pop_request_hash_from_url, AdImage , AdResponse , AdResponseValue , AdSpoc , AdTile ,
1211} ;
1312use crate :: client:: config:: AdsClientConfig ;
14- use crate :: client:: telemetry:: ClientOperationEvent ;
1513use crate :: error:: { RecordClickError , RecordImpressionError , ReportAdError , RequestAdsError } ;
16- use crate :: http_cache:: { CacheOutcome , HttpCache , HttpCacheBuilderError , RequestCachePolicy } ;
14+ use crate :: http_cache:: { HttpCache , RequestCachePolicy } ;
1715use crate :: mars:: MARSClient ;
1816use crate :: telemetry:: Telemetry ;
1917use ad_request:: { AdPlacementRequest , AdRequest } ;
@@ -26,37 +24,22 @@ use crate::http_cache::{ByteSize, HttpCacheError};
2624pub mod ad_request;
2725pub mod ad_response;
2826pub mod config;
29- pub mod telemetry;
3027
3128const DEFAULT_TTL_SECONDS : u64 = 300 ;
3229const DEFAULT_MAX_CACHE_SIZE_MIB : u64 = 10 ;
3330
3431pub struct AdsClient < T >
3532where
36- T : Telemetry < CacheOutcome >
37- + Telemetry < ClientOperationEvent >
38- + Telemetry < HttpCacheBuilderError >
39- + Telemetry < RecordClickError >
40- + Telemetry < RecordImpressionError >
41- + Telemetry < ReportAdError >
42- + Telemetry < RequestAdsError >
43- + Telemetry < serde_json:: Error > ,
33+ T : Clone + Telemetry ,
4434{
4535 client : MARSClient < T > ,
4636 context_id_component : ContextIDComponent ,
47- telemetry : Arc < T > ,
37+ telemetry : T ,
4838}
4939
5040impl < T > AdsClient < T >
5141where
52- T : Telemetry < CacheOutcome >
53- + Telemetry < serde_json:: Error >
54- + Telemetry < ClientOperationEvent >
55- + Telemetry < HttpCacheBuilderError >
56- + Telemetry < RecordClickError >
57- + Telemetry < RecordImpressionError >
58- + Telemetry < ReportAdError >
59- + Telemetry < RequestAdsError > ,
42+ T : Clone + Telemetry ,
6043{
6144 pub fn new ( client_config : AdsClientConfig < T > ) -> Self {
6245 let context_id = Uuid :: new_v4 ( ) . to_string ( ) ;
6649 cfg ! ( test) ,
6750 Box :: new ( DefaultContextIdCallback ) ,
6851 ) ;
52+ let telemetry = client_config. telemetry ;
6953
7054 // Configure the cache if a path is provided.
7155 // Defaults for ttl and cache size are also set if unspecified.
@@ -85,36 +69,28 @@ where
8569 {
8670 Ok ( cache) => Some ( cache) ,
8771 Err ( e) => {
88- client_config . telemetry . record ( & e) ;
72+ telemetry. record ( & e) ;
8973 None
9074 }
9175 } ;
9276
93- let client = MARSClient :: new (
94- client_config. environment ,
95- http_cache,
96- client_config. telemetry . clone ( ) ,
97- ) ;
77+ let client = MARSClient :: new ( client_config. environment , http_cache, telemetry. clone ( ) ) ;
9878 let client = Self {
9979 context_id_component,
10080 client,
101- telemetry : client_config . telemetry . clone ( ) ,
81+ telemetry : telemetry. clone ( ) ,
10282 } ;
103- client . telemetry . record ( & ClientOperationEvent :: New ) ;
83+ telemetry. record ( & ClientOperationEvent :: New ) ;
10484 return client;
10585 }
10686
107- let client = MARSClient :: new (
108- client_config. environment ,
109- None ,
110- client_config. telemetry . clone ( ) ,
111- ) ;
87+ let client = MARSClient :: new ( client_config. environment , None , telemetry. clone ( ) ) ;
11288 let client = Self {
11389 context_id_component,
11490 client,
115- telemetry : client_config . telemetry . clone ( ) ,
91+ telemetry : telemetry. clone ( ) ,
11692 } ;
117- client . telemetry . record ( & ClientOperationEvent :: New ) ;
93+ telemetry. record ( & ClientOperationEvent :: New ) ;
11894 client
11995 }
12096
@@ -238,19 +214,31 @@ where
238214 }
239215}
240216
217+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
218+ pub enum ClientOperationEvent {
219+ New ,
220+ RecordClick ,
221+ RecordImpression ,
222+ ReportAd ,
223+ RequestAds ,
224+ }
225+
241226#[ cfg( test) ]
242227mod tests {
243228 use crate :: {
244229 client:: config:: Environment ,
230+ ffi:: telemetry:: MozAdsTelemetryWrapper ,
245231 test_utils:: {
246232 get_example_happy_image_response, get_example_happy_spoc_response,
247- get_example_happy_uatile_response, make_happy_placement_requests, PrintAdsTelemetry ,
233+ get_example_happy_uatile_response, make_happy_placement_requests,
248234 } ,
249235 } ;
250236
251237 use super :: * ;
252238
253- fn new_with_mars_client ( client : MARSClient < PrintAdsTelemetry > ) -> AdsClient < PrintAdsTelemetry > {
239+ fn new_with_mars_client (
240+ client : MARSClient < MozAdsTelemetryWrapper > ,
241+ ) -> AdsClient < MozAdsTelemetryWrapper > {
254242 let context_id_component = ContextIDComponent :: new (
255243 & uuid:: Uuid :: new_v4 ( ) . to_string ( ) ,
256244 0 ,
@@ -260,17 +248,16 @@ mod tests {
260248 AdsClient {
261249 context_id_component,
262250 client,
263- telemetry : Arc :: new ( PrintAdsTelemetry ) ,
251+ telemetry : MozAdsTelemetryWrapper :: noop ( ) ,
264252 }
265253 }
266254
267255 #[ test]
268256 fn test_get_context_id ( ) {
269- use std:: sync:: Arc ;
270257 let config = AdsClientConfig {
271258 environment : Environment :: Test ,
272259 cache_config : None ,
273- telemetry : Arc :: new ( PrintAdsTelemetry ) ,
260+ telemetry : MozAdsTelemetryWrapper :: noop ( ) ,
274261 } ;
275262 let client = AdsClient :: new ( config) ;
276263 let context_id = client. get_context_id ( ) . unwrap ( ) ;
@@ -279,11 +266,10 @@ mod tests {
279266
280267 #[ test]
281268 fn test_cycle_context_id ( ) {
282- use std:: sync:: Arc ;
283269 let config = AdsClientConfig {
284270 environment : Environment :: Test ,
285271 cache_config : None ,
286- telemetry : Arc :: new ( PrintAdsTelemetry ) ,
272+ telemetry : MozAdsTelemetryWrapper :: noop ( ) ,
287273 } ;
288274 let mut client = AdsClient :: new ( config) ;
289275 let old_id = client. get_context_id ( ) . unwrap ( ) ;
@@ -304,8 +290,8 @@ mod tests {
304290 . with_body ( serde_json:: to_string ( & expected_response. data ) . unwrap ( ) )
305291 . create ( ) ;
306292
307- let telemetry = Arc :: new ( PrintAdsTelemetry ) ;
308- let mars_client = MARSClient :: new ( Environment :: Test , None , telemetry. clone ( ) ) ;
293+ let telemetry = MozAdsTelemetryWrapper :: noop ( ) ;
294+ let mars_client = MARSClient :: new ( Environment :: Test , None , telemetry) ;
309295 let ads_client = new_with_mars_client ( mars_client) ;
310296
311297 let ad_placement_requests = make_happy_placement_requests ( ) ;
@@ -326,8 +312,8 @@ mod tests {
326312 . with_body ( serde_json:: to_string ( & expected_response. data ) . unwrap ( ) )
327313 . create ( ) ;
328314
329- let telemetry = Arc :: new ( PrintAdsTelemetry ) ;
330- let mars_client = MARSClient :: new ( Environment :: Test , None , telemetry. clone ( ) ) ;
315+ let telemetry = MozAdsTelemetryWrapper :: noop ( ) ;
316+ let mars_client = MARSClient :: new ( Environment :: Test , None , telemetry) ;
331317 let ads_client = new_with_mars_client ( mars_client) ;
332318
333319 let ad_placement_requests = make_happy_placement_requests ( ) ;
@@ -348,7 +334,7 @@ mod tests {
348334 . with_body ( serde_json:: to_string ( & expected_response. data ) . unwrap ( ) )
349335 . create ( ) ;
350336
351- let telemetry = Arc :: new ( PrintAdsTelemetry ) ;
337+ let telemetry = MozAdsTelemetryWrapper :: noop ( ) ;
352338 let mars_client = MARSClient :: new ( Environment :: Test , None , telemetry. clone ( ) ) ;
353339 let ads_client = new_with_mars_client ( mars_client) ;
354340
@@ -365,7 +351,7 @@ mod tests {
365351 let cache = HttpCache :: builder ( "test_record_click_invalidates_cache" )
366352 . build ( )
367353 . unwrap ( ) ;
368- let telemetry = Arc :: new ( PrintAdsTelemetry ) ;
354+ let telemetry = MozAdsTelemetryWrapper :: noop ( ) ;
369355 let mars_client = MARSClient :: new ( Environment :: Test , Some ( cache) , telemetry. clone ( ) ) ;
370356 let ads_client = new_with_mars_client ( mars_client) ;
371357
0 commit comments