Skip to content

Commit

Permalink
[pinpoint-apm#9573] Apply Regex precompile to TagParser
Browse files Browse the repository at this point in the history
  • Loading branch information
donghun-cho committed Jan 4, 2023
1 parent 0f27aa2 commit aef0eb7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public class SystemMetricController {
private final TenantProvider tenantProvider;

private final TimeWindowSampler DEFAULT_TIME_WINDOW_SAMPLER = new TimeWindowSlotCentricSampler(10000L, 200);

private final TagParser tagParser = new TagParser();


public SystemMetricController(SystemMetricDataService systemMetricDataService,
SystemMetricHostInfoService systemMetricHostInfoService,
YMLSystemMetricBasicGroupManager systemMetricBasicGroupManager,
Expand Down Expand Up @@ -99,7 +97,7 @@ public SystemMetricView getCollectedMetricData(@RequestParam("hostGroupName") St
String tenantId = tenantProvider.getTenantId();
TimeWindow timeWindow = new TimeWindow(Range.newRange(from, to), DEFAULT_TIME_WINDOW_SAMPLER);
MetricDataSearchKey metricDataSearchKey = new MetricDataSearchKey(tenantId, hostGroupName, hostName, systemMetricBasicGroupManager.findMetricName(metricDefinitionId), metricDefinitionId, timeWindow);
List<Tag> tagList = tagParser.parseTags(tags);
List<Tag> tagList = TagParser.parseTags(tags);

SystemMetricData<? extends Number> systemMetricData = systemMetricDataService.getCollectedMetricData(metricDataSearchKey, timeWindow, tagList);
return new SystemMetricView(systemMetricData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
import java.util.List;

public class TagListTypeHandler implements TypeHandler<List<Tag>> {
private final TagParser parser = new TagParser();

@Override
public void setParameter(PreparedStatement ps, int i, List<Tag> parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, parameter.toString());
}

@Override
public List<Tag> getResult(ResultSet rs, String columnName) throws SQLException {
return parser.parseTags(rs.getString(columnName));
return TagParser.parseTags(rs.getString(columnName));
}

@Override
public List<Tag> getResult(ResultSet rs, int columnIndex) throws SQLException {
return parser.parseTags(rs.getString(columnIndex));
return TagParser.parseTags(rs.getString(columnIndex));
}

@Override
public List<Tag> getResult(CallableStatement cs, int columnIndex) throws SQLException {
return parser.parseTags(cs.getString(columnIndex));
return TagParser.parseTags(cs.getString(columnIndex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.sql.SQLException;

public class TagTypeHandler implements TypeHandler<Tag> {
private final TagParser parser = new TagParser();

@Override
public void setParameter(PreparedStatement ps, int i, Tag parameter, JdbcType jdbcType) throws SQLException {
Expand All @@ -20,16 +19,16 @@ public void setParameter(PreparedStatement ps, int i, Tag parameter, JdbcType jd

@Override
public Tag getResult(ResultSet rs, String columnName) throws SQLException {
return parser.parseTag(rs.getString(columnName));
return TagParser.parseTag(rs.getString(columnName));
}

@Override
public Tag getResult(ResultSet rs, int columnIndex) throws SQLException {
return parser.parseTag(rs.getString(columnIndex));
return TagParser.parseTag(rs.getString(columnIndex));
}

@Override
public Tag getResult(CallableStatement cs, int columnIndex) throws SQLException {
return parser.parseTag(cs.getString(columnIndex));
return TagParser.parseTag(cs.getString(columnIndex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
* @author Hyunjoon Cho
*/
public class TagParser {

private static final Pattern MULTI_VALUE_FIELD_PATTERN = Pattern.compile("[\\[\\]\"]");
private static final Pattern JSON_TAG_STRING_PATTERN = Pattern.compile("[{}\"]");

public TagParser() {
}

public List<Tag> parseTags(List<String> tags) {
public static List<Tag> parseTags(List<String> tags) {
if (tags == null || tags.size() == 0) {
return new ArrayList<>();
}
Expand All @@ -43,7 +48,7 @@ public List<Tag> parseTags(List<String> tags) {
return tagList;
}

public List<Tag> parseTags(String tagStrings) {
public static List<Tag> parseTags(String tagStrings) {
if (tagStrings == null || tagStrings.contains("null")) {
return new ArrayList<>();
}
Expand All @@ -59,18 +64,21 @@ public List<Tag> parseTags(String tagStrings) {
return tagList;
}

public Tag parseTag(String tagString) {
public static Tag parseTag(String tagString) {
Objects.requireNonNull(tagString, "tagString");

String[] tag = tagString.split(":");
return new Tag(tag[0], tag[1]);
}

private static String[] parseMultiValueFieldList(String string) {
return string.replaceAll("[\\[\\]\"]", "").split(",");
return MULTI_VALUE_FIELD_PATTERN.matcher(string).replaceAll("").split(",");
}

public static String toTagStrings(String jsonTagStrings) {
return jsonTagStrings.replaceAll("[{}\"]", "");
if (jsonTagStrings.equals("{}")) {
return "";
}
return JSON_TAG_STRING_PATTERN.matcher(jsonTagStrings).replaceAll("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.navercorp.pinpoint.metric.web.utill.metric;

import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.web.util.TagParser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class TagParserTest {

@Test
public void parseTagTest() {
List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("A", "1"));
tagList.add(new Tag("B", "2"));
tagList.add(new Tag("C", "3"));

List<Tag> result = new ArrayList<>();
result.add(TagParser.parseTag("A:1"));
result.add(TagParser.parseTag("B:2"));
result.add(TagParser.parseTag("C:3"));

Assertions.assertEquals(tagList, result);
}

@Test
public void parseTagsListTest() {
List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("A", "1"));
tagList.add(new Tag("B", "2"));
tagList.add(new Tag("C", "3"));

List<String> tags = new ArrayList<>();
tags.add("A:1");
tags.add("B:2");
tags.add("C:3");

List<Tag> result = TagParser.parseTags(tags);

Assertions.assertEquals(tagList, result);
}


@Test
public void parseTagsStringTest() {
List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("device", "1"));
tagList.add(new Tag("fstype", "2"));
tagList.add(new Tag("mode", "3"));
tagList.add(new Tag("path", "4"));

String multiValueFieldTagString = "[\"device:1\",\"fstype:2\",\"mode:3\",\"path:4\"]";
List<Tag> result = TagParser.parseTags(multiValueFieldTagString);

Assertions.assertEquals(tagList, result);
}

@Test
public void parseTagsStringEmptyTest() {
String multiValueFieldTagString = "[\"null\"]";
List<Tag> result = TagParser.parseTags(multiValueFieldTagString);

Assertions.assertEquals(0, result.size());
}


@Test
public void toTagsStringTest() {
String expect = "device:1,fstype:2,mode:3,path:4";

String jsonTagString = "{\"device\":\"1\",\"fstype\":\"2\",\"mode\":\"3\",\"path\":\"4\"}";
String result = TagParser.toTagStrings(jsonTagString);

Assertions.assertEquals(expect, result);
}

@Test
public void toTagsStringEmptyTest() {
String jsonTagStrings = "{}";
String result = TagParser.toTagStrings(jsonTagStrings);

Assertions.assertEquals("", result);
}
}

0 comments on commit aef0eb7

Please sign in to comment.