Skip to content

Commit 35d5e40

Browse files
authored
Merge pull request #80 from locationlabs/fix_entity_id_with_constant_tags
fix entity id with constant tags
2 parents 5176012 + 2f71ab4 commit 35d5e40

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ String tagString(final String[] tags) {
755755
public void count(final String aspect, final long delta, final String... tags) {
756756
send(new StringBuilder(prefix).append(aspect).append(":").append(delta).append("|c").append(tagString(tags)).toString());
757757
}
758-
758+
759759
/**
760760
* {@inheritDoc}
761761
*/
@@ -809,7 +809,7 @@ public void count(final String aspect, final double delta, final double sampleRa
809809
public void incrementCounter(final String aspect, final String... tags) {
810810
count(aspect, 1, tags);
811811
}
812-
812+
813813
/**
814814
* {@inheritDoc}
815815
*/
@@ -825,7 +825,7 @@ public void incrementCounter(final String aspect, final double sampleRate, final
825825
public void increment(final String aspect, final String... tags) {
826826
incrementCounter(aspect, tags);
827827
}
828-
828+
829829
/**
830830
* {@inheritDoc}
831831
*/
@@ -848,7 +848,7 @@ public void increment(final String aspect, final double sampleRate, final String
848848
public void decrementCounter(final String aspect, final String... tags) {
849849
count(aspect, -1, tags);
850850
}
851-
851+
852852
/**
853853
* {@inheritDoc}
854854
*/
@@ -864,7 +864,7 @@ public void decrementCounter(String aspect, final double sampleRate, final Strin
864864
public void decrement(final String aspect, final String... tags) {
865865
decrementCounter(aspect, tags);
866866
}
867-
867+
868868
/**
869869
* {@inheritDoc}
870870
*/
@@ -891,7 +891,7 @@ public void recordGaugeValue(final String aspect, final double value, final Stri
891891
* padding with extra 0s to represent precision */
892892
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|g").append(tagString(tags)).toString());
893893
}
894-
894+
895895
/**
896896
* {@inheritDoc}
897897
*/
@@ -910,7 +910,7 @@ public void recordGaugeValue(final String aspect, final double value, final doub
910910
public void gauge(final String aspect, final double value, final String... tags) {
911911
recordGaugeValue(aspect, value, tags);
912912
}
913-
913+
914914
/**
915915
* {@inheritDoc}
916916
*/
@@ -936,7 +936,7 @@ public void gauge(final String aspect, final double value, final double sampleRa
936936
public void recordGaugeValue(final String aspect, final long value, final String... tags) {
937937
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|g").append(tagString(tags)).toString());
938938
}
939-
939+
940940
/**
941941
* {@inheritDoc}
942942
*/
@@ -955,7 +955,7 @@ public void recordGaugeValue(final String aspect, final long value, final double
955955
public void gauge(final String aspect, final long value, final String... tags) {
956956
recordGaugeValue(aspect, value, tags);
957957
}
958-
958+
959959
/**
960960
* {@inheritDoc}
961961
*/
@@ -980,7 +980,7 @@ public void gauge(final String aspect, final long value, final double sampleRate
980980
public void recordExecutionTime(final String aspect, final long timeInMs, final String... tags) {
981981
send(new StringBuilder(prefix).append(aspect).append(":").append(timeInMs).append("|ms").append(tagString(tags)).toString());
982982
}
983-
983+
984984
/**
985985
* {@inheritDoc}
986986
*/
@@ -999,7 +999,7 @@ public void recordExecutionTime(final String aspect, final long timeInMs, final
999999
public void time(final String aspect, final long value, final String... tags) {
10001000
recordExecutionTime(aspect, value, tags);
10011001
}
1002-
1002+
10031003
/**
10041004
* {@inheritDoc}
10051005
*/
@@ -1026,7 +1026,7 @@ public void recordHistogramValue(final String aspect, final double value, final
10261026
* padding with extra 0s to represent precision */
10271027
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|h").append(tagString(tags)).toString());
10281028
}
1029-
1029+
10301030
/**
10311031
* {@inheritDoc}
10321032
*/
@@ -1047,7 +1047,7 @@ public void recordHistogramValue(final String aspect, final double value, final
10471047
public void histogram(final String aspect, final double value, final String... tags) {
10481048
recordHistogramValue(aspect, value, tags);
10491049
}
1050-
1050+
10511051
/**
10521052
* {@inheritDoc}
10531053
*/
@@ -1072,7 +1072,7 @@ public void histogram(final String aspect, final double value, final double samp
10721072
public void recordHistogramValue(final String aspect, final long value, final String... tags) {
10731073
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|h").append(tagString(tags)).toString());
10741074
}
1075-
1075+
10761076
/**
10771077
* {@inheritDoc}
10781078
*/
@@ -1091,7 +1091,7 @@ public void recordHistogramValue(final String aspect, final long value, final do
10911091
public void histogram(final String aspect, final long value, final String... tags) {
10921092
recordHistogramValue(aspect, value, tags);
10931093
}
1094-
1094+
10951095
/**
10961096
* {@inheritDoc}
10971097
*/
@@ -1104,7 +1104,7 @@ public void histogram(final String aspect, final long value, final double sample
11041104
* Records a value for the specified named distribution.
11051105
*
11061106
* <p>This method is non-blocking and is guaranteed not to throw an exception.</p>
1107-
*
1107+
*
11081108
* <p>This is a beta feature and must be enabled specifically for your organization.</p>
11091109
*
11101110
* @param aspect
@@ -1120,7 +1120,7 @@ public void recordDistributionValue(final String aspect, final double value, fin
11201120
* padding with extra 0s to represent precision */
11211121
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|d").append(tagString(tags)).toString());
11221122
}
1123-
1123+
11241124
/**
11251125
* {@inheritDoc}
11261126
*/
@@ -1141,7 +1141,7 @@ public void recordDistributionValue(final String aspect, final double value, fin
11411141
public void distribution(final String aspect, final double value, final String... tags) {
11421142
recordDistributionValue(aspect, value, tags);
11431143
}
1144-
1144+
11451145
/**
11461146
* {@inheritDoc}
11471147
*/
@@ -1153,7 +1153,7 @@ public void distribution(final String aspect, final double value, final double s
11531153
* Records a value for the specified named distribution.
11541154
*
11551155
* <p>This method is non-blocking and is guaranteed not to throw an exception.</p>
1156-
*
1156+
*
11571157
* <p>This is a beta feature and must be enabled specifically for your organization.</p>
11581158
*
11591159
* @param aspect
@@ -1167,7 +1167,7 @@ public void distribution(final String aspect, final double value, final double s
11671167
public void recordDistributionValue(final String aspect, final long value, final String... tags) {
11681168
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|d").append(tagString(tags)).toString());
11691169
}
1170-
1170+
11711171
/**
11721172
* {@inheritDoc}
11731173
*/
@@ -1186,7 +1186,7 @@ public void recordDistributionValue(final String aspect, final long value, final
11861186
public void distribution(final String aspect, final long value, final String... tags) {
11871187
recordDistributionValue(aspect, value, tags);
11881188
}
1189-
1189+
11901190
/**
11911191
* {@inheritDoc}
11921192
*/
@@ -1288,7 +1288,8 @@ private String[] updateTagsWithEntityID(String[] tags, String entityID) {
12881288
tags = new String[]{entityTag};
12891289
} else {
12901290
tags = Arrays.copyOf(tags, tags.length+1);
1291-
tags[tags.length] = entityTag;
1291+
// Now that tags is one element longer, tags.length has changed...
1292+
tags[tags.length - 1] = entityTag;
12921293
}
12931294
}
12941295
return tags;
@@ -1385,7 +1386,7 @@ public void recordSetValue(final String aspect, final String value, final String
13851386
private void send(final String message) {
13861387
statsDSender.send(message);
13871388
}
1388-
1389+
13891390
private boolean isInvalidSample(double sampleRate) {
13901391
return sampleRate != 1 && ThreadLocalRandom.current().nextDouble() > sampleRate;
13911392
}

src/test/java/com/timgroup/statsd/NonBlockingStatsDClientTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,18 @@ public void sends_gauge_entityID_from_env() throws Exception {
471471
assertThat(server.messagesReceived(), contains("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity"));
472472
}
473473

474+
@Test(timeout = 5000L)
475+
public void sends_gauge_entityID_from_env_and_constant_tags() throws Exception {
476+
final String entity_value = "foo-entity";
477+
environmentVariables.set(NonBlockingStatsDClient.DD_ENTITY_ID_ENV_VAR, entity_value);
478+
final String constantTags = "arbitraryTag:arbitraryValue";
479+
final NonBlockingStatsDClient client = new NonBlockingStatsDClient("my.prefix", "localhost", STATSD_SERVER_PORT, constantTags);
480+
client.gauge("value", 423);
481+
server.waitForMessage();
482+
483+
assertThat(server.messagesReceived(), contains("my.prefix.value:423|g|#dd.internal.entity_id:foo-entity," + constantTags));
484+
}
485+
474486
@Test(timeout = 5000L)
475487
public void sends_gauge_entityID_from_args() throws Exception {
476488
final String entity_value = "foo-entity";

0 commit comments

Comments
 (0)