|
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 | */ |
5 | 5 |
|
| 6 | +use std::any::Any; |
6 | 7 | use std::sync::Arc; |
7 | 8 |
|
8 | 9 | use crate::client::telemetry::ClientOperationEvent; |
@@ -33,83 +34,67 @@ pub struct MozAdsTelemetryWrapper { |
33 | 34 | pub inner: Arc<dyn MozAdsTelemetry>, |
34 | 35 | } |
35 | 36 |
|
36 | | -impl Telemetry<CacheOutcome> for MozAdsTelemetryWrapper { |
37 | | - fn record(&self, event: &CacheOutcome) { |
38 | | - let label = match event { |
39 | | - CacheOutcome::Hit => "hit", |
40 | | - CacheOutcome::LookupFailed(_) => "lookup_failed", |
41 | | - CacheOutcome::NoCache => "no_cache", |
42 | | - CacheOutcome::MissNotCacheable => "miss_not_cacheable", |
43 | | - CacheOutcome::MissStored => "miss_stored", |
44 | | - CacheOutcome::StoreFailed(_) => "store_failed", |
45 | | - CacheOutcome::CleanupFailed(_) => "cleanup_failed", |
46 | | - }; |
47 | | - self.inner.record_http_cache_outcome(label); |
48 | | - } |
49 | | -} |
50 | | - |
51 | | -impl Telemetry<serde_json::Error> for MozAdsTelemetryWrapper { |
52 | | - fn record(&self, event: &serde_json::Error) { |
53 | | - let label = "invalid_ad_item"; |
54 | | - let value = format!("{}", event); |
55 | | - self.inner.record_deserialization_error(label, &value); |
56 | | - } |
57 | | -} |
58 | | - |
59 | | -impl Telemetry<HttpCacheBuilderError> for MozAdsTelemetryWrapper { |
60 | | - fn record(&self, event: &HttpCacheBuilderError) { |
61 | | - let label = match event { |
62 | | - HttpCacheBuilderError::EmptyDbPath => "empty_db_path", |
63 | | - HttpCacheBuilderError::Database(_) => "database_error", |
64 | | - HttpCacheBuilderError::InvalidMaxSize { .. } => "invalid_max_size", |
65 | | - HttpCacheBuilderError::InvalidTtl { .. } => "invalid_ttl", |
66 | | - }; |
67 | | - let value = format!("{}", event); |
68 | | - self.inner.record_build_cache_error(label, &value); |
69 | | - } |
70 | | -} |
71 | | - |
72 | | -impl Telemetry<RequestAdsError> for MozAdsTelemetryWrapper { |
73 | | - fn record(&self, event: &RequestAdsError) { |
74 | | - let label = "request_ads"; |
75 | | - let value = format!("{}", event); |
76 | | - self.inner.record_client_error(label, &value); |
77 | | - } |
78 | | -} |
79 | | - |
80 | | -impl Telemetry<RecordClickError> for MozAdsTelemetryWrapper { |
81 | | - fn record(&self, event: &RecordClickError) { |
82 | | - let label = "record_click"; |
83 | | - let value = format!("{}", event); |
84 | | - self.inner.record_client_error(label, &value); |
85 | | - } |
86 | | -} |
87 | | - |
88 | | -impl Telemetry<RecordImpressionError> for MozAdsTelemetryWrapper { |
89 | | - fn record(&self, event: &RecordImpressionError) { |
90 | | - let label = "record_impression"; |
91 | | - let value = format!("{}", event); |
92 | | - self.inner.record_client_error(label, &value); |
93 | | - } |
94 | | -} |
95 | | - |
96 | | -impl Telemetry<ReportAdError> for MozAdsTelemetryWrapper { |
97 | | - fn record(&self, event: &ReportAdError) { |
98 | | - let label = "report_ad"; |
99 | | - let value = format!("{}", event); |
100 | | - self.inner.record_client_error(label, &value); |
101 | | - } |
102 | | -} |
103 | | - |
104 | | -impl Telemetry<ClientOperationEvent> for MozAdsTelemetryWrapper { |
105 | | - fn record(&self, event: &ClientOperationEvent) { |
106 | | - let label = match event { |
107 | | - ClientOperationEvent::New => "new", |
108 | | - ClientOperationEvent::RecordClick => "record_click", |
109 | | - ClientOperationEvent::RecordImpression => "record_impression", |
110 | | - ClientOperationEvent::ReportAd => "report_ad", |
111 | | - ClientOperationEvent::RequestAds => "request_ads", |
112 | | - }; |
113 | | - self.inner.record_client_operation_total(label); |
| 37 | +impl Telemetry for MozAdsTelemetryWrapper { |
| 38 | + fn record(&self, event: &dyn Any) { |
| 39 | + if let Some(cache_outcome) = event.downcast_ref::<CacheOutcome>() { |
| 40 | + self.inner.record_http_cache_outcome(match cache_outcome { |
| 41 | + CacheOutcome::Hit => "hit", |
| 42 | + CacheOutcome::LookupFailed(_) => "lookup_failed", |
| 43 | + CacheOutcome::NoCache => "no_cache", |
| 44 | + CacheOutcome::MissNotCacheable => "miss_not_cacheable", |
| 45 | + CacheOutcome::MissStored => "miss_stored", |
| 46 | + CacheOutcome::StoreFailed(_) => "store_failed", |
| 47 | + CacheOutcome::CleanupFailed(_) => "cleanup_failed", |
| 48 | + }); |
| 49 | + return; |
| 50 | + } |
| 51 | + if let Some(client_op) = event.downcast_ref::<ClientOperationEvent>() { |
| 52 | + self.inner.record_client_operation_total(match client_op { |
| 53 | + ClientOperationEvent::New => "new", |
| 54 | + ClientOperationEvent::RecordClick => "record_click", |
| 55 | + ClientOperationEvent::RecordImpression => "record_impression", |
| 56 | + ClientOperationEvent::ReportAd => "report_ad", |
| 57 | + ClientOperationEvent::RequestAds => "request_ads", |
| 58 | + }); |
| 59 | + return; |
| 60 | + } |
| 61 | + if let Some(cache_builder_error) = event.downcast_ref::<HttpCacheBuilderError>() { |
| 62 | + self.inner.record_build_cache_error( |
| 63 | + match cache_builder_error { |
| 64 | + HttpCacheBuilderError::EmptyDbPath => "empty_db_path", |
| 65 | + HttpCacheBuilderError::Database(_) => "database_error", |
| 66 | + HttpCacheBuilderError::InvalidMaxSize { .. } => "invalid_max_size", |
| 67 | + HttpCacheBuilderError::InvalidTtl { .. } => "invalid_ttl", |
| 68 | + }, |
| 69 | + &format!("{}", cache_builder_error), |
| 70 | + ); |
| 71 | + return; |
| 72 | + } |
| 73 | + if let Some(record_click_error) = event.downcast_ref::<RecordClickError>() { |
| 74 | + self.inner |
| 75 | + .record_client_error("record_click", &format!("{}", record_click_error)); |
| 76 | + return; |
| 77 | + } |
| 78 | + if let Some(record_impression_error) = event.downcast_ref::<RecordImpressionError>() { |
| 79 | + self.inner |
| 80 | + .record_client_error("record_impression", &format!("{}", record_impression_error)); |
| 81 | + return; |
| 82 | + } |
| 83 | + if let Some(report_ad_error) = event.downcast_ref::<ReportAdError>() { |
| 84 | + self.inner |
| 85 | + .record_client_error("report_ad", &format!("{}", report_ad_error)); |
| 86 | + return; |
| 87 | + } |
| 88 | + if let Some(request_ads_error) = event.downcast_ref::<RequestAdsError>() { |
| 89 | + self.inner |
| 90 | + .record_client_error("request_ads", &format!("{}", request_ads_error)); |
| 91 | + return; |
| 92 | + } |
| 93 | + if let Some(json_error) = event.downcast_ref::<serde_json::Error>() { |
| 94 | + self.inner |
| 95 | + .record_deserialization_error("invalid_ad_item", &format!("{}", json_error)); |
| 96 | + return; |
| 97 | + } |
| 98 | + eprintln!("Unsupported telemetry event type: {:?}", event.type_id()); |
114 | 99 | } |
115 | 100 | } |
0 commit comments