You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ta...@apache.org on 2021/11/07 08:54:17 UTC

[skywalking-java] branch agent_config created (now 89136cd)

This is an automated email from the ASF dual-hosted git repository.

tanjian pushed a change to branch agent_config
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git.


      at 89136cd  add docs.

This branch includes the following new commits:

     new 075cb82  set instance properties in json format: agent.instance_properties_json
     new 89136cd  add docs.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[skywalking-java] 01/02: set instance properties in json format: agent.instance_properties_json

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tanjian pushed a commit to branch agent_config
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git

commit 075cb8209d5cc2606bd155420a8739d7d6a3f7de
Author: JaredTan95 <ji...@daocloud.io>
AuthorDate: Sun Nov 7 16:49:37 2021 +0800

    set instance properties in json format: agent.instance_properties_json
---
 CHANGES.md                                         |  1 +
 .../skywalking/apm/agent/core/conf/Config.java     | 12 ++++-
 .../agent/core/remote/ServiceManagementClient.java | 17 +++----
 .../core/util/InstanceJsonPropertiesUtil.java      | 56 ++++++++++++++++++++++
 apm-sniffer/config/agent.config                    |  2 +
 .../kafka/KafkaServiceManagementServiceClient.java | 10 +---
 6 files changed, 78 insertions(+), 20 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 2054625..9152043 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -8,6 +8,7 @@ Release Notes.
 * Support `Transaction` and fix duplicated methods enhancements for `jedis-2.x` plugin.
 * Add ConsumerWrapper/FunctionWrapper to support CompletableFuture.x.thenAcceptAsync/thenApplyAsync.
 * Build CLI from Docker instead of source codes, add alpine based Docker image.
+* Support set instance properties in json format.
 
 #### Documentation
 
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
index 4195d19..46e109b 100755
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
@@ -104,10 +104,19 @@ public class Config {
 
         /**
          * service instance properties e.g. agent.instance_properties[org]=apache
+         * Notice it will be overridden by `agent.instance_properties_json `, if the key duplication.
+         * For example: <code>e.g. agent.instance_properties_json = {"org": "apache-skywalking"}</code>
          */
+        @Deprecated
         public static Map<String, String> INSTANCE_PROPERTIES = new HashMap<>();
 
         /**
+         * service instance properties in json format.
+         * e.g. agent.instance_properties_json = {"org": "apache-skywalking"}
+         */
+        public static String INSTANCE_PROPERTIES_JSON = "";
+
+        /**
          * How depth the agent goes, when log cause exceptions.
          */
         public static int CAUSE_EXCEPTION_DEPTH = 5;
@@ -169,7 +178,8 @@ public class Config {
          */
         public static long HEARTBEAT_PERIOD = 30;
         /**
-         * The agent sends the instance properties to the backend every `collector.heartbeat_period * collector.properties_report_period_factor` seconds
+         * The agent sends the instance properties to the backend every `collector.heartbeat_period *
+         * collector.properties_report_period_factor` seconds
          */
         public static int PROPERTIES_REPORT_PERIOD_FACTOR = 10;
         /**
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/ServiceManagementClient.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/ServiceManagementClient.java
index 707b7e6..f129955 100755
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/ServiceManagementClient.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/ServiceManagementClient.java
@@ -19,7 +19,6 @@
 package org.apache.skywalking.apm.agent.core.remote;
 
 import io.grpc.Channel;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledFuture;
@@ -35,6 +34,7 @@ import org.apache.skywalking.apm.agent.core.jvm.LoadedLibraryCollector;
 import org.apache.skywalking.apm.agent.core.logging.api.ILog;
 import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
 import org.apache.skywalking.apm.agent.core.os.OSUtil;
+import org.apache.skywalking.apm.agent.core.util.InstanceJsonPropertiesUtil;
 import org.apache.skywalking.apm.network.common.v3.Commands;
 import org.apache.skywalking.apm.network.common.v3.KeyStringValuePair;
 import org.apache.skywalking.apm.network.management.v3.InstancePingPkg;
@@ -69,14 +69,7 @@ public class ServiceManagementClient implements BootService, Runnable, GRPCChann
     public void prepare() {
         ServiceManager.INSTANCE.findService(GRPCChannelManager.class).addChannelListener(this);
 
-        SERVICE_INSTANCE_PROPERTIES = new ArrayList<>();
-
-        for (String key : Config.Agent.INSTANCE_PROPERTIES.keySet()) {
-            SERVICE_INSTANCE_PROPERTIES.add(KeyStringValuePair.newBuilder()
-                                                              .setKey(key)
-                                                              .setValue(Config.Agent.INSTANCE_PROPERTIES.get(key))
-                                                              .build());
-        }
+        SERVICE_INSTANCE_PROPERTIES = InstanceJsonPropertiesUtil.parseProperties();
     }
 
     @Override
@@ -108,7 +101,8 @@ public class ServiceManagementClient implements BootService, Runnable, GRPCChann
         if (GRPCChannelStatus.CONNECTED.equals(status)) {
             try {
                 if (managementServiceBlockingStub != null) {
-                    if (Math.abs(sendPropertiesCounter.getAndAdd(1)) % Config.Collector.PROPERTIES_REPORT_PERIOD_FACTOR == 0) {
+                    if (Math.abs(
+                        sendPropertiesCounter.getAndAdd(1)) % Config.Collector.PROPERTIES_REPORT_PERIOD_FACTOR == 0) {
 
                         managementServiceBlockingStub
                             .withDeadlineAfter(GRPC_UPSTREAM_TIMEOUT, TimeUnit.SECONDS)
@@ -118,7 +112,8 @@ public class ServiceManagementClient implements BootService, Runnable, GRPCChann
                                                                         .addAllProperties(OSUtil.buildOSInfo(
                                                                             Config.OsInfo.IPV4_LIST_SIZE))
                                                                         .addAllProperties(SERVICE_INSTANCE_PROPERTIES)
-                                                                        .addAllProperties(LoadedLibraryCollector.buildJVMInfo())
+                                                                        .addAllProperties(
+                                                                            LoadedLibraryCollector.buildJVMInfo())
                                                                         .build());
                     } else {
                         final Commands commands = managementServiceBlockingStub.withDeadlineAfter(
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/util/InstanceJsonPropertiesUtil.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/util/InstanceJsonPropertiesUtil.java
new file mode 100644
index 0000000..5f3bdb7
--- /dev/null
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/util/InstanceJsonPropertiesUtil.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.agent.core.util;
+
+import com.google.gson.Gson;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.network.common.v3.KeyStringValuePair;
+import org.apache.skywalking.apm.util.StringUtil;
+
+public class InstanceJsonPropertiesUtil {
+    private static final Gson GSON = new Gson();
+
+    public static List<KeyStringValuePair> parseProperties() {
+        List<KeyStringValuePair> properties = new ArrayList<>();
+
+        for (String key : Config.Agent.INSTANCE_PROPERTIES.keySet()) {
+            properties.add(KeyStringValuePair.newBuilder()
+                                             .setKey(key)
+                                             .setValue(Config.Agent.INSTANCE_PROPERTIES.get(key))
+                                             .build());
+        }
+
+        if (StringUtil.isNotEmpty(Config.Agent.INSTANCE_PROPERTIES_JSON)) {
+            Map<String, Object> jsonProperties = GSON.fromJson(Config.Agent.INSTANCE_PROPERTIES_JSON, Map.class);
+
+            for (String key : jsonProperties.keySet()) {
+                //replace and override old keyStringValuePair.
+                properties.removeIf(old -> old.getKey().equals(key));
+                properties.add(KeyStringValuePair.newBuilder()
+                                                 .setKey(key)
+                                                 .setValue(String.valueOf(jsonProperties.get(key)))
+                                                 .build());
+            }
+        }
+        return properties;
+    }
+}
diff --git a/apm-sniffer/config/agent.config b/apm-sniffer/config/agent.config
index d626014..a637cfb 100755
--- a/apm-sniffer/config/agent.config
+++ b/apm-sniffer/config/agent.config
@@ -20,6 +20,8 @@ agent.namespace=${SW_AGENT_NAMESPACE:}
 # The service name in UI
 agent.service_name=${SW_AGENT_NAME:Your_ApplicationName}
 
+agent.instance_properties_json=${SW_INSTANCE_PROPERTIES_JSON:}
+
 # The number of sampled traces per 3 seconds
 # Negative or zero means off, by default
 agent.sample_n_per_3_secs=${SW_AGENT_SAMPLE:-1}
diff --git a/apm-sniffer/optional-reporter-plugins/kafka-reporter-plugin/src/main/java/org/apache/skywalking/apm/agent/core/kafka/KafkaServiceManagementServiceClient.java b/apm-sniffer/optional-reporter-plugins/kafka-reporter-plugin/src/main/java/org/apache/skywalking/apm/agent/core/kafka/KafkaServiceManagementServiceClient.java
index c8eb5b6..bc0bb2e 100644
--- a/apm-sniffer/optional-reporter-plugins/kafka-reporter-plugin/src/main/java/org/apache/skywalking/apm/agent/core/kafka/KafkaServiceManagementServiceClient.java
+++ b/apm-sniffer/optional-reporter-plugins/kafka-reporter-plugin/src/main/java/org/apache/skywalking/apm/agent/core/kafka/KafkaServiceManagementServiceClient.java
@@ -18,7 +18,6 @@
 
 package org.apache.skywalking.apm.agent.core.kafka;
 
-import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledFuture;
@@ -37,6 +36,7 @@ import org.apache.skywalking.apm.agent.core.logging.api.ILog;
 import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
 import org.apache.skywalking.apm.agent.core.os.OSUtil;
 import org.apache.skywalking.apm.agent.core.remote.ServiceManagementClient;
+import org.apache.skywalking.apm.agent.core.util.InstanceJsonPropertiesUtil;
 import org.apache.skywalking.apm.network.common.v3.KeyStringValuePair;
 import org.apache.skywalking.apm.network.management.v3.InstancePingPkg;
 import org.apache.skywalking.apm.network.management.v3.InstanceProperties;
@@ -65,13 +65,7 @@ public class KafkaServiceManagementServiceClient implements BootService, Runnabl
         producerManager.addListener(this);
         topic = producerManager.formatTopicNameThenRegister(KafkaReporterPluginConfig.Plugin.Kafka.TOPIC_MANAGEMENT);
 
-        SERVICE_INSTANCE_PROPERTIES = new ArrayList<>();
-        for (String key : Config.Agent.INSTANCE_PROPERTIES.keySet()) {
-            SERVICE_INSTANCE_PROPERTIES.add(KeyStringValuePair.newBuilder()
-                                                              .setKey(key)
-                                                              .setValue(Config.Agent.INSTANCE_PROPERTIES.get(key))
-                                                              .build());
-        }
+        SERVICE_INSTANCE_PROPERTIES = InstanceJsonPropertiesUtil.parseProperties();
     }
 
     @Override

[skywalking-java] 02/02: add docs.

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tanjian pushed a commit to branch agent_config
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git

commit 89136cdd4a54121f7d371652387aaead7538ca4c
Author: JaredTan95 <ji...@daocloud.io>
AuthorDate: Sun Nov 7 16:53:15 2021 +0800

    add docs.
---
 docs/en/setup/service-agent/java-agent/configurations.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/docs/en/setup/service-agent/java-agent/configurations.md b/docs/en/setup/service-agent/java-agent/configurations.md
index 1dfe170..c414cf4 100644
--- a/docs/en/setup/service-agent/java-agent/configurations.md
+++ b/docs/en/setup/service-agent/java-agent/configurations.md
@@ -14,7 +14,8 @@ property key | Description | Default |
 `agent.is_cache_enhanced_class`|If true, SkyWalking agent will cache all instrumented classes files to memory or disk files (decided by class cache mode), allow another java agent to enhance those classes that enhanced by SkyWalking agent. To use some Java diagnostic tools (such as BTrace, Arthas) to diagnose applications or add a custom java agent to enhance classes, you need to enable this feature. |`false`|
 `agent.class_cache_mode`|The instrumented classes cache mode: `MEMORY` or `FILE`. `MEMORY`: cache class bytes to memory, if instrumented classes is too many or too large, it may take up more memory. `FILE`: cache class bytes in `/class-cache` folder, automatically clean up cached class files when the application exits.|`MEMORY`|
 `agent.instance_name` |Instance name is the identity of an instance, should be unique in the service. If empty, SkyWalking agent will generate an 32-bit uuid. Default, use `UUID`@`hostname` as the instance name. Max length is 50(UTF-8 char)|`""`|
-`agent.instance_properties[key]=value` | Add service instance custom properties. | Not set|
+`agent.instance_properties[key]=value` | Add service instance custom properties. Notice it will be overridden by `agent.instance_properties_json `, if the key duplication. | Not set|
+`agent.instance_properties_json={"key":"value"}` | Add service instance custom properties in json format.  | Not set|
 `agent.cause_exception_depth`|How depth the agent goes, when log all cause exceptions.|`5`|
 `agent.force_reconnection_period `|Force reconnection period of grpc, based on grpc_channel_check_interval.|`1`|
 `agent.operation_name_threshold `|The operationName max length, setting this value > 190 is not recommended.|`150`|