Skip to content

Commit

Permalink
[#10704] Create a minute unit statistical data table for agent
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoo-jung committed Apr 18, 2024
1 parent 14ad90c commit a3ef2f9
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.mybatis.typehandler.TagListSerializer;

import java.util.Collections;
import java.util.List;
Expand All @@ -29,6 +30,8 @@
*/
public class AgentStat {

private static final List<Tag> EMPTY_TAGS = Collections.emptyList();
private static final String EMPTY_JSON_TAGS = TagListSerializer.serialize(EMPTY_TAGS);;
private final String tenantId;
private final String sortKey;
private final String applicationName;
Expand All @@ -38,12 +41,13 @@ public class AgentStat {
private final double fieldValue;
private final List<Tag> tags;
private final long eventTime;
private final String jsonTags;

public AgentStat(String tenantId, String sortKey, String applicationName, String agentId, String metricName, String fieldName, double fieldValue, long eventTime) {
this(tenantId, sortKey, applicationName, agentId, metricName, fieldName, fieldValue, eventTime, Collections.emptyList());
this(tenantId, sortKey, applicationName, agentId, metricName, fieldName, fieldValue, eventTime, EMPTY_TAGS, EMPTY_JSON_TAGS);
}

public AgentStat(String tenantId, String sortKey, String applicationName, String agentId, String metricName, String fieldName, double fieldValue, long eventTime, List<Tag> tags) {
public AgentStat(String tenantId, String sortKey, String applicationName, String agentId, String metricName, String fieldName, double fieldValue, long eventTime, List<Tag> tags, String jsonTags) {
this.tenantId = tenantId;
this.sortKey = sortKey;
this.applicationName = applicationName;
Expand All @@ -53,6 +57,7 @@ public AgentStat(String tenantId, String sortKey, String applicationName, String
this.fieldValue = fieldValue;
this.eventTime = eventTime;
this.tags = tags;
this.jsonTags = jsonTags;
}

public String getTenantId() {
Expand Down Expand Up @@ -92,5 +97,9 @@ public String getSortKey() {
return sortKey;
}

public String getJsonTags() {
return jsonTags;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.navercorp.pinpoint.common.server.bo.stat.TotalThreadCountBo;
import com.navercorp.pinpoint.common.server.bo.stat.TransactionBo;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.mybatis.typehandler.TagListSerializer;
import org.apache.commons.math3.util.Precision;

import java.util.List;
Expand All @@ -50,7 +51,6 @@ public class AgentStatModelConverter<T extends AgentStatDataPoint> {
public static final String DATASOUCE_TAG_DATABASE_NAME_KEY = "databaseName";
public static final String DATASOUCE_TAG_JDBC_URL_KEY = "jdbcUrl";


public static List<AgentStat> convertCpuLoadToAgentStat(List<CpuLoadBo> cpuLoadBoList, String tenantId) {
List<AgentStat> agentStatList = cpuLoadBoList.stream()
.flatMap(cpuLoadBo -> {
Expand Down Expand Up @@ -344,13 +344,15 @@ public static List<AgentStat> convertDataSourceToAgentStat(List<DataSourceListBo
new Tag(DATASOUCE_TAG_JDBC_URL_KEY, dataSourceBo.getJdbcUrl())
);

String jsonTags = TagListSerializer.serialize(tags);

AgentStat activeConnectionSize = new AgentStat(tenantId, sortKey, dataSourceListBo.getApplicationName(), dataSourceListBo.getAgentId(),
AgentStatType.DATASOURCE.getChartType(), AgentStatField.DATASOURCE_ACTIVE_CONNECTION_SIZE.getFieldName(),
dataSourceBo.getActiveConnectionSize(), dataSourceListBo.getTimestamp(), tags);
dataSourceBo.getActiveConnectionSize(), dataSourceListBo.getTimestamp(), tags, jsonTags);

AgentStat maxConnectionSize = new AgentStat(tenantId, sortKey, dataSourceListBo.getApplicationName(), dataSourceListBo.getAgentId(),
AgentStatType.DATASOURCE.getChartType(), AgentStatField.DATASOURCE_MAX_CONNECTION_SIZE.getFieldName(),
dataSourceBo.getMaxConnectionSize(), dataSourceListBo.getTimestamp(), tags);
dataSourceBo.getMaxConnectionSize(), dataSourceListBo.getTimestamp(), tags, jsonTags);

builder.add(activeConnectionSize);
builder.add(maxConnectionSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.metric.common.model;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.navercorp.pinpoint.metric.common.model.Tag;

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

public class Tags {
private final List<Tag> tags = new ArrayList<>();

@JsonAnySetter
public void add(String name, String value) {
tags.add(new Tag(name, value));
}

public List<Tag> getTags() {
return tags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2024 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.metric.common.mybatis.typehandler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
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;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

/**
* @author minwoo-jung
*/
public class TagListSerializer {

private final static Logger logger = LogManager.getLogger(TagListSerializer.class.getName());

private final static ObjectMapper OBJECT_MAPPER = getMapper();

static ObjectMapper getMapper() {
return Jackson.newBuilder()
.serializerByType(List.class, new TagMySqlSerializer())
.build();
}

public static String serialize(List<Tag> tagList) {
try {
return OBJECT_MAPPER.writeValueAsString(tagList);
} catch (JsonProcessingException e) {
logger.error("Error serializing List<Tag> : {}", tagList, e);
throw new JsonRuntimeException("Error serializing tagList", e);
}
}

public static List<Tag> deserialize(String tagListJson) {
try {
Tags tags = OBJECT_MAPPER.readValue(tagListJson, Tags.class);
return tags.getTags();
} catch (JsonProcessingException e) {
logger.error("Error deserializing tagList json : {}", tagListJson, e);
throw new JsonRuntimeException("Error deserializing tagListJson", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2021 NAVER Corp.
* Copyright 2024 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -14,14 +14,14 @@
* limitations under the License.
*/

package com.navercorp.pinpoint.metric.common.model.mybatis;
package com.navercorp.pinpoint.metric.common.mybatis.typehandler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
import com.navercorp.pinpoint.common.server.util.json.JsonRuntimeException;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.model.json.Tags;
import com.navercorp.pinpoint.metric.common.model.Tags;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.TypeHandler;
Expand All @@ -38,17 +38,9 @@
* @author minwoo.jung
*/
@MappedJdbcTypes({JdbcType.VARCHAR})
public class TagListTypeHandler implements TypeHandler<List<Tag>> {
public class TagListTypeHandler extends TagListSerializer implements TypeHandler<List<Tag>> {

private final Logger logger = LogManager.getLogger(this.getClass());
private final static ObjectMapper OBJECT_MAPPER = getMapper();


static ObjectMapper getMapper() {
return Jackson.newBuilder()
.serializerByType(List.class, new TagMySqlSerializer())
.build();
}

@Override
public void setParameter(PreparedStatement ps, int i, List<Tag> parameter, JdbcType jdbcType) throws SQLException {
Expand All @@ -74,22 +66,4 @@ public List<Tag> getResult(CallableStatement cs, int columnIndex) throws SQLExce
return deserialize(tagListJson);
}

public String serialize(List<Tag> tagList) {
try {
return OBJECT_MAPPER.writeValueAsString(tagList);
} catch (JsonProcessingException e) {
logger.error("Error serializing List<Tag> : {}", tagList, e);
throw new JsonRuntimeException("Error serializing tagList", e);
}
}

public List<Tag> deserialize(String tagListJson) {
try {
Tags tags = OBJECT_MAPPER.readValue(tagListJson, Tags.class);
return tags.getTags();
} catch (JsonProcessingException e) {
logger.error("Error deserializing tagList json : {}", tagListJson, e);
throw new JsonRuntimeException("Error deserializing tagListJson", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.metric.common.mybatis.typehandler;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.navercorp.pinpoint.metric.common.model.Tag;

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

public class TagMySqlSerializer extends JsonSerializer<List<Tag>> {

@Override
public void serialize(List<Tag> tags, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
for (Tag tag : tags) {
gen.writeStringField(tag.getName(), tag.getValue());
}
gen.writeEndObject();
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
package com.navercorp.pinpoint.metric.common.model.mybatis;
/*
* Copyright 2024 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.metric.common.mybatis.typehandler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.model.json.Tags;
import com.navercorp.pinpoint.metric.common.model.Tags;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Assertions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.navercorp.pinpoint.metric.common.model.MetricTag;
import com.navercorp.pinpoint.metric.common.model.MetricTagCollection;
import com.navercorp.pinpoint.metric.common.model.MetricTagKey;
import com.navercorp.pinpoint.metric.common.model.mybatis.TagListTypeHandler;
import com.navercorp.pinpoint.metric.common.mybatis.typehandler.TagListTypeHandler;
import com.navercorp.pinpoint.pinot.kafka.util.KafkaCallbacks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.model.json.Tags;
import com.navercorp.pinpoint.metric.common.model.Tags;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void saveMetricTag(String tenantId, String hostGroupName, SystemMetric sy
final List<Tag> tagList = systemMetric.getTags();
final long saveTime = getSaveTime();

MetricTagCollection metricTagCollection = metricTagCache.getMetricTag(new MetricTagKey(tenantId, hostGroupName, hostName, metricName, fieldName, saveTime));
MetricTagCollection metricTagCollection = metricTagCache.getMetricTag(new MetricTagKey(tenantId, hostGroupName, hostName, metricName, fieldName, saveTime));//

if (metricTagCollection == null) {
MetricTagKey metricTagKey = new MetricTagKey(tenantId, hostGroupName, hostName, metricName, fieldName, saveTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.navercorp.pinpoint.metric.common.model.MetricTagKey;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.model.mybatis.MetricDataTypeHandler;
import com.navercorp.pinpoint.metric.common.mybatis.typehandler.TagListTypeHandler;
import com.navercorp.pinpoint.mybatis.MyBatisRegistryHandler;
import org.apache.ibatis.type.TypeAliasRegistry;
import org.apache.ibatis.type.TypeHandlerRegistry;
Expand All @@ -34,14 +35,14 @@ public void registerTypeAlias(TypeAliasRegistry typeAliasRegistry) {
typeAliasRegistry.registerAlias(MetricTag.class);
typeAliasRegistry.registerAlias(MetricTagKey.class);
typeAliasRegistry.registerAlias(MetricDataTypeHandler.class);
typeAliasRegistry.registerAlias(com.navercorp.pinpoint.metric.common.model.mybatis.TagListTypeHandler.class);
typeAliasRegistry.registerAlias(TagListTypeHandler.class);
}


@Override
public void registerTypeHandler(TypeHandlerRegistry typeHandlerRegistry) {
typeHandlerRegistry.register(MetricDataType.class, MetricDataTypeHandler.class);
typeHandlerRegistry.register(List.class, com.navercorp.pinpoint.metric.common.model.mybatis.TagListTypeHandler.class);
typeHandlerRegistry.register(List.class, TagListTypeHandler.class);
}

}

This file was deleted.

Loading

0 comments on commit a3ef2f9

Please sign in to comment.