Skip to content

Commit

Permalink
feat($Zipkin): support trackable HTTP response
Browse files Browse the repository at this point in the history
support trackable HTTP response

BREAKING CHANGE: support trackable HTTP response
  • Loading branch information
johnnymillergh committed Mar 12, 2022
1 parent 7fdce5a commit ee8dab4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
*/
@Data
@SuppressWarnings("unused")
public class PageResponseBodyBean<T> implements Serializable {
@EqualsAndHashCode(callSuper = true)
public class PageResponseBodyBean<T> extends TrackableBean implements Serializable {
/**
* The constant serialVersionUID.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
*/
@Data
@SuppressWarnings("unused")
public class ResponseBodyBean<T> implements Serializable {
@EqualsAndHashCode(callSuper = true)
public class ResponseBodyBean<T> extends TrackableBean implements Serializable{
private static final long serialVersionUID = 4645469240048361965L;

/**
Expand Down
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;
}
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;
}
}

0 comments on commit ee8dab4

Please sign in to comment.