diff --git a/CHANGELOG.md b/CHANGELOG.md index 71d85b2d..aaa882e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This change ## Unreleased Changes +## 3.1.0-alpha.4 - 2019-12-4 +- Reintroduces old metrics format (statsd). Ziggurat now pushes metrics in both formats (statsd and prometheus like). +- Reverts the changes for exponential backoff, the current implementation was broken and a new PR is being raised with the correct approach. + ## 3.1.0-alpha.3 - 2019-11-11 - Renames report-time to report-histogram while being backward compatible diff --git a/project.clj b/project.clj index b5a1f7ec..6bc5893e 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject tech.gojek/ziggurat "3.1.0-alpha.3" +(defproject tech.gojek/ziggurat "3.1.0-alpha.4" :description "A stream processing framework to build stateless applications on kafka" :url "https://github.com/gojektech/ziggurat" :license {:name "Apache License, Version 2.0" diff --git a/src/ziggurat/kafka_delay.clj b/src/ziggurat/kafka_delay.clj index 3d4e3b01..19c36176 100644 --- a/src/ziggurat/kafka_delay.clj +++ b/src/ziggurat/kafka_delay.clj @@ -3,9 +3,11 @@ [ziggurat.util.time :refer :all])) (defn calculate-and-report-kafka-delay - ([metric-namespace record-timestamp] - (calculate-and-report-kafka-delay metric-namespace record-timestamp nil)) - ([metric-namespace record-timestamp additional-tags] - (let [now-millis (get-current-time-in-millis) - delay (- now-millis record-timestamp)] - (metrics/report-histogram metric-namespace delay additional-tags)))) + ([metric-namespaces record-timestamp] + (calculate-and-report-kafka-delay metric-namespaces record-timestamp nil)) + ([metric-namespaces record-timestamp additional-tags] + (let [now-millis (get-current-time-in-millis) + delay (- now-millis record-timestamp) + default-namespace (last metric-namespaces) + multi-namespaces [metric-namespaces [default-namespace]]] + (metrics/multi-ns-report-histogram multi-namespaces delay additional-tags)))) diff --git a/src/ziggurat/mapper.clj b/src/ziggurat/mapper.clj index c66e90a7..0b7e149a 100644 --- a/src/ziggurat/mapper.clj +++ b/src/ziggurat/mapper.clj @@ -16,69 +16,78 @@ (defn mapper-func [mapper-fn channels] (fn [{:keys [topic-entity message] :as message-payload}] - (let [topic-entity-name (name topic-entity) - new-relic-transaction-name (str topic-entity-name ".handler-fn") - default-namespace "message-processing" - additional-tags {:topic_name topic-entity-name} - success-metric "success" - retry-metric "retry" - skip-metric "skip" - failure-metric "failure"] + (let [service-name (:app-name (ziggurat-config)) + topic-entity-name (name topic-entity) + new-relic-transaction-name (str topic-entity-name ".handler-fn") + message-processing-namespace "message-processing" + base-metric-namespaces [service-name topic-entity-name] + message-processing-namespaces (conj base-metric-namespaces message-processing-namespace) + additional-tags {:topic_name topic-entity-name} + success-metric "success" + retry-metric "retry" + skip-metric "skip" + failure-metric "failure" + multi-message-processing-namespaces [message-processing-namespaces [message-processing-namespace]]] (nr/with-tracing "job" new-relic-transaction-name (try - (let [start-time (.toEpochMilli (Instant/now)) - return-code (mapper-fn message) - end-time (.toEpochMilli (Instant/now)) - time-val (- end-time start-time) - execution-time-namespace "handler-fn-execution-time"] - (metrics/report-histogram execution-time-namespace time-val additional-tags) + (let [start-time (.toEpochMilli (Instant/now)) + return-code (mapper-fn message) + end-time (.toEpochMilli (Instant/now)) + time-val (- end-time start-time) + execution-time-namespace "handler-fn-execution-time" + multi-execution-time-namespaces [(conj base-metric-namespaces execution-time-namespace) + [execution-time-namespace]]] + (metrics/multi-ns-report-histogram multi-execution-time-namespaces time-val additional-tags) (case return-code - :success (metrics/increment-count default-namespace success-metric additional-tags) - :retry (do (metrics/increment-count default-namespace retry-metric additional-tags) + :success (metrics/multi-ns-increment-count multi-message-processing-namespaces success-metric additional-tags) + :retry (do (metrics/multi-ns-increment-count multi-message-processing-namespaces retry-metric additional-tags) (producer/retry message-payload)) - :skip (metrics/increment-count default-namespace skip-metric additional-tags) + :skip (metrics/multi-ns-increment-count multi-message-processing-namespaces skip-metric additional-tags) :block 'TODO (do (send-msg-to-channel channels message-payload return-code) - (metrics/increment-count default-namespace success-metric additional-tags)))) + (metrics/multi-ns-increment-count multi-message-processing-namespaces success-metric additional-tags)))) (catch Throwable e (producer/retry message-payload) (sentry/report-error sentry-reporter e (str "Actor execution failed for " topic-entity-name)) - (metrics/increment-count default-namespace failure-metric additional-tags))))))) + (metrics/multi-ns-increment-count multi-message-processing-namespaces failure-metric additional-tags))))))) (defn channel-mapper-func [mapper-fn channel] (fn [{:keys [topic-entity message] :as message-payload}] - (let [service-name (:app-name (ziggurat-config)) - topic-entity-name (name topic-entity) - channel-name (name channel) - default-namespace "message-processing" - base-namespaces [service-name topic-entity-name channel-name] - metric-namespaces (conj base-namespaces default-namespace) - additional-tags {:topic_name topic-entity-name :channel_name channel-name} - metric-namespace (str/join "." metric-namespaces) - success-metric "success" - retry-metric "retry" - skip-metric "skip" - failure-metric "failure"] + (let [service-name (:app-name (ziggurat-config)) + topic-entity-name (name topic-entity) + channel-name (name channel) + message-processing-namespace "message-processing" + base-metric-namespaces [service-name topic-entity-name channel-name] + message-processing-namespaces (conj base-metric-namespaces message-processing-namespace) + additional-tags {:topic_name topic-entity-name :channel_name channel-name} + metric-namespace (str/join "." message-processing-namespaces) + success-metric "success" + retry-metric "retry" + skip-metric "skip" + failure-metric "failure" + multi-message-processing-namespaces [message-processing-namespaces [message-processing-namespace]]] (nr/with-tracing "job" metric-namespace (try - (let [start-time (.toEpochMilli (Instant/now)) - return-code (mapper-fn message) - end-time (.toEpochMilli (Instant/now)) - time-val (- end-time start-time) - execution-time-namespace "execution-time"] - (metrics/report-histogram execution-time-namespace time-val additional-tags) + (let [start-time (.toEpochMilli (Instant/now)) + return-code (mapper-fn message) + end-time (.toEpochMilli (Instant/now)) + time-val (- end-time start-time) + execution-time-namespace "execution-time" + multi-execution-time-namespace [(conj base-metric-namespaces execution-time-namespace) + [execution-time-namespace]]] + (metrics/multi-ns-report-histogram multi-execution-time-namespace time-val additional-tags) (case return-code - :success (metrics/increment-count default-namespace success-metric additional-tags) - :retry (do (metrics/increment-count default-namespace retry-metric additional-tags) + :success (metrics/multi-ns-increment-count multi-message-processing-namespaces success-metric additional-tags) + :retry (do (metrics/multi-ns-increment-count multi-message-processing-namespaces retry-metric additional-tags) (producer/retry-for-channel message-payload channel)) - :skip (metrics/increment-count default-namespace skip-metric additional-tags) + :skip (metrics/multi-ns-increment-count multi-message-processing-namespaces skip-metric additional-tags) :block 'TODO (throw (ex-info "Invalid mapper return code" {:code return-code})))) (catch Throwable e (producer/retry-for-channel message-payload channel) (sentry/report-error sentry-reporter e (str "Channel execution failed for " topic-entity-name " and for channel " channel-name)) - (metrics/increment-count default-namespace failure-metric additional-tags))))))) + (metrics/multi-ns-increment-count multi-message-processing-namespaces failure-metric additional-tags))))))) (defrecord MessagePayload [message topic-entity]) diff --git a/src/ziggurat/metrics.clj b/src/ziggurat/metrics.clj index c7059245..c0b60012 100644 --- a/src/ziggurat/metrics.clj +++ b/src/ziggurat/metrics.clj @@ -14,8 +14,8 @@ ^{:static true} [incrementCount [String String java.util.Map] void] ^{:static true} [decrementCount [String String] void] ^{:static true} [decrementCount [String String java.util.Map] void] - ^{:static true} [reportTime [String long] void] - ^{:static true} [reportTime [String long java.util.Map] void]])) + ^{:static true} [reportTime [String long] void] + ^{:static true} [reportTime [String long java.util.Map] void]])) (defonce metrics-registry (MetricRegistry.)) @@ -53,11 +53,16 @@ [names] (str/join "." names)) +(defn remove-topic-tag-for-old-namespace + [additional-tags ns] + (let [topic-name (:topic_name additional-tags)] + (dissoc additional-tags (when (some #(= % topic-name) ns) :topic_name)))) + (defn- get-metric-namespaces [metric-namespaces] (if (vector? metric-namespaces) (intercalate-dot metric-namespaces) - metric-namespaces)) + (str (:app-name (ziggurat-config)) "." metric-namespaces))) (defn- get-v [f d v] @@ -76,22 +81,32 @@ (inc-or-dec-count sign {:metric-namespace metric-namespace :metric metric :n n :additional-tags additional-tags})) ([sign {:keys [metric-namespace metric n additional-tags]}] (let [metric-ns (get-metric-namespaces metric-namespace) - meter ^Meter (mk-meter metric-ns metric (get-map additional-tags))] + meter ^Meter (mk-meter metric-ns metric (remove-topic-tag-for-old-namespace (get-map additional-tags) metric-namespace))] (.mark meter (sign (get-int n)))))) (def increment-count (partial inc-or-dec-count +)) (def decrement-count (partial inc-or-dec-count -)) +(defn multi-ns-increment-count [nss metric additional-tags] + (doseq [ns nss] + (increment-count ns metric additional-tags))) + (defn report-histogram ([metric-namespaces val] (report-histogram metric-namespaces val nil)) ([metric-namespaces val additional-tags] (let [metric-namespace (get-metric-namespaces metric-namespaces) - histogram ^Histogram (mk-histogram metric-namespace "all" additional-tags)] + histogram ^Histogram (mk-histogram metric-namespace "all" (remove-topic-tag-for-old-namespace additional-tags metric-namespaces))] (.update histogram (get-int val))))) -(def report-time report-histogram) ;; for backward compatibility +(def report-time report-histogram) ;; for backward compatibility + +(defn multi-ns-report-histogram [nss time-val additional-tags] + (doseq [ns nss] + (report-histogram ns time-val additional-tags))) + +(def multi-ns-report-time multi-ns-report-histogram) ;; for backward compatibility (defn start-statsd-reporter [statsd-config env] (let [{:keys [enabled host port]} statsd-config] @@ -101,10 +116,10 @@ (.withPort port) (.build)) - reporter (-> (DatadogReporter/forRegistry metrics-registry) - (.withTransport transport) - (.withTags [(str env)]) - (.build))] + reporter (-> (DatadogReporter/forRegistry metrics-registry) + (.withTransport transport) + (.withTags [(str env)]) + (.build))] (log/info "Starting statsd reporter") (.start reporter 1 TimeUnit/SECONDS) {:reporter reporter :transport transport})))) diff --git a/src/ziggurat/middleware/default.clj b/src/ziggurat/middleware/default.clj index 55f632da..de33c550 100644 --- a/src/ziggurat/middleware/default.clj +++ b/src/ziggurat/middleware/default.clj @@ -24,9 +24,11 @@ (catch Throwable e (let [service-name (:app-name (ziggurat-config)) additional-tags {:topic_name topic-entity-name} - default-namespace "message-parsing"] + default-namespace "message-parsing" + metric-namespaces [service-name topic-entity-name default-namespace] + multi-namespaces [metric-namespaces [default-namespace]]] (sentry/report-error sentry-reporter e (str "Couldn't parse the message with proto - " proto-class)) - (metrics/increment-count default-namespace "failed" additional-tags) + (metrics/multi-ns-increment-count multi-namespaces "failed" additional-tags) nil))) message)) diff --git a/src/ziggurat/middleware/json.clj b/src/ziggurat/middleware/json.clj index 2611bda2..d703b0ca 100644 --- a/src/ziggurat/middleware/json.clj +++ b/src/ziggurat/middleware/json.clj @@ -4,6 +4,7 @@ " (:require [cheshire.core :refer [parse-string]] [sentry-clj.async :as sentry] + [ziggurat.config :refer [ziggurat-config]] [ziggurat.sentry :refer [sentry-reporter]] [ziggurat.metrics :as metrics])) @@ -12,10 +13,13 @@ (try (parse-string message key-fn) (catch Exception e - (let [additional-tags {:topic_name (name topic-entity)} - default-namespace "json-message-parsing"] + (let [service-name (:app-name (ziggurat-config)) + additional-tags {:topic_name (name topic-entity)} + default-namespace "json-message-parsing" + metric-namespaces [service-name topic-entity default-namespace] + multi-namespaces [metric-namespaces [default-namespace]]] (sentry/report-error sentry-reporter e (str "Could not parse JSON message " message)) - (metrics/increment-count default-namespace "failed" additional-tags) + (metrics/multi-ns-increment-count multi-namespaces "failed" additional-tags) nil)))) (defn parse-json diff --git a/src/ziggurat/streams.clj b/src/ziggurat/streams.clj index 76532f2b..56327c5a 100644 --- a/src/ziggurat/streams.clj +++ b/src/ziggurat/streams.clj @@ -89,11 +89,14 @@ (set-encoding-config key-deserializer-encoding value-deserializer-encoding deserializer-encoding))) (defn- log-and-report-metrics [topic-entity message] - (let [topic-entity-name (name topic-entity) - additional-tags {:topic_name topic-entity-name} - default-namespace "message" - metric "read"] - (metrics/increment-count default-namespace metric additional-tags)) + (let [service-name (:app-name (ziggurat-config)) + topic-entity-name (name topic-entity) + additional-tags {:topic_name topic-entity-name} + message-read-metric-namespace "message" + metric-namespaces [service-name topic-entity-name message-read-metric-namespace] + multi-namespaces [metric-namespaces [message-read-metric-namespace]] + metric "read"] + (metrics/multi-ns-increment-count multi-namespaces metric additional-tags)) message) (defn store-supplier-builder [] @@ -110,9 +113,9 @@ (.mapValues stream-builder (value-mapper mapper-fn))) (defn- timestamp-transformer-supplier - [metric-namespace oldest-processed-message-in-s additional-tags] + [metric-namespaces oldest-processed-message-in-s additional-tags] (reify TransformerSupplier - (get [_] (timestamp-transformer/create metric-namespace oldest-processed-message-in-s additional-tags)))) + (get [_] (timestamp-transformer/create metric-namespaces oldest-processed-message-in-s additional-tags)))) (defn- header-transformer-supplier [] @@ -121,9 +124,10 @@ (defn- timestamp-transform-values [topic-entity-name oldest-processed-message-in-s stream-builder] (let [service-name (:app-name (ziggurat-config)) - metric-namespace "message-received-delay-histogram" + delay-metric-namespace "message-received-delay-histogram" + metric-namespaces [service-name topic-entity-name delay-metric-namespace] additional-tags {:topic_name topic-entity-name}] - (.transform stream-builder (timestamp-transformer-supplier metric-namespace oldest-processed-message-in-s additional-tags) (into-array [(.name (store-supplier-builder))])))) + (.transform stream-builder (timestamp-transformer-supplier metric-namespaces oldest-processed-message-in-s additional-tags) (into-array [(.name (store-supplier-builder))])))) (defn- header-transform-values [stream-builder] (.transformValues stream-builder (header-transformer-supplier) (into-array [(.name (store-supplier-builder))]))) diff --git a/test/ziggurat/mapper_test.clj b/test/ziggurat/mapper_test.clj index 29160f9d..c5ed115f 100644 --- a/test/ziggurat/mapper_test.clj +++ b/test/ziggurat/mapper_test.clj @@ -13,24 +13,28 @@ fix/silence-logging])) (deftest mapper-func-test - (let [service-name (:app-name (ziggurat-config)) - stream-routes {:default {:handler-fn #(constantly nil)}} - topic-entity (name (first (keys stream-routes))) - message-payload {:message {:foo "bar"} :topic-entity (keyword topic-entity)} - expected-additional-tags {:topic_name topic-entity} - expected-metric-namespace "message-processing" - expected-report-time-namespace "handler-fn-execution-time"] + (let [service-name (:app-name (ziggurat-config)) + stream-routes {:default {:handler-fn #(constantly nil)}} + topic-entity (name (first (keys stream-routes))) + message-payload {:message {:foo "bar"} :topic-entity (keyword topic-entity)} + expected-additional-tags {:topic_name topic-entity} + expected-metric-namespace "message-processing" + report-time-namespace "handler-fn-execution-time" + expected-metric-namespaces [topic-entity expected-metric-namespace] + expected-report-time-namespaces [topic-entity report-time-namespace]] (testing "message process should be successful" (let [successfully-processed? (atom false) successfully-reported-time? (atom false) expected-metric "success"] - (with-redefs [metrics/increment-count (fn [metric-namespace metric additional-tags] - (when (and (= metric-namespace expected-metric-namespace) + (with-redefs [metrics/increment-count (fn [metric-namespaces metric additional-tags] + (when (and (or (= metric-namespaces expected-metric-namespaces) + (= metric-namespaces [expected-metric-namespace])) (= metric expected-metric) (= additional-tags expected-additional-tags)) (reset! successfully-processed? true))) - metrics/report-histogram (fn [metric-namespace _ _] - (when (= metric-namespace expected-report-time-namespace) + metrics/report-histogram (fn [metric-namespaces _ _] + (when (or (= metric-namespaces expected-report-time-namespaces) + (= metric-namespaces [report-time-namespace])) (reset! successfully-reported-time? true)))] ((mapper-func (constantly :success) []) message-payload) (is @successfully-processed?) @@ -41,7 +45,8 @@ (let [successfully-processed? (atom false) expected-metric "success"] (with-redefs [metrics/increment-count (fn [metric-namespace metric additional-tags] - (when (and (= metric-namespace expected-metric-namespace) + (when (and (or (= metric-namespace [service-name topic-entity expected-metric-namespace]) + (= metric-namespace [expected-metric-namespace])) (= metric expected-metric) (= additional-tags expected-additional-tags)) (reset! successfully-processed? true)))] @@ -68,7 +73,8 @@ expected-metric "retry"] (with-redefs [metrics/increment-count (fn [metric-namespace metric additional-tags] - (when (and (= metric-namespace expected-metric-namespace) + (when (and (or (= metric-namespace [service-name topic-entity expected-metric-namespace]) + (= metric-namespace [expected-metric-namespace])) (= metric expected-metric) (= additional-tags expected-additional-tags)) (reset! unsuccessfully-processed? true)))] @@ -85,7 +91,8 @@ expected-metric "failure"] (with-redefs [sentry-report (fn [_ _ _ & _] (reset! sentry-report-fn-called? true)) metrics/increment-count (fn [metric-namespace metric additional-tags] - (when (and (= metric-namespace expected-metric-namespace) + (when (and (or (= metric-namespace [service-name topic-entity expected-metric-namespace]) + (= metric-namespace [expected-metric-namespace])) (= metric expected-metric) (= additional-tags expected-additional-tags)) (reset! unsuccessfully-processed? true)))] @@ -97,11 +104,12 @@ (testing "reports execution time with topic prefix" (let [reported-execution-time? (atom false) - expected-metric-namespace "handler-fn-execution-time"] - (with-redefs [metrics/report-histogram (fn [metric-namespace _ _] - (when (= metric-namespace expected-metric-namespace) + expected-metric-namespace "handler-fn-execution-time" + expected-metric-namespaces [service-name "default" expected-metric-namespace]] + (with-redefs [metrics/report-histogram (fn [metric-namespaces _ _] + (when (or (= metric-namespaces expected-metric-namespaces) + (= metric-namespaces [expected-metric-namespace])) (reset! reported-execution-time? true)))] - ((mapper-func (constantly :success) []) message-payload) (is @reported-execution-time?)))))) @@ -117,12 +125,14 @@ :topic-entity topic} expected-topic-entity-name (name topic) expected-additional-tags {:topic_name expected-topic-entity-name :channel_name channel-name} - expected-metric-namespace "message-processing"] + increment-count-namespace "message-processing" + expected-increment-count-namespaces [service-name topic channel-name increment-count-namespace]] (testing "message process should be successful" (let [successfully-processed? (atom false) expected-metric "success"] (with-redefs [metrics/increment-count (fn [metric-namespace metric additional-tags] - (when (and (= metric-namespace expected-metric-namespace) + (when (and (or (= metric-namespace expected-increment-count-namespaces) + (= metric-namespace [increment-count-namespace])) (= metric expected-metric) (= additional-tags expected-additional-tags)) (reset! successfully-processed? true)))] @@ -136,7 +146,8 @@ expected-metric "retry"] (with-redefs [metrics/increment-count (fn [metric-namespace metric additional-tags] - (when (and (= metric-namespace expected-metric-namespace) + (when (and (or (= metric-namespace expected-increment-count-namespaces) + (= metric-namespace [increment-count-namespace])) (= metric expected-metric) (= additional-tags expected-additional-tags)) (reset! unsuccessfully-processed? true)))] @@ -153,7 +164,8 @@ expected-metric "failure"] (with-redefs [sentry-report (fn [_ _ _ & _] (reset! sentry-report-fn-called? true)) metrics/increment-count (fn [metric-namespace metric additional-tags] - (when (and (= metric-namespace expected-metric-namespace) + (when (and (or (= metric-namespace expected-increment-count-namespaces) + (= metric-namespace [increment-count-namespace])) (= metric expected-metric) (= additional-tags expected-additional-tags)) (reset! unsuccessfully-processed? true)))] @@ -162,12 +174,13 @@ (is (= message-from-mq expected-message))) (is @unsuccessfully-processed?) (is @sentry-report-fn-called?))))) - (testing "reports execution time with topic prefix" (let [reported-execution-time? (atom false) - execution-time-namespace "execution-time"] - (with-redefs [metrics/report-histogram (fn [metric-namespace _ _] - (when (= metric-namespace execution-time-namespace) + execution-time-namespace "execution-time" + expected-execution-time-namespaces [service-name expected-topic-entity-name channel-name execution-time-namespace]] + (with-redefs [metrics/report-histogram (fn [metric-namespaces _ _] + (when (or (= metric-namespaces expected-execution-time-namespaces) + (= metric-namespaces [execution-time-namespace])) (reset! reported-execution-time? true)))] ((channel-mapper-func (constantly :success) channel) message-payload) (is @reported-execution-time?)))))) diff --git a/test/ziggurat/metrics_test.clj b/test/ziggurat/metrics_test.clj index 54450a9b..e5e96371 100644 --- a/test/ziggurat/metrics_test.clj +++ b/test/ziggurat/metrics_test.clj @@ -50,18 +50,18 @@ input-additional-tags {:topic_name expected-topic-entity-name} expected-n 1] (testing "increases count on the meter - vector as an argument" - (let [expected-metric-namespace ["metric" "ns"] + (let [expected-metric-namespaces [expected-topic-entity-name "metric-ns"] mk-meter-args (atom nil) meter (Meter.) - expected-additional-tags input-additional-tags] - (with-redefs [metrics/mk-meter (fn [metric-namespace metric additional-tags] + expected-additional-tags {}] + (with-redefs [metrics/mk-meter (fn [metric-namespaces metric additional-tags] (is (= additional-tags expected-additional-tags)) - (reset! mk-meter-args {:metric-namespace metric-namespace + (reset! mk-meter-args {:metric-namespaces metric-namespaces :metric metric}) meter)] - (metrics/increment-count expected-metric-namespace metric expected-n input-additional-tags) + (metrics/increment-count expected-metric-namespaces metric expected-n input-additional-tags) (is (= expected-n (.getCount meter))) - (is (= (metrics/intercalate-dot expected-metric-namespace) (:metric-namespace @mk-meter-args))) + (is (= (metrics/intercalate-dot expected-metric-namespaces) (:metric-namespaces @mk-meter-args))) (is (= metric (:metric @mk-meter-args)))))) (testing "increases count on the meter - 3rd argument is a number" (let [expected-metric-namespace ["metric" "ns"] @@ -103,21 +103,21 @@ meter)] (metrics/increment-count expected-metric-namespaces metric expected-n input-additional-tags) (is (= expected-n (.getCount meter))) - (is (= expected-metric-namespaces (:metric-namespaces @mk-meter-args))) + (is (= (str (:app-name (ziggurat-config)) "." expected-metric-namespaces) (:metric-namespaces @mk-meter-args))) (is (= metric (:metric @mk-meter-args)))))) (testing "increases count on the meter - w/o additional-tags argument" - (let [expected-metric-namespace "metric-ns" + (let [expected-metric-namespaces [expected-topic-entity-name "metric-ns"] mk-meter-args (atom nil) meter (Meter.) expected-additional-tags {}] - (with-redefs [metrics/mk-meter (fn [metric-namespace metric additional-tags] + (with-redefs [metrics/mk-meter (fn [metric-namespaces metric additional-tags] (is (= additional-tags expected-additional-tags)) - (reset! mk-meter-args {:metric-namespace metric-namespace + (reset! mk-meter-args {:metric-namespace metric-namespaces :metric metric}) meter)] - (metrics/increment-count expected-metric-namespace metric) + (metrics/increment-count expected-metric-namespaces metric) (is (= expected-n (.getCount meter))) - (is (= expected-metric-namespace (:metric-namespace @mk-meter-args))) + (is (= (metrics/intercalate-dot expected-metric-namespaces) (:metric-namespace @mk-meter-args))) (is (= metric (:metric @mk-meter-args)))))) (testing "increases count on the meter when additional-tags is nil" (let [expected-metric-namespace "metric-ns" @@ -129,9 +129,9 @@ (reset! mk-meter-args {:metric-namespace metric-namespace :metric metric}) meter)] - (metrics/increment-count expected-metric-namespace metric expected-n expected-additional-tags) + (metrics/increment-count expected-metric-namespace metric expected-n nil) (is (= expected-n (.getCount meter))) - (is (= expected-metric-namespace (:metric-namespace @mk-meter-args))) + (is (= (str (:app-name (ziggurat-config)) "." expected-metric-namespace) (:metric-namespace @mk-meter-args))) (is (= metric (:metric @mk-meter-args)))))) (testing "-incrementCount calls increment-count with the correct arguments" (let [metric-namespace "namespace" @@ -165,18 +165,18 @@ input-additional-tags {:topic_name expected-topic-name} expected-n 1] (testing "decreases count on the meter - vector as an argument" - (let [expected-additional-tags input-additional-tags - expected-metric-namespace ["metric" "ns"]] - (with-redefs [metrics/mk-meter (fn [metric-namespace metric additional-tags] + (let [expected-additional-tags {} + expected-metric-namespaces [expected-topic-name "metric-ns"]] + (with-redefs [metrics/mk-meter (fn [metric-namespaces metric additional-tags] (is (= additional-tags expected-additional-tags)) - (reset! mk-meter-args {:metric-namespace metric-namespace + (reset! mk-meter-args {:metric-namespaces metric-namespaces :metric metric}) meter)] - (metrics/increment-count expected-metric-namespace metric expected-n input-additional-tags) + (metrics/increment-count expected-metric-namespaces metric expected-n input-additional-tags) (is (= expected-n (.getCount meter))) - (metrics/decrement-count expected-metric-namespace metric expected-n input-additional-tags) + (metrics/decrement-count expected-metric-namespaces metric expected-n input-additional-tags) (is (zero? (.getCount meter))) - (is (= (metrics/intercalate-dot expected-metric-namespace) (:metric-namespace @mk-meter-args))) + (is (= (metrics/intercalate-dot expected-metric-namespaces) (:metric-namespaces @mk-meter-args))) (is (= metric (:metric @mk-meter-args)))))) (testing "decreases count on the meter - string as an argument" (let [expected-additional-tags input-additional-tags @@ -190,7 +190,7 @@ (is (= expected-n (.getCount meter))) (metrics/decrement-count expected-metric-namespaces metric expected-n input-additional-tags) (is (zero? (.getCount meter))) - (is (= expected-metric-namespaces (:metric-namespaces @mk-meter-args))) + (is (= (str (:app-name (ziggurat-config)) "." expected-metric-namespaces) (:metric-namespaces @mk-meter-args))) (is (= metric (:metric @mk-meter-args)))))) (testing "decreases count on the meter - without topic name on the namespace" (let [expected-additional-tags input-additional-tags @@ -214,11 +214,11 @@ (reset! mk-meter-args {:metric-namespace metric-namespace :metric metric}) meter)] - (metrics/increment-count expected-metric-namespace metric expected-n expected-additional-tags) + (metrics/increment-count expected-metric-namespace metric expected-n nil) (is (= expected-n (.getCount meter))) - (metrics/decrement-count expected-metric-namespace metric expected-n expected-additional-tags) + (metrics/decrement-count expected-metric-namespace metric expected-n nil) (is (zero? (.getCount meter))) - (is (= expected-metric-namespace (:metric-namespace @mk-meter-args))) + (is (= (str (:app-name (ziggurat-config)) "." expected-metric-namespace) (:metric-namespace @mk-meter-args))) (is (= metric (:metric @mk-meter-args)))))) (testing "-decrementCount passes the correct arguments to decrement-count" (let [metric-namespace "namespace" @@ -249,11 +249,11 @@ input-additional-tags {:topic_name expected-topic-entity-name} time-val 10] (testing "updates time-val - vector as an argument" - (let [expected-metric-namespace ["message-received-delay-histogram" "ns"] + (let [expected-metric-namespace [expected-topic-entity-name "message-received-delay-histogram"] mk-histogram-args (atom nil) reservoir (UniformReservoir.) histogram (Histogram. reservoir) - expected-additional-tags input-additional-tags] + expected-additional-tags {}] (with-redefs [metrics/mk-histogram (fn [metric-namespace metric additional-tags] (is (= additional-tags expected-additional-tags)) (reset! mk-histogram-args {:metric-namespace metric-namespace @@ -276,7 +276,7 @@ histogram)] (metrics/report-histogram expected-metric-namespaces time-val input-additional-tags) (is (= 1 (.getCount histogram))) - (is (= expected-metric-namespaces (:metric-namespaces @mk-histogram-args))) + (is (= (str (:app-name (ziggurat-config)) "." expected-metric-namespaces) (:metric-namespaces @mk-histogram-args))) (is (= "all" (:metric @mk-histogram-args)))))) (testing "updates time-val - w/o additional-tags argument" (let [expected-metric-namespace "message-received-delay-histogram" @@ -291,7 +291,7 @@ histogram)] (metrics/report-histogram expected-metric-namespace time-val) (is (= 1 (.getCount histogram))) - (is (= expected-metric-namespace (:metric-namespace @mk-histogram-args))) + (is (= (str (:app-name (ziggurat-config)) "." expected-metric-namespace) (:metric-namespace @mk-histogram-args))) (is (= "all" (:metric @mk-histogram-args))))))) (testing "report time java function passes the correct parameters to report time" (let [expected-metric-namespace "namespace" @@ -317,3 +317,33 @@ (reset! report-histogram-called? true)))] (metrics/-reportTime expected-metric-namespace expected-time-val additional-tags) (is (true? @report-histogram-called?)))))) + +(deftest multi-ns-increment-count-test + (testing "multi-ns-increment-count calls increment-count for every namespace list passed" + (let [metric-namespaces-list [["test" "multi" "ns"] ["test-ns"]] + expected-metric "test-metric" + expected-additional-tags {:foo "bar"} + increment-count-call-counts (atom 0) + expected-increment-count-call-counts 2] + (with-redefs [metrics/increment-count (fn [metric-namespaces metric additional-tags] + (when (and (some #{metric-namespaces} metric-namespaces-list) + (= metric expected-metric) + (= additional-tags expected-additional-tags)) + (swap! increment-count-call-counts inc)))] + (metrics/multi-ns-increment-count metric-namespaces-list expected-metric expected-additional-tags) + (is (= expected-increment-count-call-counts @increment-count-call-counts)))))) + +(deftest multi-ns-report-time-test + (testing "multi-ns-report-time calls report-histogram for every namespace list passed" + (let [metric-namespaces-list [["test" "multi" "ns"] ["test-ns"]] + expected-metric "test-metric" + expected-additional-tags {:foo "bar"} + report-histogram-call-counts (atom 0) + expected-report-histogram-call-counts 2] + (with-redefs [metrics/increment-count (fn [metric-namespaces metric additional-tags] + (when (and (some #{metric-namespaces} metric-namespaces-list) + (= metric expected-metric) + (= additional-tags expected-additional-tags)) + (swap! report-histogram-call-counts inc)))] + (metrics/multi-ns-increment-count metric-namespaces-list expected-metric expected-additional-tags) + (is (= expected-report-histogram-call-counts @report-histogram-call-counts)))))) \ No newline at end of file diff --git a/test/ziggurat/middleware/default_test.clj b/test/ziggurat/middleware/default_test.clj index bf951f85..c117e927 100644 --- a/test/ziggurat/middleware/default_test.clj +++ b/test/ziggurat/middleware/default_test.clj @@ -30,8 +30,8 @@ handler-fn (fn [msg] (if (nil? msg) (reset! handler-fn-called? true)))] - (with-redefs [metrics/increment-count (fn [_ _ _] - (reset! metric-reporter-called? true))] + (with-redefs [metrics/multi-ns-increment-count (fn [_ _ _] + (reset! metric-reporter-called? true))] ((protobuf->hash handler-fn nil topic-entity-name) nil)) (is (true? @handler-fn-called?)) (is (true? @metric-reporter-called?)))) diff --git a/test/ziggurat/middleware/json_test.clj b/test/ziggurat/middleware/json_test.clj index 0f1a009a..994e2b57 100644 --- a/test/ziggurat/middleware/json_test.clj +++ b/test/ziggurat/middleware/json_test.clj @@ -52,8 +52,8 @@ handler-fn (fn [msg] (if (nil? msg) (reset! handler-fn-called? true)))] - (with-redefs [metrics/increment-count (fn [_ _ _] - (reset! metric-reporter-called? true))] + (with-redefs [metrics/multi-ns-increment-count (fn [_ _ _] + (reset! metric-reporter-called? true))] ((parse-json handler-fn topic-entity-name true) message)) (is (true? @handler-fn-called?)) (is (true? @metric-reporter-called?)))))