From 5f2d75ca461f583e4bc5fdb4af603bceaf287b5a Mon Sep 17 00:00:00 2001 From: yuanzhibang Date: Fri, 30 Aug 2024 17:14:42 +0800 Subject: [PATCH 1/3] #6: sync requests carry agent version and app metadata. --- .../com/jd/live/agent/core/Constants.java | 9 +++++++ .../live/agent/core/instance/Application.java | 25 +++++++++++++++++++ .../live/agent/core/bootstrap/Bootstrap.java | 19 ++++++++++++++ .../policy/multilive/LiveSpaceSyncer.java | 1 + 4 files changed, 54 insertions(+) diff --git a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/Constants.java b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/Constants.java index aadca5f1e..f64c00b20 100644 --- a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/Constants.java +++ b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/Constants.java @@ -115,6 +115,15 @@ public interface Constants { * Constant for the service consumer. */ String LABEL_SERVICE_CONSUMER = LABEL_SERVICE_PREFIX + "consumer"; + /** + * Constant for the instance ip. + */ + String LABEL_INSTANCE_IP = LABEL_SERVICE_PREFIX + "instance-ip"; + + /** + * Constant for the live agent version. + */ + String LABEL_AGENT_VERSION = LABEL_SERVICE_PREFIX + "agent-version"; /** * Default value for labels. diff --git a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/instance/Application.java b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/instance/Application.java index fd3ca1002..99e519900 100644 --- a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/instance/Application.java +++ b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/instance/Application.java @@ -179,6 +179,31 @@ public void label(BiConsumer consumer) { } } + /** + * Applies sync metadata to the application based on its properties. + * + * @param consumer label consumer + */ + public void sync(BiConsumer 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. * diff --git a/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/Bootstrap.java b/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/Bootstrap.java index 7c242795e..3073ff74b 100644 --- a/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/Bootstrap.java +++ b/joylive-core/joylive-core-framework/src/main/java/com/jd/live/agent/core/bootstrap/Bootstrap.java @@ -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; @@ -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; @@ -474,6 +476,7 @@ private Application createApplication() { injector.inject(app); AppService appService = app.getService(); Location location = app.getLocation(); + Map meta = app.getMeta(); if (location == null) { location = new Location(); app.setLocation(location); @@ -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()); @@ -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; diff --git a/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveSpaceSyncer.java b/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveSpaceSyncer.java index c5d51ed44..e973e7c57 100644 --- a/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveSpaceSyncer.java +++ b/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveSpaceSyncer.java @@ -304,6 +304,7 @@ private Response 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()); } From 2f3899647467a39dbe9f73554e316207582051cf Mon Sep 17 00:00:00 2001 From: yuanzhibang Date: Fri, 30 Aug 2024 17:21:58 +0800 Subject: [PATCH 2/3] #6: sync requests carry agent version and app metadata. --- .../com/jd/live/agent/core/Constants.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/Constants.java b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/Constants.java index f64c00b20..b0c6eb0ae 100644 --- a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/Constants.java +++ b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/Constants.java @@ -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. */ @@ -115,15 +125,6 @@ public interface Constants { * Constant for the service consumer. */ String LABEL_SERVICE_CONSUMER = LABEL_SERVICE_PREFIX + "consumer"; - /** - * Constant for the instance ip. - */ - String LABEL_INSTANCE_IP = LABEL_SERVICE_PREFIX + "instance-ip"; - - /** - * Constant for the live agent version. - */ - String LABEL_AGENT_VERSION = LABEL_SERVICE_PREFIX + "agent-version"; /** * Default value for labels. From 67c7e35a54950e46e1691194b06972e45c54e163 Mon Sep 17 00:00:00 2001 From: yuanzhibang Date: Sat, 31 Aug 2024 16:35:19 +0800 Subject: [PATCH 3/3] #6: add requests metadata to all Syncers --- .../service/policy/microservice/MicroServiceSyncer.java | 1 + .../service/policy/multilive/LiveServiceSyncer.java | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/joylive-implement/joylive-service/joylive-service-microservice/src/main/java/com/jd/live/agent/implement/service/policy/microservice/MicroServiceSyncer.java b/joylive-implement/joylive-service/joylive-service-microservice/src/main/java/com/jd/live/agent/implement/service/policy/microservice/MicroServiceSyncer.java index 80db0b478..ff32dec34 100644 --- a/joylive-implement/joylive-service/joylive-service-microservice/src/main/java/com/jd/live/agent/implement/service/policy/microservice/MicroServiceSyncer.java +++ b/joylive-implement/joylive-service/joylive-service-microservice/src/main/java/com/jd/live/agent/implement/service/policy/microservice/MicroServiceSyncer.java @@ -332,6 +332,7 @@ private Response 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()); } diff --git a/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveServiceSyncer.java b/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveServiceSyncer.java index a989a8de6..9f6460a89 100644 --- a/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveServiceSyncer.java +++ b/joylive-implement/joylive-service/joylive-service-multilive/src/main/java/com/jd/live/agent/implement/service/policy/multilive/LiveServiceSyncer.java @@ -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; @@ -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; @@ -320,6 +324,7 @@ private Response 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()); }