Skip to content

Commit

Permalink
[pinpoint-apm#12021] Replace ErrorAttributes with Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
intr3p1d committed Feb 12, 2025
1 parent c860c7d commit 64b7648
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 256 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@
<jaxb4.version>4.0.0</jaxb4.version>
<jakarta.validation-api3.version>3.0.2</jakarta.validation-api3.version>

<problem-spring-web.version>0.29.1</problem-spring-web.version>

<!-- <slf4j.version>1.7.30</slf4j.version>-->
<slf4j.version>2.0.16</slf4j.version>
<log4j2.version>2.24.2</log4j2.version>
Expand Down Expand Up @@ -764,6 +766,11 @@
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web-starter</artifactId>
<version>${problem-spring-web.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.context.annotation.Import;
/**
* @author minwoo.jung
Expand All @@ -49,7 +50,8 @@
SpringDataWebAutoConfiguration.class,
RedisAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class,
RedisReactiveAutoConfiguration.class
RedisReactiveAutoConfiguration.class,
ErrorMvcAutoConfiguration.class,
})
@Import({
PinpointWebModule.class,
Expand Down
1 change: 0 additions & 1 deletion web-starter/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ spring:
server:
port: 8080
error:
path: /api-public/error
include-exception: true
include-message: always
include-binding-errors: always
Expand Down
4 changes: 4 additions & 0 deletions web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web-starter</artifactId>
</dependency>

<!-- Mybatis dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2025 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.web;

import com.navercorp.pinpoint.web.problem.ProblemWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.NativeWebRequest;
import org.zalando.problem.Problem;
import org.zalando.problem.spring.web.advice.ProblemHandling;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author intr3p1d
*/
@ControllerAdvice
final class ExceptionHandling implements ProblemHandling {

private final String hostname;

ExceptionHandling() {
this.hostname = getHostName();
}

static private String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
return "unknown";
}
}

@Override
public ResponseEntity<Problem> process(ResponseEntity<Problem> entity, NativeWebRequest request) {
Problem originalProblem = entity.getBody();
if (originalProblem == null) {
return entity;
}

HttpServletRequest httpRequest = request.getNativeRequest(HttpServletRequest.class);
HttpServletResponse httpResponse = request.getNativeResponse(HttpServletResponse.class);

String path = (httpRequest != null) ? httpRequest.getRequestURI() : "unknown";
String method = (httpRequest != null) ? httpRequest.getMethod() : "unknown";
int statusCode = (httpResponse != null) ? httpResponse.getStatus() : entity.getStatusCode().value();

Problem modifiedProblem = new ProblemWrapper(originalProblem, hostname, path, method);
return ResponseEntity.status(statusCode).body(modifiedProblem);
}

@Override
public boolean isCausalChainsEnabled() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
WebServiceConfig.class,
RealtimeConfig.class,
MainDataSourceConfiguration.class,
ProblemSpringWebConfig.class,

CacheConfiguration.class,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2025 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.web;

import com.navercorp.pinpoint.web.problem.ProblemWrapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.zalando.problem.Problem;
import org.zalando.problem.jackson.ProblemModule;
import org.zalando.problem.spring.web.advice.AdviceTrait;
import org.zalando.problem.violations.ConstraintViolationProblemModule;

/**
* @author intr3p1d
*/
@Configuration
public class ProblemSpringWebConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization(
@Qualifier("pinpointWebProblemModule") ProblemModule problemModule,
@Qualifier("pinpointConstraintViolationProblemModule") ConstraintViolationProblemModule constraintViolationProblemModule
) {
return (Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) ->
jacksonObjectMapperBuilder.modules(
problemModule, constraintViolationProblemModule
).mixIn(Problem.class, ProblemWrapper.class);
}

@Bean
public ProblemModule pinpointWebProblemModule() {
return new ProblemModule().withStackTraces(true);
}

@Bean
public ConstraintViolationProblemModule pinpointConstraintViolationProblemModule() {
return new ConstraintViolationProblemModule();
}

@Bean
@Primary
public AdviceTrait pinpointExceptionHandling() {
return new ExceptionHandling();
}
}
2 changes: 2 additions & 0 deletions web/src/main/java/com/navercorp/pinpoint/web/WebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.context.annotation.Import;

@SpringBootConfiguration
Expand All @@ -40,6 +41,7 @@
RedisAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class,
RedisReactiveAutoConfiguration.class,
ErrorMvcAutoConfiguration.class,
})
@Import({
PinpointWebModule.class,
Expand Down

This file was deleted.

Loading

0 comments on commit 64b7648

Please sign in to comment.