Skip to content

Commit

Permalink
Fix annotation scan not working in v3 server
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu committed Oct 18, 2023
1 parent 1c74d42 commit 657c6a8
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 11 deletions.
5 changes: 5 additions & 0 deletions zipkin-server/server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>zipkin-receiver-plugin</artifactId>
<version>${skywalking.version}</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.162</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public class CoreModuleProvider extends ModuleProvider {

private EndpointNameGrouping endpointNameGrouping;
private final ZipkinSourceReceiverImpl receiver;
private final AnnotationScan annotationScan;
private final ZipkinAnnotationScan annotationScan;
private final StorageModels storageModels;
private RemoteClientManager remoteClientManager;
private GRPCServer grpcServer;
private HTTPServer httpServer;

public CoreModuleProvider() {
this.annotationScan = new AnnotationScan();
this.annotationScan = new ZipkinAnnotationScan();
this.receiver = new ZipkinSourceReceiverImpl();
this.storageModels = new StorageModels();
}
Expand Down Expand Up @@ -147,16 +147,9 @@ public void prepare() throws ServiceNotProvidedException, ModuleStartException {
);
this.registerServiceImplementation(NamingControl.class, namingControl);

annotationScan.registerListener(new DefaultScopeDefine.Listener());
annotationScan.registerListener(new ZipkinStreamAnnotationListener(getManager()));

AnnotationScan scopeScan = new AnnotationScan();
scopeScan.registerListener(new DefaultScopeDefine.Listener());
try {
scopeScan.scan();
} catch (Exception e) {
throw new ModuleStartException(e.getMessage(), e);
}

HTTPServerConfig httpServerConfig = HTTPServerConfig.builder()
.host(moduleConfig.getRestHost())
.port(moduleConfig.getRestPort())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2015-2023 The OpenZipkin Authors
*
* Licensed 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 zipkin.server.core;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ScanResult;
import org.apache.skywalking.oap.server.core.annotation.AnnotationListener;
import org.apache.skywalking.oap.server.core.storage.StorageException;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class ZipkinAnnotationScan {
private final List<AnnotationListenerCache> listeners;

public ZipkinAnnotationScan() {
this.listeners = new LinkedList<>();
}

/**
* Register the callback listener
*
* @param listener to be called after class found w/ annotation
*/
public void registerListener(AnnotationListener listener) {
listeners.add(new AnnotationListenerCache(listener));
}

public void scan() throws IOException, StorageException {
ClassGraph classGraph = new ClassGraph();
classGraph.enableClassInfo();
final ScanResult scan = classGraph.scan();
for (ClassInfo classInfo : scan.getAllClasses()) {
// not skywalking package or a subclass should ignore
if (!classInfo.getName().startsWith("org.apache.skywalking") || classInfo.getName().contains("$")) {
continue;
}
final Class<?> aClass = classInfo.loadClass();
for (AnnotationListenerCache listener : listeners) {
if (aClass.isAnnotationPresent(listener.annotation())) {
listener.addMatch(aClass);
}
}
}

for (AnnotationListenerCache listener : listeners) {
listener.complete();
}
}

private class AnnotationListenerCache {
private AnnotationListener listener;
private List<Class<?>> matchedClass;

private AnnotationListenerCache(AnnotationListener listener) {
this.listener = listener;
matchedClass = new LinkedList<>();
}

private Class<? extends Annotation> annotation() {
return this.listener.annotation();
}

private void addMatch(Class aClass) {
matchedClass.add(aClass);
}

private void complete() throws StorageException {
matchedClass.sort(Comparator.comparing(Class::getName));
for (Class<?> aClass : matchedClass) {
listener.notify(aClass);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,31 @@

package zipkin.server.core;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
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 java.io.IOException;

public class ZipkinDispatcherManager extends DispatcherManager {

@Override
public void scan() throws IOException, IllegalAccessException, InstantiationException {
ClassGraph classGraph = new ClassGraph();
classGraph.enableClassInfo();
final ScanResult scan = classGraph.scan();
for (ClassInfo classInfo : scan.getAllClasses()) {
// not skywalking package or a subclass should ignore
if (!classInfo.getName().startsWith("org.apache.skywalking") || classInfo.getName().contains("$")) {
continue;
}
final Class<?> aClass = classInfo.loadClass();
addIfAsSourceDispatcher(aClass);
}
}

@Override
public void addIfAsSourceDispatcher(Class aClass) throws IllegalAccessException, InstantiationException {
if (aClass.getSimpleName().startsWith("Zipkin") || aClass.equals(TagAutocompleteDispatcher.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.apache.skywalking.oap.server.core.analysis.StreamAnnotationListener;
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.library.module.ModuleDefineHolder;

Expand All @@ -28,7 +29,7 @@ public ZipkinStreamAnnotationListener(ModuleDefineHolder moduleDefineHolder) {
@Override
public void notify(Class aClass) throws StorageException {
// only including all zipkin streaming
if (aClass.getSimpleName().startsWith("Zipkin") || aClass.equals(TagAutocompleteData.class)) {
if (aClass.getSimpleName().startsWith("Zipkin") || aClass.equals(TagAutocompleteData.class) || aClass.equals(SpanAttachedEventRecord.class)) {
super.notify(aClass);
}
}
Expand Down

0 comments on commit 657c6a8

Please sign in to comment.