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 2140f2a
Show file tree
Hide file tree
Showing 52 changed files with 341 additions and 216 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 @@ -28,7 +28,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 +40,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 = (Holder) exchange.getProperty(ExchangePropertyKey.ACTIVE_SPAN_PROPERTY);
if (holder != null) {
return holder.getSpan();
}
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
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;

public class TimerSpanDecorator extends AbstractSpanDecorator {

Expand Down
Loading

0 comments on commit 2140f2a

Please sign in to comment.