From b34a84e76bbc3e25a93a1216db3249b3fb6f5b31 Mon Sep 17 00:00:00 2001 From: mrproliu <741550557@qq.com> Date: Thu, 26 Oct 2023 15:52:59 +0800 Subject: [PATCH] Support search enable configuration (#3579) --- .../server/query/http/HTTPQueryConfig.java | 13 +--- .../server/query/http/HTTPQueryHandler.java | 61 ++++++++++++++++++- .../server/query/http/HTTPQueryProvider.java | 6 +- .../zipkin/server/core/CoreModuleConfig.java | 10 +++ .../server/core/CoreModuleProvider.java | 10 +-- .../server/core/ZipkinDispatcherManager.java | 10 +++ .../server/core/ZipkinSourceReceiverImpl.java | 4 +- .../core/ZipkinStreamAnnotationListener.java | 8 ++- .../core/services/ZipkinConfigService.java | 4 ++ .../src/main/resources/application.yml | 1 + 10 files changed, 104 insertions(+), 23 deletions(-) diff --git a/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryConfig.java b/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryConfig.java index d55fac3cdad..4c2db3a05be 100644 --- a/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryConfig.java +++ b/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryConfig.java @@ -36,7 +36,6 @@ public class HTTPQueryConfig extends ModuleConfig { private int uiQueryLimit = 10; private String uiEnvironment = ""; private long uiDefaultLookback = 900000L; - private boolean uiSearchEnabled = true; private String allowedOrigins = "*"; private boolean uiEnable = true; @@ -46,14 +45,14 @@ public class HTTPQueryConfig extends ModuleConfig { private double dependencyLowErrorRate = 0.5; // 50% of calls in error turns line yellow private double dependencyHighErrorRate = 0.75;// 75% of calls in error turns line red - public ZipkinQueryConfig toSkyWalkingConfig() { + public ZipkinQueryConfig toSkyWalkingConfig(boolean searchEnabled) { final ZipkinQueryConfig result = new ZipkinQueryConfig(); result.setLookback(lookback); result.setNamesMaxAge(namesMaxAge); result.setUiQueryLimit(uiQueryLimit); result.setUiEnvironment(uiEnvironment); result.setUiDefaultLookback(uiDefaultLookback); - result.setUiSearchEnabled(uiSearchEnabled); + result.setUiSearchEnabled(searchEnabled); return result; } @@ -97,14 +96,6 @@ public void setUiDefaultLookback(long uiDefaultLookback) { this.uiDefaultLookback = uiDefaultLookback; } - public boolean getUiSearchEnabled() { - return uiSearchEnabled; - } - - public void setUiSearchEnabled(boolean uiSearchEnabled) { - this.uiSearchEnabled = uiSearchEnabled; - } - public boolean getStrictTraceId() { return strictTraceId; } diff --git a/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryHandler.java b/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryHandler.java index fab3dc224fc..a97b91e7aae 100644 --- a/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryHandler.java +++ b/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryHandler.java @@ -40,6 +40,7 @@ import java.io.IOException; import java.io.StringWriter; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -56,15 +57,69 @@ public class HTTPQueryHandler extends ZipkinQueryHandler { private final HTTPQueryConfig config; private final ModuleManager moduleManager; + private final boolean searchEnable; + + private final AggregatedHttpResponse EMPTY_ARRAY_RESPONSE = AggregatedHttpResponse.of(ResponseHeaders.builder(HttpStatus.OK) + .contentType(MediaType.JSON) + .build(), HttpData.wrap("[]".getBytes(StandardCharsets.UTF_8))); private IZipkinQueryDAO zipkinQueryDAO; private IZipkinDependencyQueryDAO dependencyQueryDAO; - public HTTPQueryHandler(HTTPQueryConfig config, ModuleManager moduleManager) { - super(config.toSkyWalkingConfig(), moduleManager); + public HTTPQueryHandler(boolean searchEnable, HTTPQueryConfig config, ModuleManager moduleManager) { + super(config.toSkyWalkingConfig(searchEnable), moduleManager); + this.searchEnable = searchEnable; this.config = config; this.moduleManager = moduleManager; } + @Override + public AggregatedHttpResponse getServiceNames() throws IOException { + if (!searchEnable) { + return EMPTY_ARRAY_RESPONSE; + } + return super.getServiceNames(); + } + + @Override + public AggregatedHttpResponse getRemoteServiceNames(String serviceName) throws IOException { + if (!searchEnable) { + return EMPTY_ARRAY_RESPONSE; + } + return super.getRemoteServiceNames(serviceName); + } + + @Override + public AggregatedHttpResponse getAutocompleteKeys() throws IOException { + if (!searchEnable) { + return EMPTY_ARRAY_RESPONSE; + } + return super.getAutocompleteKeys(); + } + + @Override + public AggregatedHttpResponse getAutocompleteValues(String key) throws IOException { + if (!searchEnable) { + return EMPTY_ARRAY_RESPONSE; + } + return super.getAutocompleteValues(key); + } + + @Override + public AggregatedHttpResponse getTraces(Optional serviceName, Optional remoteServiceName, Optional spanName, Optional annotationQuery, Optional minDuration, Optional maxDuration, Optional endTs, Optional lookback, int limit) throws IOException { + if (!searchEnable) { + return EMPTY_ARRAY_RESPONSE; + } + return super.getTraces(serviceName, remoteServiceName, spanName, annotationQuery, minDuration, maxDuration, endTs, lookback, limit); + } + + @Override + public AggregatedHttpResponse getSpanNames(String serviceName) throws IOException { + if (!searchEnable) { + return EMPTY_ARRAY_RESPONSE; + } + return super.getSpanNames(serviceName); + } + @Override public AggregatedHttpResponse getUIConfig() throws IOException { StringWriter writer = new StringWriter(); @@ -73,7 +128,7 @@ public AggregatedHttpResponse getUIConfig() throws IOException { generator.writeStringField("environment", config.getUiEnvironment()); generator.writeNumberField("queryLimit", config.getUiQueryLimit()); generator.writeNumberField("defaultLookback", config.getUiDefaultLookback()); - generator.writeBooleanField("searchEnabled", config.getUiSearchEnabled()); + generator.writeBooleanField("searchEnabled", searchEnable); generator.writeObjectFieldStart("dependency"); generator.writeBooleanField("enabled", config.getDependencyEnabled()); generator.writeNumberField("lowErrorRate", config.getDependencyLowErrorRate()); diff --git a/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryProvider.java b/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryProvider.java index 84bf5d103f4..400fa141235 100644 --- a/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryProvider.java +++ b/zipkin-server/http-query-plugin/src/main/java/zipkin/server/query/http/HTTPQueryProvider.java @@ -28,6 +28,7 @@ import com.linecorp.armeria.server.file.HttpFile; import org.apache.skywalking.oap.query.zipkin.ZipkinQueryModule; import org.apache.skywalking.oap.server.core.CoreModule; +import org.apache.skywalking.oap.server.core.config.ConfigService; import org.apache.skywalking.oap.server.core.server.HTTPHandlerRegister; import org.apache.skywalking.oap.server.library.module.ModuleConfig; import org.apache.skywalking.oap.server.library.module.ModuleDefine; @@ -37,6 +38,7 @@ import org.apache.skywalking.oap.server.library.server.http.HTTPServer; import org.apache.skywalking.oap.server.library.server.http.HTTPServerConfig; import zipkin.server.core.services.HTTPConfigurableServer; +import zipkin.server.core.services.ZipkinConfigService; import java.io.IOException; import java.io.InputStream; @@ -115,7 +117,9 @@ public void start() throws ServiceNotProvidedException, ModuleStartException { server.decorator(corsBuilder::build); }; - final HTTPQueryHandler httpService = new HTTPQueryHandler(moduleConfig, getManager()); + final ConfigService configService = getManager().find(CoreModule.NAME).provider().getService(ConfigService.class); + final boolean searchEnable = ((ZipkinConfigService) configService).getSearchEnable(); + final HTTPQueryHandler httpService = new HTTPQueryHandler(searchEnable, moduleConfig, getManager()); if (httpServer != null) { httpServer.addHandler(httpService, Collections.singletonList(HttpMethod.GET)); httpServer.addHandler(corsConfiguration, Arrays.asList(HttpMethod.GET, HttpMethod.POST)); diff --git a/zipkin-server/server-core/src/main/java/zipkin/server/core/CoreModuleConfig.java b/zipkin-server/server-core/src/main/java/zipkin/server/core/CoreModuleConfig.java index 3c99c1ba98c..dbbb0dd71be 100644 --- a/zipkin-server/server-core/src/main/java/zipkin/server/core/CoreModuleConfig.java +++ b/zipkin-server/server-core/src/main/java/zipkin/server/core/CoreModuleConfig.java @@ -102,6 +102,8 @@ public class CoreModuleConfig extends ModuleConfig { */ private int remoteTimeout = 20; + private boolean searchEnable = true; + private static final String DEFAULT_SEARCHABLE_TAG_KEYS = String.join( Const.COMMA, "http.method" @@ -354,4 +356,12 @@ public int getRestMaxRequestHeaderSize() { public void setRestMaxRequestHeaderSize(int restMaxRequestHeaderSize) { this.restMaxRequestHeaderSize = restMaxRequestHeaderSize; } + + public boolean getSearchEnable() { + return searchEnable; + } + + public void setSearchEnable(boolean searchEnable) { + this.searchEnable = searchEnable; + } } diff --git a/zipkin-server/server-core/src/main/java/zipkin/server/core/CoreModuleProvider.java b/zipkin-server/server-core/src/main/java/zipkin/server/core/CoreModuleProvider.java index 7b41cf933f2..53b91e86cf4 100644 --- a/zipkin-server/server-core/src/main/java/zipkin/server/core/CoreModuleProvider.java +++ b/zipkin-server/server-core/src/main/java/zipkin/server/core/CoreModuleProvider.java @@ -97,17 +97,16 @@ public class CoreModuleProvider extends ModuleProvider { private CoreModuleConfig moduleConfig; - private EndpointNameGrouping endpointNameGrouping; - private final ZipkinSourceReceiverImpl receiver; - private final ZipkinAnnotationScan annotationScan; private final StorageModels storageModels; + private final ZipkinAnnotationScan annotationScan; + private EndpointNameGrouping endpointNameGrouping; + private ZipkinSourceReceiverImpl receiver; private RemoteClientManager remoteClientManager; private GRPCServer grpcServer; private HTTPServer httpServer; public CoreModuleProvider() { this.annotationScan = new ZipkinAnnotationScan(); - this.receiver = new ZipkinSourceReceiverImpl(); this.storageModels = new StorageModels(); } @@ -147,8 +146,9 @@ public void prepare() throws ServiceNotProvidedException, ModuleStartException { ); this.registerServiceImplementation(NamingControl.class, namingControl); + receiver = new ZipkinSourceReceiverImpl(moduleConfig.getSearchEnable()); annotationScan.registerListener(new DefaultScopeDefine.Listener()); - annotationScan.registerListener(new ZipkinStreamAnnotationListener(getManager())); + annotationScan.registerListener(new ZipkinStreamAnnotationListener(getManager(), moduleConfig.getSearchEnable())); HTTPServerConfig httpServerConfig = HTTPServerConfig.builder() .host(moduleConfig.getRestHost()) diff --git a/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinDispatcherManager.java b/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinDispatcherManager.java index 81579eb44a1..719a91a3f21 100644 --- a/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinDispatcherManager.java +++ b/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinDispatcherManager.java @@ -19,10 +19,16 @@ import io.github.classgraph.ScanResult; import org.apache.skywalking.oap.server.core.analysis.DispatcherManager; import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.TagAutocompleteDispatcher; +import org.apache.skywalking.oap.server.core.zipkin.dispatcher.ZipkinSpanRecordDispatcher; import java.io.IOException; public class ZipkinDispatcherManager extends DispatcherManager { + private final boolean searchEnable; + + public ZipkinDispatcherManager(boolean searchEnable) { + this.searchEnable = searchEnable; + } @Override public void scan() throws IOException, IllegalAccessException, InstantiationException { @@ -42,6 +48,10 @@ public void scan() throws IOException, IllegalAccessException, InstantiationExce @Override public void addIfAsSourceDispatcher(Class aClass) throws IllegalAccessException, InstantiationException { if (aClass.getSimpleName().startsWith("Zipkin") || aClass.equals(TagAutocompleteDispatcher.class)) { + // If search is disabled, only the span can be stored. + if (!searchEnable && !aClass.equals(ZipkinSpanRecordDispatcher.class)) { + return; + } super.addIfAsSourceDispatcher(aClass); } } diff --git a/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinSourceReceiverImpl.java b/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinSourceReceiverImpl.java index ce29494b666..50b8575b13a 100644 --- a/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinSourceReceiverImpl.java +++ b/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinSourceReceiverImpl.java @@ -23,8 +23,8 @@ public class ZipkinSourceReceiverImpl implements SourceReceiver { private final ZipkinDispatcherManager mgr; - public ZipkinSourceReceiverImpl() { - mgr = new ZipkinDispatcherManager(); + public ZipkinSourceReceiverImpl(boolean searchEnable) { + mgr = new ZipkinDispatcherManager(searchEnable); } @Override diff --git a/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinStreamAnnotationListener.java b/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinStreamAnnotationListener.java index 1d605b67b80..53925fbe13c 100644 --- a/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinStreamAnnotationListener.java +++ b/zipkin-server/server-core/src/main/java/zipkin/server/core/ZipkinStreamAnnotationListener.java @@ -18,18 +18,24 @@ import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.TagAutocompleteData; import org.apache.skywalking.oap.server.core.analysis.manual.spanattach.SpanAttachedEventRecord; import org.apache.skywalking.oap.server.core.storage.StorageException; +import org.apache.skywalking.oap.server.core.zipkin.ZipkinSpanRecord; import org.apache.skywalking.oap.server.library.module.ModuleDefineHolder; public class ZipkinStreamAnnotationListener extends StreamAnnotationListener { + private final boolean searchEnable; - public ZipkinStreamAnnotationListener(ModuleDefineHolder moduleDefineHolder) { + public ZipkinStreamAnnotationListener(ModuleDefineHolder moduleDefineHolder, boolean searchEnable) { super(moduleDefineHolder); + this.searchEnable = searchEnable; } @Override public void notify(Class aClass) throws StorageException { // only including all zipkin streaming if (aClass.getSimpleName().startsWith("Zipkin") || aClass.equals(TagAutocompleteData.class) || aClass.equals(SpanAttachedEventRecord.class)) { + if (!searchEnable && !aClass.equals(ZipkinSpanRecord.class)) { + return; + } super.notify(aClass); } } diff --git a/zipkin-server/server-core/src/main/java/zipkin/server/core/services/ZipkinConfigService.java b/zipkin-server/server-core/src/main/java/zipkin/server/core/services/ZipkinConfigService.java index 2b39fd77da8..2c1403b5da4 100644 --- a/zipkin-server/server-core/src/main/java/zipkin/server/core/services/ZipkinConfigService.java +++ b/zipkin-server/server-core/src/main/java/zipkin/server/core/services/ZipkinConfigService.java @@ -26,6 +26,10 @@ public ZipkinConfigService(CoreModuleConfig moduleConfig, ModuleProvider provide this.moduleConfig = moduleConfig; } + public boolean getSearchEnable() { + return moduleConfig.getSearchEnable(); + } + public ZipkinReceiverConfig toZipkinReceiverConfig() { final ZipkinReceiverConfig config = new ZipkinReceiverConfig(); config.setSearchableTracesTags(moduleConfig.getSearchableTracesTags()); diff --git a/zipkin-server/server-starter/src/main/resources/application.yml b/zipkin-server/server-starter/src/main/resources/application.yml index 3ccc985e91e..9a75e924656 100644 --- a/zipkin-server/server-starter/src/main/resources/application.yml +++ b/zipkin-server/server-starter/src/main/resources/application.yml @@ -55,6 +55,7 @@ core: restIdleTimeOut: ${ZIPKIN_REST_IDLE_TIMEOUT:30000} restAcceptQueueSize: ${ZIPKIN_REST_QUEUE_SIZE:0} restMaxRequestHeaderSize: ${ZIPKIN_REST_MAX_REQUEST_HEADER_SIZE:8192} + searchEnable: ${ZIPKIN_SEARCH_ENABLED:true} storage: &storage selector: ${ZIPKIN_STORAGE:h2}