Skip to content

Commit

Permalink
[#9631] Add exceptionTrace Module
Browse files Browse the repository at this point in the history
  • Loading branch information
intr3p1d committed Sep 1, 2023
1 parent e04017a commit 6b2ab57
Show file tree
Hide file tree
Showing 59 changed files with 3,834 additions and 4 deletions.
4 changes: 4 additions & 0 deletions commons-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>

<!--<dependency> -->
<!--<groupId>org.slf4j</groupId> -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2023 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.common.server.mapper;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.type.TypeReference;
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.common.util.CollectionUtils;
import com.navercorp.pinpoint.common.util.StringUtils;
import org.mapstruct.Qualifier;
import org.springframework.stereotype.Component;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Component
public class MapStructUtils {
private final ObjectMapper mapper;

public MapStructUtils(ObjectMapper mapper) {
this.mapper = Objects.requireNonNull(mapper, "mapper");
}


@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface JsonStrToList {

}

@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface listToJsonStr {

}


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

@listToJsonStr
public <T> String listToJsonStr(List<T> lists) {
if (CollectionUtils.isEmpty(lists)) {
return "";
}
try {
return mapper.writeValueAsString(lists);
} catch (JacksonException e) {
throw new JsonRuntimeException("Json Write error", e);
}
}

}
84 changes: 84 additions & 0 deletions exceptiontrace/exceptiontrace-collector/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pinpoint-exceptiontrace-module</artifactId>
<groupId>com.navercorp.pinpoint</groupId>
<version>2.6.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>pinpoint-exceptiontrace-collector</artifactId>

<properties>
<jdk.version>11</jdk.version>
<jdk.home>${env.JAVA_11_HOME}</jdk.home>
</properties>

<dependencies>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-pinot-kafka</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-exceptiontrace-common</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-metric</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-commons-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>${spring.kafka.version}</version>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-collector</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 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.exceptiontrace.collector;

import com.navercorp.pinpoint.exceptiontrace.collector.config.ExceptionMetricKafkaConfiguration;
import com.navercorp.pinpoint.pinot.config.PinotConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

/**
* @author intr3p1d
*/
@Configuration
@Import({PinotConfiguration.class, ExceptionMetricKafkaConfiguration.class})
@ComponentScan({
"com.navercorp.pinpoint.common.server.mapper",
"com.navercorp.pinpoint.exceptiontrace.collector.service",
"com.navercorp.pinpoint.exceptiontrace.collector.dao",
"com.navercorp.pinpoint.exceptiontrace.collector.mapper",
})
@PropertySource({ExceptionTraceCollectorConfig.KAFKA_TOPIC_PROPERTIES})
@ConditionalOnProperty(name = "pinpoint.modules.collector.exceptiontrace.enabled", havingValue = "true")
public class ExceptionTraceCollectorConfig {
public static final String KAFKA_TOPIC_PROPERTIES = "classpath:profiles/${pinpoint.profiles.active}/kafka-topic-exception.properties";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 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.exceptiontrace.collector;

/**
* @author intr3p1d
*/
public class ExceptionTraceCollectorPropertySources {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023 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.exceptiontrace.collector.config;

import com.navercorp.pinpoint.exceptiontrace.collector.entity.ExceptionMetaDataEntity;
import com.navercorp.pinpoint.pinot.kafka.KafkaConfiguration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

/**
* @author intr3p1d
*/
@Configuration
@Import({KafkaConfiguration.class})
public class ExceptionMetricKafkaConfiguration {

@Bean
public KafkaTemplate<String, ExceptionMetaDataEntity> kafkaExceptionMetaDataTemplate(
@Qualifier("kafkaProducerFactory") ProducerFactory producerFactory
) {
return new KafkaTemplate<String, ExceptionMetaDataEntity>(producerFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 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.exceptiontrace.collector.dao;

import com.navercorp.pinpoint.exceptiontrace.common.model.ExceptionMetaData;

import java.util.List;

/**
* @author intr3p1d
*/
public interface ExceptionTraceDao {
void insert(List<ExceptionMetaData> exceptionMetaData);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2023 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.exceptiontrace.collector.dao;

import com.navercorp.pinpoint.common.server.util.StringPrecondition;
import com.navercorp.pinpoint.exceptiontrace.collector.entity.ExceptionMetaDataEntity;
import com.navercorp.pinpoint.exceptiontrace.collector.mapper.ExceptionMetaDataMapper;
import com.navercorp.pinpoint.exceptiontrace.common.model.ExceptionMetaData;
import com.navercorp.pinpoint.pinot.kafka.util.KafkaCallbacks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Repository;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.List;
import java.util.Objects;

/**
* @author intr3p1d
*/
@Repository
public class PinotExceptionTraceDao implements ExceptionTraceDao {
private final Logger logger = LogManager.getLogger(this.getClass());

private final KafkaTemplate<String, ExceptionMetaDataEntity> kafkaExceptionMetaDataTemplate;

private final ExceptionMetaDataMapper mapper;

private final String topic;

private final ListenableFutureCallback<SendResult<String, ExceptionMetaDataEntity>> resultCallback
= KafkaCallbacks.loggingCallback("Kafka(ExceptionMetaDataEntity)", logger);


public PinotExceptionTraceDao(
@Qualifier("kafkaExceptionMetaDataTemplate") KafkaTemplate<String, ExceptionMetaDataEntity> kafkaExceptionMetaDataTemplate,
@Value("${kafka.exception.topic}") String topic,
ExceptionMetaDataMapper mapper
) {
this.kafkaExceptionMetaDataTemplate = Objects.requireNonNull(kafkaExceptionMetaDataTemplate, "kafkaExceptionMetaDataTemplate");
this.topic = StringPrecondition.requireHasLength(topic, "topic");
this.mapper = Objects.requireNonNull(mapper, "mapper");
}

@Override
public void insert(List<ExceptionMetaData> exceptionMetaData) {
Objects.requireNonNull(exceptionMetaData);
logger.info("Pinot data insert: {}", exceptionMetaData);

for (ExceptionMetaData e : exceptionMetaData) {
ExceptionMetaDataEntity dataEntity = mapper.toEntity(e);
ListenableFuture<SendResult<String, ExceptionMetaDataEntity>> response = this.kafkaExceptionMetaDataTemplate.send(
topic, dataEntity
);
response.addCallback(resultCallback);
}
}
}
Loading

0 comments on commit 6b2ab57

Please sign in to comment.