Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double quote escaping in field values; introduce new line escaping in measurement names and tags #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class InfluxDbWriteObjectSerializer {
private static final Pattern SPACE = Pattern.compile(" ");
private static final Pattern EQUAL = Pattern.compile("=");
private static final Pattern DOUBLE_QUOTE = Pattern.compile("\"");
private static final Pattern NEW_LINE = Pattern.compile("\n");
private final String measurementPrefix;

public InfluxDbWriteObjectSerializer(String measurementPrefix) {
Expand Down Expand Up @@ -100,15 +101,17 @@ private void formattedTime(Long time, TimeUnit precision, StringBuilder stringBu
private String escapeKey(String key) {
String toBeEscaped = SPACE.matcher(key).replaceAll("\\\\ ");
toBeEscaped = COMMA.matcher(toBeEscaped).replaceAll("\\\\,");
return EQUAL.matcher(toBeEscaped).replaceAll("\\\\=");
toBeEscaped = EQUAL.matcher(toBeEscaped).replaceAll("\\\\=");
return NEW_LINE.matcher(toBeEscaped).replaceAll("\\\\n");
}

private String escapeMeasurement(String key) {
String toBeEscaped = SPACE.matcher(key).replaceAll("\\\\ ");
return COMMA.matcher(toBeEscaped).replaceAll("\\\\,");
toBeEscaped = COMMA.matcher(toBeEscaped).replaceAll("\\\\,");
return NEW_LINE.matcher(toBeEscaped).replaceAll("\\\\n");
}

private String escapeField(String field) {
return DOUBLE_QUOTE.matcher(field).replaceAll("\\\"");
return DOUBLE_QUOTE.matcher(field).replaceAll("\\\\\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ public void shouldEscapeMeasurement() {
.isEqualTo("my\\ measurement\\,1=1 field1\\ Key=\"field1Value\" 456000\n");
}

@Test
public void shouldEscapeFieldValue() {
Map<String, String> tags = new HashMap<String, String>();
Map<String, Object> fields = new HashMap<String, Object>();
fields.put("field1 Key", "field \"the field\" value");
InfluxDbWriteObject influxDbWriteObject = new InfluxDbWriteObject("test-db", TimeUnit.MICROSECONDS);
influxDbWriteObject.getPoints().add(new InfluxDbPoint("metric", tags, 456l, fields));

InfluxDbWriteObjectSerializer influxDbWriteObjectSerializer = new InfluxDbWriteObjectSerializer("");
String lineString = influxDbWriteObjectSerializer.getLineProtocolString(influxDbWriteObject);

assertThat(lineString)
.isEqualTo("metric field1\\ Key=\"field \\\"the field\\\" value\" 456000\n");
}

@Test
public void shouldEscapeNewLine() {
Map<String, String> tags = new HashMap<String, String>();
tags.put("multiline\ntagKey", "multiline\ntagValue");
Map<String, Object> fields = new HashMap<String, Object>();
fields.put("multiline\nfieldKey", "multiline\nfieldValue");
InfluxDbWriteObject influxDbWriteObject = new InfluxDbWriteObject("test-db", TimeUnit.MICROSECONDS);
influxDbWriteObject.getPoints().add(new InfluxDbPoint("multiline\nmeasurement", tags, 456l, fields));

InfluxDbWriteObjectSerializer influxDbWriteObjectSerializer = new InfluxDbWriteObjectSerializer("");
String lineString = influxDbWriteObjectSerializer.getLineProtocolString(influxDbWriteObject);

assertThat(lineString)
.isEqualTo("multiline\\nmeasurement,multiline\\ntagKey=multiline\\ntagValue multiline\\nfieldKey=\"multiline\nfieldValue\" 456000\n");
}

@Test
public void shouldAddPrefixToMeasurementName() {
Map<String, String> tags = new HashMap<String, String>();
Expand Down