This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.4.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.4.x by this push: new 5cd896e CAMEL-15710: Fixing onExchangeBegin/onExchangeEnd (#4496) 5cd896e is described below commit 5cd896e9522027ebffe41152de4a745624f1daf0 Author: Bogdan Ilchyshyn <orange-buff...@users.noreply.github.com> AuthorDate: Fri Oct 23 15:10:15 2020 +1100 CAMEL-15710: Fixing onExchangeBegin/onExchangeEnd (#4496) --- .../camel/opentracing/ActiveScopeManager.java | 87 ++++++++++++++++++++++ .../camel/opentracing/OpenTracingTracer.java | 11 +-- 2 files changed, 91 insertions(+), 7 deletions(-) diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/ActiveScopeManager.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/ActiveScopeManager.java new file mode 100644 index 0000000..4c2b317 --- /dev/null +++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/ActiveScopeManager.java @@ -0,0 +1,87 @@ +/* + * 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.camel.opentracing; + +import io.opentracing.Scope; +import io.opentracing.Span; +import io.opentracing.Tracer; +import org.apache.camel.Exchange; + +/** + * Utility class for managing active tracing scope as a stack associated with + * an exchange. Should be a part of {@link ActiveSpanManager} but is a separate class to keep API backwards compatible. + * + */ +final class ActiveScopeManager { + + private static final String ACTIVE_SCOPE_PROPERTY = "OpenTracing.activeScope"; + + private ActiveScopeManager() { + } + + /** + * Activates the supplied span for the supplied tracer and adds the activated scope into the stack on exchange. + * + * @param exchange The exchange + * @param span The span + */ + static void activate(Exchange exchange, Span span, Tracer tracer) { + Scope scope = tracer.activateSpan(span); + exchange.setProperty(ACTIVE_SCOPE_PROPERTY, + new Holder(exchange.getProperty(ACTIVE_SCOPE_PROPERTY, Holder.class), scope)); + } + + /** + * Deactivates an existing active span scope associated with the + * supplied exchange. Once deactivated, if a parent scope is found + * associated with the stack for the exchange, it will be restored + * as the current scope for that exchange. + * + * @param exchange The exchange + */ + static void deactivate(Exchange exchange) { + Holder holder = exchange.getProperty(ACTIVE_SCOPE_PROPERTY, Holder.class); + if (holder != null) { + holder.getScope().close(); + exchange.setProperty(ACTIVE_SCOPE_PROPERTY, holder.getParent()); + } + } + + /** + * Simple holder for the currently active scope and an optional reference to + * the parent holder. This will be used to maintain a stack for scopes, built + * up during the execution of a series of chained camel exchanges, and then + * unwound when the responses are processed. + */ + private static final class Holder { + private final Holder parent; + private final Scope scope; + + Holder(Holder parent, Scope scope) { + this.parent = parent; + this.scope = scope; + } + + Holder getParent() { + return parent; + } + + Scope getScope() { + return scope; + } + } +} diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java index 4f170fa..9b95143 100644 --- a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java +++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/OpenTracingTracer.java @@ -16,7 +16,6 @@ */ package org.apache.camel.opentracing; -import io.opentracing.Scope; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -271,9 +270,7 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact sd.pre(span, ese.getExchange(), ese.getEndpoint()); tracer.inject(span.context(), Format.Builtin.TEXT_MAP, sd.getInjectAdapter(ese.getExchange().getIn().getHeaders(), encoding)); ActiveSpanManager.activate(ese.getExchange(), span); - Scope scope = tracer.activateSpan(span); - // TODO: this seems to be better encapsulated in ActiveSpanManager, but how to pass tracer without breaking public API? - ese.getExchange().setProperty("OpenTracing.activeScope", scope); + ActiveScopeManager.activate(ese.getExchange(), span, tracer); if (LOG.isTraceEnabled()) { LOG.trace("OpenTracing: start client span={}", span); @@ -292,9 +289,7 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact sd.post(span, ese.getExchange(), ese.getEndpoint()); span.finish(); ActiveSpanManager.deactivate(ese.getExchange()); - // TODO: this seems to be better encapsulated in ActiveSpanManager, but how to pass tracer without breaking public API? - Scope scope = ese.getExchange().getProperty("OpenTracing.activeScope", Scope.class); - scope.close(); + ActiveScopeManager.deactivate(ese.getExchange()); } else { LOG.warn("OpenTracing: could not find managed span for exchange={}", ese.getExchange()); } @@ -333,6 +328,7 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact .withTag(Tags.SPAN_KIND.getKey(), sd.getReceiverSpanKind()).start(); sd.pre(span, exchange, route.getEndpoint()); ActiveSpanManager.activate(exchange, span); + ActiveScopeManager.activate(exchange, span, tracer); if (LOG.isTraceEnabled()) { LOG.trace("OpenTracing: start server span={}", span); } @@ -357,6 +353,7 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact sd.post(span, exchange, route.getEndpoint()); span.finish(); ActiveSpanManager.deactivate(exchange); + ActiveScopeManager.deactivate(exchange); } else { LOG.warn("OpenTracing: could not find managed span for exchange={}", exchange); }