Skip to content

feat: add trace report support #1322

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

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions spring-cloud-starter-tencent-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-contract</artifactId>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-trace-plugin</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-tencent-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,10 @@ public static final class Modifier {
* Order of service contract configuration modifier.
*/
public static Integer SERVICE_CONTRACT_ORDER = Integer.MAX_VALUE - 9;

/**
* Order of trace configuration modifier.
*/
public static Integer TRACE_ORDER = 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void accept(String s, MetadataValue metadataValue) {
return values;
}

private void putMetadataAsMap(MetadataType metadataType, TransitiveType transitiveType, boolean downstream, Map<String, String> values) {
public void putMetadataAsMap(MetadataType metadataType, TransitiveType transitiveType, boolean downstream, Map<String, String> values) {
MetadataContainer metadataContainer = getMetadataContainer(metadataType, downstream);
for (Map.Entry<String, String> entry : values.entrySet()) {
metadataContainer.putMetadataStringValue(entry.getKey(), entry.getValue(), transitiveType);
Expand Down Expand Up @@ -258,7 +258,7 @@ public Map<String, String> getFragmentContext(String fragment) {
case FRAGMENT_RAW_TRANSHEADERS:
return getMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS, TransitiveType.NONE, false);
case FRAGMENT_RAW_TRANSHEADERS_KV:
return getMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS_KV, TransitiveType.PASS_THROUGH, false);
return getMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS_KV, TransitiveType.NONE, false);
default:
return getMapMetadataAsMap(MetadataType.CUSTOM, fragment, TransitiveType.NONE, false);
}
Expand Down Expand Up @@ -293,7 +293,7 @@ public void putFragmentContext(String fragment, Map<String, String> context) {
putMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS, TransitiveType.NONE, false, context);
break;
case FRAGMENT_RAW_TRANSHEADERS_KV:
putMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS_KV, TransitiveType.PASS_THROUGH, false, context);
putMapMetadataAsMap(MetadataType.CUSTOM, FRAGMENT_RAW_TRANSHEADERS_KV, TransitiveType.NONE, false, context);
break;
default:
putMapMetadataAsMap(MetadataType.CUSTOM, fragment, TransitiveType.NONE, false, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/

package org.springframework.tsf.core.context;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;

import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.polaris.metadata.core.MetadataType;
import com.tencent.polaris.metadata.core.TransitiveType;

import org.springframework.tsf.core.entity.Tag;

public final class TsfContext {

static final int MAX_KEY_LENGTH = 32;
static final int MAX_VALUE_LENGTH = 128;

private TsfContext() {

}

public static void putTags(Map<String, String> tagMap, Tag.ControlFlag... flags) {
if (tagMap == null) {
return;
}
MetadataContext tsfCoreContext = MetadataContextHolder.get();
TransitiveType transitive = TransitiveType.NONE;
if (null != flags) {
for (Tag.ControlFlag flag : flags) {
if (flag == Tag.ControlFlag.TRANSITIVE) {
transitive = TransitiveType.PASS_THROUGH;
break;
}
}
}
for (Map.Entry<String, String> entry : tagMap.entrySet()) {
validateTag(entry.getKey(), entry.getValue());
}
tsfCoreContext.putMetadataAsMap(MetadataType.CUSTOM, transitive, false, tagMap);
}

public static void putTag(String key, String value, Tag.ControlFlag... flags) {
putTags(Collections.singletonMap(key, value), flags);
}

private static void validateTag(String key, String value) {
int keyLength = key.getBytes(StandardCharsets.UTF_8).length;
int valueLength = value.getBytes(StandardCharsets.UTF_8).length;

if (keyLength > MAX_KEY_LENGTH) {
throw new RuntimeException(String.format("Key \"%s\" length (after UTF-8 encoding) exceeding limit (%d)", key,
MAX_KEY_LENGTH));
}
if (valueLength > MAX_VALUE_LENGTH) {
throw new RuntimeException(String.format("Value \"%s\" length (after UTF-8 encoding) exceeding limit (%d)", value,
MAX_VALUE_LENGTH));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/

package org.springframework.tsf.core.entity;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Tag implements Serializable {

/**
* update version whenever change the content in tag.
*/
public static final int VERSION = 1;

public enum ControlFlag {

/**
* tag transitive by all services.
*/
@SerializedName("0")
TRANSITIVE,

/**
* tag not used in auth.
*/
@SerializedName("1")
NOT_IN_AUTH,

/**
* tag not used in route.
*/
@SerializedName("2")
NOT_IN_ROUTE,

/**
* tag not used in trace.
*/
@SerializedName("3")
NOT_IN_SLEUTH,

/**
* tag not used in lane.
*/
@SerializedName("4")
NOT_IN_LANE,

/**
* tag not used in unit.
*/
@SerializedName("5")
IN_UNIT
}

@SerializedName("k")
@Expose
private String key;

@SerializedName("v")
@Expose
private String value;

@SerializedName("f")
@Expose
private Set<ControlFlag> flags = new HashSet<>();

public Tag(String key, String value, ControlFlag... flags) {
this.key = key;
this.value = value;
this.flags = new HashSet<>(Arrays.asList(flags));
}

public Tag() {
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Set<ControlFlag> getFlags() {
return flags;
}

public void setFlags(Set<ControlFlag> flags) {
this.flags = flags;
}

@Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Tag tag = (Tag) object;
return (key == null ? tag.key == null : key.equals(tag.key))
&& (flags == null ? tag.flags == null : flags.equals(tag.flags));
}
return false;
}

@Override
public int hashCode() {
return (key == null ? 0 : key.hashCode()) + (flags == null ? 0 : flags.hashCode());
}


@Override
public String toString() {
return "Tag{" +
"key='" + key + '\'' +
", value='" + value + '\'' +
", flags=" + flags +
'}';
}
}
6 changes: 6 additions & 0 deletions spring-cloud-tencent-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-trace-plugin</artifactId>
<version>${revision}</version>
</dependency>

<!-- third part framework dependencies -->
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.0.0-RC1</version>
<version>2022.0.0.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package com.tencent.cloud.quickstart.caller;

import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -63,6 +66,8 @@ public class QuickstartCallerController {
*/
@GetMapping("/feign")
public String feign(@RequestParam int value1, @RequestParam int value2) {
MetadataContext metadataContext = MetadataContextHolder.get();
metadataContext.setTransitiveMetadata(Collections.singletonMap("feign-trace", String.format("%d+%d", value1, value2)));
return quickstartCalleeService.sum(value1, value2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

package com.tencent.cloud.tsf.demo.consumer.controller;

import java.util.HashMap;
import java.util.Map;

import com.tencent.cloud.tsf.demo.consumer.proxy.ProviderDemoService;
import com.tencent.cloud.tsf.demo.consumer.proxy.ProviderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.tsf.core.context.TsfContext;
import org.springframework.tsf.core.entity.Tag;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -38,16 +43,31 @@ public class ConsumerController {

@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
public String restProvider(@PathVariable String str) {
TsfContext.putTag("operation", "rest");
Map<String, String> mTags = new HashMap<>();
mTags.put("rest-trace-key1", "value1");
mTags.put("rest-trace-key2", "value2");
TsfContext.putTags(mTags, Tag.ControlFlag.TRANSITIVE);
return restTemplate.getForObject("http://provider-demo/echo/" + str, String.class);
}

@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
public String feignProvider(@PathVariable String str) {
TsfContext.putTag("operation", "feign");
Map<String, String> mTags = new HashMap<>();
mTags.put("feign-trace-key1", "value1");
mTags.put("feign-trace-key2", "value2");
TsfContext.putTags(mTags, Tag.ControlFlag.TRANSITIVE);
return providerDemoService.echo(str);
}

@RequestMapping(value = "/echo-feign-url/{str}", method = RequestMethod.GET)
public String feignUrlProvider(@PathVariable String str) {
TsfContext.putTag("operation", "feignUrl");
Map<String, String> mTags = new HashMap<>();
mTags.put("feignUrl-trace-key1", "value1");
mTags.put("feignUrl-trace-key2", "value2");
TsfContext.putTags(mTags, Tag.ControlFlag.TRANSITIVE);
return providerService.echo(str);
}
}
1 change: 1 addition & 0 deletions spring-cloud-tencent-plugin-starters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>spring-cloud-starter-tencent-discovery-adapter-plugin</module>
<module>spring-cloud-tencent-lossless-plugin</module>
<module>spring-cloud-starter-tencent-threadlocal-plugin</module>
<module>spring-cloud-tencent-trace-plugin</module>
</modules>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.0.0-RC1</version>
<version>2022.0.0.0</version>
<optional>true</optional>
</dependency>

Expand Down
Loading
Loading