Skip to content

Commit 5f9757c

Browse files
Add missing decrement method to DogStatsDClient (#5330)
I ported an open PR and added additional fixes to the code. The `NoopDogStatsDClient` gets used by default in unit tests. However, it is missing the `decrement` method which causes tests to throw exceptions when trying to call it. The `DogStatsDClient` is also missing the decrement method. Instead, the increment method was used by the the custom metric implementation. This is adjusted. Please check the commit messages for further information. Fixes #4285 Closes #5241 The `NoopDogStatsDClient` gets used by default in unit tests. However, it is missing the `decrement` method which causes tests to throw exceptions when trying to call it. --------- Co-authored-by: Justin Johnson <booleangate@gmail.com>
1 parent 7880319 commit 5f9757c

File tree

5 files changed

+49
-27
lines changed

5 files changed

+49
-27
lines changed

index.d.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -813,43 +813,43 @@ declare namespace tracer {
813813
export interface DogStatsD {
814814
/**
815815
* Increments a metric by the specified value, optionally specifying tags.
816-
* @param {string} stat The dot-separated metric name.
817-
* @param {number} value The amount to increment the stat by.
818-
* @param {[tag:string]:string|number} tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
816+
* @param stat The dot-separated metric name.
817+
* @param value The amount to increment the stat by.
818+
* @param tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
819819
*/
820-
increment(stat: string, value?: number, tags?: { [tag: string]: string|number }): void
820+
increment(stat: string, value?: number, tags?: Record<string, string|number>): void
821821

822822
/**
823823
* Decrements a metric by the specified value, optionally specifying tags.
824-
* @param {string} stat The dot-separated metric name.
825-
* @param {number} value The amount to decrement the stat by.
826-
* @param {[tag:string]:string|number} tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
824+
* @param stat The dot-separated metric name.
825+
* @param value The amount to decrement the stat by.
826+
* @param tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
827827
*/
828-
decrement(stat: string, value?: number, tags?: { [tag: string]: string|number }): void
828+
decrement(stat: string, value?: number, tags?: Record<string, string|number>): void
829829

830830
/**
831831
* Sets a distribution value, optionally specifying tags.
832-
* @param {string} stat The dot-separated metric name.
833-
* @param {number} value The amount to increment the stat by.
834-
* @param {[tag:string]:string|number} tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
832+
* @param stat The dot-separated metric name.
833+
* @param value The amount to increment the stat by.
834+
* @param tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
835835
*/
836-
distribution(stat: string, value?: number, tags?: { [tag: string]: string|number }): void
836+
distribution(stat: string, value?: number, tags?: Record<string, string|number>): void
837837

838838
/**
839839
* Sets a gauge value, optionally specifying tags.
840-
* @param {string} stat The dot-separated metric name.
841-
* @param {number} value The amount to increment the stat by.
842-
* @param {[tag:string]:string|number} tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
840+
* @param stat The dot-separated metric name.
841+
* @param value The amount to increment the stat by.
842+
* @param tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
843843
*/
844-
gauge(stat: string, value?: number, tags?: { [tag: string]: string|number }): void
844+
gauge(stat: string, value?: number, tags?: Record<string, string|number>): void
845845

846846
/**
847847
* Sets a histogram value, optionally specifying tags.
848-
* @param {string} stat The dot-separated metric name.
849-
* @param {number} value The amount to increment the stat by.
850-
* @param {[tag:string]:string|number} tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
848+
* @param stat The dot-separated metric name.
849+
* @param value The amount to increment the stat by.
850+
* @param tags Tags to pass along, such as `{ foo: 'bar' }`. Values are combined with config.tags.
851851
*/
852-
histogram(stat: string, value?: number, tags?: { [tag: string]: string|number }): void
852+
histogram(stat: string, value?: number, tags?: Record<string, string|number>): void
853853

854854
/**
855855
* Forces any unsent metrics to be sent
@@ -871,7 +871,7 @@ declare namespace tracer {
871871

872872
/**
873873
* Links a failed login event to the current trace.
874-
* @param {string} userId The user id of the attemped login.
874+
* @param {string} userId The user id of the attempted login.
875875
* @param {boolean} exists If the user id exists.
876876
* @param {[key: string]: string} metadata Custom fields to link to the login failure event.
877877
*

packages/dd-trace/src/dogstatsd.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const TYPE_GAUGE = 'g'
1414
const TYPE_DISTRIBUTION = 'd'
1515
const TYPE_HISTOGRAM = 'h'
1616

17+
/**
18+
* @import { DogStatsD } from "../../../index.d.ts"
19+
* @implements {DogStatsD}
20+
*/
1721
class DogStatsDClient {
1822
constructor (options = {}) {
1923
if (options.metricsProxyUrl) {
@@ -39,6 +43,10 @@ class DogStatsDClient {
3943
this._add(stat, value, TYPE_COUNTER, tags)
4044
}
4145

46+
decrement (stat, value, tags) {
47+
this._add(stat, -value, TYPE_COUNTER, tags)
48+
}
49+
4250
gauge (stat, value, tags) {
4351
this._add(stat, value, TYPE_GAUGE, tags)
4452
}
@@ -77,7 +85,7 @@ class DogStatsDClient {
7785
// we're not getting a 200 from the proxy endpoint. If it's a 404,
7886
// then we know we'll never have the endpoint, so just clear out the
7987
// options. Either way, we can give UDP a try.
80-
this._httpOptions = null
88+
this._httpOptions = undefined
8189
}
8290
this._sendUdp(queue)
8391
}
@@ -185,7 +193,11 @@ class DogStatsDClient {
185193
}
186194
}
187195

188-
// This is a simplified user-facing proxy to the underlying DogStatsDClient instance
196+
/**
197+
* This is a simplified user-facing proxy to the underlying DogStatsDClient instance
198+
*
199+
* @implements {DogStatsD}
200+
*/
189201
class CustomMetrics {
190202
constructor (config) {
191203
const clientConfig = DogStatsDClient.generateClientConfig(config)
@@ -208,9 +220,9 @@ class CustomMetrics {
208220
}
209221

210222
decrement (stat, value = 1, tags) {
211-
return this.dogstatsd.increment(
223+
return this.dogstatsd.decrement(
212224
stat,
213-
value * -1,
225+
value,
214226
CustomMetrics.tagTranslator(tags)
215227
)
216228
}

packages/dd-trace/src/noop/dogstatsd.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/**
2+
* @import { DogStatsD } from "../../../../index.d.ts"
3+
* @implements {DogStatsD}
4+
*/
15
module.exports = class NoopDogStatsDClient {
26
increment () { }
37

8+
decrement () { }
9+
410
gauge () { }
511

612
distribution () { }

packages/dd-trace/test/dogstatsd.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,12 @@ describe('dogstatsd', () => {
155155

156156
client.gauge('test.avg', 10)
157157
client.increment('test.count', 10)
158+
client.decrement('test.count', 5)
158159
client.flush()
159160

160161
expect(udp4.send).to.have.been.called
161-
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.avg:10|g\ntest.count:10|c\n')
162-
expect(udp4.send.firstCall.args[2]).to.equal(30)
162+
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.avg:10|g\ntest.count:10|c\ntest.count:-5|c\n')
163+
expect(udp4.send.firstCall.args[2]).to.equal(46)
163164
})
164165

165166
it('should support tags', () => {

packages/dd-trace/test/proxy.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ describe('TracerProxy', () => {
7575

7676
noopDogStatsDClient = {
7777
increment: sinon.spy(),
78+
decrement: sinon.spy(),
7879
gauge: sinon.spy(),
7980
distribution: sinon.spy(),
8081
histogram: sinon.spy(),
@@ -654,6 +655,8 @@ describe('TracerProxy', () => {
654655
it('should not throw when calling noop methods', () => {
655656
proxy.dogstatsd.increment('inc')
656657
expect(noopDogStatsDClient.increment).to.have.been.calledWith('inc')
658+
proxy.dogstatsd.decrement('dec')
659+
expect(noopDogStatsDClient.decrement).to.have.been.calledWith('dec')
657660
proxy.dogstatsd.distribution('dist')
658661
expect(noopDogStatsDClient.distribution).to.have.been.calledWith('dist')
659662
proxy.dogstatsd.histogram('hist')

0 commit comments

Comments
 (0)