Skip to content

Commit

Permalink
Merge pull request jd-opensource#29 from zinu-x/6-sync-metadata
Browse files Browse the repository at this point in the history
jd-opensource#6: sync requests carry agent version and app metadata.
  • Loading branch information
zinu-x authored Aug 31, 2024
2 parents ac0185a + 67c7e35 commit a964f33
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public interface Constants {
*/
String LABEL_REGION = LABEL_LIVE_PREFIX + "region";

/**
* Constant for the instance ip.
*/
String LABEL_INSTANCE_IP = LABEL_LIVE_PREFIX + "instance-ip";

/**
* Constant for the live agent version.
*/
String LABEL_AGENT_VERSION = LABEL_LIVE_PREFIX + "agent-version";

/**
* Prefix for lane-related constants.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ public void label(BiConsumer<String, String> consumer) {
}
}

/**
* Applies sync metadata to the application based on its properties.
*
* @param consumer label consumer
*/
public void sync(BiConsumer<String, String> consumer) {
if (consumer != null) {
accept(consumer, Constants.LABEL_APPLICATION, name);
accept(consumer, Constants.LABEL_INSTANCE_ID, instance);
if (location != null) {
accept(consumer, Constants.LABEL_REGION, location.getRegion());
accept(consumer, Constants.LABEL_ZONE, location.getZone());
accept(consumer, Constants.LABEL_LIVE_SPACE_ID, location.getLiveSpaceId());
accept(consumer, Constants.LABEL_UNIT, location.getUnit());
accept(consumer, Constants.LABEL_CELL, location.getCell());
accept(consumer, Constants.LABEL_LANE, location.getLane());
accept(consumer, Constants.LABEL_CLUSTER, location.getCluster());
accept(consumer, Constants.LABEL_INSTANCE_IP, location.getIp());
}
if (meta != null) {
accept(consumer, Constants.LABEL_AGENT_VERSION, meta.get(Constants.LABEL_AGENT_VERSION));
}
}
}

/**
* Helper method for applying labels.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import com.jd.live.agent.core.util.shutdown.ShutdownHookAdapter;
import com.jd.live.agent.core.util.time.TimeScheduler;
import com.jd.live.agent.core.util.time.Timer;
import com.jd.live.agent.core.util.type.Artifact;
import com.jd.live.agent.core.util.version.JVM;
import com.jd.live.agent.core.util.version.VersionExpression;

Expand All @@ -77,6 +78,7 @@
import java.io.Reader;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -474,6 +476,7 @@ private Application createApplication() {
injector.inject(app);
AppService appService = app.getService();
Location location = app.getLocation();
Map<String, String> meta = app.getMeta();
if (location == null) {
location = new Location();
app.setLocation(location);
Expand All @@ -482,6 +485,11 @@ private Application createApplication() {
appService = new AppService();
app.setService(appService);
}
if (meta == null) {
meta = new HashMap<>();
app.setMeta(meta);
}
setAgentVersion();
location.setIp(Ipv4.getLocalIp());
location.setHost(Ipv4.getLocalHost());
setProperty(Constants.LABEL_INSTANCE_ID, app.getInstance());
Expand Down Expand Up @@ -615,6 +623,17 @@ private void setupLogger() {
location.getUnit(), location.getCell(), location.getLane()));
}

private void setAgentVersion() {
try {
CodeSource codeSource = this.getClass().getProtectionDomain().getCodeSource();
Artifact artifact = new Artifact(codeSource.getLocation().getPath());
String agentVersion = artifact.getVersion();
application.getMeta().put(Constants.LABEL_AGENT_VERSION, agentVersion);
} catch (Throwable t) {
logger.error(t.getMessage(), t);
}
}

private boolean supportEnhance() {
String version = agentConfig.getEnhanceConfig().getJavaVersion();
version = version == null || version.isEmpty() ? EnhanceConfig.SUPPORT_JAVA_VERSION : version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ private Response<Service> getService(String name, ServiceSyncMeta meta, SyncConf
*/
private void configure(SyncConfig config, HttpURLConnection conn) {
config.header(conn::setRequestProperty);
application.sync(conn::setRequestProperty);
conn.setRequestProperty("Accept", "application/json");
conn.setConnectTimeout((int) config.getTimeout());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.jd.live.agent.core.inject.annotation.Config;
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.core.instance.Application;
import com.jd.live.agent.core.parser.ObjectParser;
import com.jd.live.agent.core.parser.TypeReference;
import com.jd.live.agent.core.service.AbstractService;
Expand Down Expand Up @@ -85,6 +86,9 @@ public class LiveServiceSyncer extends AbstractService implements PolicyService,
@Inject(PolicySupervisor.COMPONENT_POLICY_SUPERVISOR)
private PolicySupervisor policySupervisor;

@Inject(Application.COMPONENT_APPLICATION)
private Application application;

@Inject(Timer.COMPONENT_TIMER)
private Timer timer;

Expand Down Expand Up @@ -320,6 +324,7 @@ private Response<Service> getService(String name, ServiceSyncMeta meta, SyncConf
*/
private void configure(SyncConfig config, HttpURLConnection conn) {
config.header(conn::setRequestProperty);
application.sync(conn::setRequestProperty);
conn.setRequestProperty("Accept", "application/json");
conn.setConnectTimeout((int) config.getTimeout());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ private Response<LiveSpace> getSpace(String workspaceId, Long version, LiveSyncC
*/
private void configure(SyncConfig config, HttpURLConnection conn) {
config.header(conn::setRequestProperty);
application.sync(conn::setRequestProperty);
conn.setRequestProperty("Accept", "application/json");
conn.setConnectTimeout((int) config.getTimeout());
}
Expand Down

0 comments on commit a964f33

Please sign in to comment.