feat:add metadata transfer for http header via spring.cloud.tencent.metadata.headers. (#1175)

Co-authored-by: wenxuan70 <t736660416@gmail.com>
pull/1179/head
Haotian Zhang 1 year ago committed by GitHub
parent 050c35b522
commit f48b1b231d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,3 +13,4 @@
- [fix:fix retry loadbalancer not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1156) - [fix:fix retry loadbalancer not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1156)
- [fix:fix header validation when using Chinese char.](https://github.com/Tencent/spring-cloud-tencent/pull/1168) - [fix:fix header validation when using Chinese char.](https://github.com/Tencent/spring-cloud-tencent/pull/1168)
- [feat: add circuit breaker actuator.](https://github.com/Tencent/spring-cloud-tencent/pull/1171) - [feat: add circuit breaker actuator.](https://github.com/Tencent/spring-cloud-tencent/pull/1171)
- [feat: add metadata transfer for http header via spring.cloud.tencent.metadata.headers.](https://github.com/Tencent/spring-cloud-tencent/pull/1175)

@ -68,8 +68,8 @@ public final class MetadataContextHolder {
metadataContext.setTransitiveMetadata(staticMetadataManager.getMergedStaticTransitiveMetadata()); metadataContext.setTransitiveMetadata(staticMetadataManager.getMergedStaticTransitiveMetadata());
metadataContext.setDisposableMetadata(staticMetadataManager.getMergedStaticDisposableMetadata()); metadataContext.setDisposableMetadata(staticMetadataManager.getMergedStaticDisposableMetadata());
if (StringUtils.hasText(staticMetadataManager.getTransHeaderFromEnv())) { if (StringUtils.hasText(staticMetadataManager.getTransHeader())) {
metadataContext.setTransHeaders(staticMetadataManager.getTransHeaderFromEnv(), ""); metadataContext.setTransHeaders(staticMetadataManager.getTransHeader(), "");
} }
METADATA_CONTEXT.set(metadataContext); METADATA_CONTEXT.set(metadataContext);

@ -17,8 +17,11 @@
package com.tencent.cloud.common.metadata; package com.tencent.cloud.common.metadata;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -71,6 +74,7 @@ public class StaticMetadataManager {
private Map<String, String> configMetadata; private Map<String, String> configMetadata;
private Map<String, String> configTransitiveMetadata; private Map<String, String> configTransitiveMetadata;
private Map<String, String> configDisposableMetadata; private Map<String, String> configDisposableMetadata;
private String configTransHeaders;
private Map<String, String> customSPIMetadata; private Map<String, String> customSPIMetadata;
private Map<String, String> customSPITransitiveMetadata; private Map<String, String> customSPITransitiveMetadata;
private Map<String, String> customSPIDisposableMetadata; private Map<String, String> customSPIDisposableMetadata;
@ -162,6 +166,7 @@ public class StaticMetadataManager {
Map<String, String> allMetadata = metadataLocalProperties.getContent(); Map<String, String> allMetadata = metadataLocalProperties.getContent();
List<String> transitiveKeys = metadataLocalProperties.getTransitive(); List<String> transitiveKeys = metadataLocalProperties.getTransitive();
List<String> disposableKeys = metadataLocalProperties.getDisposable(); List<String> disposableKeys = metadataLocalProperties.getDisposable();
List<String> headers = metadataLocalProperties.getHeaders();
Map<String, String> transitiveResult = new HashMap<>(); Map<String, String> transitiveResult = new HashMap<>();
for (String key : transitiveKeys) { for (String key : transitiveKeys) {
@ -179,6 +184,7 @@ public class StaticMetadataManager {
configTransitiveMetadata = Collections.unmodifiableMap(transitiveResult); configTransitiveMetadata = Collections.unmodifiableMap(transitiveResult);
configDisposableMetadata = Collections.unmodifiableMap(disposableResult); configDisposableMetadata = Collections.unmodifiableMap(disposableResult);
configTransHeaders = CollectionUtils.isEmpty(headers) ? null : String.join(",", headers);
configMetadata = Collections.unmodifiableMap(allMetadata); configMetadata = Collections.unmodifiableMap(allMetadata);
} }
@ -313,6 +319,29 @@ public class StaticMetadataManager {
return envNotReportMetadata.get(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS); return envNotReportMetadata.get(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS);
} }
public String getTransHeaderFromConfig() {
return configTransHeaders;
}
public String getTransHeader() {
Set<String> transHeaderSet = new HashSet<>();
String transHeaderFromEnv = getTransHeaderFromEnv();
String transHeaderFromConfig = getTransHeaderFromConfig();
Set<String> transHeaderFromEnvSet = StringUtils.isNotBlank(transHeaderFromEnv)
? Arrays.stream(transHeaderFromEnv.split(",")).collect(Collectors.toSet())
: Collections.emptySet();
Set<String> transHeaderFromConfigSet = StringUtils.isNotBlank(transHeaderFromConfig)
? Arrays.stream(transHeaderFromConfig.split(",")).collect(Collectors.toSet())
: Collections.emptySet();
transHeaderSet.addAll(transHeaderFromEnvSet);
transHeaderSet.addAll(transHeaderFromConfigSet);
return new ArrayList<>(transHeaderSet).stream().sorted().collect(Collectors.joining(","));
}
public Map<String, String> getEnvTransitiveMetadata() { public Map<String, String> getEnvTransitiveMetadata() {
return envTransitiveMetadata; return envTransitiveMetadata;
} }
@ -390,6 +419,7 @@ public class StaticMetadataManager {
", envTransitiveMetadata=" + envTransitiveMetadata + ", envTransitiveMetadata=" + envTransitiveMetadata +
", configMetadata=" + configMetadata + ", configMetadata=" + configMetadata +
", configTransitiveMetadata=" + configTransitiveMetadata + ", configTransitiveMetadata=" + configTransitiveMetadata +
", configTransHeaders='" + configTransHeaders + '\'' +
", customSPIMetadata=" + customSPIMetadata + ", customSPIMetadata=" + customSPIMetadata +
", customSPITransitiveMetadata=" + customSPITransitiveMetadata + ", customSPITransitiveMetadata=" + customSPITransitiveMetadata +
", mergedStaticMetadata=" + mergedStaticMetadata + ", mergedStaticMetadata=" + mergedStaticMetadata +

@ -48,6 +48,11 @@ public class MetadataLocalProperties {
*/ */
private List<String> disposable; private List<String> disposable;
/**
* A transitive http header key list.
*/
private List<String> headers;
public Map<String, String> getContent() { public Map<String, String> getContent() {
if (CollectionUtils.isEmpty(content)) { if (CollectionUtils.isEmpty(content)) {
content = new HashMap<>(); content = new HashMap<>();
@ -80,4 +85,15 @@ public class MetadataLocalProperties {
public void setDisposable(List<String> disposable) { public void setDisposable(List<String> disposable) {
this.disposable = disposable; this.disposable = disposable;
} }
public List<String> getHeaders() {
if (CollectionUtils.isEmpty(headers)) {
headers = new ArrayList<>();
}
return headers;
}
public void setHeaders(List<String> headers) {
this.headers = headers;
}
} }

@ -14,6 +14,11 @@
"name": "spring.cloud.tencent.metadata.disposable", "name": "spring.cloud.tencent.metadata.disposable",
"type": "java.util.List", "type": "java.util.List",
"description": "Custom disposable metadata key list." "description": "Custom disposable metadata key list."
},
{
"name": "spring.cloud.tencent.metadata.headers",
"type": "java.util.List",
"description": "Custom transitive http header key list."
} }
] ]
} }

@ -52,6 +52,10 @@ public class MetadataContextHolderTest {
Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1");
Assertions.assertThat(customMetadata.get("b")).isEqualTo("2"); Assertions.assertThat(customMetadata.get("b")).isEqualTo("2");
Map<String, String> transHeaders = MetadataContextHolder.get().getTransHeaders();
Assertions.assertThat(transHeaders.size()).isEqualTo(1);
Assertions.assertThat(transHeaders.keySet().iterator().next()).isEqualTo("c,d");
MetadataContextHolder.remove(); MetadataContextHolder.remove();
customMetadata = new HashMap<>(); customMetadata = new HashMap<>();
@ -65,6 +69,10 @@ public class MetadataContextHolderTest {
Assertions.assertThat(customMetadata.get("b")).isEqualTo("22"); Assertions.assertThat(customMetadata.get("b")).isEqualTo("22");
Assertions.assertThat(customMetadata.get("c")).isEqualTo("3"); Assertions.assertThat(customMetadata.get("c")).isEqualTo("3");
Assertions.assertThat(MetadataContext.LOCAL_NAMESPACE).isEqualTo("default"); Assertions.assertThat(MetadataContext.LOCAL_NAMESPACE).isEqualTo("default");
transHeaders = MetadataContextHolder.get().getTransHeaders();
Assertions.assertThat(transHeaders.size()).isEqualTo(1);
Assertions.assertThat(transHeaders.keySet().iterator().next()).isEqualTo("c,d");
} }
@Test @Test

@ -97,6 +97,7 @@ public class StaticMetadataManagerTest {
when(metadataLocalProperties.getContent()).thenReturn(content); when(metadataLocalProperties.getContent()).thenReturn(content);
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1")); when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
when(metadataLocalProperties.getDisposable()).thenReturn(Collections.singletonList("k1")); when(metadataLocalProperties.getDisposable()).thenReturn(Collections.singletonList("k1"));
when(metadataLocalProperties.getHeaders()).thenReturn(Arrays.asList("a", "d"));
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null); StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null);
@ -119,6 +120,9 @@ public class StaticMetadataManagerTest {
Map<String, String> locationInfo = metadataManager.getLocationMetadata(); Map<String, String> locationInfo = metadataManager.getLocationMetadata();
assertThat(locationInfo.get("zone")).isEqualTo("zone1"); assertThat(locationInfo.get("zone")).isEqualTo("zone1");
assertThat(locationInfo.get("region")).isEqualTo("region1"); assertThat(locationInfo.get("region")).isEqualTo("region1");
String transHeaderFromConfig = metadataManager.getTransHeaderFromConfig();
assertThat(transHeaderFromConfig).isEqualTo("a,d");
} }
@Test @Test
@ -161,6 +165,9 @@ public class StaticMetadataManagerTest {
@Test @Test
public void testMergedMetadata() { public void testMergedMetadata() {
// set environment variables
environmentVariables.set("SCT_TRAFFIC_CONTENT_RAW_TRANSHEADERS", "a,b,c,e");
Map<String, String> content = new HashMap<>(); Map<String, String> content = new HashMap<>();
content.put("k1", "v1"); content.put("k1", "v1");
content.put("k2", "v2"); content.put("k2", "v2");
@ -170,6 +177,7 @@ public class StaticMetadataManagerTest {
when(metadataLocalProperties.getContent()).thenReturn(content); when(metadataLocalProperties.getContent()).thenReturn(content);
when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1")); when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1"));
when(metadataLocalProperties.getHeaders()).thenReturn(Arrays.asList("b", "d"));
StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties,
Arrays.asList(new MockedMetadataProvider(), new DefaultInstanceMetadataProvider(null))); Arrays.asList(new MockedMetadataProvider(), new DefaultInstanceMetadataProvider(null)));
@ -204,6 +212,9 @@ public class StaticMetadataManagerTest {
assertThat(locationInfo.get("zone")).isEqualTo("zone2"); assertThat(locationInfo.get("zone")).isEqualTo("zone2");
assertThat(locationInfo.get("region")).isEqualTo("region1"); assertThat(locationInfo.get("region")).isEqualTo("region1");
assertThat(locationInfo.get("campus")).isEqualTo("campus2"); assertThat(locationInfo.get("campus")).isEqualTo("campus2");
String transHeader = metadataManager.getTransHeader();
assertThat(transHeader).isEqualTo("a,b,c,d,e");
} }
@Test @Test

@ -55,6 +55,12 @@ public class MetadataLocalPropertiesTest {
.isTrue(); .isTrue();
} }
@Test
public void test3() {
Assertions.assertThat(metadataLocalProperties.getHeaders().contains("c")).isTrue();
Assertions.assertThat(metadataLocalProperties.getHeaders().contains("d")).isTrue();
}
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {

@ -14,3 +14,6 @@ spring:
b: 2 b: 2
transitive: transitive:
- b - b
headers:
- c
- d

Loading…
Cancel
Save