Skip to content

Commit

Permalink
[pinpoint-apm#12021] Replace ErrorAttributes with ProblemDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
intr3p1d committed Feb 21, 2025
1 parent 05038d9 commit aeedd5a
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
SpringDataWebAutoConfiguration.class,
RedisAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class,
RedisReactiveAutoConfiguration.class
RedisReactiveAutoConfiguration.class,
})
@Import({
PinpointWebModule.class,
Expand Down
5 changes: 4 additions & 1 deletion web-starter/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ spring:
default-view-inclusion: true
profiles:
active: release, metric
mvc:
problemdetails:
enabled: true

server:
port: 8080
Expand All @@ -30,7 +33,7 @@ pinpoint:
enabled: true
otlpmetric:
enabled: true

pinpoint.web.websocket:
async-send-timeout:
max-session-idle-timeout: 10800000 # 3 hours
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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 org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;

import static com.navercorp.pinpoint.web.problem.StackTraceProcessor.COMPOUND;
import static java.util.Arrays.asList;

/**
* @author intr3p1d
*/
@ControllerAdvice
final class CustomExceptionHandler extends ResponseEntityExceptionHandler {

private static final ToStringSerializer serializer = new ToStringSerializer();
private final String hostname;

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

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

@ExceptionHandler({Exception.class})
public ResponseEntity<ProblemDetail> handleGeneralException(
Exception ex,
WebRequest request
) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
ProblemDetail problemDetail = ProblemDetail.forStatus(status.value());
problemDetail.setTitle("Internal Server Error");
problemDetail.setDetail(ex.getMessage());
addProperties(problemDetail, request);
addStackTraces(problemDetail, ex);

return new ResponseEntity<>(problemDetail, status);
}

@Override
protected ResponseEntity<Object> handleExceptionInternal(
Exception ex,
@Nullable Object body,
HttpHeaders headers,
HttpStatusCode statusCode,
WebRequest request
) {
ResponseEntity<Object> response = super.handleExceptionInternal(ex, body, headers, statusCode, request);

if (response.getBody() instanceof ProblemDetail problemDetail) {
addProperties(problemDetail, request);
addStackTraces(problemDetail, ex);
return this.createResponseEntity(problemDetail, headers, statusCode, request);
}
return response;
}

public void addProperties(ProblemDetail problemDetail, WebRequest request) {
problemDetail.setProperty("hostname", hostname);
problemDetail.setProperty("method", request.getDescription(false));
problemDetail.setProperty("parameters", request.getParameterMap());
}

public void addStackTraces(ProblemDetail problemDetail, Throwable th) {
final Collection<StackTraceElement> stackTrace = COMPOUND.process(asList(th.getStackTrace()));
String[] trace = traceToStringArray(stackTrace);
problemDetail.setProperty("trace", trace);
}

private String[] traceToStringArray(Collection<StackTraceElement> stackTrace) {
return stackTrace.stream().map(serializer::valueToString).toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
WebServiceConfig.class,
RealtimeConfig.class,
MainDataSourceConfiguration.class,
ProblemSpringWebConfig.class,

WebPinpointIdCacheConfiguration.class,
CacheConfiguration.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
* @author intr3p1d
*/
@Configuration
public class ProblemSpringWebConfig {

@Bean
@Primary
public ResponseEntityExceptionHandler pinpointExceptionHandling() {
return new CustomExceptionHandler();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.problem;

import static java.util.ServiceLoader.load;
import static java.util.stream.StreamSupport.stream;

import java.util.Collection;
/**
* <a href="https://github.com/zalando/problem/blob/main/problem/src/main/java/org/zalando/problem/spi/StackTraceProcessor.java">...</a>
*/
public interface StackTraceProcessor {

StackTraceProcessor DEFAULT = elements -> elements;
StackTraceProcessor COMPOUND = stream(load(StackTraceProcessor.class).spliterator(), false)
.reduce(DEFAULT, (first, second) -> elements -> second.process(first.process(elements)));

Collection<StackTraceElement> process(final Collection<StackTraceElement> elements);

}

This file was deleted.

Loading

0 comments on commit aeedd5a

Please sign in to comment.