@@ -77,39 +77,48 @@ public List<Record> parse(final ByteBuffer datagram) throws ParsingException {
77
77
// CHECKSTYLE.OFF: IllegalInstantiation - This is the recommended way
78
78
final String datagramAsString = new String (datagram .array (), Charsets .UTF_8 );
79
79
final ImmutableList .Builder <Record > recordListBuilder = ImmutableList .builder ();
80
- for (final String line : LINE_SPLITTER .split (datagramAsString )) {
81
- // CHECKSTYLE.ON: IllegalInstantiation
82
- final Matcher matcher = STATSD_PATTERN .matcher (line );
83
- if (!matcher .matches ()) {
84
- throw new ParsingException ("Invalid statsd line" , line .getBytes (Charsets .UTF_8 ));
85
- }
80
+ try {
81
+ for (final String line : LINE_SPLITTER .split (datagramAsString )) {
82
+ // CHECKSTYLE.ON: IllegalInstantiation
83
+ final Matcher matcher = STATSD_PATTERN .matcher (line );
84
+ if (!matcher .matches ()) {
85
+ throw new ParsingException ("Invalid statsd line" , line .getBytes (Charsets .UTF_8 ));
86
+ }
86
87
87
- // Parse the name
88
- final String name = parseName (datagram , matcher .group ("NAME" ));
88
+ // Parse the name
89
+ final String name = parseName (datagram , matcher .group ("NAME" ));
89
90
90
- // Parse the _metricType
91
- final StatsdType type = parseStatsdType (datagram , matcher .group ("TYPE" ));
91
+ // Parse the _metricType
92
+ final StatsdType type = parseStatsdType (datagram , matcher .group ("TYPE" ));
92
93
93
- // Parse the value
94
- final Number value = parseValue (datagram , matcher .group ("VALUE" ), type );
94
+ // Parse the value
95
+ final Number value = parseValue (datagram , matcher .group ("VALUE" ), type );
95
96
96
- // Parse the sample rate
97
- final Optional <Double > sampleRate = parseSampleRate (datagram , matcher .group ("SAMPLERATE" ), type );
97
+ // Parse the sample rate
98
+ final Optional <Double > sampleRate = parseSampleRate (datagram , matcher .group ("SAMPLERATE" ), type );
98
99
99
- // Parse the tags
100
- final ImmutableMap <String , String > annotations = parseTags (matcher .group ("TAGS" ));
100
+ // Parse the tags
101
+ final ImmutableMap <String , String > annotations = ImmutableMap .<String , String >builder ()
102
+ .putAll (parseTags (matcher .group ("TAGS" )))
103
+ .putAll (parseInfluxStyleTags (matcher .group ("INFLUXTAGS" )))
104
+ .build ();
101
105
102
- // Enforce sampling
103
- if (sampleRate .isPresent () && sampleRate .get ().compareTo (1.0 ) != 0 ) {
104
- if (sampleRate .get ().compareTo (0.0 ) == 0 ) {
105
- return Collections .emptyList ();
106
- }
107
- if (Double .compare (_randomSupplier .get ().nextDouble (), sampleRate .get ()) > 0 ) {
108
- return Collections .emptyList ();
106
+ // Enforce sampling
107
+ if (sampleRate .isPresent () && sampleRate .get ().compareTo (1.0 ) != 0 ) {
108
+ if (sampleRate .get ().compareTo (0.0 ) == 0 ) {
109
+ return Collections .emptyList ();
110
+ }
111
+ if (Double .compare (_randomSupplier .get ().nextDouble (), sampleRate .get ()) > 0 ) {
112
+ return Collections .emptyList ();
113
+ }
109
114
}
110
- }
111
115
112
- recordListBuilder .add (createRecord (name , value , type , annotations ));
116
+ recordListBuilder .add (createRecord (name , value , type , annotations ));
117
+ }
118
+ // CHECKSTYLE.OFF: IllegalCatch - We want to turn any exceptions we catch into a ParsingException
119
+ } catch (final RuntimeException e ) {
120
+ // CHECKSTYLE.ON: IllegalCatch
121
+ throw new ParsingException ("Error pasring record" , datagram .array (), e );
113
122
}
114
123
115
124
return recordListBuilder .build ();
@@ -153,17 +162,20 @@ private Number parseValue(
153
162
154
163
@ SuppressFBWarnings ("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE" )
155
164
// See: https://github.com/findbugsproject/findbugs/issues/79
156
- private ImmutableMap <String , String > parseTags (@ Nullable final String taqsAsString ) {
157
- final ImmutableMap .Builder <String , String > annotations = ImmutableMap .builder ();
158
- if (null != taqsAsString ) {
159
- for (final String keyValue : taqsAsString .split ("," )) {
160
- final int pivot = keyValue .indexOf (':' );
161
- annotations .put (
162
- keyValue .substring (0 , pivot ),
163
- keyValue .substring (pivot + 1 ));
164
- }
165
+ private ImmutableMap <String , String > parseTags (@ Nullable final String tagsAsString ) {
166
+ if (null != tagsAsString ) {
167
+ return ImmutableMap .copyOf (Splitter .on (',' ).withKeyValueSeparator (':' ).split (tagsAsString ));
168
+ }
169
+ return ImmutableMap .of ();
170
+ }
171
+
172
+ @ SuppressFBWarnings ("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE" )
173
+ // See: https://github.com/findbugsproject/findbugs/issues/79
174
+ private ImmutableMap <String , String > parseInfluxStyleTags (@ Nullable final String tagsAsString ) {
175
+ if (null != tagsAsString ) {
176
+ return ImmutableMap .copyOf (Splitter .on (',' ).withKeyValueSeparator ('=' ).split (tagsAsString ));
165
177
}
166
- return annotations . build ();
178
+ return ImmutableMap . of ();
167
179
}
168
180
169
181
private Optional <Double > parseSampleRate (
@@ -237,7 +249,7 @@ public StatsdToRecordParser() {
237
249
private static final Splitter LINE_SPLITTER = Splitter .on ('\n' ).omitEmptyStrings ();
238
250
private static final ThreadLocal <NumberFormat > NUMBER_FORMAT = ThreadLocal .withInitial (NumberFormat ::getInstance );
239
251
private static final Pattern STATSD_PATTERN = Pattern .compile (
240
- "^(?<NAME>[^:@|]+):(?<VALUE>[^|]+)\\ |(?<TYPE>[^|]+)(\\ |@(?<SAMPLERATE>[^|]+))?(\\ |#(?<TAGS>.+))?$" );
252
+ "^(?<NAME>[^:@|, ]+)(,(?<INFLUXTAGS>[^:@|]+))? :(?<VALUE>[^|]+)\\ |(?<TYPE>[^|]+)(\\ |@(?<SAMPLERATE>[^|]+))?(\\ |#(?<TAGS>.+))?$" );
241
253
242
254
private enum StatsdType {
243
255
COUNTER ("c" , MetricType .COUNTER , null ),
0 commit comments