@@ -5,19 +5,24 @@ use crate::errors::ParseError;
55use  crate :: { constants,  datadog} ; 
66use  ddsketch_agent:: DDSketch ; 
77use  fnv:: FnvHasher ; 
8- use  lazy_static:: lazy_static; 
98use  protobuf:: Chars ; 
109use  regex:: Regex ; 
1110use  std:: hash:: { Hash ,  Hasher } ; 
11+ use  std:: sync:: OnceLock ; 
1212use  ustr:: Ustr ; 
1313
1414pub  const  EMPTY_TAGS :  SortedTags  = SortedTags  {  values :  Vec :: new ( )  } ; 
1515
1616// https://docs.datadoghq.com/developers/dogstatsd/datagram_shell?tab=metrics#dogstatsd-protocol-v13 
17- lazy_static !  { 
18-     static  ref METRIC_REGEX :  Regex  = Regex :: new( 
19-         r"^(?P<name>[^:]+):(?P<values>[^|]+)\|(?P<type>[a-zA-Z]+)(?:\|@(?P<sample_rate>[\d.]+))?(?:\|#(?P<tags>[^|]+))?(?:\|c:(?P<container_id>[^|]+))?(?:\|T(?P<timestamp>[^|]+))?$" , 
20-     ) . expect( "Failed to create metric regex" ) ; 
17+ static  METRIC_REGEX :  OnceLock < Regex >  = OnceLock :: new ( ) ; 
18+ fn  get_metric_regex ( )  -> & ' static  Regex  { 
19+     #[ allow( clippy:: expect_used) ]  
20+     METRIC_REGEX . get_or_init ( || { 
21+         Regex :: new ( 
22+             r"^(?P<name>[^:]+):(?P<values>[^|]+)\|(?P<type>[a-zA-Z]+)(?:\|@(?P<sample_rate>[\d.]+))?(?:\|#(?P<tags>[^|]+))?(?:\|c:(?P<container_id>[^|]+))?(?:\|T(?P<timestamp>[^|]+))?$" , 
23+         ) 
24+         . expect ( "Failed to create metric regex" ) 
25+     } ) 
2126} 
2227
2328#[ derive( Clone ,  Debug ) ]  
@@ -181,6 +186,7 @@ impl Metric {
181186        tags :  Option < SortedTags > , 
182187        timestamp :  Option < i64 > , 
183188    )  -> Metric  { 
189+         #[ allow( clippy:: expect_used) ]  
184190        let  parsed_timestamp = timestamp_to_bucket ( timestamp. unwrap_or_else ( || { 
185191            std:: time:: UNIX_EPOCH 
186192                . elapsed ( ) 
@@ -204,6 +210,7 @@ impl Metric {
204210// Round down to the nearest 10 seconds 
205211// to form a bucket of metric contexts aggregated per 10s 
206212pub  fn  timestamp_to_bucket ( timestamp :  i64 )  -> i64  { 
213+     #[ allow( clippy:: expect_used) ]  
207214    let  now_seconds:  i64  = std:: time:: UNIX_EPOCH 
208215        . elapsed ( ) 
209216        . expect ( "unable to poll clock, unrecoverable" ) 
@@ -228,7 +235,7 @@ pub fn timestamp_to_bucket(timestamp: i64) -> i64 {
228235/// example aj-test.increment:1|c|#user:aj-test from 127.0.0.1:50983 
229236pub  fn  parse ( input :  & str )  -> Result < Metric ,  ParseError >  { 
230237    // TODO must enforce / exploit constraints given in `constants`. 
231-     if  let  Some ( caps)  = METRIC_REGEX . captures ( input)  { 
238+     if  let  Some ( caps)  = get_metric_regex ( ) . captures ( input)  { 
232239        // unused for now 
233240        // let sample_rate = caps.name("sample_rate").map(|m| m.as_str()); 
234241
@@ -238,8 +245,14 @@ pub fn parse(input: &str) -> Result<Metric, ParseError> {
238245        }  else  { 
239246            tags = None ; 
240247        } 
248+ 
249+         #[ allow( clippy:: unwrap_used) ]  
241250        let  val = first_value ( caps. name ( "values" ) . unwrap ( ) . as_str ( ) ) ?; 
251+ 
252+         #[ allow( clippy:: unwrap_used) ]  
242253        let  t = caps. name ( "type" ) . unwrap ( ) . as_str ( ) ; 
254+ 
255+         #[ allow( clippy:: expect_used) ]  
243256        let  now = std:: time:: UNIX_EPOCH 
244257            . elapsed ( ) 
245258            . expect ( "unable to poll clock, unrecoverable" ) 
@@ -266,6 +279,7 @@ pub fn parse(input: &str) -> Result<Metric, ParseError> {
266279                return  Err ( ParseError :: Raw ( format ! ( "Invalid metric type: {t}" ) ) ) ; 
267280            } 
268281        } ; 
282+         #[ allow( clippy:: unwrap_used) ]  
269283        let  name = Ustr :: from ( caps. name ( "name" ) . unwrap ( ) . as_str ( ) ) ; 
270284        let  id = id ( name,  & tags,  parsed_timestamp) ; 
271285        return  Ok ( Metric  { 
0 commit comments