Skip to content

Commit

Permalink
Refactoring tracing interfaces to camel-api and camel-support
Browse files Browse the repository at this point in the history
This allows moving the span decorators closer to the components.
  • Loading branch information
ammachado committed Aug 16, 2024
1 parent e5ab164 commit 8f9a1bb
Show file tree
Hide file tree
Showing 52 changed files with 355 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import org.apache.camel.Message;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.tracing.decorators.AbstractInternalSpanDecorator;
import org.apache.camel.tracing.ExtractAdapter;
import org.apache.camel.tracing.InjectAdapter;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.SpanDecorator;
import org.apache.camel.tracing.SpanKind;
import org.apache.camel.tracing.decorators.AbstractInternalSpanDecorator;

@ManagedResource(description = "MicrometerObservationTracer")
public class MicrometerObservationTracer extends org.apache.camel.tracing.Tracer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import org.apache.camel.spi.Configurer;
import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.tracing.decorators.AbstractInternalSpanDecorator;
import org.apache.camel.tracing.ExtractAdapter;
import org.apache.camel.tracing.InjectAdapter;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.SpanDecorator;
import org.apache.camel.tracing.decorators.AbstractInternalSpanDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.tracing;

import org.apache.camel.Exchange;
import org.apache.camel.ExchangePropertyKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
Expand All @@ -28,7 +29,6 @@ public final class ActiveSpanManager {

public static final String MDC_TRACE_ID = "trace_id";
public static final String MDC_SPAN_ID = "span_id";
private static final String ACTIVE_SPAN_PROPERTY = "OpenTracing.activeSpan";
private static final Logger LOG = LoggerFactory.getLogger(ActiveSpanManager.class);

private ActiveSpanManager() {
Expand All @@ -41,7 +41,7 @@ private ActiveSpanManager() {
* @return The current active span, or null if none exists
*/
public static SpanAdapter getSpan(Exchange exchange) {
Holder holder = (Holder) exchange.getProperty(ACTIVE_SPAN_PROPERTY);
Holder holder = exchange.getProperty(ExchangePropertyKey.ACTIVE_SPAN, Holder.class);
if (holder != null) {
return holder.getSpan();
}
Expand All @@ -56,9 +56,9 @@ public static SpanAdapter getSpan(Exchange exchange) {
* @param span The span
*/
public static void activate(Exchange exchange, SpanAdapter span) {
exchange.setProperty(ACTIVE_SPAN_PROPERTY,
new Holder((Holder) exchange.getProperty(ACTIVE_SPAN_PROPERTY), span));
if (exchange.getContext().isUseMDCLogging()) {
exchange.setProperty(ExchangePropertyKey.ACTIVE_SPAN,
new Holder(exchange.getProperty(ExchangePropertyKey.ACTIVE_SPAN, Holder.class), span));
if (Boolean.TRUE.equals(exchange.getContext().isUseMDCLogging())) {
MDC.put(MDC_TRACE_ID, span.traceId());
MDC.put(MDC_SPAN_ID, span.spanId());
}
Expand All @@ -72,12 +72,12 @@ public static void activate(Exchange exchange, SpanAdapter span) {
* @param exchange The exchange
*/
public static void deactivate(Exchange exchange) {
Holder holder = (Holder) exchange.getProperty(ACTIVE_SPAN_PROPERTY);
Holder holder = exchange.getProperty(ExchangePropertyKey.ACTIVE_SPAN, Holder.class);
if (holder != null) {
exchange.setProperty(ACTIVE_SPAN_PROPERTY, holder.getParent());
exchange.setProperty(ExchangePropertyKey.ACTIVE_SPAN, holder.getParent());

holder.closeScope();
if (exchange.getContext().isUseMDCLogging()) {
if (Boolean.TRUE.equals(exchange.getContext().isUseMDCLogging())) {
Holder parent = holder.getParent();
if (parent != null) {
SpanAdapter span = holder.getParent().getSpan();
Expand All @@ -92,14 +92,14 @@ public static void deactivate(Exchange exchange) {
}

/**
* If underlying span is active, closes its scope without ending the span. This methods should be called after async
* execution is started on the same thread on which span was activated. ExchangeAsyncStartedEvent is used to notify
* about it.
* If the underlying span is active, closes its scope without ending the span. This method should be called after
* async execution is started on the same thread on which span was activated. ExchangeAsyncStartedEvent is used to
* notify about it.
*
* @param exchange The exchange
*/
public static void endScope(Exchange exchange) {
Holder holder = (Holder) exchange.getProperty(ACTIVE_SPAN_PROPERTY);
Holder holder = exchange.getProperty(ExchangePropertyKey.ACTIVE_SPAN, Holder.class);
if (holder != null) {
holder.closeScope();
}
Expand All @@ -111,9 +111,9 @@ public static void endScope(Exchange exchange) {
* unwound when the responses are processed.
*/
public static class Holder {
private Holder parent;
private SpanAdapter span;
private AutoCloseable scope;
private final Holder parent;
private final SpanAdapter span;
private final AutoCloseable scope;

public Holder(Holder parent, SpanAdapter span) {
this.parent = parent;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,30 @@
import org.apache.camel.support.RoutePolicySupport;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class Tracer extends ServiceSupport implements CamelTracingService, RoutePolicyFactory, StaticService {
protected static final Map<String, SpanDecorator> DECORATORS = new HashMap<>();
static final AutoCloseable NOOP_CLOSEABLE = () -> {
};
private static final Logger LOG = LoggerFactory.getLogger(Tracer.class);

private static final SpanDecorator DEFAULT_SPAN_DECORATOR = new AbstractSpanDecorator() {

@Override
public String getComponent() {
return null;
}

@Override
public String getComponentClassName() {
return null;
}

};

static {
ServiceLoader.load(SpanDecorator.class).forEach(d -> {
SpanDecorator existing = DECORATORS.get(d.getComponent());
Expand Down Expand Up @@ -194,18 +207,16 @@ protected SpanDecorator getSpanDecorator(Endpoint endpoint) {
String scheme = splitURI[0];
sd = DECORATORS.get(scheme);
}
if (sd == null) {
// okay there was no decorator found via component name (scheme), then try FQN
if (endpoint instanceof DefaultEndpoint de) {
Component comp = de.getComponent();
String fqn = comp.getClass().getName();
// lookup via FQN
sd = DECORATORS.values().stream().filter(d -> fqn.equals(d.getComponentClassName())).findFirst()
.orElse(null);
}
// okay, there was no decorator found via component name (scheme), then try FQN
if (sd == null && endpoint instanceof DefaultEndpoint de) {
Component comp = de.getComponent();
String fqn = comp.getClass().getName();
// lookup via FQN
sd = DECORATORS.values().stream().filter(d -> fqn.equals(d.getComponentClassName())).findFirst()
.orElse(null);
}
if (sd == null) {
sd = SpanDecorator.DEFAULT;
sd = DEFAULT_SPAN_DECORATOR;
}

return sd;
Expand Down Expand Up @@ -238,8 +249,7 @@ public TracingEventNotifier() {
@Override
public void notify(CamelEvent event) throws Exception {
try {
if (event instanceof CamelEvent.ExchangeSendingEvent) {
CamelEvent.ExchangeSendingEvent ese = (CamelEvent.ExchangeSendingEvent) event;
if (event instanceof CamelEvent.ExchangeSendingEvent ese) {
SpanDecorator sd = getSpanDecorator(ese.getEndpoint());
if (shouldExclude(sd, ese.getExchange(), ese.getEndpoint())) {
return;
Expand All @@ -255,8 +265,7 @@ public void notify(CamelEvent event) throws Exception {
if (LOG.isTraceEnabled()) {
LOG.trace("Tracing: start client span: {}", span);
}
} else if (event instanceof CamelEvent.ExchangeSentEvent) {
CamelEvent.ExchangeSentEvent ese = (CamelEvent.ExchangeSentEvent) event;
} else if (event instanceof CamelEvent.ExchangeSentEvent ese) {
SpanDecorator sd = getSpanDecorator(ese.getEndpoint());
if (shouldExclude(sd, ese.getExchange(), ese.getEndpoint())) {
return;
Expand All @@ -273,11 +282,9 @@ public void notify(CamelEvent event) throws Exception {
} else {
LOG.warn("Tracing: could not find managed span for exchange: {}", ese.getExchange());
}
} else if (event instanceof CamelEvent.ExchangeAsyncProcessingStartedEvent) {
CamelEvent.ExchangeAsyncProcessingStartedEvent eap = (CamelEvent.ExchangeAsyncProcessingStartedEvent) event;

} else if (event instanceof CamelEvent.ExchangeAsyncProcessingStartedEvent eap) {
// no need to filter scopes here. It's ok to close a scope multiple times and
// implementations check if scope being disposed is current
// implementations check if the scope being disposed is current
// and should not do anything if scopes don't match.
ActiveSpanManager.endScope(eap.getExchange());
}
Expand All @@ -288,8 +295,7 @@ public void notify(CamelEvent event) throws Exception {
}

private boolean shouldExclude(SpanDecorator sd, Exchange exchange, Endpoint endpoint) {
return !sd.newSpan()
|| isExcluded(exchange, endpoint);
return !sd.newSpan() || isExcluded(exchange, endpoint);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.TagConstants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;
import org.apache.camel.support.tracing.propagators.CamelMessagingHeadersExtractAdapter;
import org.apache.camel.support.tracing.propagators.CamelMessagingHeadersInjectAdapter;
import org.apache.camel.tracing.ExtractAdapter;
import org.apache.camel.tracing.InjectAdapter;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.SpanKind;
import org.apache.camel.tracing.TagConstants;
import org.apache.camel.tracing.propagation.CamelMessagingHeadersExtractAdapter;
import org.apache.camel.tracing.propagation.CamelMessagingHeadersInjectAdapter;

public abstract class AbstractMessagingSpanDecorator extends AbstractSpanDecorator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.TagConstants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.tracing.decorators;

import org.apache.camel.support.tracing.decorators.AbstractInternalSpanDecorator;

public class DirectSpanDecorator extends AbstractInternalSpanDecorator {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.tracing.decorators;

import org.apache.camel.support.tracing.decorators.AbstractInternalSpanDecorator;

public class DisruptorSpanDecorator extends AbstractInternalSpanDecorator {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.tracing.decorators;

import org.apache.camel.support.tracing.decorators.AbstractInternalSpanDecorator;

public class DisruptorvmSpanDecorator extends AbstractInternalSpanDecorator {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.TagConstants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.TagConstants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.TagConstants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.tracing.decorators;

import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;

public class LogSpanDecorator extends AbstractSpanDecorator {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.support.tracing.decorators.AbstractSpanDecorator;
import org.apache.camel.tracing.SpanAdapter;
import org.apache.camel.tracing.TagConstants;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.tracing.decorators;

import org.apache.camel.support.tracing.decorators.AbstractInternalSpanDecorator;

public class SedaSpanDecorator extends AbstractInternalSpanDecorator {

@Override
Expand Down
Loading

0 comments on commit 8f9a1bb

Please sign in to comment.