-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($Zipkin): support trackable HTTP response
support trackable HTTP response BREAKING CHANGE: support trackable HTTP response
- Loading branch information
1 parent
7fdce5a
commit ee8dab4
Showing
4 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
common/src/main/java/com/jmsoftware/maf/common/bean/TrackableBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.jmsoftware.maf.common.bean; | ||
|
||
import lombok.Data; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* <h1>TrackableBean</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 3/12/22 1:17 AM | ||
**/ | ||
@Data | ||
public class TrackableBean { | ||
/** | ||
* The Track. Pattern: [Zipkin trace id]#[Span id] | ||
*/ | ||
@Nullable | ||
private String track; | ||
} |
62 changes: 62 additions & 0 deletions
62
.../java/com/jmsoftware/maf/springcloudstarter/aspect/TrackableResponseControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.jmsoftware.maf.springcloudstarter.aspect; | ||
|
||
import com.jmsoftware.maf.common.bean.TrackableBean; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.cloud.sleuth.Tracer; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServerHttpResponse; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
||
import java.util.Objects; | ||
|
||
import static cn.hutool.core.text.StrFormatter.format; | ||
|
||
/** | ||
* <h1>TrackableResponseControllerAdvice</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 3/12/22 1:55 AM | ||
**/ | ||
@Slf4j | ||
@RestControllerAdvice | ||
@RequiredArgsConstructor | ||
public class TrackableResponseControllerAdvice implements ResponseBodyAdvice<TrackableBean> { | ||
private final Tracer tracer; | ||
|
||
@Override | ||
@SuppressWarnings("NullableProblems") | ||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
val equaled = TrackableBean.class.equals(returnType.getContainingClass()); | ||
if (!equaled) { | ||
log.warn("TrackableResponseControllerAdvice supports failed, returnType: {}, converterType: {}", returnType, | ||
converterType); | ||
} | ||
return equaled; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("NullableProblems") | ||
public TrackableBean beforeBodyWrite( | ||
TrackableBean body, | ||
MethodParameter returnType, | ||
MediaType selectedContentType, | ||
Class<? extends HttpMessageConverter<?>> selectedConverterType, | ||
ServerHttpRequest request, | ||
ServerHttpResponse response | ||
) { | ||
if (tracer.currentSpan() == null) { | ||
body.setTrack("No trace"); | ||
return body; | ||
} | ||
val traceContext = Objects.requireNonNull(tracer.currentSpan()).context(); | ||
body.setTrack(format("{}#{}", traceContext.traceId(), traceContext.spanId())); | ||
return body; | ||
} | ||
} |