From c995fd96f5ec6e71529282bfbb5e15beef4c4acf Mon Sep 17 00:00:00 2001 From: "minwoo.jung" Date: Wed, 24 Jul 2024 18:46:26 +0900 Subject: [PATCH] [#10776] Add /metricDef/userDefined api logic. --- otlpmetric/otlpmetric-common/pom.xml | 7 ++ .../common/defined/AppMetricDefinition.java | 91 +++++++++++++++++++ .../definition/{ => property}/Metric.java | 2 +- .../MetricDefinitionProperty.java | 2 +- .../{ => property}/MetricDescriptor.java | 2 +- .../{ => property}/MetricGroup.java | 2 +- .../common/definition/{ => property}/Tag.java | 2 +- .../otlp/web/OtlpMetricWebConfig.java | 7 +- .../mysql/OtlpMetricMysqlRegistryHandler.java | 39 ++++++++ .../OtlpMetricWebMysqlDaoConfiguration.java | 79 ++++++++++++++++ .../OtlpMetricPinotRegistryHandler.java} | 22 ++++- .../OtlpMetricWebPinotDaoConfiguration.java | 20 +++- .../MetricDefinitionController.java | 30 ++++-- .../otlp/web/dao/AppMetricDefinitionDao.java | 28 ++++++ .../otlp/web/dao/MetricDefinitionDao.java | 2 +- .../otlp/web/dao/model/AppMetricDefDto.java | 23 +++++ .../mysql/MysqlAppMetricDefinitionDao.java | 85 +++++++++++++++++ .../web/dao/pinot/PinotMetricMetaDataDao.java | 4 +- .../service/AppMetricDefinitionService.java | 26 ++++++ .../AppMetricDefinitionServiceImpl.java | 44 +++++++++ .../service/MetricDefinitionServiceImpl.java | 6 +- ...ervice.java => MetricMetadataService.java} | 4 +- .../otlp/sql/CreateTableStatement-mysql.sql | 6 ++ .../mysql/AppMetricDefinitionMapper.xml | 18 ++++ 24 files changed, 522 insertions(+), 29 deletions(-) create mode 100644 otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/defined/AppMetricDefinition.java rename otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/{ => property}/Metric.java (94%) rename otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/{ => property}/MetricDefinitionProperty.java (92%) rename otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/{ => property}/MetricDescriptor.java (92%) rename otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/{ => property}/MetricGroup.java (95%) rename otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/{ => property}/Tag.java (91%) create mode 100644 otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/mysql/OtlpMetricMysqlRegistryHandler.java create mode 100644 otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/mysql/OtlpMetricWebMysqlDaoConfiguration.java rename otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/{OtlpMetricWebRegistryHandler.java => pinot/OtlpMetricPinotRegistryHandler.java} (62%) rename otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/{ => pinot}/OtlpMetricWebPinotDaoConfiguration.java (78%) create mode 100644 otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/AppMetricDefinitionDao.java create mode 100644 otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/model/AppMetricDefDto.java create mode 100644 otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java create mode 100644 otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/AppMetricDefinitionService.java create mode 100644 otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/AppMetricDefinitionServiceImpl.java rename otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/{MetricDefinitionService.java => MetricMetadataService.java} (85%) create mode 100644 otlpmetric/otlpmetric-web/src/main/resources/otlp/sql/CreateTableStatement-mysql.sql create mode 100644 otlpmetric/otlpmetric-web/src/main/resources/otlp/web/mapper/mysql/AppMetricDefinitionMapper.xml diff --git a/otlpmetric/otlpmetric-common/pom.xml b/otlpmetric/otlpmetric-common/pom.xml index f40066c4e454..c5f1605ceb75 100644 --- a/otlpmetric/otlpmetric-common/pom.xml +++ b/otlpmetric/otlpmetric-common/pom.xml @@ -15,5 +15,12 @@ 17 ${env.JAVA_17_HOME} + + + com.navercorp.pinpoint + pinpoint-commons-server + provided + + \ No newline at end of file diff --git a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/defined/AppMetricDefinition.java b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/defined/AppMetricDefinition.java new file mode 100644 index 000000000000..722d74848874 --- /dev/null +++ b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/defined/AppMetricDefinition.java @@ -0,0 +1,91 @@ +/* + * 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.otlp.common.defined; + +import com.navercorp.pinpoint.common.server.util.StringPrecondition; + +/** + * @author minwoo-jung + */ +public class AppMetricDefinition { + + public static final int SCHEMA_VERSION = 1; + + private final String applicationName; + private final String id; + private final String title; + private final String metricGroupName; + private final String metricName; + private final String fieldName; + private final String tags; + private final String unit; + private final String chartType; + + + public AppMetricDefinition(String applicationName, String id, String title, String metricGroupName, String metricName, String fieldName, String tags, String unit, String chartType) { + this.applicationName = StringPrecondition.requireHasLength(applicationName, "applicationName"); + this.id = StringPrecondition.requireHasLength(id, "id"); + this.title = StringPrecondition.requireHasLength(title, "title"); + this.metricGroupName = StringPrecondition.requireHasLength(metricGroupName, "metricGroupName"); + this.metricName = StringPrecondition.requireHasLength(metricName, "metricName"); + this.fieldName = ""; + this.tags = StringPrecondition.requireHasLength(tags, "tags"); + this.unit = StringPrecondition.requireHasLength(unit, "unit"); + this.chartType = StringPrecondition.requireHasLength(chartType, "chartType"); + } + + public String getApplicationName() { + return applicationName; + } + + public String getId() { + return id; + } + + public String getTitle() { + return title; + } + + public String getMetricGroupName() { + return metricGroupName; + } + + public String getMetricName() { + return metricName; + } + + public String getFieldName() { + return fieldName; + } + + public String getTags() { + return tags; + } + + public String getUnit() { + return unit; + } + + public String getChartType() { + return chartType; + } + + public int getSchemaVersion() { + return SCHEMA_VERSION; + } + +} diff --git a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/Metric.java b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/Metric.java similarity index 94% rename from otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/Metric.java rename to otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/Metric.java index 0c35b96740da..2407e87c602d 100644 --- a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/Metric.java +++ b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/Metric.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.navercorp.pinpoint.otlp.common.definition; +package com.navercorp.pinpoint.otlp.common.definition.property; import java.util.HashMap; import java.util.List; diff --git a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricDefinitionProperty.java b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricDefinitionProperty.java similarity index 92% rename from otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricDefinitionProperty.java rename to otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricDefinitionProperty.java index f2e16f019034..56c86f032460 100644 --- a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricDefinitionProperty.java +++ b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricDefinitionProperty.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.navercorp.pinpoint.otlp.common.definition; +package com.navercorp.pinpoint.otlp.common.definition.property; import java.util.List; diff --git a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricDescriptor.java b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricDescriptor.java similarity index 92% rename from otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricDescriptor.java rename to otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricDescriptor.java index 23857b596549..3d09c305dc15 100644 --- a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricDescriptor.java +++ b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricDescriptor.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.navercorp.pinpoint.otlp.common.definition; +package com.navercorp.pinpoint.otlp.common.definition.property; /** * @author minwoo-jung diff --git a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricGroup.java b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricGroup.java similarity index 95% rename from otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricGroup.java rename to otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricGroup.java index d6d8e1ab0021..b368d4aaf3e0 100644 --- a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/MetricGroup.java +++ b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/MetricGroup.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.navercorp.pinpoint.otlp.common.definition; +package com.navercorp.pinpoint.otlp.common.definition.property; import java.util.HashMap; import java.util.List; diff --git a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/Tag.java b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/Tag.java similarity index 91% rename from otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/Tag.java rename to otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/Tag.java index f8c8231bc86a..9c118d7f95b0 100644 --- a/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/Tag.java +++ b/otlpmetric/otlpmetric-common/src/main/java/com/navercorp/pinpoint/otlp/common/definition/property/Tag.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.navercorp.pinpoint.otlp.common.definition; +package com.navercorp.pinpoint.otlp.common.definition.property; /** * @author minwoo-jung diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/OtlpMetricWebConfig.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/OtlpMetricWebConfig.java index f56f5af6de34..8f91414da376 100644 --- a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/OtlpMetricWebConfig.java +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/OtlpMetricWebConfig.java @@ -14,8 +14,8 @@ * limitations under the License. */ -import com.navercorp.pinpoint.datasource.MainDataSourcePropertySource; -import com.navercorp.pinpoint.otlp.web.config.OtlpMetricWebPinotDaoConfiguration; +import com.navercorp.pinpoint.otlp.web.config.mysql.OtlpMetricWebMysqlDaoConfiguration; +import com.navercorp.pinpoint.otlp.web.config.pinot.OtlpMetricWebPinotDaoConfiguration; import com.navercorp.pinpoint.pinot.config.PinotConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.ComponentScan; @@ -30,7 +30,8 @@ }) @Import({ PinotConfiguration.class, - OtlpMetricWebPinotDaoConfiguration.class + OtlpMetricWebPinotDaoConfiguration.class, + OtlpMetricWebMysqlDaoConfiguration.class }) @ConditionalOnProperty(name = "pinpoint.modules.web.otlpmetric.enabled", havingValue = "true") public class OtlpMetricWebConfig { diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/mysql/OtlpMetricMysqlRegistryHandler.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/mysql/OtlpMetricMysqlRegistryHandler.java new file mode 100644 index 000000000000..facd89bb2c96 --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/mysql/OtlpMetricMysqlRegistryHandler.java @@ -0,0 +1,39 @@ +/* + * 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.otlp.web.config.mysql; + +import com.navercorp.pinpoint.mybatis.MyBatisRegistryHandler; +import com.navercorp.pinpoint.otlp.common.defined.AppMetricDefinition; +import com.navercorp.pinpoint.otlp.web.dao.model.AppMetricDefDto; +import org.apache.ibatis.type.TypeAliasRegistry; +import org.apache.ibatis.type.TypeHandlerRegistry; + +/** + * @author minwoo-jung + */ +public class OtlpMetricMysqlRegistryHandler implements MyBatisRegistryHandler { + @Override + public void registerTypeAlias(TypeAliasRegistry typeAliasRegistry) { + typeAliasRegistry.registerAlias(AppMetricDefinition.class); + typeAliasRegistry.registerAlias(AppMetricDefDto.class); + } + + @Override + public void registerTypeHandler(TypeHandlerRegistry typeHandlerRegistry) { + + } +} diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/mysql/OtlpMetricWebMysqlDaoConfiguration.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/mysql/OtlpMetricWebMysqlDaoConfiguration.java new file mode 100644 index 000000000000..8cfe5383195e --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/mysql/OtlpMetricWebMysqlDaoConfiguration.java @@ -0,0 +1,79 @@ +/* + * 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.otlp.web.config.mysql; + +import com.navercorp.pinpoint.mybatis.MyBatisConfigurationCustomizer; +import com.navercorp.pinpoint.mybatis.MyBatisRegistryHandler; +import com.navercorp.pinpoint.mybatis.plugin.BindingLogPlugin; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.SqlSessionTemplate; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.core.io.Resource; + +import javax.sql.DataSource; + +/** + * @author minwoo-jung + */ +@org.springframework.context.annotation.Configuration +public class OtlpMetricWebMysqlDaoConfiguration { + + private final Logger logger = LogManager.getLogger(OtlpMetricWebMysqlDaoConfiguration.class); + + @Bean + public FactoryBean otlpMysqlSqlSessionFactory( + @Qualifier("myBatisConfigurationCustomizer") MyBatisConfigurationCustomizer customizer, + @Qualifier("dataSource") DataSource dataSource, + @Value("classpath*:otlp/web/mapper/mysql/*Mapper.xml") Resource[] mappers, + BindingLogPlugin bindingLogPlugin) { + + for (Resource mapper : mappers) { + logger.info("Mapper location: {}", mapper.getDescription()); + } + + SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); + sessionFactoryBean.setDataSource(dataSource); + sessionFactoryBean.setMapperLocations(mappers); + + Configuration config = new Configuration(); + + sessionFactoryBean.setConfiguration(config); + sessionFactoryBean.setFailFast(true); + sessionFactoryBean.setPlugins(bindingLogPlugin); + + MyBatisRegistryHandler myBatisRegistryHandler = new OtlpMetricMysqlRegistryHandler(); + myBatisRegistryHandler.registerTypeAlias(config.getTypeAliasRegistry()); + myBatisRegistryHandler.registerTypeHandler(config.getTypeHandlerRegistry()); + + return sessionFactoryBean; + } + + @Bean + public SqlSessionTemplate otlpMysqlSqlSessionTemplate( + @Qualifier("otlpMysqlSqlSessionFactory") SqlSessionFactory sessionFactory) { + return new SqlSessionTemplate(sessionFactory); + } + + +} diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/OtlpMetricWebRegistryHandler.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/pinot/OtlpMetricPinotRegistryHandler.java similarity index 62% rename from otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/OtlpMetricWebRegistryHandler.java rename to otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/pinot/OtlpMetricPinotRegistryHandler.java index 92d7d9b12bb5..c5df3517237c 100644 --- a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/OtlpMetricWebRegistryHandler.java +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/pinot/OtlpMetricPinotRegistryHandler.java @@ -1,7 +1,23 @@ -package com.navercorp.pinpoint.otlp.web.config; +/* + * 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.otlp.web.config.pinot; import com.navercorp.pinpoint.mybatis.MyBatisRegistryHandler; -import com.navercorp.pinpoint.otlp.common.definition.MetricDescriptor; +import com.navercorp.pinpoint.otlp.common.definition.property.MetricDescriptor; import com.navercorp.pinpoint.otlp.common.model.AggreFunc; import com.navercorp.pinpoint.otlp.common.model.DataType; import com.navercorp.pinpoint.otlp.web.vo.handler.FieldAttributeHandler; @@ -9,7 +25,7 @@ import org.apache.ibatis.type.TypeAliasRegistry; import org.apache.ibatis.type.TypeHandlerRegistry; -public class OtlpMetricWebRegistryHandler implements MyBatisRegistryHandler { +public class OtlpMetricPinotRegistryHandler implements MyBatisRegistryHandler { @Override public void registerTypeAlias(TypeAliasRegistry typeAliasRegistry) { typeAliasRegistry.registerAlias(OtlpMetricGroupsQueryParam.class); diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/OtlpMetricWebPinotDaoConfiguration.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/pinot/OtlpMetricWebPinotDaoConfiguration.java similarity index 78% rename from otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/OtlpMetricWebPinotDaoConfiguration.java rename to otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/pinot/OtlpMetricWebPinotDaoConfiguration.java index 497b42435b41..12b451efa520 100644 --- a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/OtlpMetricWebPinotDaoConfiguration.java +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/config/pinot/OtlpMetricWebPinotDaoConfiguration.java @@ -1,4 +1,20 @@ -package com.navercorp.pinpoint.otlp.web.config; +/* + * 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.otlp.web.config.pinot; import com.navercorp.pinpoint.mybatis.MyBatisConfigurationCustomizer; import com.navercorp.pinpoint.mybatis.MyBatisRegistryHandler; @@ -47,7 +63,7 @@ private TransactionFactory transactionFactory() { } private void registryHandler(org.apache.ibatis.session.Configuration config) { - MyBatisRegistryHandler registryHandler = new OtlpMetricWebRegistryHandler(); + MyBatisRegistryHandler registryHandler = new OtlpMetricPinotRegistryHandler(); registryHandler.registerTypeAlias(config.getTypeAliasRegistry()); registryHandler.registerTypeHandler(config.getTypeHandlerRegistry()); } diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/controller/MetricDefinitionController.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/controller/MetricDefinitionController.java index 1ac2b8795127..993a0d73ceb8 100644 --- a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/controller/MetricDefinitionController.java +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/controller/MetricDefinitionController.java @@ -16,9 +16,15 @@ package com.navercorp.pinpoint.otlp.web.controller; -import com.navercorp.pinpoint.otlp.common.definition.MetricDefinitionProperty; -import com.navercorp.pinpoint.otlp.web.service.MetricDefinitionService; +import com.navercorp.pinpoint.common.server.response.Response; +import com.navercorp.pinpoint.common.server.response.SimpleResponse; +import com.navercorp.pinpoint.otlp.common.defined.AppMetricDefinition; +import com.navercorp.pinpoint.otlp.common.definition.property.MetricDefinitionProperty; +import com.navercorp.pinpoint.otlp.web.service.AppMetricDefinitionService; +import com.navercorp.pinpoint.otlp.web.service.MetricMetadataService; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -30,14 +36,22 @@ @RequestMapping(value = "/api/otlp") public class MetricDefinitionController { - private final MetricDefinitionService metricDefinitionService; + private final MetricMetadataService metricMetadataService; + private final AppMetricDefinitionService appMetricDefinitionService; - public MetricDefinitionController(MetricDefinitionService metricDefinitionService) { - this.metricDefinitionService = metricDefinitionService; + public MetricDefinitionController(MetricMetadataService metricMetadataService, AppMetricDefinitionService appMetricDefinitionService) { + this.appMetricDefinitionService = appMetricDefinitionService; + this.metricMetadataService = metricMetadataService; } - @GetMapping("/metricDefinition/info") - public MetricDefinitionProperty getMetricDefinitionInfo(@RequestParam("applicationName") String applicationName) { - return metricDefinitionService.getMetricDefinitionInfo(applicationName); + @GetMapping("/metricDef/property") + public MetricDefinitionProperty getMetricDefinitionProperty(@RequestParam("applicationName") String applicationName) { + return metricMetadataService.getMetricDefinitionInfo(applicationName); + } + + @PostMapping("/metricDef/userDefined") + public Response addUserDefinedMetric(@RequestBody AppMetricDefinition appMetricDefinition) { + appMetricDefinitionService.addUserDefinedMetric(appMetricDefinition); + return SimpleResponse.ok(); } } diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/AppMetricDefinitionDao.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/AppMetricDefinitionDao.java new file mode 100644 index 000000000000..924fff910923 --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/AppMetricDefinitionDao.java @@ -0,0 +1,28 @@ +/* + * 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.otlp.web.dao; + +import com.navercorp.pinpoint.otlp.common.defined.AppMetricDefinition; + +import java.util.List; + +/** + * @author minwoo-jung + */ +public interface AppMetricDefinitionDao { + void insertAppMetricDefinitionList(List appMetricMetadataList); +} diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/MetricDefinitionDao.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/MetricDefinitionDao.java index a3375d6d1833..e69d08ec3dd0 100644 --- a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/MetricDefinitionDao.java +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/MetricDefinitionDao.java @@ -16,7 +16,7 @@ package com.navercorp.pinpoint.otlp.web.dao; -import com.navercorp.pinpoint.otlp.common.definition.MetricGroup; +import com.navercorp.pinpoint.otlp.common.definition.property.MetricGroup; import java.util.List; diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/model/AppMetricDefDto.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/model/AppMetricDefDto.java new file mode 100644 index 000000000000..77cad607cdbe --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/model/AppMetricDefDto.java @@ -0,0 +1,23 @@ +/* + * 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.otlp.web.dao.model; + +/** + * @author minwoo-jung + */ +public record AppMetricDefDto(String applicationName, int schemaVersion, String metricDefinition) { +} diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java new file mode 100644 index 000000000000..a53431e5b600 --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/mysql/MysqlAppMetricDefinitionDao.java @@ -0,0 +1,85 @@ +/* + * 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.otlp.web.dao.mysql; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.navercorp.pinpoint.common.server.util.json.JsonRuntimeException; +import com.navercorp.pinpoint.otlp.common.defined.AppMetricDefinition; +import com.navercorp.pinpoint.otlp.web.dao.AppMetricDefinitionDao; +import com.navercorp.pinpoint.otlp.web.dao.model.AppMetricDefDto; +import org.mybatis.spring.SqlSessionTemplate; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Repository; + +import java.util.HashSet; +import java.util.List; +import java.util.Objects; + +/** + * @author minwoo-jung + */ +@Repository +public class MysqlAppMetricDefinitionDao implements AppMetricDefinitionDao { + + private static final String NAMESPACE = AppMetricDefinitionDao.class.getName() + "."; + private final SqlSessionTemplate sqlSessionTemplate; + private final Mapper mapper; + + public MysqlAppMetricDefinitionDao(@Qualifier("otlpMysqlSqlSessionTemplate") SqlSessionTemplate sqlSessionTemplate, ObjectMapper mapper) { + this.sqlSessionTemplate = sqlSessionTemplate; + this.mapper = new Mapper(mapper); + } + + @Override + public void insertAppMetricDefinitionList(List appMetricDefinitionList) { + AppMetricDefDto appMetricDefDto = mapper.toDto(appMetricDefinitionList); + sqlSessionTemplate.insert(NAMESPACE + "insertAppMetricDefinition", appMetricDefDto); + } + + static class Mapper { + private final ObjectMapper mapper; + private final TypeReference> REF_LIST_APP_METRIC_DEFINITION = new TypeReference<>() {}; + + public Mapper(ObjectMapper mapper) { + this.mapper = Objects.requireNonNull(mapper, "mapper"); + } + + public AppMetricDefDto toDto(List appMetricDefinitionList) { + Objects.requireNonNull(appMetricDefinitionList, "appMetricDefinitionList"); + + String applicationName = appMetricDefinitionList.get(0).getApplicationName(); + + try { + String metricConfigJson = mapper.writeValueAsString(appMetricDefinitionList); + return new AppMetricDefDto(applicationName, AppMetricDefinition.SCHEMA_VERSION, metricConfigJson); + } catch (JsonProcessingException e) { + throw new JsonRuntimeException("can not convert appMetricDefinitionList to json :" + appMetricDefinitionList, e); + } + } + + public List toModel(AppMetricDefDto appMetricDefDto) { + try { + List appMetricDefinitionList = mapper.readValue(appMetricDefDto.metricDefinition(), REF_LIST_APP_METRIC_DEFINITION); + return appMetricDefinitionList; + } catch (JsonProcessingException e) { + throw new JsonRuntimeException("can not convert appMetricDefDto to model :" + appMetricDefDto, e); + } + } + } +} diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/pinot/PinotMetricMetaDataDao.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/pinot/PinotMetricMetaDataDao.java index 84fdba0e5168..ef92e1aea6f2 100644 --- a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/pinot/PinotMetricMetaDataDao.java +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/dao/pinot/PinotMetricMetaDataDao.java @@ -16,8 +16,8 @@ package com.navercorp.pinpoint.otlp.web.dao.pinot; -import com.navercorp.pinpoint.otlp.common.definition.MetricDescriptor; -import com.navercorp.pinpoint.otlp.common.definition.MetricGroup; +import com.navercorp.pinpoint.otlp.common.definition.property.MetricDescriptor; +import com.navercorp.pinpoint.otlp.common.definition.property.MetricGroup; import com.navercorp.pinpoint.otlp.web.dao.MetricDefinitionDao; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Qualifier; diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/AppMetricDefinitionService.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/AppMetricDefinitionService.java new file mode 100644 index 000000000000..88ec69647877 --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/AppMetricDefinitionService.java @@ -0,0 +1,26 @@ +/* + * 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.otlp.web.service; + +import com.navercorp.pinpoint.otlp.common.defined.AppMetricDefinition; + +/** + * @author minwoo-jung + */ +public interface AppMetricDefinitionService { + void addUserDefinedMetric(AppMetricDefinition appMetricMetadata); +} diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/AppMetricDefinitionServiceImpl.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/AppMetricDefinitionServiceImpl.java new file mode 100644 index 000000000000..70955d2163bb --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/AppMetricDefinitionServiceImpl.java @@ -0,0 +1,44 @@ +/* + * 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.otlp.web.service; + +import com.navercorp.pinpoint.otlp.common.defined.AppMetricDefinition; +import com.navercorp.pinpoint.otlp.web.dao.AppMetricDefinitionDao; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author minwoo-jung + */ +@Service +public class AppMetricDefinitionServiceImpl implements AppMetricDefinitionService { + + private final AppMetricDefinitionDao appMetricDefinitionDao; + + public AppMetricDefinitionServiceImpl(AppMetricDefinitionDao appMetricDefinitionDao) { + this.appMetricDefinitionDao = appMetricDefinitionDao; + } + + @Override + public void addUserDefinedMetric(AppMetricDefinition appMetricDefinition) { + List appMetricDefinitionList = new ArrayList<>(); + appMetricDefinitionList.add(appMetricDefinition); + appMetricDefinitionDao.insertAppMetricDefinitionList(appMetricDefinitionList); + } +} diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricDefinitionServiceImpl.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricDefinitionServiceImpl.java index d2201293b5cb..8eba0ddd4f84 100644 --- a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricDefinitionServiceImpl.java +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricDefinitionServiceImpl.java @@ -16,8 +16,8 @@ package com.navercorp.pinpoint.otlp.web.service; -import com.navercorp.pinpoint.otlp.common.definition.MetricDefinitionProperty; -import com.navercorp.pinpoint.otlp.common.definition.MetricGroup; +import com.navercorp.pinpoint.otlp.common.definition.property.MetricDefinitionProperty; +import com.navercorp.pinpoint.otlp.common.definition.property.MetricGroup; import com.navercorp.pinpoint.otlp.web.dao.MetricDefinitionDao; import jakarta.validation.Valid; import org.apache.logging.log4j.LogManager; @@ -31,7 +31,7 @@ * @author minwoo-jung */ @Service -public class MetricDefinitionServiceImpl implements MetricDefinitionService { +public class MetricDefinitionServiceImpl implements MetricMetadataService { private final Logger logger = LogManager.getLogger(this.getClass()); private final MetricDefinitionDao metricDefinitionDao; diff --git a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricDefinitionService.java b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricMetadataService.java similarity index 85% rename from otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricDefinitionService.java rename to otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricMetadataService.java index 5d9ae80150ef..c4bf23c0a2b8 100644 --- a/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricDefinitionService.java +++ b/otlpmetric/otlpmetric-web/src/main/java/com/navercorp/pinpoint/otlp/web/service/MetricMetadataService.java @@ -16,11 +16,11 @@ package com.navercorp.pinpoint.otlp.web.service; -import com.navercorp.pinpoint.otlp.common.definition.MetricDefinitionProperty; +import com.navercorp.pinpoint.otlp.common.definition.property.MetricDefinitionProperty; /** * @author minwoo-jung */ -public interface MetricDefinitionService { +public interface MetricMetadataService { MetricDefinitionProperty getMetricDefinitionInfo(String applicationName); } diff --git a/otlpmetric/otlpmetric-web/src/main/resources/otlp/sql/CreateTableStatement-mysql.sql b/otlpmetric/otlpmetric-web/src/main/resources/otlp/sql/CreateTableStatement-mysql.sql new file mode 100644 index 000000000000..b4ba159b9753 --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/resources/otlp/sql/CreateTableStatement-mysql.sql @@ -0,0 +1,6 @@ +CREATE TABLE `application_metric_definition` ( + `application_name` VARCHAR(60) NOT NULL, + `metric_definition` text NOT NULL, + `schema_version` int(10) NOT NULL, + PRIMARY KEY (`application_name`) +); \ No newline at end of file diff --git a/otlpmetric/otlpmetric-web/src/main/resources/otlp/web/mapper/mysql/AppMetricDefinitionMapper.xml b/otlpmetric/otlpmetric-web/src/main/resources/otlp/web/mapper/mysql/AppMetricDefinitionMapper.xml new file mode 100644 index 000000000000..adaa8a498d66 --- /dev/null +++ b/otlpmetric/otlpmetric-web/src/main/resources/otlp/web/mapper/mysql/AppMetricDefinitionMapper.xml @@ -0,0 +1,18 @@ + + + + + + INSERT INTO application_metric_definition + (application_name, metric_definition, schema_version) + VALUES + (#{applicationName}, #{metricDefinition}, #{schemaVersion}) + + + + + \ No newline at end of file