Skip to content

Commit

Permalink
【fix】ignore tag injection when the key exists
Browse files Browse the repository at this point in the history
Signed-off-by: lilai <[email protected]>
  • Loading branch information
lilai23 committed Nov 25, 2024
1 parent f6f66b0 commit 36d34b0
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ protected void injectTrafficTag2Carrier(RpcInvocation invocation) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original attachment contains the specific key, then ignore
String originalAttachment = invocation.getAttachment(key);
if (originalAttachment != null) {
continue;
}

List<String> values = entry.getValue();

// If the tag value is not null on the provider side, it will be converted to list storage. If it is null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ protected void injectTrafficTag2Carrier(RpcInvocation invocation) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original attachment contains the specific key, then ignore
String originalAttachment = invocation.getAttachment(key);
if (originalAttachment != null) {
continue;
}

List<String> values = entry.getValue();

// If the tag value is not null on the provider side, it will be converted to list storage. If it is null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ protected void injectTrafficTag2Carrier(RpcInvocation invocation) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original attachment contains the specific key, then ignore
Object originalAttachment = invocation.getObjectAttachment(key);
if (originalAttachment != null) {
continue;
}

List<String> values = entry.getValue();

// If the tag value is not null on the provider side, it will be converted to list storage. If it is null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ protected void injectTrafficTag2Carrier(Metadata header) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original metadata contains the specific key, then ignore
String originalHeaderValue = header.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
if (originalHeaderValue != null) {
continue;
}

List<String> values = entry.getValue();

// The server side converts the label value to list storage when it is not null. If it is null, it directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.sermant.tag.transmission.config.strategy.TagKeyMatcher;
import io.sermant.tag.transmission.interceptors.AbstractClientInterceptor;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;

import java.util.List;
Expand Down Expand Up @@ -66,6 +67,13 @@ protected void injectTrafficTag2Carrier(HttpMethod httpMethod) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original headers contains the specific key, then ignore
Header originalRequestHeader = httpMethod.getRequestHeader(key);
if (originalRequestHeader != null) {
continue;
}

List<String> values = entry.getValue();

// The server side converts the label value to list storage when it is not null. If it is null, it directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.sermant.tag.transmission.config.strategy.TagKeyMatcher;
import io.sermant.tag.transmission.interceptors.AbstractClientInterceptor;

import org.apache.http.Header;
import org.apache.http.HttpRequest;

import java.util.List;
Expand Down Expand Up @@ -66,6 +67,13 @@ protected void injectTrafficTag2Carrier(HttpRequest httpRequest) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original headers contains the specific key, then ignore
Header[] originalHeaders = httpRequest.getHeaders(key);
if (originalHeaders != null && originalHeaders.length != 0) {
continue;
}

List<String> values = entry.getValue();

// The server side converts the label value to list storage when it is not null. If it is null, it directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ protected void injectTrafficTag2Carrier(MessageHeader messageHeader) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original headers contains the specific key, then ignore
Map<String, List<String>> originalHeaders = messageHeader.getHeaders();
if (!CollectionUtils.isEmpty(originalHeaders.get(key))) {
continue;
}

List<String> values = entry.getValue();

// The server side converts the label value to list storage when it is not null. If it is null, it directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.sermant.tag.transmission.interceptors.AbstractClientInterceptor;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.Headers;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -69,6 +70,13 @@ protected void injectTrafficTag2Carrier(ProducerRecord<?, ?> producerRecord) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original headers contains the specific key, then ignore
Header originalHeader = headers.lastHeader(key);
if (originalHeader != null) {
continue;
}

List<String> values = entry.getValue();

// On the producer side, if the tag value is not null, it is converted to list storage. If it is null, it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@

package io.sermant.tag.transmission.okhttpv2.interceptors;

import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Request.Builder;

import io.sermant.core.common.LoggerFactory;
import io.sermant.core.plugin.agent.entity.ExecuteContext;
import io.sermant.core.utils.CollectionUtils;
import io.sermant.core.utils.ReflectUtils;
import io.sermant.core.utils.tag.TrafficUtils;
import io.sermant.tag.transmission.config.strategy.TagKeyMatcher;
import io.sermant.tag.transmission.interceptors.AbstractClientInterceptor;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -75,6 +78,17 @@ protected void injectTrafficTag2Carrier(Builder builder) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original headers contains the specific key, then ignore
Optional<Object> headersBuilderObject = ReflectUtils.getFieldValue(builder, "headers");
if (headersBuilderObject.isPresent()) {
Headers.Builder headersBuilder = (Headers.Builder) headersBuilderObject.get();
String value = headersBuilder.get(key);
if (value != null) {
continue;
}
}

List<String> values = entry.getValue();

// The server side converts the label value to list storage when it is not null. If it is null, it directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ protected void injectTrafficTag2Carrier(SendMessageRequestHeader header) {
}

/**
* Insert the traffic tag into the properties string. The format of the original properties is as
* follows: key1[SOH]value1[STX]key2[SOH]value2, [SOH] is the symbol of ASCII=1, [STX] is the symbol of ASCII=2
* Insert the traffic tag into the properties string. The format of the original properties is as follows:
* key1[SOH]value1[STX]key2[SOH]value2, [SOH] is the symbol of ASCII=1, [STX] is the symbol of ASCII=2
*
* @param oldProperties original properties
* @return String
Expand All @@ -88,6 +88,12 @@ private String insertTags2Properties(String oldProperties) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original properties contains the specific key, then ignore
if (oldProperties.contains(key)) {
continue;
}

List<String> values = entry.getValue();
newProperties.append(key);
newProperties.append(LINK_MARK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ protected void injectTrafficTag2Carrier(SendMessageRequestHeader header) {
}

/**
* Insert the traffic tag into the properties string. The format of the original properties is as
* follows: key1[SOH]value1[STX]key2[SOH]value2, [SOH] is the symbol of ASCII=1, [STX] is the symbol of ASCII=2
* Insert the traffic tag into the properties string. The format of the original properties is as follows:
* key1[SOH]value1[STX]key2[SOH]value2, [SOH] is the symbol of ASCII=1, [STX] is the symbol of ASCII=2
*
* @param oldProperties original properties
* @return String
Expand All @@ -88,6 +88,12 @@ private String insertTags2Properties(String oldProperties) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original properties contains the specific key, then ignore
if (oldProperties.contains(key)) {
continue;
}

List<String> values = entry.getValue();
newProperties.append(key);
newProperties.append(LINK_MARK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ protected void injectTrafficTag2Carrier(Invocation invocation) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original headers contains the specific key, then ignore
String originalHeader = invocation.getContext().get(key);
if (originalHeader != null) {
continue;
}

List<String> values = entry.getValue();

// If the tag value is not null on the provider side, it will be converted to list storage. If it is null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ protected void injectTrafficTag2Carrier(SofaRequest sofaRequest) {
if (!TagKeyMatcher.isMatch(key)) {
continue;
}

// if original headers contains the specific key, then ignore
Object originalHeader = sofaRequest.getRequestProp(key);
if (originalHeader != null) {
continue;
}

List<String> values = entry.getValue();

// The server side converts the label value to list storage when it is not null. If it is null, it directly
Expand Down

0 comments on commit 36d34b0

Please sign in to comment.