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

Supports query and cookie matching for RPC calls #102

Merged
merged 1 commit into from
Oct 18, 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 @@ -21,6 +21,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

import static com.jd.live.agent.core.util.type.TypeScanner.UNTIL_OBJECT;
Expand All @@ -31,6 +32,8 @@
*/
public class ValuePath implements ObjectGetter {

private static final Map<String, ValuePath> VALUE_PATHS = new ConcurrentHashMap<>();

@Getter
protected final String path;

Expand Down Expand Up @@ -59,6 +62,16 @@ public ValuePath(String path, Predicate<Object> predicate) {
this.paths = parse(path);
}

/**
* Creates a new ValuePath instance with the specified path.
*
* @param path the path to the value
* @return a new ValuePath instance
*/
public static ValuePath of(String path) {
return VALUE_PATHS.computeIfAbsent(path, ValuePath::new);
}

@SuppressWarnings("unchecked")
@Override
public Object get(Object target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.jd.live.agent.governance.invoke.matcher;

import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.ServiceRequest;
import com.jd.live.agent.governance.rule.tag.TagCondition;

import java.util.List;
Expand All @@ -33,7 +33,7 @@ public abstract class AbstractTagMatcher implements TagMatcher {
* @return true if the condition matches the request, false otherwise.
*/
@Override
public boolean match(TagCondition condition, Request request) {
public boolean match(TagCondition condition, ServiceRequest request) {
return condition.match(getValues(condition, request));
}

Expand All @@ -44,5 +44,5 @@ public boolean match(TagCondition condition, Request request) {
* @param request The request.
* @return The value list to be matched.
*/
protected abstract List<String> getValues(TagCondition condition, Request request);
protected abstract List<String> getValues(TagCondition condition, ServiceRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.jd.live.agent.governance.invoke.matcher;

import com.jd.live.agent.core.extension.annotation.Extensible;
import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.ServiceRequest;
import com.jd.live.agent.governance.rule.tag.TagCondition;

/**
Expand All @@ -37,6 +37,6 @@ public interface TagMatcher {
* @param request The {@code Request} object containing the data to be evaluated against the condition.
* @return {@code true} if the request meets the tag condition; {@code false} otherwise.
*/
boolean match(TagCondition condition, Request request);
boolean match(TagCondition condition, ServiceRequest request);
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.governance.invoke.matcher.AbstractTagMatcher;
import com.jd.live.agent.governance.request.HttpRequest;
import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.ServiceRequest;
import com.jd.live.agent.governance.rule.tag.TagCondition;

import java.util.List;
Expand All @@ -33,7 +32,7 @@
public class CookieTagMatcher extends AbstractTagMatcher {

@Override
protected List<String> getValues(TagCondition condition, Request request) {
return request instanceof HttpRequest ? ((HttpRequest) request).getCookies(condition.getKey()) : null;
protected List<String> getValues(TagCondition condition, ServiceRequest request) {
return request.getCookies(condition.getKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
package com.jd.live.agent.governance.invoke.matcher.header;

import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.core.util.tag.Label;
import com.jd.live.agent.governance.invoke.matcher.AbstractTagMatcher;
import com.jd.live.agent.governance.invoke.matcher.TagMatcher;
import com.jd.live.agent.governance.request.HttpRequest;
import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.RpcRequest;
import com.jd.live.agent.governance.request.ServiceRequest;
import com.jd.live.agent.governance.rule.tag.TagCondition;

import java.util.List;
Expand All @@ -36,16 +33,7 @@
public class HeaderTagMatcher extends AbstractTagMatcher {

@Override
protected List<String> getValues(TagCondition condition, Request request) {
List<String> values = null;
if (request instanceof HttpRequest) {
values = ((HttpRequest) request).getHeaders(condition.getKey());
} else if (request instanceof RpcRequest) {
Object value = ((RpcRequest) request).getAttachment(condition.getKey());
if (value instanceof String) {
values = Label.parseValue((String) value);
}
}
return values;
protected List<String> getValues(TagCondition condition, ServiceRequest request) {
return request.getHeaders(condition.getKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.governance.invoke.matcher.AbstractTagMatcher;
import com.jd.live.agent.governance.invoke.matcher.TagMatcher;
import com.jd.live.agent.governance.request.HttpRequest;
import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.ServiceRequest;
import com.jd.live.agent.governance.rule.tag.TagCondition;

import java.util.List;
Expand All @@ -34,7 +33,7 @@
public class QueryTagMatcher extends AbstractTagMatcher {

@Override
protected List<String> getValues(TagCondition condition, Request request) {
return request instanceof HttpRequest ? ((HttpRequest) request).getQueries(condition.getKey()) : null;
protected List<String> getValues(TagCondition condition, ServiceRequest request) {
return request.getQueries(condition.getKey());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
package com.jd.live.agent.governance.invoke.matcher.system;

import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.ServiceRequest;
import com.jd.live.agent.governance.request.ServiceRequest.InboundRequest;

import java.util.Collections;
import java.util.List;

/**
* A system tag provider that provides the client IP address as a tag value.
*/
@Extension(value = "clientIp")
public class ClientIpTagProvider implements SystemTagProvider {

@Override
public List<String> getValues(Request request, String key) {
public List<String> getValues(ServiceRequest request) {
return request instanceof InboundRequest ? Collections.singletonList(((InboundRequest) request).getClientIp()) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.governance.context.RequestContext;
import com.jd.live.agent.governance.context.bag.Cargo;
import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.ServiceRequest;

import java.util.List;

/**
* A system tag provider that provides the consumer information as tag values.
*/
@Extension(value = "consumer")
public class ConsumerTagProvider implements SystemTagProvider {

@Override
public List<String> getValues(Request request, String key) {
public List<String> getValues(ServiceRequest request) {
Cargo cargo = RequestContext.getCargo(Constants.LABEL_SERVICE_CONSUMER);
return cargo == null ? null : cargo.getValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.governance.invoke.matcher.AbstractTagMatcher;
import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.ServiceRequest;
import com.jd.live.agent.governance.rule.tag.TagCondition;

import java.util.List;
import java.util.Map;

/**
* A system tag matcher that matches tags based on system-defined criteria.
*/
@Injectable
@Extension(value = "system")
public class SystemTagMatcher extends AbstractTagMatcher {
Expand All @@ -33,8 +36,8 @@ public class SystemTagMatcher extends AbstractTagMatcher {
private Map<String, SystemTagProvider> providers;

@Override
protected List<String> getValues(TagCondition condition, Request request) {
protected List<String> getValues(TagCondition condition, ServiceRequest request) {
SystemTagProvider provider = providers.get(condition.getKey());
return provider == null ? null : provider.getValues(request, condition.getKey());
return provider == null ? null : provider.getValues(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.jd.live.agent.governance.invoke.matcher.system;

import com.jd.live.agent.core.extension.annotation.Extensible;
import com.jd.live.agent.governance.request.Request;
import com.jd.live.agent.governance.request.ServiceRequest;

import java.util.List;

Expand All @@ -30,8 +30,7 @@ public interface SystemTagProvider {
* Retrieves values associated with the given key from the request.
*
* @param request The request object.
* @param key The key for which values are to be retrieved.
* @return A list of values associated with the key.
*/
List<String> getValues(Request request, String key);
List<String> getValues(ServiceRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,13 @@ public interface HttpRequest extends ServiceRequest {
*/
Map<String, List<String>> getHeaders();

/**
* Returns the values of a specific header.
*
* @param key The name of the header.
* @return A list of values for the specified header, or null if the header does not exist.
*/
@Override
default List<String> getHeaders(String key) {
Map<String, List<String>> result = getHeaders();
return result == null || key == null ? null : result.get(key);
}

/**
* Returns the first value of a specific header.
*
* @param key The name of the header.
* @return The first value of the specified header, or null if the header does not exist or has no values.
*/
@Override
default String getHeader(String key) {
List<String> headers = getHeaders(key);
return headers == null || headers.isEmpty() ? null : headers.get(0);
Expand All @@ -109,23 +99,13 @@ default String getHeader(String key) {
*/
Map<String, List<String>> getQueries();

/**
* Returns the values of a specific query.
*
* @param key The name of the query.
* @return A list of values for the specified query, or null if the query does not exist.
*/
@Override
default List<String> getQueries(String key) {
Map<String, List<String>> result = getQueries();
return result == null || key == null ? null : result.get(key);
}

/**
* Returns the value of a specific query parameter.
*
* @param key The name of the query parameter.
* @return The value of the specified query parameter, or null if it does not exist.
*/
@Override
default String getQuery(String key) {
List<String> queries = getQueries(key);
return queries == null || queries.isEmpty() ? null : queries.get(0);
Expand All @@ -138,29 +118,18 @@ default String getQuery(String key) {
*/
Map<String, List<String>> getCookies();

/**
* Returns the values of a specific cookie.
*
* @param key The name of the header.
* @return A list of values for the specified cookie, or null if the cookie does not exist.
*/
@Override
default List<String> getCookies(String key) {
Map<String, List<String>> result = getCookies();
return result == null || key == null ? null : result.get(key);
}

/**
* Returns the value of a specific cookie.
*
* @param key The name of the cookie.
* @return The value of the specified cookie, or null if it does not exist.
*/
@Override
default String getCookie(String key) {
List<String> cookies = getCookies(key);
return cookies == null || cookies.isEmpty() ? null : cookies.get(0);
}


/**
* Defines an interface for inbound HTTP requests.
* <p>
Expand Down
Loading
Loading