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

fix: ignore tag injection when the key exists #1677

Merged
merged 1 commit into from
Nov 26, 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
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
Loading