     (is (= expected actual))))
 
 (deftest test-remove-and-check-metrics-except-outstanding
-  (let [metrics-registry mc/default-registry
-        create-metrics (fn [service-id]
-                         (histograms/update! (histograms/histogram ["services" service-id "test-histogram1"]) (hash service-id))
-                         (histograms/update! (histograms/histogram ["services" service-id "instance" "test-histogram2"]) (hash service-id))
-                         (timers/start-stop-time! (timers/timer ["services" service-id "test-response-duration"]))
-                         (timers/start-stop-time! (timers/timer ["services" service-id "nested" "test-response-duration"]))
-                         (meters/mark! (meters/meter ["services" service-id "test-throughput"]))
-                         (meters/mark! (meters/meter ["services" service-id "nested" "test-throughput"]))
-                         (counters/inc! (counters/counter ["services" service-id "foo" "bar"]))
-                         (counters/inc! (counters/counter ["services" service-id "outstanding" "fum"])))
-        metrics-per-service 8
-        has-metrics-except-outstanding? (fn has-metrics-except-outstanding? [service-id]
-                                          (let [metric-filter (reify MetricFilter
-                                                                (matches [_ name _]
-                                                                  (and (str/includes? name service-id)
-                                                                       (not (str/includes? name "outstanding")))))]
-                                            (or (not-empty (.getCounters metrics-registry metric-filter))
-                                                (not-empty (.getHistograms metrics-registry metric-filter))
-                                                (not-empty (.getMeters metrics-registry metric-filter))
-                                                (not-empty (.getTimers metrics-registry metric-filter)))))]
-    (testing "Delete metrics for specified services"
-      (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))
-      (is (zero? (count (.getMetrics metrics-registry))))
-      (create-metrics "test-service-1")
-      (create-metrics "test-service-2")
-      (create-metrics "test-service-3")
-      (is (= (* 3 metrics-per-service) (count (.getMetrics metrics-registry))))
-      (create-metrics "test-service-2")
-      (is (= (* 3 metrics-per-service) (count (.getMetrics metrics-registry))))
-      (create-metrics "test-service-1")
-      (is (= (* 3 metrics-per-service) (count (.getMetrics metrics-registry))))
-      (is (has-metrics-except-outstanding? "test-service-1"))
-      (is (has-metrics-except-outstanding? "test-service-2"))
-      (is (has-metrics-except-outstanding? "test-service-3"))
-      (remove-metrics-except-outstanding metrics-registry "test-service-1")
-      (is (= (+ (* 1 1) (* 2 metrics-per-service)) (count (.getMetrics metrics-registry))))
-      (is (not (has-metrics-except-outstanding? "test-service-1")))
-      (is (has-metrics-except-outstanding? "test-service-2"))
-      (is (has-metrics-except-outstanding? "test-service-3"))
-      (remove-metrics-except-outstanding metrics-registry "test-service-2")
-      (is (= (+ (* 2 1) (* 1 metrics-per-service)) (count (.getMetrics metrics-registry))))
-      (is (not (has-metrics-except-outstanding? "test-service-1")))
-      (is (not (has-metrics-except-outstanding? "test-service-2")))
-      (is (has-metrics-except-outstanding? "test-service-3"))
-      (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true))))))
+  (with-isolated-registry
+    (let [metrics-registry mc/default-registry
+          create-metrics (fn [service-id]
+                           (histograms/update! (histograms/histogram ["services" service-id "test-histogram1"]) (hash service-id))
+                           (histograms/update! (histograms/histogram ["services" service-id "instance" "test-histogram2"]) (hash service-id))
+                           (timers/start-stop-time! (timers/timer ["services" service-id "test-response-duration"]))
+                           (timers/start-stop-time! (timers/timer ["services" service-id "nested" "test-response-duration"]))
+                           (meters/mark! (meters/meter ["services" service-id "test-throughput"]))
+                           (meters/mark! (meters/meter ["services" service-id "nested" "test-throughput"]))
+                           (counters/inc! (counters/counter ["services" service-id "foo" "bar"]))
+                           (counters/inc! (counters/counter ["services" service-id "outstanding" "fum"])))
+          metrics-per-service 8
+          has-metrics-except-outstanding? (fn has-metrics-except-outstanding? [service-id]
+                                            (let [metric-filter (reify MetricFilter
+                                                                  (matches [_ name _]
+                                                                    (and (str/includes? name service-id)
+                                                                         (not (str/includes? name "outstanding")))))]
+                                              (or (not-empty (.getCounters metrics-registry metric-filter))
+                                                  (not-empty (.getHistograms metrics-registry metric-filter))
+                                                  (not-empty (.getMeters metrics-registry metric-filter))
+                                                  (not-empty (.getTimers metrics-registry metric-filter)))))]
+      (testing "Delete metrics for specified services"
+        (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))
+        (let [metrics-items (.getMetrics metrics-registry)]
+          (is (zero? (count metrics-items)) (str metrics-items)))
+        (create-metrics "test-service-1")
+        (create-metrics "test-service-2")
+        (create-metrics "test-service-3")
+        (let [metrics-items (.getMetrics metrics-registry)]
+          (is (= (* 3 metrics-per-service) (count metrics-items)) (str metrics-items)))
+        (create-metrics "test-service-2")
+        (let [metrics-items (.getMetrics metrics-registry)]
+          (is (= (* 3 metrics-per-service) (count metrics-items)) (str metrics-items)))
+        (create-metrics "test-service-1")
+        (let [metrics-items (.getMetrics metrics-registry)]
+          (is (= (* 3 metrics-per-service) (count metrics-items)) (str metrics-items)))
+        (is (has-metrics-except-outstanding? "test-service-1"))
+        (is (has-metrics-except-outstanding? "test-service-2"))
+        (is (has-metrics-except-outstanding? "test-service-3"))
+        (remove-metrics-except-outstanding metrics-registry "test-service-1")
+        (let [metrics-items (.getMetrics metrics-registry)]
+          (is (= (+ (* 1 1) (* 2 metrics-per-service)) (count metrics-items)) (str metrics-items)))
+        (is (not (has-metrics-except-outstanding? "test-service-1")))
+        (is (has-metrics-except-outstanding? "test-service-2"))
+        (is (has-metrics-except-outstanding? "test-service-3"))
+        (remove-metrics-except-outstanding metrics-registry "test-service-2")
+        (let [metrics-items (.getMetrics metrics-registry)]
+          (is (= (+ (* 2 1) (* 1 metrics-per-service)) (count metrics-items)) (str metrics-items)))
+        (is (not (has-metrics-except-outstanding? "test-service-1")))
+        (is (not (has-metrics-except-outstanding? "test-service-2")))
+        (is (has-metrics-except-outstanding? "test-service-3"))
+        (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))))))
 
 (deftest test-transient-metrics-data-producer
   (let [service-id->metrics-atom (atom {})

                                                  ["nested" "test-throughput"]
                                                  ["foo" "bar"]
                                                  ["outstanding" "fum"]]))]
    (testing "retrieving metrics for specified services"
      (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))
      (is (zero? (count (.getMetrics metrics-registry))))
      (create-metrics "test-service-1")
      (is (all-service-metrics-available? (retrieve-service-metrics "test-service-1")))
      (is (zero? (count (retrieve-service-metrics "test-service-2"))))
      (is (zero? (count (retrieve-service-metrics "test-service-3"))))
      (create-metrics "test-service-2")
      (create-metrics "test-service-3")
      (is (all-service-metrics-available? (retrieve-service-metrics "test-service-1")))
      (is (all-service-metrics-available? (retrieve-service-metrics "test-service-2")))
      (is (all-service-metrics-available? (retrieve-service-metrics "test-service-3")))
      (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true))))))

(deftest test-get-waiter-metrics
  (let [metrics-registry mc/default-registry

                                                 ["nested" "test-throughput"]
                                                 ["foo" "bar"]
                                                 ["outstanding" "fum"]]))]
    (with-isolated-registry
      (testing "retrieving metrics for specified services"
        (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))
        (is (zero? (count (.getMetrics metrics-registry))))
        (create-metrics "services.test-service-1")
        (create-metrics "waiter")
        (create-metrics "services.test-service-2")
        (is (all-waiter-metrics-available? (get-waiter-metrics)))
        (.removeMatching metrics-registry (reify MetricFilter (matches [_ _ _] true)))))))

(deftest test-aggregate-router-codahale-metrics
  (let [router->metrics {"router-a" {"counters" {"instance-counts" {"a" 10, "b" 20 "in-use" 2 "my-instances" 1
