@@ -543,10 +543,8 @@ public function test_its_request_logger_logs_custom_metadata()
543543 $ logger = $ this ->newSubject ();
544544 $ logger ->logRequest ([]);
545545 $ entry = $ this ->assertLoggedOneLine ();
546- $ this ->assertSame (
547- ['user ' => 'john@do.com ' ],
548- $ entry ['context ' ]
549- );
546+ $ this ->assertSame (['mem_mb ' , 'user ' ], array_keys ($ entry ['context ' ]));
547+ $ this ->assertSame ('john@do.com ' , $ entry ['context ' ]['user ' ]);
550548 }
551549
552550 public function test_its_request_logger_logs_http_response_code ()
@@ -571,6 +569,16 @@ public function test_its_request_logger_logs_user_agent_from_global_array($serve
571569 $ this ->assertSame ($ expect , $ entry ['httpRequest ' ]['userAgent ' ]);
572570 }
573571
572+ public function test_its_request_logger_truncates_long_user_agents ()
573+ {
574+ $ ua_val = 'abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 long ※ note the utf8 safe truncation and trimming ' ;
575+ $ expect = 'abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 abcdefghijklmnopqrstuvwxyz 1234567890 long… ' ;
576+ $ logger = $ this ->newSubject ();
577+ $ logger ->logRequest (['HTTP_USER_AGENT ' => $ ua_val ]);
578+ $ entry = $ this ->assertLoggedOneLine ();
579+ $ this ->assertSame ($ expect , $ entry ['httpRequest ' ]['userAgent ' ]);
580+ }
581+
574582 public function test_its_request_logger_logs_latency_since_start_time_if_provided ()
575583 {
576584 $ start = \microtime (TRUE );
@@ -591,6 +599,31 @@ public function test_its_request_logger_logs_null_latency_if_no_start_time()
591599 $ this ->assertNull ($ entry ['httpRequest ' ]['latency ' ]);
592600 }
593601
602+ public function test_its_request_logger_logs_peak_memory_usage_in_mb ()
603+ {
604+ $ logger = $ this ->newSubject ();
605+ $ peak_before = \memory_get_peak_usage ();
606+ $ logger ->logRequest ([]);
607+ $ peak_after = \memory_get_peak_usage ();
608+ $ entry = $ this ->assertLoggedOneLine ();
609+ $ this ->assertIsString (
610+ $ entry ['context ' ]['mem_mb ' ],
611+ 'Encode memory as string to avoid risk of JSON serialization precision increasing payload size '
612+ );
613+ $ this ->assertMatchesRegularExpression ('/^\d+\.\d{2}$/ ' , $ entry ['context ' ]['mem_mb ' ]);
614+
615+ $ logged_meg = $ entry ['context ' ]['mem_mb ' ];
616+ // Sanity check that it's definitely logging in megabytes. This is tricky as there's no way to stub the actual
617+ // memory usage that the logger saw - but it must be no less than the value before we called it, and no more
618+ // than the value immediately after we called it
619+ $ this ->assertTrue (
620+ ($ logged_meg >= round ($ peak_before / 1000 / 1000 , 2 ))
621+ and
622+ ($ logged_meg <= round ($ peak_after / 1000 / 1000 , 2 )),
623+ "Logged value should be within $ peak_before and $ peak_after and in the right units "
624+ );
625+ }
626+
594627 /**
595628 * @return array
596629 */
0 commit comments