From 3d00d83fc6cea95d443987d17b881bbbbc14746f Mon Sep 17 00:00:00 2001 From: Stalary Date: Wed, 28 Aug 2019 22:18:11 +0800 Subject: [PATCH] ADD: add operationName length threshold (#3357) * ADD: add operationName length threshold * MOD:move operationName threshold to Config, simplified code * MOD:update agent set-up document, clean code * MOD:add agent.operation_name_threshold conf prefix --- .../java/org/apache/skywalking/apm/util/StringUtil.java | 7 +++++++ .../org/apache/skywalking/apm/util/StringUtilTest.java | 8 ++++++++ .../org/apache/skywalking/apm/agent/core/conf/Config.java | 5 +++++ .../skywalking/apm/agent/core/context/ContextManager.java | 6 ++++++ apm-sniffer/config/agent.config | 3 +++ docs/en/setup/service-agent/java-agent/README.md | 1 + 6 files changed, 30 insertions(+) diff --git a/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/StringUtil.java b/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/StringUtil.java index ce236b43d..5c7dc2126 100644 --- a/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/StringUtil.java +++ b/apm-commons/apm-util/src/main/java/org/apache/skywalking/apm/util/StringUtil.java @@ -62,4 +62,11 @@ public final class StringUtil { } return true; } + + public static String cut(String str, int threshold) { + if (isEmpty(str) || str.length() <= threshold) { + return str; + } + return str.substring(0, threshold); + } } diff --git a/apm-commons/apm-util/src/test/java/org/apache/skywalking/apm/util/StringUtilTest.java b/apm-commons/apm-util/src/test/java/org/apache/skywalking/apm/util/StringUtilTest.java index b918e2e4b..e5158a3a3 100644 --- a/apm-commons/apm-util/src/test/java/org/apache/skywalking/apm/util/StringUtilTest.java +++ b/apm-commons/apm-util/src/test/java/org/apache/skywalking/apm/util/StringUtilTest.java @@ -53,4 +53,12 @@ public class StringUtilTest { Assert.assertFalse(StringUtil.substringMatch("", 4770, "")); } + @Test + public void testCut() { + String str = "aaaaaaabswbswbbsbwbsbbwbsbwbsbwbbsbbebewewewewewewewewewewew"; + String shortStr = "ab"; + Assert.assertEquals(10, StringUtil.cut(str, 10).length()); + Assert.assertEquals(2, StringUtil.cut(shortStr, 10).length()); + } + } \ No newline at end of file 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 74589959a..376ee6b97 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 @@ -105,6 +105,11 @@ public class Config { * status more than this number. The channel check will call channel.getState(true) to requestConnection. */ public static long FORCE_RECONNECTION_PERIOD = 1; + + /** + * Limit the length of the operationName to prevent errors when inserting elasticsearch + **/ + public static int OPERATION_NAME_THRESHOLD = 500; } public static class Collector { diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java index 206cb6845..9a0dfa639 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java @@ -26,6 +26,8 @@ import org.apache.skywalking.apm.agent.core.logging.api.*; import org.apache.skywalking.apm.agent.core.sampling.SamplingService; import org.apache.skywalking.apm.util.StringUtil; +import static org.apache.skywalking.apm.agent.core.conf.Config.Agent.OPERATION_NAME_THRESHOLD; + /** * {@link ContextManager} controls the whole context of {@link TraceSegment}. Any {@link TraceSegment} relates to * single-thread, so this context use {@link ThreadLocal} to maintain the context, and make sure, since a {@link @@ -89,6 +91,7 @@ public class ContextManager implements BootService { public static AbstractSpan createEntrySpan(String operationName, ContextCarrier carrier) { AbstractSpan span; AbstractTracerContext context; + operationName = StringUtil.cut(operationName, OPERATION_NAME_THRESHOLD); if (carrier != null && carrier.isValid()) { SamplingService samplingService = ServiceManager.INSTANCE.findService(SamplingService.class); samplingService.forceSampled(); @@ -103,6 +106,7 @@ public class ContextManager implements BootService { } public static AbstractSpan createLocalSpan(String operationName) { + operationName = StringUtil.cut(operationName, OPERATION_NAME_THRESHOLD); AbstractTracerContext context = getOrCreate(operationName, false); return context.createLocalSpan(operationName); } @@ -111,6 +115,7 @@ public class ContextManager implements BootService { if (carrier == null) { throw new IllegalArgumentException("ContextCarrier can't be null."); } + operationName = StringUtil.cut(operationName, OPERATION_NAME_THRESHOLD); AbstractTracerContext context = getOrCreate(operationName, false); AbstractSpan span = context.createExitSpan(operationName, remotePeer); context.inject(carrier); @@ -118,6 +123,7 @@ public class ContextManager implements BootService { } public static AbstractSpan createExitSpan(String operationName, String remotePeer) { + operationName = StringUtil.cut(operationName, OPERATION_NAME_THRESHOLD); AbstractTracerContext context = getOrCreate(operationName, false); AbstractSpan span = context.createExitSpan(operationName, remotePeer); return span; diff --git a/apm-sniffer/config/agent.config b/apm-sniffer/config/agent.config index 3041be1f6..3507eded3 100644 --- a/apm-sniffer/config/agent.config +++ b/apm-sniffer/config/agent.config @@ -38,6 +38,9 @@ agent.service_name=${SW_AGENT_NAME:Your_ApplicationName} # Skywalking team may ask for these files in order to resolve compatible problem. # agent.is_open_debugging_class = ${SW_AGENT_OPEN_DEBUG:true} +# The operationName max length +# agent.operation_name_threshold=${SW_AGENT_OPERATION_NAME_THRESHOLD:500} + # Backend service addresses. collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:127.0.0.1:11800} diff --git a/docs/en/setup/service-agent/java-agent/README.md b/docs/en/setup/service-agent/java-agent/README.md index e6d48dd23..26e4c7932 100755 --- a/docs/en/setup/service-agent/java-agent/README.md +++ b/docs/en/setup/service-agent/java-agent/README.md @@ -77,6 +77,7 @@ property key | Description | Default | `agent.active_v1_header `|Deactivate V1 header in default.|`false`| `agent.cool_down_threshold `|How long should the agent wait (in minute) before re-registering to the OAP server after receiving reset command.|`10`| `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 > 500 is not recommended.|`500`| `collector.grpc_channel_check_interval`|grpc channel status check interval.|`30`| `collector.app_and_service_register_check_interval`|application and service registry check interval.|`3`| `collector.backend_service`|Collector SkyWalking trace receiver service addresses.|`127.0.0.1:11800`|