Browse Source

fix(lingxing): 保留平台 request_id 并在审计中补 platformCode

- ChannelException 新增 platformRequestId,与本地链路 requestId 分开
- LingxingEnvelope 解析响应顶层 request_id(兼容 requestId)并写入异常
- CallRecord 分别记录平台 request_id 与业务码 platformCode
- 失败日志补 platformCode,并对平台文案做换行清洗与截断
- LingxingCallObserverTest 断言 3001008 与平台 request_id 不被本地 ID 覆盖

验证:mvn -q -pl core,lingxing -am -Dtest='LingxingCallObserverTest,LingxingEnvelopeTest' -DfailIfNoTests=false test
feature/supplier-lingxing
lml 2 weeks ago
parent
commit
7747125099
  1. 17
      core/src/main/java/common/ecommerce/audit/CallRecord.java
  2. 19
      core/src/main/java/common/ecommerce/audit/Slf4jCallObserver.java
  3. 19
      core/src/main/java/common/ecommerce/error/ChannelException.java
  4. 6
      lingxing/src/main/java/common/ecommerce/lingxing/http/LingxingEnvelope.java
  5. 4
      lingxing/src/test/java/common/ecommerce/lingxing/adapter/LingxingCallObserverTest.java

17
core/src/main/java/common/ecommerce/audit/CallRecord.java

@ -38,6 +38,7 @@ public final class CallRecord {
private final String errorMessage; private final String errorMessage;
private final Boolean retryable; private final Boolean retryable;
private final String platformRequestId; private final String platformRequestId;
private final String platformCode;
private CallRecord(Builder b) { private CallRecord(Builder b) {
this.occurredAt = Objects.requireNonNull(b.occurredAt, "occurredAt"); this.occurredAt = Objects.requireNonNull(b.occurredAt, "occurredAt");
@ -56,6 +57,7 @@ public final class CallRecord {
this.errorMessage = b.errorMessage; this.errorMessage = b.errorMessage;
this.retryable = b.retryable; this.retryable = b.retryable;
this.platformRequestId = b.platformRequestId; this.platformRequestId = b.platformRequestId;
this.platformCode = b.platformCode;
} }
public static Builder builder() { public static Builder builder() {
@ -162,6 +164,16 @@ public final class CallRecord {
return platformRequestId; return platformRequestId;
} }
/**
* 平台业务码 / HTTP 状态码;仅当失败异常是 {@code ChannelException} 时有值。
*
* <p>领星场景下就是 {@code 3001008}、{@code 2001003} 这类业务码,
* 审计表可以直接按它聚合,不用 LIKE 匹配 message。</p>
*/
public String platformCode() {
return platformCode;
}
public boolean failed() { public boolean failed() {
return outcome == CallOutcome.FAILURE; return outcome == CallOutcome.FAILURE;
} }
@ -180,6 +192,7 @@ public final class CallRecord {
+ " params=" + params + " params=" + params
+ (failed() ? " error=" + errorType + ": " + errorMessage + (failed() ? " error=" + errorType + ": " + errorMessage
+ " retryable=" + retryable + " retryable=" + retryable
+ " platformCode=" + platformCode
+ " platformRequestId=" + platformRequestId + " platformRequestId=" + platformRequestId
: "") : "")
+ '}'; + '}';
@ -202,6 +215,7 @@ public final class CallRecord {
private String errorMessage; private String errorMessage;
private Boolean retryable; private Boolean retryable;
private String platformRequestId; private String platformRequestId;
private String platformCode;
public Builder occurredAt(Instant occurredAt) { public Builder occurredAt(Instant occurredAt) {
this.occurredAt = occurredAt; this.occurredAt = occurredAt;
@ -265,7 +279,8 @@ public final class CallRecord {
if (error instanceof ChannelException) { if (error instanceof ChannelException) {
ChannelException channelError = (ChannelException) error; ChannelException channelError = (ChannelException) error;
this.retryable = channelError.retryable(); this.retryable = channelError.retryable();
this.platformRequestId = channelError.requestId(); this.platformRequestId = channelError.platformRequestId();
this.platformCode = channelError.platformCode();
} }
} }
return this; return this;

19
core/src/main/java/common/ecommerce/audit/Slf4jCallObserver.java

@ -80,14 +80,14 @@ public final class Slf4jCallObserver implements CallObserver {
public void onCall(CallRecord record) { public void onCall(CallRecord record) {
if (record.failed()) { if (record.failed()) {
LOG.warn("sdk-call-failed at={} op={} provider={} platform={} omsStoreId={} storeId={} " LOG.warn("sdk-call-failed at={} op={} provider={} platform={} omsStoreId={} storeId={} "
+ "requestId={} cost={}ms result={} params={} retryable={} platformRequestId={} " + "requestId={} cost={}ms result={} params={} retryable={} platformCode={} "
+ "error={}: {}", + "platformRequestId={} error={}: {}",
at(record.occurredAt()), at(record.occurredAt()),
record.operation(), record.provider(), record.platform(), record.operation(), record.provider(), record.platform(),
record.omsStoreId(), record.externalStoreId(), record.requestId(), record.omsStoreId(), record.externalStoreId(), record.requestId(),
record.durationMillis(), record.resultCount(), record.params(), record.durationMillis(), record.resultCount(), record.params(),
record.retryable(), record.platformRequestId(), record.retryable(), record.platformCode(), record.platformRequestId(),
record.errorType(), record.errorMessage()); record.errorType(), safe(record.errorMessage()));
} else { } else {
LOG.info("sdk-call at={} op={} provider={} platform={} omsStoreId={} storeId={} " LOG.info("sdk-call at={} op={} provider={} platform={} omsStoreId={} storeId={} "
+ "requestId={} cost={}ms result={} params={}", + "requestId={} cost={}ms result={} params={}",
@ -101,4 +101,15 @@ public final class Slf4jCallObserver implements CallObserver {
private String at(Instant instant) { private String at(Instant instant) {
return format(instant); return format(instant);
} }
/**
* 平台文案不可控,去掉换行/控制字符并截断,避免一条日志被拆成多行。
*/
private static String safe(String value) {
if (value == null) {
return null;
}
String oneLine = value.replaceAll("\\p{Cntrl}", " ").trim();
return oneLine.length() <= 512 ? oneLine : oneLine.substring(0, 512) + "...";
}
} }

19
core/src/main/java/common/ecommerce/error/ChannelException.java

@ -16,6 +16,7 @@ public class ChannelException extends RuntimeException {
private final String operation; private final String operation;
private final String requestId; private final String requestId;
private final String platformCode; private final String platformCode;
private final String platformRequestId;
private final boolean retryable; private final boolean retryable;
private final String rawResponse; private final String rawResponse;
@ -27,6 +28,7 @@ public class ChannelException extends RuntimeException {
this.operation = b.operation; this.operation = b.operation;
this.requestId = b.requestId; this.requestId = b.requestId;
this.platformCode = b.platformCode; this.platformCode = b.platformCode;
this.platformRequestId = b.platformRequestId;
this.retryable = b.retryable; this.retryable = b.retryable;
this.rawResponse = b.rawResponse; this.rawResponse = b.rawResponse;
} }
@ -59,6 +61,16 @@ public class ChannelException extends RuntimeException {
return platformCode; return platformCode;
} }
/**
* 平台返回的 request_id(如领星响应体顶层 {@code request_id})。
*
* <p>与 {@link #requestId()} 区分:后者是我方链路 ID,用来串本地日志;
* 前者是平台侧工单号,用来找平台日志。</p>
*/
public String platformRequestId() {
return platformRequestId;
}
public boolean retryable() { public boolean retryable() {
return retryable; return retryable;
} }
@ -76,6 +88,7 @@ public class ChannelException extends RuntimeException {
protected String operation; protected String operation;
protected String requestId; protected String requestId;
protected String platformCode; protected String platformCode;
protected String platformRequestId;
protected boolean retryable; protected boolean retryable;
protected String rawResponse; protected String rawResponse;
@ -114,6 +127,12 @@ public class ChannelException extends RuntimeException {
return this; return this;
} }
/** 平台返回的 request_id;与本地链路 ID {@link #requestId(String)} 分开。 */
public Builder platformRequestId(String platformRequestId) {
this.platformRequestId = platformRequestId;
return this;
}
public Builder retryable(boolean retryable) { public Builder retryable(boolean retryable) {
this.retryable = retryable; this.retryable = retryable;
return this; return this;

6
lingxing/src/main/java/common/ecommerce/lingxing/http/LingxingEnvelope.java

@ -56,6 +56,11 @@ public final class LingxingEnvelope {
if (message == null) { if (message == null) {
message = json.stringAt(body, "msg"); message = json.stringAt(body, "msg");
} }
// 平台侧工单号:领星顶层字段是 request_id,兼容驼峰写法
String platformRequestId = json.stringAt(body, "request_id");
if (platformRequestId == null) {
platformRequestId = json.stringAt(body, "requestId");
}
ChannelException.Builder b = ChannelException.builder() ChannelException.Builder b = ChannelException.builder()
.message("lingxing code=" + code + " msg=" + message + " on " + operation) .message("lingxing code=" + code + " msg=" + message + " on " + operation)
@ -63,6 +68,7 @@ public final class LingxingEnvelope {
.provider(LingxingConstants.ADAPTER_ID) .provider(LingxingConstants.ADAPTER_ID)
.omsStoreId(omsStoreId) .omsStoreId(omsStoreId)
.requestId(requestId) .requestId(requestId)
.platformRequestId(platformRequestId)
.platformCode(code) .platformCode(code)
.rawResponse(body); .rawResponse(body);

4
lingxing/src/test/java/common/ecommerce/lingxing/adapter/LingxingCallObserverTest.java

@ -65,7 +65,7 @@ class LingxingCallObserverTest {
+ "}"; + "}";
private static final String RATE_LIMITED_JSON = private static final String RATE_LIMITED_JSON =
"{\"code\":3001008,\"message\":\"请求过于频繁\"}"; "{\"code\":3001008,\"message\":\"请求过于频繁\",\"request_id\":\"lx-req-1\"}";
/** 收集回调记录,避免依赖 SLF4J 绑定。 */ /** 收集回调记录,避免依赖 SLF4J 绑定。 */
private static final class CollectingObserver implements CallObserver { private static final class CollectingObserver implements CallObserver {
@ -159,6 +159,8 @@ class LingxingCallObserverTest {
assertEquals(CallOutcome.FAILURE, record.outcome()); assertEquals(CallOutcome.FAILURE, record.outcome());
assertTrue(record.errorType().contains("RateLimitException"), record.errorType()); assertTrue(record.errorType().contains("RateLimitException"), record.errorType());
assertTrue(record.errorMessage().contains("3001008"), record.errorMessage()); assertTrue(record.errorMessage().contains("3001008"), record.errorMessage());
assertEquals("3001008", record.platformCode());
assertEquals("lx-req-1", record.platformRequestId(), "平台 request_id 不能被本地链路 ID 覆盖");
} }
@Test @Test

Loading…
Cancel
Save