-
-
Notifications
You must be signed in to change notification settings - Fork 31
Add metrics with increment, gauge, and distribution support #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,6 @@ foo.clj | |
| .env | ||
| .clj-kondo/.cache | ||
| tags.lock | ||
| .idea/ | ||
| *.iml | ||
| .cpcache/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| (ns sentry-clj.metrics | ||
| "Metrics integration with Sentry. | ||
|
|
||
| Provides functions for sending counters, gauges, and distributions: | ||
| - `increment` - Track incrementing values (e.g., button clicks, function calls) | ||
| - `gauge` - Track values that go up and down (e.g., memory usage, queue depth) | ||
| - `distribution` - Track value distributions (e.g., response times) | ||
|
|
||
| ## Basic Usage | ||
| ```clojure | ||
| (increment \"page_view\") | ||
| (gauge \"queue_depth\" 42.0) | ||
| (distribution \"response_time\" 187.5 :millisecond) | ||
| ``` | ||
|
|
||
| ## With Attributes | ||
| ```clojure | ||
| (distribution \"page_load\" 1.0 :millisecond {:browser \"Firefox\"}) | ||
| (increment \"api_call\" 1.0 nil {:endpoint \"/users\"}) | ||
| ```" | ||
| (:import [io.sentry Sentry SentryAttributes SentryAttribute] | ||
| [io.sentry.metrics IMetricsApi SentryMetricsParameters MetricsUnit$Duration MetricsUnit$Information MetricsUnit$Fraction])) | ||
|
|
||
| (set! *warn-on-reflection* true) | ||
|
|
||
| (defn- get-metrics-api | ||
| "Returns the IMetricsApi instance from Sentry." | ||
| ^IMetricsApi [] | ||
| (Sentry/metrics)) | ||
|
|
||
| (def ^:private unit-map | ||
| {:none nil | ||
| ;; Duration | ||
| :nanosecond MetricsUnit$Duration/NANOSECOND | ||
| :microsecond MetricsUnit$Duration/MICROSECOND | ||
| :millisecond MetricsUnit$Duration/MILLISECOND | ||
| :second MetricsUnit$Duration/SECOND | ||
| :minute MetricsUnit$Duration/MINUTE | ||
| :hour MetricsUnit$Duration/HOUR | ||
| :day MetricsUnit$Duration/DAY | ||
| :week MetricsUnit$Duration/WEEK | ||
| ;; Information | ||
| :bit MetricsUnit$Information/BIT | ||
| :byte MetricsUnit$Information/BYTE | ||
| :kilobyte MetricsUnit$Information/KILOBYTE | ||
| :kibibyte MetricsUnit$Information/KIBIBYTE | ||
| :megabyte MetricsUnit$Information/MEGABYTE | ||
| :mebibyte MetricsUnit$Information/MEBIBYTE | ||
| :gigabyte MetricsUnit$Information/GIGABYTE | ||
| :gibibyte MetricsUnit$Information/GIBIBYTE | ||
| :terabyte MetricsUnit$Information/TERABYTE | ||
| :tebibyte MetricsUnit$Information/TEBIBYTE | ||
| :petabyte MetricsUnit$Information/PETABYTE | ||
| :pebibyte MetricsUnit$Information/PEBIBYTE | ||
| :exabyte MetricsUnit$Information/EXABYTE | ||
| :exbibyte MetricsUnit$Information/EXBIBYTE | ||
| ;; Fraction | ||
| :ratio MetricsUnit$Fraction/RATIO | ||
| :percent MetricsUnit$Fraction/PERCENT}) | ||
|
|
||
| (defn- keyword->unit | ||
| "Converts a keyword to a MetricsUnit string constant. | ||
| Accepts keywords from unit-map, raw strings, or nil. | ||
| Both :none and nil mean no unit." | ||
| ^String [unit] | ||
| (cond | ||
| (nil? unit) nil | ||
| (string? unit) unit | ||
| (keyword? unit) (if (contains? unit-map unit) | ||
| (get unit-map unit) | ||
| (throw (IllegalArgumentException. (str "Unknown metric unit: " unit)))) | ||
| :else (throw (IllegalArgumentException. (str "Invalid metric unit type: " (type unit)))))) | ||
|
|
||
| (defn- attrs->params | ||
| "Converts a Clojure map of attributes to SentryMetricsParameters. | ||
| Reuses the SentryAttribute type detection pattern from sentry-clj.logging." | ||
| ^SentryMetricsParameters [attrs] | ||
| (let [attributes (reduce-kv | ||
| (fn [acc k v] | ||
| (let [attr-name (name k) | ||
| attr (cond | ||
| (string? v) (SentryAttribute/stringAttribute attr-name v) | ||
| (boolean? v) (SentryAttribute/booleanAttribute attr-name v) | ||
| (integer? v) (if (<= Integer/MIN_VALUE v Integer/MAX_VALUE) | ||
| (SentryAttribute/integerAttribute attr-name (int v)) | ||
| (SentryAttribute/doubleAttribute attr-name (double v))) | ||
| (or (double? v) (float? v)) (SentryAttribute/doubleAttribute attr-name (double v)) | ||
| :else (SentryAttribute/named attr-name v))] | ||
| (conj acc attr))) | ||
| [] | ||
| attrs)] | ||
| (SentryMetricsParameters/create | ||
| (SentryAttributes/of (into-array SentryAttribute attributes))))) | ||
|
|
||
| (defn increment | ||
| "Increment a counter metric. | ||
| Wraps Sentry.metrics().count(). | ||
| Named `increment` to avoid clash with clojure.core/count. | ||
|
|
||
| - `(increment name)` — increment by 1.0 | ||
| - `(increment name value)` — increment by value | ||
| - `(increment name value unit)` — with unit keyword or string | ||
| - `(increment name value unit attrs)` — with attributes map" | ||
| ([metric-name] | ||
| (.count (get-metrics-api) ^String metric-name)) | ||
| ([metric-name value] | ||
| (.count (get-metrics-api) ^String metric-name ^Double (double value))) | ||
| ([metric-name value unit] | ||
| (.count (get-metrics-api) ^String metric-name ^Double (double value) ^String (keyword->unit unit))) | ||
| ([metric-name value unit attrs] | ||
| (.count (get-metrics-api) ^String metric-name ^Double (double value) ^String (keyword->unit unit) ^SentryMetricsParameters (attrs->params attrs)))) | ||
|
|
||
| (defn gauge | ||
| "Record a gauge metric value. | ||
|
|
||
| - `(gauge name value)` — record value | ||
| - `(gauge name value unit)` — with unit keyword or string | ||
| - `(gauge name value unit attrs)` — with attributes map" | ||
| ([metric-name value] | ||
| (.gauge (get-metrics-api) ^String metric-name ^Double (double value))) | ||
| ([metric-name value unit] | ||
| (.gauge (get-metrics-api) ^String metric-name ^Double (double value) ^String (keyword->unit unit))) | ||
| ([metric-name value unit attrs] | ||
| (.gauge (get-metrics-api) ^String metric-name ^Double (double value) ^String (keyword->unit unit) ^SentryMetricsParameters (attrs->params attrs)))) | ||
|
|
||
| (defn distribution | ||
| "Record a distribution metric value. | ||
|
|
||
| - `(distribution name value)` — record value | ||
| - `(distribution name value unit)` — with unit keyword or string | ||
| - `(distribution name value unit attrs)` — with attributes map" | ||
| ([metric-name value] | ||
| (.distribution (get-metrics-api) ^String metric-name ^Double (double value))) | ||
| ([metric-name value unit] | ||
| (.distribution (get-metrics-api) ^String metric-name ^Double (double value) ^String (keyword->unit unit))) | ||
| ([metric-name value unit attrs] | ||
| (.distribution (get-metrics-api) ^String metric-name ^Double (double value) ^String (keyword->unit unit) ^SentryMetricsParameters (attrs->params attrs)))) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| (ns sentry-clj.metrics-test | ||
| (:require | ||
| [expectations.clojure.test :refer [defexpect expect expecting]] | ||
| [sentry-clj.metrics :as sut]) | ||
| (:import | ||
| [io.sentry Sentry SentryOptions] | ||
| [io.sentry.metrics SentryMetricsParameters MetricsUnit$Duration MetricsUnit$Information MetricsUnit$Fraction])) | ||
|
|
||
| (defn- get-test-options ^SentryOptions | ||
| [] | ||
| (let [sentry-options (SentryOptions.)] | ||
| (.setDsn sentry-options "https://key@sentry.io/proj") | ||
| (.setEnvironment sentry-options "test") | ||
| (.setRelease sentry-options "release@1.0.0") | ||
| sentry-options)) | ||
|
|
||
| (defn- setup-test-sentry! | ||
| "Initializes Sentry for testing." | ||
| [] | ||
| (let [sentry-options (get-test-options)] | ||
| (Sentry/init ^SentryOptions sentry-options) | ||
| sentry-options)) | ||
|
|
||
| ;; Unit keyword mapping tests | ||
|
|
||
| (defexpect keyword->unit-test | ||
| (expecting "converts duration keywords correctly" | ||
| (expect MetricsUnit$Duration/NANOSECOND (#'sut/keyword->unit :nanosecond)) | ||
| (expect MetricsUnit$Duration/MICROSECOND (#'sut/keyword->unit :microsecond)) | ||
| (expect MetricsUnit$Duration/MILLISECOND (#'sut/keyword->unit :millisecond)) | ||
| (expect MetricsUnit$Duration/SECOND (#'sut/keyword->unit :second)) | ||
| (expect MetricsUnit$Duration/MINUTE (#'sut/keyword->unit :minute)) | ||
| (expect MetricsUnit$Duration/HOUR (#'sut/keyword->unit :hour)) | ||
| (expect MetricsUnit$Duration/DAY (#'sut/keyword->unit :day)) | ||
| (expect MetricsUnit$Duration/WEEK (#'sut/keyword->unit :week))) | ||
|
|
||
| (expecting "converts information keywords correctly" | ||
| (expect MetricsUnit$Information/BIT (#'sut/keyword->unit :bit)) | ||
| (expect MetricsUnit$Information/BYTE (#'sut/keyword->unit :byte)) | ||
| (expect MetricsUnit$Information/KILOBYTE (#'sut/keyword->unit :kilobyte)) | ||
| (expect MetricsUnit$Information/KIBIBYTE (#'sut/keyword->unit :kibibyte)) | ||
| (expect MetricsUnit$Information/MEGABYTE (#'sut/keyword->unit :megabyte)) | ||
| (expect MetricsUnit$Information/MEBIBYTE (#'sut/keyword->unit :mebibyte)) | ||
| (expect MetricsUnit$Information/GIGABYTE (#'sut/keyword->unit :gigabyte)) | ||
| (expect MetricsUnit$Information/GIBIBYTE (#'sut/keyword->unit :gibibyte)) | ||
| (expect MetricsUnit$Information/TERABYTE (#'sut/keyword->unit :terabyte)) | ||
| (expect MetricsUnit$Information/TEBIBYTE (#'sut/keyword->unit :tebibyte)) | ||
| (expect MetricsUnit$Information/PETABYTE (#'sut/keyword->unit :petabyte)) | ||
| (expect MetricsUnit$Information/PEBIBYTE (#'sut/keyword->unit :pebibyte)) | ||
| (expect MetricsUnit$Information/EXABYTE (#'sut/keyword->unit :exabyte)) | ||
| (expect MetricsUnit$Information/EXBIBYTE (#'sut/keyword->unit :exbibyte))) | ||
|
|
||
| (expecting "converts fraction keywords correctly" | ||
| (expect MetricsUnit$Fraction/RATIO (#'sut/keyword->unit :ratio)) | ||
| (expect MetricsUnit$Fraction/PERCENT (#'sut/keyword->unit :percent))) | ||
|
|
||
| (expecting ":none and nil map to no unit" | ||
| (expect nil (#'sut/keyword->unit :none)) | ||
| (expect nil (#'sut/keyword->unit nil))) | ||
|
|
||
| (expecting "raw strings pass through" | ||
| (expect "custom_unit" (#'sut/keyword->unit "custom_unit"))) | ||
|
|
||
| (expecting "invalid keyword throws" | ||
| (expect IllegalArgumentException (#'sut/keyword->unit :bogus)))) | ||
|
|
||
| ;; attrs->params tests | ||
|
|
||
| (defexpect attrs->params-test | ||
| (expecting "converts a map to SentryMetricsParameters" | ||
| (let [params (#'sut/attrs->params {:endpoint "/users" :method "GET"})] | ||
| (expect true (instance? SentryMetricsParameters params)))) | ||
|
|
||
| (expecting "handles various attribute types" | ||
| (let [params (#'sut/attrs->params {:name "test" | ||
| :active true | ||
| :count 42 | ||
| :score 98.5})] | ||
| (expect true (instance? SentryMetricsParameters params)))) | ||
|
|
||
| (expecting "handles large integer values without overflow" | ||
| (let [params (#'sut/attrs->params {:timestamp 1700000000000})] | ||
| (expect true (instance? SentryMetricsParameters params))))) | ||
|
|
||
| ;; Smoke tests - verify the public API functions execute without errors. | ||
| ;; These call the real Sentry SDK (with a test DSN) which silently drops the | ||
| ;; metrics. We verify no exceptions are thrown and the correct nil return. | ||
|
|
||
| (defexpect increment-smoke-test | ||
| (setup-test-sentry!) | ||
| (expecting "increment with name only" | ||
| (expect nil? (sut/increment "page_view"))) | ||
| (expecting "increment with name and value" | ||
| (expect nil? (sut/increment "button_click" 2.0))) | ||
| (expecting "increment with name, value, and unit" | ||
| (expect nil? (sut/increment "requests" 1.0 :millisecond))) | ||
| (expecting "increment with name, value, nil unit" | ||
| (expect nil? (sut/increment "requests" 1.0 :none))) | ||
| (expecting "increment with name, value, string unit" | ||
| (expect nil? (sut/increment "requests" 1.0 "custom_unit"))) | ||
| (expecting "increment with name, value, unit, and attrs" | ||
| (expect nil? (sut/increment "api_call" 1.0 :none {:endpoint "/users"})))) | ||
|
|
||
| (defexpect gauge-smoke-test | ||
| (setup-test-sentry!) | ||
| (expecting "gauge with name and value" | ||
| (expect nil? (sut/gauge "queue_depth" 42.0))) | ||
| (expecting "gauge with name, value, and unit" | ||
| (expect nil? (sut/gauge "memory_usage" 1024.0 :byte))) | ||
| (expecting "gauge with name, value, unit, and attrs" | ||
| (expect nil? (sut/gauge "cpu_usage" 0.85 :ratio {:region "us-east-1"})))) | ||
|
|
||
| (defexpect distribution-smoke-test | ||
| (setup-test-sentry!) | ||
| (expecting "distribution with name and value" | ||
| (expect nil? (sut/distribution "response_time" 187.5))) | ||
| (expecting "distribution with name, value, and unit" | ||
| (expect nil? (sut/distribution "response_time" 187.5 :millisecond))) | ||
| (expecting "distribution with name, value, unit, and attrs" | ||
| (expect nil? (sut/distribution "page_load" 1.0 :millisecond {:browser "Firefox" :region "us-east-1"})))) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The
attrs->paramsfunction callsreduce-kvonattrswithout anilcheck, which can cause aNullPointerExceptionif metric functions are called withnilattributes.Severity: HIGH
Suggested Fix
Before calling
reduce-kvon theattrsparameter within theattrs->paramsfunction, add a check to ensureattrsis notnil. If it isnil, return an empty collection to avoid theNullPointerException. A(when (map? attrs) ...)guard would be an effective way to handle this.Prompt for AI Agent
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like,
reduce-kvonnilreturns the initial value ([]) - it does not throw aNullPointerException. So, the fix might be not needed.