From a454a2dfdb2484035ffd14bcc50554822ad7ccc8 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Fri, 14 Jan 2022 22:15:27 -0500 Subject: [PATCH] Allow gauge() to accept floats too (#56) --- CHANGELOG.md | 1 + src/Client.php | 4 ++-- src/StatsDClient.php | 4 ++-- tests/GaugeTest.php | 6 ++++++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eff5abd..deb4e96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi ### Changed - Supported PHP versions are now 7.4, 8.0, and 8.1 - All properties and methods now have type hints where applicable + - `gauge()` now accepts both `int` and `float` values (#56) - `ConnectionException` message is now also propagated via `trigger_error()` (#57) - The following methods return the `StatsDClient` interface instead of `Client`: - `ConfigurationException::getInstance()` diff --git a/src/Client.php b/src/Client.php index bd3a3a0..80e8377 100644 --- a/src/Client.php +++ b/src/Client.php @@ -263,12 +263,12 @@ public function time(string $metric, $func, array $tags = []): void * Gauges * * @param string $metric Metric to gauge - * @param int $value Set the value of the gauge + * @param int|float $value Set the value of the gauge * @param array $tags A list of metric tags values * * @throws ConnectionException */ - public function gauge(string $metric, int $value, array $tags = []): void + public function gauge(string $metric, $value, array $tags = []): void { $this->send([$metric => $value . '|g'], $tags); } diff --git a/src/StatsDClient.php b/src/StatsDClient.php index 4383cbc..090a439 100644 --- a/src/StatsDClient.php +++ b/src/StatsDClient.php @@ -87,12 +87,12 @@ public function time(string $metric, $func, array $tags = []): void; * Gauges * * @param string $metric Metric to gauge - * @param int $value Set the value of the gauge + * @param int|float $value Set the value of the gauge * @param array $tags A list of metric tags values * * @throws ConnectionException */ - public function gauge(string $metric, int $value, array $tags = []): void; + public function gauge(string $metric, $value, array $tags = []): void; /** * Sets - count the number of unique values passed to a key diff --git a/tests/GaugeTest.php b/tests/GaugeTest.php index 58d2aff..f0f4c49 100644 --- a/tests/GaugeTest.php +++ b/tests/GaugeTest.php @@ -9,4 +9,10 @@ public function testGauge() $this->client->gauge('test_metric', 456); $this->assertEquals('test_metric:456|g', $this->client->getLastMessage()); } + + public function testGaugeWithFloat() + { + $this->client->gauge('test_metric', 3.14); + $this->assertEquals('test_metric:3.14|g', $this->client->getLastMessage()); + } }