-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathMessageUtil.java
More file actions
48 lines (40 loc) · 1.84 KB
/
MessageUtil.java
File metadata and controls
48 lines (40 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.timgroup.statsd;
import java.text.NumberFormat;
import java.util.Locale;
/**
* Created by c6s on 18-9-1
*/
public class MessageUtil {
private static String messageFor(String prefix, String aspect, String value, String type) {
return messageFor(prefix, aspect, value, type, 1.0);
}
private static String messageFor(String prefix, String aspect, String value, String type, double sampleRate) {
final String message = prefix + aspect + ':' + value + '|' + type;
return (sampleRate == 1.0)
? message
: (message + "|@" + stringValueOf(sampleRate));
}
static String stringValueOf(double value) {
NumberFormat formatter = NumberFormat.getInstance(Locale.US);
formatter.setGroupingUsed(false);
formatter.setMaximumFractionDigits(19);
return formatter.format(value);
}
static String makeCountMessage(String prefix, String aspect, long delta, double sampleRate) {
return messageFor(prefix, aspect, Long.toString(delta), "c", sampleRate);
}
static String makeGaugeMessage(String prefix, String aspect, String value, boolean negative, boolean delta) {
StringBuilder message = new StringBuilder();
if (!delta && negative) {
message.append(messageFor(prefix, aspect, "0", "g")).append('\n');
}
message.append(messageFor(prefix, aspect, (delta && !negative) ? ("+" + value) : value, "g"));
return message.toString();
}
static String makeRecordSetEventMessage(String prefix, String aspect, String eventName) {
return messageFor(prefix, aspect, eventName, "s");
}
static String makeRecordExecutionTimeMessage(String prefix, String aspect, long timeInMs, double sampleRate) {
return messageFor(prefix, aspect, Long.toString(timeInMs), "ms", sampleRate);
}
}