-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from moxygen2001/main
Support W3C Baggage specification
- Loading branch information
Showing
10 changed files
with
314 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ve-governance-api/src/main/java/com/jd/live/agent/governance/context/LivePropagation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.jd.live.agent.governance.context; | ||
|
||
import com.jd.live.agent.core.extension.annotation.Extension; | ||
import com.jd.live.agent.core.inject.annotation.Inject; | ||
import com.jd.live.agent.core.inject.annotation.Injectable; | ||
import com.jd.live.agent.governance.context.bag.Cargo; | ||
import com.jd.live.agent.governance.context.bag.CargoRequire; | ||
import com.jd.live.agent.governance.context.bag.CargoRequires; | ||
import com.jd.live.agent.governance.context.bag.Carrier; | ||
import com.jd.live.agent.governance.request.HeaderReader; | ||
import com.jd.live.agent.governance.request.HeaderWriter; | ||
|
||
import java.util.*; | ||
|
||
@Injectable | ||
@Extension("LivePropagation") | ||
public class LivePropagation implements Propagation { | ||
private final CargoRequires require; | ||
@Inject | ||
private List<CargoRequire> requires; | ||
|
||
{ | ||
this.require = new CargoRequires(requires); | ||
} | ||
|
||
@Override | ||
public void write(Carrier carrier, HeaderWriter writer) { | ||
Collection<Cargo> cargos = carrier.getCargos(); | ||
if (cargos != null) { | ||
List<String> values; | ||
for (Cargo cargo : cargos) { | ||
values = cargo.getValues(); | ||
int size = values == null ? 0 : values.size(); | ||
switch (size) { | ||
case 0: | ||
writer.setHeader(cargo.getKey(), null); | ||
break; | ||
case 1: | ||
writer.setHeader(cargo.getKey(), values.get(0)); | ||
break; | ||
default: | ||
for (String value : values) { | ||
writer.setHeader(cargo.getKey(), value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void read(Carrier carrier, HeaderReader reader) { | ||
Iterator<String> headerNames = reader.getHeaderNames(); | ||
while (headerNames.hasNext()) { | ||
String headerName = headerNames.next(); | ||
List<String> headerValues = reader.getHeaders(headerName); | ||
if (require.match(headerName) && headerValues != null && !headerValues.isEmpty()) { | ||
carrier.addCargo(new Cargo(headerName, new ArrayList<>(headerValues))); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...oylive-governance-api/src/main/java/com/jd/live/agent/governance/context/Propagation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.jd.live.agent.governance.context; | ||
|
||
import com.jd.live.agent.governance.context.bag.Carrier; | ||
import com.jd.live.agent.governance.request.HeaderReader; | ||
import com.jd.live.agent.governance.request.HeaderWriter; | ||
|
||
public interface Propagation { | ||
void write(Carrier carrier, HeaderWriter writer); | ||
|
||
void read(Carrier carrier, HeaderReader reader); | ||
} |
94 changes: 94 additions & 0 deletions
94
...ernance-api/src/main/java/com/jd/live/agent/governance/context/W3cBaggagePropagation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.jd.live.agent.governance.context; | ||
|
||
import com.jd.live.agent.bootstrap.logger.Logger; | ||
import com.jd.live.agent.bootstrap.logger.LoggerFactory; | ||
import com.jd.live.agent.core.extension.annotation.Extension; | ||
import com.jd.live.agent.core.inject.annotation.Inject; | ||
import com.jd.live.agent.core.inject.annotation.Injectable; | ||
import com.jd.live.agent.governance.context.bag.Cargo; | ||
import com.jd.live.agent.governance.context.bag.CargoRequire; | ||
import com.jd.live.agent.governance.context.bag.CargoRequires; | ||
import com.jd.live.agent.governance.context.bag.Carrier; | ||
import com.jd.live.agent.governance.request.HeaderReader; | ||
import com.jd.live.agent.governance.request.HeaderWriter; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.net.URLEncoder; | ||
import java.util.*; | ||
|
||
@Injectable | ||
@Extension("W3cBaggagePropagation") | ||
public class W3cBaggagePropagation implements Propagation { | ||
private static final Logger logger = LoggerFactory.getLogger(W3cBaggagePropagation.class); | ||
|
||
private final CargoRequires require; | ||
@Inject | ||
private List<CargoRequire> requires; | ||
|
||
{ | ||
this.require = new CargoRequires(requires); | ||
} | ||
|
||
@Override | ||
public void write(Carrier carrier, HeaderWriter writer) { | ||
StringBuilder baggage = new StringBuilder(); | ||
Collection<Cargo> cargos = carrier.getCargos(); | ||
|
||
if (cargos != null) { | ||
for (Cargo cargo : cargos) { | ||
if (cargo.getValues() != null) { | ||
List<String> values = new ArrayList<>(); | ||
for (String value : cargo.getValues()) { | ||
try { | ||
String encodedValue = URLEncoder.encode(value, "UTF-8"); | ||
values.add(encodedValue); | ||
} catch (UnsupportedEncodingException e) { | ||
logger.error("URL encoding failed for value: " + value, e); | ||
} | ||
} | ||
|
||
if (!values.isEmpty()) { | ||
baggage.append(cargo.getKey()).append("=") | ||
.append(String.join(";", values)).append(","); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (baggage.length() > 0) { | ||
baggage.setLength(baggage.length() - 1); | ||
writer.setHeader("baggage", baggage.toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public void read(Carrier carrier, HeaderReader reader) { | ||
String baggage = reader.getHeader("baggage"); | ||
if (baggage == null || baggage.isEmpty()) | ||
return; | ||
|
||
String[] keyValuePairs = baggage.split(","); | ||
for (String pair : keyValuePairs) { | ||
String[] keyValue = pair.split("=", 2); | ||
if (keyValue.length == 2) { | ||
String headerName = keyValue[0]; | ||
if (require.match(headerName)) { | ||
List<String> headerValues = new ArrayList<>(); | ||
String[] values = keyValue[1].split(";"); | ||
for (String value : values) { | ||
try { | ||
String decodedValue = URLDecoder.decode(value, "UTF-8"); | ||
headerValues.add(decodedValue); | ||
} catch (UnsupportedEncodingException e) { | ||
System.err.println("URL decoding failed for value: " + value); | ||
} | ||
} | ||
|
||
if (!headerValues.isEmpty()) { | ||
carrier.addCargo(new Cargo(headerName, headerValues)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...ylive-governance-api/src/main/java/com/jd/live/agent/governance/request/HeaderReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.jd.live.agent.governance.request; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public interface HeaderReader { | ||
Iterator<String> getHeaderNames(); | ||
|
||
List<String> getHeaders(String key); | ||
|
||
default String getHeader(String key) { | ||
List<String> values = getHeaders(key); | ||
return values.isEmpty() ? null : values.get(0); | ||
} | ||
|
||
default List<String> getHeader(String key, Function<String, List<String>> func) { | ||
String value = getHeader(key); | ||
return value == null ? null : func.apply(value); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ylive-governance-api/src/main/java/com/jd/live/agent/governance/request/HeaderWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.jd.live.agent.governance.request; | ||
|
||
public interface HeaderWriter { | ||
void setHeader(String key, String value); | ||
} |
31 changes: 31 additions & 0 deletions
31
...ive-governance-api/src/main/java/com/jd/live/agent/governance/request/MultiMapHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.jd.live.agent.governance.request; | ||
|
||
import java.util.*; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Supplier; | ||
|
||
public class MultiMapHeader implements HeaderReader, HeaderWriter { | ||
private Map<String, List<String>> map; | ||
|
||
private BiConsumer<String, String> setHeader; | ||
|
||
public MultiMapHeader(Supplier<Map<String, List<String>>> getHeader, BiConsumer<String, String> setHeader) { | ||
this.setHeader = setHeader; | ||
this.map = getHeader.get() == null ? new HashMap<>() : getHeader.get(); | ||
} | ||
|
||
@Override | ||
public Iterator<String> getHeaderNames() { | ||
return map.keySet().iterator(); | ||
} | ||
|
||
@Override | ||
public List<String> getHeaders(String key) { | ||
return map.get(key) == null ? null : map.get(key); | ||
} | ||
|
||
@Override | ||
public void setHeader(String key, String value) { | ||
setHeader.accept(key, value); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ve-governance-api/src/main/java/com/jd/live/agent/governance/request/ObjectMapHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.jd.live.agent.governance.request; | ||
|
||
import com.jd.live.agent.core.util.tag.Label; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Supplier; | ||
|
||
public class ObjectMapHeader implements HeaderReader, HeaderWriter { | ||
private Map<String, Object> map; | ||
|
||
private BiConsumer<String, String> setHeader; | ||
|
||
public ObjectMapHeader(Supplier<Map<String, Object>> getHeader, BiConsumer<String, String> setHeader) { | ||
this.setHeader = setHeader; | ||
this.map = getHeader.get() == null ? new HashMap<>() : getHeader.get(); | ||
} | ||
|
||
@Override | ||
public Iterator<String> getHeaderNames() { | ||
return map.keySet().iterator(); | ||
} | ||
|
||
@Override | ||
public List<String> getHeaders(String key) { | ||
Object value = map.get(key); | ||
return value == null ? null : Label.parseValue(value.toString()); | ||
} | ||
|
||
@Override | ||
public void setHeader(String key, String value) { | ||
setHeader.accept(key, value); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ve-governance-api/src/main/java/com/jd/live/agent/governance/request/StringMapHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.jd.live.agent.governance.request; | ||
|
||
import java.util.*; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Supplier; | ||
|
||
public class StringMapHeader implements HeaderReader, HeaderWriter { | ||
private Map<String, String> map; | ||
|
||
private BiConsumer<String, String> setHeader; | ||
|
||
public StringMapHeader(Supplier<Map<String, String>> getHeader, BiConsumer<String, String> setHeader) { | ||
this.setHeader = setHeader; | ||
this.map = getHeader.get() == null ? new HashMap<>() : getHeader.get(); | ||
} | ||
|
||
@Override | ||
public Iterator<String> getHeaderNames() { | ||
return map.keySet().iterator(); | ||
} | ||
|
||
@Override | ||
public List<String> getHeaders(String key) { | ||
return map.get(key) == null ? null : Collections.singletonList(map.get(key)); | ||
} | ||
|
||
@Override | ||
public void setHeader(String key, String value) { | ||
setHeader.accept(key, value); | ||
} | ||
} |