Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support opentracing & demo #311

Merged
merged 4 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions motan-extension/filter-extension/filter-opentracing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0"?>
<!-- ~ Copyright 2009-2016 Weibo, Inc. ~ ~ 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. -->
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.weibo</groupId>
<artifactId>filter-extension</artifactId>
<version>0.2.3-SNAPSHOT</version>
</parent>
<artifactId>filter-opentracing</artifactId>
<name>filter-opentracing</name>
<url>https://github.com/weibocom/motan</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<opentracing.version>0.20.4</opentracing.version>
</properties>
<dependencies>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>${opentracing.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-impl</artifactId>
<version>${opentracing.version}</version>
</dependency>

<!-- for test -->
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
<version>${opentracing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentracing.brave</groupId>
<artifactId>brave-opentracing</artifactId>
<version>0.16.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-spancollector-http</artifactId>
<version>3.16.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-transport-netty</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-springsupport</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2009-2016 Weibo, Inc.
*
* 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 com.weibo.api.motan.filter.opentracing;

import io.opentracing.Span;
import io.opentracing.Tracer;

import com.weibo.api.motan.rpc.RpcContext;

/**
*
* @Description OpenTracingContext hold a TracerFactory which can replaced by different tracer
* implementation.
* @author zhanglei
* @date Dec 8, 2016
*
*/
public class OpenTracingContext {
// replace TracerFactory with any tracer implementation
public static TracerFactory tracerFactory = TracerFactory.DEFAULT;
public static final String ACTIVE_SPAN = "ot_active_span";

public static Tracer getTracer() {
return tracerFactory.getTracer();
}

public static Span getActiveSpan() {
Object span = RpcContext.getContext().getAttribute(ACTIVE_SPAN);
if (span != null && span instanceof Span) {
return (Span) span;
}
return null;
}

public static void setActiveSpan(Span span) {
RpcContext.getContext().putAttribute(ACTIVE_SPAN, span);
}

public void setTracerFactory(TracerFactory tracerFactory) {
OpenTracingContext.tracerFactory = tracerFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright 2009-2016 Weibo, Inc.
*
* 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 com.weibo.api.motan.filter.opentracing;

import io.opentracing.NoopTracer;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.Tracer.SpanBuilder;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
import io.opentracing.propagation.TextMapExtractAdapter;

import java.util.Iterator;
import java.util.Map.Entry;

import com.weibo.api.motan.core.extension.Activation;
import com.weibo.api.motan.core.extension.SpiMeta;
import com.weibo.api.motan.filter.Filter;
import com.weibo.api.motan.rpc.Caller;
import com.weibo.api.motan.rpc.Provider;
import com.weibo.api.motan.rpc.Request;
import com.weibo.api.motan.rpc.Response;
import com.weibo.api.motan.util.LoggerUtil;
import com.weibo.api.motan.util.MotanFrameworkUtil;

/**
*
* @Description This filter enables distributed tracing in Motan clients and servers via @see <a
* href="http://opentracing.io">The OpenTracing Project </a> : a set of consistent,
* expressive, vendor-neutral APIs for distributed tracing and context propagation.
* @author zhanglei
* @date Dec 8, 2016
*
*/
@SpiMeta(name = "opentracing")
@Activation(sequence = 30)
public class OpenTracingFilter implements Filter {

@Override
public Response filter(Caller<?> caller, Request request) {
Tracer tracer = getTracer();
if (tracer == null || tracer instanceof NoopTracer) {
return caller.call(request);
}
if (caller instanceof Provider) { // server end
return processProviderTrace(tracer, caller, request);
} else { // client end
return processRefererTrace(tracer, caller, request);
}
}

protected Tracer getTracer(){
return OpenTracingContext.getTracer();
}

/**
* process trace in client end
*
* @param caller
* @param request
* @return
*/
protected Response processRefererTrace(Tracer tracer, Caller<?> caller, Request request) {
String operationName = buildOperationName(request);
SpanBuilder spanBuilder = tracer.buildSpan(operationName);
Span activeSpan = OpenTracingContext.getActiveSpan();
if (activeSpan != null) {
spanBuilder.asChildOf(activeSpan);
}
Span span = spanBuilder.start();
span.setTag("requestId", request.getRequestId());

attachTraceInfo(tracer, span, request);
return process(caller, request, span);

}

protected Response process(Caller<?> caller, Request request, Span span) {
Exception ex = null;
boolean exception = true;
try {
Response response = caller.call(request);
if (response.getException() != null) {
ex = response.getException();
} else {
exception = false;
}
return response;
} catch (RuntimeException e) {
ex = e;
throw e;
} finally {
try {
if (exception) {
span.log("request fail." + (ex == null ? "unknown exception" : ex.getMessage()));
} else {
span.log("request success.");
}
span.finish();
} catch (Exception e) {
LoggerUtil.error("opentracing span finish error!", e);
}
}
}

protected String buildOperationName(Request request) {
return "Motan_" + MotanFrameworkUtil.getGroupMethodString(request);
}

protected void attachTraceInfo(Tracer tracer, Span span, final Request request) {
tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMap() {

@Override
public void put(String key, String value) {
request.setAttachment(key, value);
}

@Override
public Iterator<Entry<String, String>> iterator() {
throw new UnsupportedOperationException("TextMapInjectAdapter should only be used with Tracer.inject()");
}
});
}

/**
* process trace in server end
*
* @param caller
* @param request
* @return
*/
protected Response processProviderTrace(Tracer tracer, Caller<?> caller, Request request) {
Span span = extractTraceInfo(request, tracer);
span.setTag("requestId", request.getRequestId());
OpenTracingContext.setActiveSpan(span);
return process(caller, request, span);
}

protected Span extractTraceInfo(Request request, Tracer tracer) {
String operationName = buildOperationName(request);
SpanBuilder span = tracer.buildSpan(operationName);
try {
SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments()));
if (spanContext != null) {
span.asChildOf(spanContext);
}
} catch (Exception e) {
span.withTag("Error", "extract from request fail, error msg:" + e.getMessage());
}
return span.start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2009-2016 Weibo, Inc.
*
* 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 com.weibo.api.motan.filter.opentracing;

import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;

import java.util.Iterator;
import java.util.ServiceLoader;

import com.weibo.api.motan.util.LoggerUtil;
/**
*
* @Description TracerFactory
* @author zhanglei
* @date Dec 8, 2016
*
*/
public interface TracerFactory {
public static final TracerFactory DEFAULT = new DefaultTracerFactory();

/**
* get a Tracer implementation. this method may called every request, consider whether singleton
* pattern is needed
*
* @return
*/
Tracer getTracer();

class DefaultTracerFactory implements TracerFactory {
private static Tracer tracer = NoopTracerFactory.create();

static {
loadDefaultTracer();
}

/**
* load SPI Tracer and set default only if one tracer is found.
*/
private static void loadDefaultTracer() {
try {
Iterator<Tracer> implementations = ServiceLoader.load(Tracer.class, Tracer.class.getClassLoader()).iterator();
if (implementations.hasNext()) {
Tracer firstTracer = implementations.next();
if(!implementations.hasNext()){
// only one tracer is found.
tracer = firstTracer;
LoggerUtil.info("io.opentracing.Tracer load in DefaultTracerFactory, " + tracer.getClass().getSimpleName()
+ " is used as default tracer.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该在此处判断,如果有超过一个实现,使用NoopTracer

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改。

} else {
LoggerUtil.info("io.opentracing.Tracer load in DefaultTracerFactory, NoopTracer is used as default tracer since more than one tracer is found.");
}

}
} catch (Exception e) {
LoggerUtil.warn("DefaultTracerFactory load Tracer fail.", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果发生任何异常,需要使用NoopTracer,确保tracer不能为空

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}


@Override
public Tracer getTracer() {
return tracer;
}

}
}
Loading