启动时SPI注入的模块TraceModuleProvider,start方法中注入了两种协议的处理器(grpc和rest):
@Override
public void start() {
GRPCHandlerRegister grpcHandlerRegister = getManager().find(SharingServerModule.NAME)
.provider()
.getService(GRPCHandlerRegister.class);
JettyHandlerRegister jettyHandlerRegister = getManager().find(SharingServerModule.NAME)
.provider()
.getService(JettyHandlerRegister.class);
TraceSegmentReportServiceHandler traceSegmentReportServiceHandler = new TraceSegmentReportServiceHandler(getManager());
grpcHandlerRegister.addHandler(traceSegmentReportServiceHandler);
grpcHandlerRegister.addHandler(new TraceSegmentReportServiceHandlerCompat(traceSegmentReportServiceHandler));
jettyHandlerRegister.addHandler(new TraceSegmentReportListServletHandler(getManager()));
jettyHandlerRegister.addHandler(new TraceSegmentReportSingleServletHandler(getManager()));
}
以grpc为例,看TraceSegmentReportServiceHandler:
@Override
public StreamObserver<SegmentObject> collect(StreamObserver<Commands> responseObserver) {
return new StreamObserver<SegmentObject>() {
@Override
public void onNext(SegmentObject segment) {
if (log.isDebugEnabled()) {
log.debug("received segment in streaming");
}
HistogramMetrics.Timer timer = histogram.createTimer();
try {
segmentParserService.send(segment);
} catch (Exception e) {
errorCounter.inc();
log.error(e.getMessage(), e);
} finally {
timer.finish();
}
}
@Override
public void onError(Throwable throwable) {
log.error(throwable.getMessage(), throwable);
responseObserver.onCompleted();
}
@Override
public void onCompleted() {
responseObserver.onNext(Commands.newBuilder().build());
responseObserver.onCompleted();
}
};
}
当收到消息时,调用onNext,主要就是 segmentParserService.send(segment):
@Override
public void send(SegmentObject segment) {
final TraceAnalyzer traceAnalyzer = new TraceAnalyzer(moduleManager, listenerManager, config);
traceAnalyzer.doAnalysis(segment);
}
trance解析器解析segment:
public void doAnalysis(SegmentObject segmentObject) {
if (segmentObject.getSpansList().size() == 0) {
return;
}
createSpanListeners();
notifySegmentListener(segmentObject);
segmentObject.getSpansList().forEach(spanObject -> {
if (spanObject.getSpanId() == 0) {
notifyFirstListener(spanObject, segmentObject);
}
if (SpanType.Exit.equals(spanObject.getSpanType())) {
notifyExitListener(spanObject, segmentObject);
} else if (SpanType.Entry.equals(spanObject.getSpanType())) {
notifyEntryListener(spanObject, segmentObject);
} else if (SpanType.Local.equals(spanObject.getSpanType())) {
notifyLocalListener(spanObject, segmentObject);
} else {
log.error("span type value was unexpected, span type name: {}", spanObject.getSpanType()
.name());
}
});
notifyListenerToBuild();
}
里面有几个监听器通知,主要是先创建一个SegmentAnalysisListener对象,通知时对segmentObject做些处理,最后通知build:
private void notifyListenerToBuild() {
analysisListeners.forEach(AnalysisListener::build);
}
这里analysisListeners是个集合,有多个监听器,其中
MultiScopesAnalysisListener晚点再说,主要是构造出多种作用域对象进行处理,指标操作在这里。
NetworkAddressAliasMappingListener的build是空实现,暂时不用管。
SegmentAnalysisListener的build如下:
@Override
public void build() {
if (log.isDebugEnabled()) {
log.debug("segment listener build, segment id: {}", segment.getSegmentId());
}
if (sampleStatus.equals(SAMPLE_STATUS.IGNORE)) {
return;
}
segment.setEndpointId(endpointId);
segment.setEndpointName(endpointName);
sourceReceiver.receive(segment);
}
sampleStatus、segment、endpointId、endpointName在之前通知时已经处理过了。主要是sourceReceiver.receive(segment):
@Override
public void receive(Source source) {
dispatcherManager.forward(source);
}
dispatcherManager进行分发:
public void forward(Source source) {
if (source == null) {