This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push: new 55784d1 [SCB-1965] add zipkin filter 55784d1 is described below commit 55784d1cfe089cf301f5dfa9398dabc76ceb00b4 Author: wujimin <wuji...@huawei.com> AuthorDate: Sat May 30 21:06:10 2020 +0800 [SCB-1965] add zipkin filter --- .../tracing/zipkin/ZipkinFilterProvider.java | 30 ++++++++++ .../tracing/zipkin/ZipkinTracingFilter.java | 69 ++++++++++++++++++++++ ...g.apache.servicecomb.core.filter.FilterProvider | 18 ++++++ 3 files changed, 117 insertions(+) diff --git a/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/ZipkinFilterProvider.java b/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/ZipkinFilterProvider.java new file mode 100644 index 0000000..7e8d6c5 --- /dev/null +++ b/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/ZipkinFilterProvider.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.servicecomb.tracing.zipkin; + +import java.util.Arrays; +import java.util.List; + +import org.apache.servicecomb.core.filter.Filter; +import org.apache.servicecomb.core.filter.FilterProvider; + +public class ZipkinFilterProvider implements FilterProvider { + @Override + public List<Class<? extends Filter>> getFilters() { + return Arrays.asList(ZipkinTracingFilter.class); + } +} diff --git a/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/ZipkinTracingFilter.java b/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/ZipkinTracingFilter.java new file mode 100644 index 0000000..9d76b26 --- /dev/null +++ b/handlers/handler-tracing-zipkin/src/main/java/org/apache/servicecomb/tracing/zipkin/ZipkinTracingFilter.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.servicecomb.tracing.zipkin; + +import static org.apache.servicecomb.swagger.invocation.InvocationType.PRODUCER; + +import java.util.concurrent.CompletableFuture; + +import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.core.SCBEngine; +import org.apache.servicecomb.core.exception.Exceptions; +import org.apache.servicecomb.core.filter.Filter; +import org.apache.servicecomb.core.filter.FilterMeta; +import org.apache.servicecomb.core.filter.FilterNode; +import org.apache.servicecomb.foundation.common.utils.BeanUtils; +import org.apache.servicecomb.swagger.invocation.Response; + +import brave.Span; +import brave.Tracer.SpanInScope; +import brave.http.HttpTracing; + +@FilterMeta(name = "zipkin") +public class ZipkinTracingFilter implements Filter { + private ZipkinConsumerDelegate consumer; + + private ZipkinProviderDelegate producer; + + @Override + public void init(SCBEngine engine) { + HttpTracing httpTracing = BeanUtils.getContext().getBean(HttpTracing.class); + this.consumer = new ZipkinConsumerDelegate(httpTracing); + this.producer = new ZipkinProviderDelegate(httpTracing); + } + + @SuppressWarnings({"try", "unused"}) + @Override + public CompletableFuture<Response> onFilter(Invocation invocation, FilterNode nextNode) { + ZipkinTracingDelegate tracing = collectTracing(invocation); + + Span span = tracing.createSpan(invocation); + try (SpanInScope scope = tracing.tracer().tracer().withSpanInScope(span)) { + return nextNode.onFilter(invocation) + .whenComplete((response, exception) -> tracing.onResponse(span, response, Exceptions.unwrap(exception))); + } + } + + private ZipkinTracingDelegate collectTracing(Invocation invocation) { + if (PRODUCER.equals(invocation.getInvocationType())) { + return producer; + } + + return consumer; + } +} diff --git a/handlers/handler-tracing-zipkin/src/main/resources/META-INF/services/org.apache.servicecomb.core.filter.FilterProvider b/handlers/handler-tracing-zipkin/src/main/resources/META-INF/services/org.apache.servicecomb.core.filter.FilterProvider new file mode 100644 index 0000000..400b61e --- /dev/null +++ b/handlers/handler-tracing-zipkin/src/main/resources/META-INF/services/org.apache.servicecomb.core.filter.FilterProvider @@ -0,0 +1,18 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# + +org.apache.servicecomb.tracing.zipkin.ZipkinFilterProvider \ No newline at end of file