Skip to content
Merged
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 @@ -19,8 +19,8 @@

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.navercorp.pinpoint.common.server.util.json.JsonRuntimeException;
import com.navercorp.pinpoint.common.util.CollectionUtils;
import com.navercorp.pinpoint.common.util.StringUtils;
Expand All @@ -37,13 +37,19 @@

@Component
public class MapStructUtils {

private final ObjectMapper mapper;

private final JavaType listLongJavaType;
private final ObjectReader integerListReader;
private final ObjectReader longListReader;
private final ObjectReader stringListReader;

public MapStructUtils(ObjectMapper mapper) {
this.mapper = Objects.requireNonNull(mapper, "mapper");
this.listLongJavaType = mapper.getTypeFactory().constructCollectionType(List.class, Long.class);

this.integerListReader = mapper.readerForListOf(Integer.class);
this.longListReader = mapper.readerForListOf(Long.class);
this.stringListReader = mapper.readerForListOf(String.class);
}


Expand All @@ -63,23 +69,46 @@


@JsonStrToList
public <T> List<T> jsonStrToList(String s) {
if (StringUtils.isEmpty(s)) {
@Deprecated
public <T> List<T> jsonStrToList(String json) {
if (StringUtils.isEmpty(json)) {
return Collections.emptyList();

Check warning on line 75 in commons-server/src/main/java/com/navercorp/pinpoint/common/server/mapper/MapStructUtils.java

View check run for this annotation

Codecov / codecov/patch

commons-server/src/main/java/com/navercorp/pinpoint/common/server/mapper/MapStructUtils.java#L75

Added line #L75 was not covered by tests
}
try {
return mapper.readValue(json, new TypeReference<>() {});
} catch (JacksonException e) {
throw new JsonRuntimeException("Json read error", e);

Check warning on line 80 in commons-server/src/main/java/com/navercorp/pinpoint/common/server/mapper/MapStructUtils.java

View check run for this annotation

Codecov / codecov/patch

commons-server/src/main/java/com/navercorp/pinpoint/common/server/mapper/MapStructUtils.java#L79-L80

Added lines #L79 - L80 were not covered by tests
}
}

public List<String> jsonToStringList(String json) {
if (StringUtils.isEmpty(json)) {
return Collections.emptyList();

Check warning on line 86 in commons-server/src/main/java/com/navercorp/pinpoint/common/server/mapper/MapStructUtils.java

View check run for this annotation

Codecov / codecov/patch

commons-server/src/main/java/com/navercorp/pinpoint/common/server/mapper/MapStructUtils.java#L86

Added line #L86 was not covered by tests
}
try {
return stringListReader.readValue(json);
} catch (JacksonException e) {
throw new JsonRuntimeException("Json read error", e);

Check warning on line 91 in commons-server/src/main/java/com/navercorp/pinpoint/common/server/mapper/MapStructUtils.java

View check run for this annotation

Codecov / codecov/patch

commons-server/src/main/java/com/navercorp/pinpoint/common/server/mapper/MapStructUtils.java#L90-L91

Added lines #L90 - L91 were not covered by tests
}
}

public List<Integer> jsonToIntegerList(String json) {
if (StringUtils.isEmpty(json)) {
return Collections.emptyList();
}
try {
return mapper.readValue(s, new TypeReference<>() {});
return integerListReader.readValue(json);
} catch (JacksonException e) {
throw new JsonRuntimeException("Json read error", e);
}
}

public List<Long> jsonStrToLongList(String s) {
if (StringUtils.isEmpty(s)) {
public List<Long> jsonToLongList(String json) {
if (StringUtils.isEmpty(json)) {
return Collections.emptyList();
}
try {
return mapper.readValue(s, listLongJavaType);
return longListReader.readValue(json);
} catch (JacksonException e) {
throw new JsonRuntimeException("Json read error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,44 @@
class MapStructUtilsTest {

@Test
void jsonStrToLongList() {
void jsonStrToList() {
ObjectMapper mapper = Jackson.newMapper();
MapStructUtils mapStructUtils = new MapStructUtils(mapper);

List<Long> longs = mapStructUtils.jsonStrToLongList("[1,2,3]");
// List<Long> longs = mapStructUtils.jsonStrToList("[1,2,3]");
// assertThat(longs).containsExactly(1L, 2L, 3L);

List<Integer> integers = mapStructUtils.jsonStrToList("[1,2,3]");
assertThat(integers).containsExactly(1, 2, 3);

List<Integer> objects = mapStructUtils.jsonStrToList("[1,\"abc\",3]");
// assertThat(objects).contains(1, "abc", 3);
}

@Test
void jsonToLongList() {
ObjectMapper mapper = Jackson.newMapper();
MapStructUtils mapStructUtils = new MapStructUtils(mapper);

List<Long> longs = mapStructUtils.jsonToLongList("[1,2,3]");
assertThat(longs).containsExactly(1L, 2L, 3L);
}

@Test
void jsonStrToIntegerList() {
ObjectMapper mapper = Jackson.newMapper();
MapStructUtils mapStructUtils = new MapStructUtils(mapper);

List<Integer> integers = mapStructUtils.jsonToIntegerList("[1,2,3]");
assertThat(integers).containsExactly(1, 2, 3);
}

@Test
void jsonStrToStringList() {
ObjectMapper mapper = Jackson.newMapper();
MapStructUtils mapStructUtils = new MapStructUtils(mapper);

List<String> strings = mapStructUtils.jsonToStringList("[1,2,3]");
assertThat(strings).containsExactly("1", "2", "3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public StackTraceMapper(MapStructUtils mapStructUtils) {

@StringsToStackTrace
public List<StackTraceElementWrapper> stackTrace(ExceptionMetaDataEntity entity) {
List<String> classNameIterable = mapStructUtils.jsonStrToList(entity.getStackTraceClassName());
List<String> fileNameIterable = mapStructUtils.jsonStrToList(entity.getStackTraceFileName());
List<Integer> lineNumberIterable = mapStructUtils.jsonStrToList(entity.getStackTraceLineNumber());
List<String> methodNameIterable = mapStructUtils.jsonStrToList(entity.getStackTraceMethodName());
List<String> classNameIterable = mapStructUtils.jsonToStringList(entity.getStackTraceClassName());
List<String> fileNameIterable = mapStructUtils.jsonToStringList(entity.getStackTraceFileName());
List<Integer> lineNumberIterable = mapStructUtils.jsonToIntegerList(entity.getStackTraceLineNumber());
List<String> methodNameIterable = mapStructUtils.jsonToStringList(entity.getStackTraceMethodName());

List<StackTraceElementWrapper> wrappers = new ArrayList<>();
for (int i = 0; i < classNameIterable.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import com.navercorp.pinpoint.exceptiontrace.web.entity.ExceptionGroupSummaryEntity;
import com.navercorp.pinpoint.exceptiontrace.web.entity.ExceptionMetaDataEntity;
import com.navercorp.pinpoint.exceptiontrace.web.entity.GroupedFieldNameEntity;
import com.navercorp.pinpoint.exceptiontrace.web.view.ExceptionChartValueView;
import com.navercorp.pinpoint.exceptiontrace.web.model.ExceptionGroupSummary;
import com.navercorp.pinpoint.exceptiontrace.web.util.GroupByAttributes;
import com.navercorp.pinpoint.exceptiontrace.web.view.ExceptionChartValueView;
import com.navercorp.pinpoint.exceptiontrace.web.view.ExceptionDetailView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -82,10 +82,10 @@ public void testEntityToModel() {
String lineNumbers = expected.getStackTraceLineNumber();
String methodNames = expected.getStackTraceMethodName();

List<String> classNameIter = convertToList(classNames);
List<String> fileNameIter = convertToList(fileNames);
List<Integer> lineNumberIter = convertToList(lineNumbers);
List<String> methodNameIter = convertToList(methodNames);
List<String> classNameIter = mapStructUtils.jsonToStringList(classNames);
List<String> fileNameIter = mapStructUtils.jsonToStringList(fileNames);
List<Integer> lineNumberIter = mapStructUtils.jsonToIntegerList(lineNumbers);
List<String> methodNameIter = mapStructUtils.jsonToStringList(methodNames);

List<StackTraceElementWrapper> actualStackTrace = actual.getStackTrace();

Expand Down Expand Up @@ -128,10 +128,10 @@ public void testEntityToView() {
String lineNumbers = expected.getStackTraceLineNumber();
String methodNames = expected.getStackTraceMethodName();

List<String> classNameIter = convertToList(classNames);
List<String> fileNameIter = convertToList(fileNames);
List<Integer> lineNumberIter = convertToList(lineNumbers);
List<String> methodNameIter = convertToList(methodNames);
List<String> classNameIter = mapStructUtils.jsonToStringList(classNames);
List<String> fileNameIter = mapStructUtils.jsonToStringList(fileNames);
List<Integer> lineNumberIter = mapStructUtils.jsonToIntegerList(lineNumbers);
List<String> methodNameIter = mapStructUtils.jsonToStringList(methodNames);

List<StackTraceElementWrapper> actualStackTrace = actual.getStackTrace();

Expand Down Expand Up @@ -173,10 +173,6 @@ private <T> String toFlattenedString(List<StackTraceElement> elements, Function<
return mapStructUtils.listToJsonStr(collect);
}

public <T> List<T> convertToList(String json) {
return mapStructUtils.jsonStrToList(json);
}

@Test
public void testEntityToValueView() {
ExceptionChartValueViewEntity expected = newExceptionMetaDataEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.navercorp.pinpoint.common.server.util.json.JsonRuntimeException;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.model.Tags;
Expand All @@ -35,18 +36,19 @@ public class TagListSerializer {

private final Logger logger = LogManager.getLogger(this.getClass());

private final ObjectMapper mapper;
private final ObjectWriter writer;
private final ObjectReader reader;

public TagListSerializer(ObjectMapper mapper) {
this.mapper = Objects.requireNonNull(mapper, "mapper");
reader = mapper.readerFor(Tags.class);
Objects.requireNonNull(mapper, "mapper");
this.writer = mapper.writerFor(Tags.class);
this.reader = mapper.readerFor(Tags.class);
}

public String serialize(List<Tag> tagList) {
try {
Tags tags = new Tags(tagList);
return mapper.writeValueAsString(tags);
return writer.writeValueAsString(tags);
} catch (JsonProcessingException e) {
logger.error("Error serializing List<Tag> : {}", tagList, e);
throw new JsonRuntimeException("Error serializing tagList", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.navercorp.pinpoint.metric.collector.model.TelegrafMetric;
import com.navercorp.pinpoint.metric.collector.model.TelegrafMetrics;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.model.TagComparator;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -38,7 +35,8 @@
@Component
public class TelegrafJsonDeserializer extends JsonDeserializer<TelegrafMetrics> {

private final static Comparator<Tag> TAG_COMPARATOR = TagComparator.INSTANCE;
private static final TypeReference<List<TelegrafMetric>> REF_BATCH = new TypeReference<>() {
};

public TelegrafJsonDeserializer() {
}
Expand All @@ -54,9 +52,7 @@ public TelegrafMetrics deserialize(JsonParser jp, DeserializationContext ctxt) t
if (jp.nextToken() != JsonToken.START_ARRAY) {
ctxt.handleUnexpectedToken(TelegrafMetrics.class, jp);
}
TypeReference<List<TelegrafMetric>> batch = new TypeReference<>() {
};
List<TelegrafMetric> metrics = jp.readValueAs(batch);
List<TelegrafMetric> metrics = jp.readValueAs(REF_BATCH);
return new TelegrafMetrics(metrics);
} else {
// standard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.navercorp.pinpoint.common.server.util.json.JsonRuntimeException;
import com.navercorp.pinpoint.otlp.common.web.defined.AppMetricDefinition;
import com.navercorp.pinpoint.otlp.web.dao.AppMetricDefinitionDao;
Expand Down Expand Up @@ -65,18 +67,18 @@
}

static class Mapper {
private final ObjectMapper mapper;
private final TypeReference<List<AppMetricDefinition>> REF_LIST_APP_METRIC_DEFINITION = new TypeReference<>() {};
private final ObjectWriter writer;
private final ObjectReader reader;

public Mapper(ObjectMapper mapper) {
this.mapper = Objects.requireNonNull(mapper, "mapper");
this.writer = mapper.writerFor(new TypeReference<List<AppMetricDefinition>>() {});
this.reader = mapper.readerForListOf(AppMetricDefinition.class);

Check warning on line 75 in otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java

View check run for this annotation

Codecov / codecov/patch

otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java#L74-L75

Added lines #L74 - L75 were not covered by tests
}

public AppMetricDefDto toDto(String applicationName, List<AppMetricDefinition> appMetricDefinitionList) {
Objects.requireNonNull(appMetricDefinitionList, "appMetricDefinitionList");

try {
String metricConfigJson = mapper.writeValueAsString(appMetricDefinitionList);
String metricConfigJson = writer.writeValueAsString(appMetricDefinitionList);

Check warning on line 81 in otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java

View check run for this annotation

Codecov / codecov/patch

otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java#L81

Added line #L81 was not covered by tests
return new AppMetricDefDto(applicationName, metricConfigJson, AppMetricDefinition.SCHEMA_VERSION);
} catch (JsonProcessingException e) {
throw new JsonRuntimeException("can not convert appMetricDefinitionList to json :" + appMetricDefinitionList, e);
Expand All @@ -89,8 +91,7 @@
}

try {
List<AppMetricDefinition> appMetricDefinitionList = mapper.readValue(appMetricDefDto.metricDefinition(), REF_LIST_APP_METRIC_DEFINITION);
return appMetricDefinitionList;
return reader.readValue(appMetricDefDto.metricDefinition());

Check warning on line 94 in otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java

View check run for this annotation

Codecov / codecov/patch

otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java#L94

Added line #L94 was not covered by tests
} catch (JsonProcessingException e) {
throw new JsonRuntimeException("can not convert appMetricDefDto to model :" + appMetricDefDto, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

@Override
public ClusterKey deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
TreeNode root = parser.getCodec().readTree(parser);
TreeNode root = parser.readValueAsTree();

Check warning on line 39 in realtime/realtime-common/src/main/java/com/navercorp/pinpoint/realtime/serde/ClusterKeyDeserializer.java

View check run for this annotation

Codecov / codecov/patch

realtime/realtime-common/src/main/java/com/navercorp/pinpoint/realtime/serde/ClusterKeyDeserializer.java#L39

Added line #L39 was not covered by tests
String applicationName = ((TextNode) root.get("applicationName")).asText();
String agentId = ((TextNode) root.get("agentId")).asText();
long startTimestamp = ((LongNode) root.get("startTimestamp")).asLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,24 @@

package com.navercorp.pinpoint.web.filter;

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.navercorp.pinpoint.common.server.bo.SpanBo;

import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.loader.service.AnnotationKeyRegistryService;
import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
*
* @author netspider
Expand All @@ -58,7 +54,7 @@
public DefaultFilterBuilder(ObjectMapper mapper, ServiceTypeRegistryService serviceTypeRegistryService, AnnotationKeyRegistryService annotationKeyRegistryService) {
Objects.requireNonNull(mapper, "mapper");
this.filterHintReader = mapper.readerFor(FilterHint.class);
this.filterDescriptorReader = mapper.readerFor(new TypeReference<List<FilterDescriptor>>() {});
this.filterDescriptorReader = mapper.readerForListOf(FilterDescriptor.class);

Check warning on line 57 in web/src/main/java/com/navercorp/pinpoint/web/filter/DefaultFilterBuilder.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/filter/DefaultFilterBuilder.java#L57

Added line #L57 was not covered by tests

this.serviceTypeRegistryService = Objects.requireNonNull(serviceTypeRegistryService, "serviceTypeRegistryService");
this.annotationKeyRegistryService = Objects.requireNonNull(annotationKeyRegistryService, "annotationKeyRegistryService");
Expand Down