Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add service name to context for propagation #31

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.arextest</groupId>
<artifactId>arex-common</artifactId>
<packaging>jar</packaging>
<version>0.1.28</version>
<version>0.1.29</version>


<name>${project.groupId}:${project.artifactId}</name>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/arextest/common/filter/ContentFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha

private void logResponseSize(HttpServletRequest httpServletRequest,
ServletResponse servletResponse) {
String clientApp = httpServletRequest.getHeader(CLIENT_APP_HEADER);
String clientApp = httpServletRequest.getHeader(SERVICE_NAME_HEADER);
String category = httpServletRequest.getHeader(CATEGORY_TYPE_HEADER);
String requestURI = httpServletRequest.getRequestURI();
long requestLength = httpServletRequest.getContentLengthLong();
Expand Down Expand Up @@ -120,7 +120,7 @@ private void recordPayloadInfo(String clientApp, String category,
return;
}
Map<String, String> tags = new HashMap<>();
putIfValueNotEmpty(clientApp, CLIENT_APP, tags);
putIfValueNotEmpty(clientApp, SERVICE_NAME, tags);
putIfValueNotEmpty(category, CATEGORY, tags);
putIfValueNotEmpty(path, PATH, tags);
for (MetricListener metricListener : metricListeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ public void afterCompletion(HttpServletRequest request, HttpServletResponse resp

long executeMillis = System.currentTimeMillis() - (long) startTime;

recordExecuteMillis(request.getHeader(CLIENT_APP_HEADER), request.getHeader(CATEGORY_TYPE_HEADER),
recordExecuteMillis(request.getHeader(SERVICE_NAME_HEADER), request.getHeader(CATEGORY_TYPE_HEADER),
request.getRequestURI(), executeMillis);
}

public void recordExecuteMillis(String clientApp, String category,
public void recordExecuteMillis(String serviceName, String category,
String path, long executeMillis) {
if (CollectionUtils.isEmpty(metricListeners)) {
return;
}

Map<String, String> tags = new HashMap<>();
putIfValueNotEmpty(clientApp, CLIENT_APP, tags);
putIfValueNotEmpty(serviceName, SERVICE_NAME, tags);
putIfValueNotEmpty(category, CATEGORY, tags);
putIfValueNotEmpty(path, PATH, tags);
for (MetricListener metricListener : metricListeners) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/arextest/common/model/TenantContext.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.arextest.common.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;

@Data
@FieldNameConstants
@NoArgsConstructor
public class TenantContext {

private String tenantCode;
private String serviceName;

public TenantContext(TenantContext other) {
this.tenantCode = other.tenantCode;
this.serviceName = other.serviceName;
}

public TenantContext deepClone() {
return new TenantContext(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class AbstractContextWithTraceRunnable implements Runnable {

public AbstractContextWithTraceRunnable() {
this.traceMap = MDC.getCopyOfContextMap();
this.tenantContext = TenantContextUtil.getAll();
this.tenantContext = TenantContextUtil.getContext();
}


Expand Down Expand Up @@ -57,17 +57,17 @@ private TenantContext markContext() {
if (this.tenantContext == null) {
return null;
} else {
TenantContext tenantContext = TenantContextUtil.getCopyOfAll();
TenantContextUtil.setAll(this.tenantContext);
TenantContext tenantContext = TenantContextUtil.getCopyOfContext();
TenantContextUtil.setContext(this.tenantContext);
return tenantContext;
}
}

private void removeContext(TenantContext tenantContext) {
if (tenantContext == null) {
TenantContextUtil.clearAll();
TenantContextUtil.clear();
} else {
TenantContextUtil.setAll(tenantContext);
TenantContextUtil.setContext(tenantContext);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/arextest/common/utils/MetricUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
@Slf4j
public final class MetricUtils {
public static final String GET_METHOD = "GET";
public static final String CLIENT_APP_HEADER = "client-app";
public static final String CATEGORY_TYPE_HEADER = "category-type";
public static final String SERVICE_NAME_HEADER = "arex-service-name";
public static final String CATEGORY_TYPE_HEADER = "arex-category-type";
public static final String ENTRY_PAYLOAD_NAME = "service.entry.payload";
public static final String TYPE = "type";
public static final String REQUEST_TAG = "request";
public static final String RESPONSE_TAG = "response";
public static final String CLIENT_APP = "clientApp";
public static final String SERVICE_NAME = "serviceName";
public static final String PATH = "path";
public static final String CATEGORY = "category";
public static final String START_TIME = "startTime";
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/arextest/common/utils/TenantContextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.alibaba.ttl.TransmittableThreadLocal;
import com.arextest.common.model.TenantContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand All @@ -14,33 +13,36 @@ public static void setTenantCode(String tenantCode) {
getOrCreateContext().setTenantCode(tenantCode);
}

public static void setServiceName(String serviceName) {
getOrCreateContext().setServiceName(serviceName);
}

public static String getTenantCode() {
return getOrCreateContext().getTenantCode();
}

public static void setAll(TenantContext tenantContext) {
public static String getServiceName() {
return getOrCreateContext().getServiceName();
}

public static void setContext(TenantContext tenantContext) {
LOCAL.set(tenantContext);
}

public static TenantContext getAll() {
public static TenantContext getContext() {
return LOCAL.get();
}

public static TenantContext getCopyOfAll() {
public static TenantContext getCopyOfContext() {
TenantContext tenantContext = LOCAL.get();
if (tenantContext == null) {
return null;
}
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(objectMapper.writeValueAsString(tenantContext), TenantContext.class);
} catch (Exception e) {
LOGGER.error("Failed to get copy of all", e);
return null;
}

return tenantContext.deepClone();
}

public static void clearAll() {
public static void clear() {
LOCAL.remove();
}

Expand Down
Loading