Skip to content
Closed
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
4 changes: 2 additions & 2 deletions camel-core/src/main/java/org/apache/camel/CamelContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,6 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration {
/**
* Adds a {@link LogListener}.
*/
void addlogListener(LogListener listener);
void addLogListener(LogListener listener);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2691,7 +2691,7 @@ public Set<LogListener> getLogListeners() {
return logListeners;
}

public void addlogListener(LogListener listener) {
public void addLogListener(LogListener listener) {
logListeners.add(listener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testLogMask() throws Exception {
CamelContext context = createCamelContext();
MockEndpoint mock = context.getEndpoint("mock:foo", MockEndpoint.class);
mock.expectedMessageCount(1);
context.addlogListener((exchange, camelLogger, message) -> {
context.addLogListener((exchange, camelLogger, message) -> {
Assert.assertEquals("Exchange[ExchangePattern: InOnly, BodyType: String, Body: hello]", message);
listenerFired = true;
return message + " - modified by listener";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testLogListener() throws Exception {
CamelContext context = createCamelContext();
MockEndpoint mock = context.getEndpoint("mock:foo", MockEndpoint.class);
mock.expectedMessageCount(1);
context.addlogListener((exchange, camelLogger, message) -> {
context.addLogListener((exchange, camelLogger, message) -> {
Assert.assertEquals("Got hello", message);
listenerFired = true;
return message + " - modified by listener";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
import org.apache.camel.opentracing.concurrent.OpenTracingExecutorServiceManager;
import org.apache.camel.opentracing.propagation.CamelHeadersExtractAdapter;
import org.apache.camel.opentracing.propagation.CamelHeadersInjectAdapter;
import org.apache.camel.spi.LogListener;
import org.apache.camel.spi.RoutePolicy;
import org.apache.camel.spi.RoutePolicyFactory;
import org.apache.camel.support.EventNotifierSupport;
import org.apache.camel.support.RoutePolicySupport;
import org.apache.camel.support.ServiceSupport;
import org.apache.camel.util.CamelLogger;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.ServiceHelper;
import org.slf4j.Logger;
Expand All @@ -72,6 +74,7 @@ public class OpenTracingTracer extends ServiceSupport implements RoutePolicyFact
private static Map<String, SpanDecorator> decorators = new HashMap<>();

private final OpenTracingEventNotifier eventNotifier = new OpenTracingEventNotifier();
private final OpenTracingLogListener logListener = new OpenTracingLogListener();
private final CamelSpanManager spanManager = CamelSpanManager.getInstance();
private Tracer tracer;
private CamelContext camelContext;
Expand Down Expand Up @@ -111,6 +114,7 @@ public void init(CamelContext camelContext) {
// Wrap the ExecutorServiceManager with a SpanManager aware version
camelContext.setExecutorServiceManager(
new OpenTracingExecutorServiceManager(camelContext.getExecutorServiceManager(), spanManager));

} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
Expand Down Expand Up @@ -143,6 +147,7 @@ protected void doStart() throws Exception {
if (!camelContext.getRoutePolicyFactories().contains(this)) {
camelContext.addRoutePolicyFactory(this);
}
camelContext.addLogListener(logListener);

if (tracer == null) {
Set<Tracer> tracers = camelContext.getRegistry().findByType(Tracer.class);
Expand Down Expand Up @@ -196,6 +201,9 @@ public void notify(EventObject event) throws Exception {
ExchangeSendingEvent ese = (ExchangeSendingEvent) event;
SpanManager.ManagedSpan parent = spanManager.current();
SpanDecorator sd = getSpanDecorator(ese.getEndpoint());
if (!sd.newSpan()) {
return;
}
SpanBuilder spanBuilder = tracer.buildSpan(sd.getOperationName(ese.getExchange(), ese.getEndpoint()))
.withTag(Tags.SPAN_KIND.getKey(), sd.getInitiatorSpanKind());
// Temporary workaround to avoid adding 'null' span as a parent
Expand All @@ -213,6 +221,10 @@ public void notify(EventObject event) throws Exception {
}
} else if (event instanceof ExchangeSentEvent) {
ExchangeSentEvent ese = (ExchangeSentEvent) event;
SpanDecorator sd = getSpanDecorator(ese.getEndpoint());
if (!sd.newSpan()) {
return;
}
SpanManager.ManagedSpan managedSpan = (SpanManager.ManagedSpan)
ese.getExchange().getProperty(MANAGED_SPAN_PROPERTY);
if (managedSpan != null) {
Expand All @@ -224,7 +236,6 @@ public void notify(EventObject event) throws Exception {
if (LOG.isTraceEnabled()) {
LOG.trace("OpenTracing: start client span=" + managedSpan.getSpan());
}
SpanDecorator sd = getSpanDecorator(ese.getEndpoint());
sd.post(managedSpan.getSpan(), ese.getExchange(), ese.getEndpoint());
managedSpan.getSpan().finish();
managedSpan.deactivate();
Expand Down Expand Up @@ -285,4 +296,24 @@ public void onExchangeDone(Route route, Exchange exchange) {
}
}

private final class OpenTracingLogListener implements LogListener {

@Override
public String onLog(Exchange exchange, CamelLogger camelLogger, String message) {
SpanManager.ManagedSpan managedSpan = (SpanManager.ManagedSpan)
exchange.getProperty(MANAGED_SPAN_PROPERTY);
Span span = null;
if (managedSpan != null) {
span = managedSpan.getSpan();
} else {
span = spanManager.current().getSpan();
}
if (span != null) {
Map<String, Object> fields = new HashMap<>();
fields.put("message", message);
span.log(fields);
}
return message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public String getComponent() {

};

/**
* This method indicates whether the component associated with the SpanDecorator
* should result in a new span being created.
*
* @return Whether a new span should be created
*/
boolean newSpan();

/**
* The camel component associated with the decorator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
*/
public abstract class AbstractSpanDecorator implements SpanDecorator {

@Override
public boolean newSpan() {
return true;
}

@Override
public String getOperationName(Exchange exchange, Endpoint endpoint) {
// OpenTracing aims to use low cardinality operation names. Ideally a specific
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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.decorators;

public class LogSpanDecorator extends AbstractSpanDecorator {

@Override
public String getComponent() {
return "log";
}

@Override
public boolean newSpan() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ org.apache.camel.opentracing.decorators.JdbcSpanDecorator
org.apache.camel.opentracing.decorators.JettySpanDecorator
org.apache.camel.opentracing.decorators.JmsSpanDecorator
org.apache.camel.opentracing.decorators.KafkaSpanDecorator
org.apache.camel.opentracing.decorators.LogSpanDecorator
org.apache.camel.opentracing.decorators.MongoDBSpanDecorator
org.apache.camel.opentracing.decorators.MqttSpanDecorator
org.apache.camel.opentracing.decorators.NettyHttp4SpanDecorator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class ABCRouteTest extends CamelOpenTracingTestSupport {

private static SpanTestData[] testdata = {
new SpanTestData().setLabel("seda:b server").setUri("seda://b").setOperation("b")
.setKind(Tags.SPAN_KIND_SERVER).setParentId(1),
.setKind(Tags.SPAN_KIND_SERVER).setParentId(1).addLogMessage("routing at b"),
new SpanTestData().setLabel("seda:b client").setUri("seda://b").setOperation("b")
.setKind(Tags.SPAN_KIND_CLIENT).setParentId(4),
new SpanTestData().setLabel("seda:c server").setUri("seda://c").setOperation("c")
.setKind(Tags.SPAN_KIND_SERVER).setParentId(3),
.setKind(Tags.SPAN_KIND_SERVER).setParentId(3).addLogMessage("Exchange[ExchangePattern: InOut, BodyType: String, Body: Hello]"),
new SpanTestData().setLabel("seda:c client").setUri("seda://c").setOperation("c")
.setKind(Tags.SPAN_KIND_CLIENT).setParentId(4),
new SpanTestData().setLabel("seda:a server").setUri("seda://a").setOperation("a")
.setKind(Tags.SPAN_KIND_SERVER).setParentId(5),
.setKind(Tags.SPAN_KIND_SERVER).setParentId(5).addLogMessage("routing at a").addLogMessage("End of routing"),
new SpanTestData().setLabel("seda:a client").setUri("seda://a").setOperation("a")
.setKind(Tags.SPAN_KIND_CLIENT).setParentId(6),
new SpanTestData().setLabel("direct:start server").setUri("direct://start").setOperation("start")
Expand Down Expand Up @@ -72,7 +72,7 @@ public void configure() throws Exception {
.delay(simple("${random(1000,2000)}"));

from("seda:c").routeId("c")
.log("routing at ${routeId}")
.to("log:test")
.delay(simple("${random(0,100)}"));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import io.opentracing.Span;
Expand Down Expand Up @@ -95,28 +94,38 @@ protected MockSpan findSpan(SpanTestData testdata, List<MockSpan> spans) {
}

protected void verifySpan(int index, SpanTestData[] testdata, List<MockSpan> spans) {
String component = (String) spans.get(index).tags().get(Tags.COMPONENT.getKey());
MockSpan span = spans.get(index);
SpanTestData td = testdata[index];

String component = (String) span.tags().get(Tags.COMPONENT.getKey());
assertNotNull(component);
assertEquals(testdata[index].getLabel(),
SpanDecorator.CAMEL_COMPONENT + URI.create((String) testdata[index].getUri()).getScheme(),
assertEquals(td.getLabel(),
SpanDecorator.CAMEL_COMPONENT + URI.create((String) td.getUri()).getScheme(),
component);
assertEquals(testdata[index].getLabel(), testdata[index].getUri(), spans.get(index).tags().get("camel.uri"));
assertEquals(td.getLabel(), td.getUri(), span.tags().get("camel.uri"));

// If span associated with TestSEDASpanDecorator, check that pre/post tags have been defined
if ("camel-seda".equals(component)) {
assertTrue(spans.get(index).tags().containsKey("pre"));
assertTrue(spans.get(index).tags().containsKey("post"));
assertTrue(span.tags().containsKey("pre"));
assertTrue(span.tags().containsKey("post"));
}

assertEquals(testdata[index].getLabel(), testdata[index].getOperation(), spans.get(index).operationName());
assertEquals(td.getLabel(), td.getOperation(), span.operationName());

assertEquals(td.getLabel(), td.getKind(),
span.tags().get(Tags.SPAN_KIND.getKey()));

assertEquals(testdata[index].getLabel(), testdata[index].getKind(),
spans.get(index).tags().get(Tags.SPAN_KIND.getKey()));
if (td.getParentId() != -1) {
assertEquals(td.getLabel(),
spans.get(td.getParentId()).context().spanId(),
span.parentId());
}

if (testdata[index].getParentId() != -1) {
assertEquals(testdata[index].getLabel(),
spans.get(testdata[index].getParentId()).context().spanId(),
spans.get(index).parentId());
if (!td.getLogMessages().isEmpty()) {
assertEquals("Number of log messages", td.getLogMessages().size(), span.logEntries().size());
for (int i = 0; i < td.getLogMessages().size(); i++) {
assertEquals(td.getLogMessages().get(i), span.logEntries().get(i).fields().get("message"));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class MulticastParallelRouteTest extends CamelOpenTracingTestSupport {

private static SpanTestData[] testdata = {
new SpanTestData().setLabel("seda:b server").setUri("seda://b").setOperation("b")
.setKind(Tags.SPAN_KIND_SERVER).setParentId(1),
.setKind(Tags.SPAN_KIND_SERVER).setParentId(1).addLogMessage("routing at b"),
new SpanTestData().setLabel("seda:b client").setUri("seda://b").setOperation("b")
.setKind(Tags.SPAN_KIND_CLIENT).setParentId(4),
new SpanTestData().setLabel("seda:c server").setUri("seda://c").setOperation("c")
.setKind(Tags.SPAN_KIND_SERVER).setParentId(3),
.setKind(Tags.SPAN_KIND_SERVER).setParentId(3).addLogMessage("routing at c"),
new SpanTestData().setLabel("seda:c client").setUri("seda://c").setOperation("c")
.setKind(Tags.SPAN_KIND_CLIENT).setParentId(4),
new SpanTestData().setLabel("seda:a server").setUri("seda://a").setOperation("a")
.setKind(Tags.SPAN_KIND_SERVER).setParentId(5),
.setKind(Tags.SPAN_KIND_SERVER).setParentId(5).addLogMessage("routing at a").addLogMessage("End of routing"),
new SpanTestData().setLabel("seda:a client").setUri("seda://a").setOperation("a")
.setKind(Tags.SPAN_KIND_CLIENT).setParentId(6),
new SpanTestData().setLabel("direct:start server").setUri("direct://start").setOperation("start")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
*/
package org.apache.camel.opentracing;

import java.util.ArrayList;
import java.util.List;

public class SpanTestData {

private String label;
private String uri;
private String operation;
private String kind;
private int parentId = -1;
private List<String> logMessages = new ArrayList<>();

public String getLabel() {
return label;
Expand Down Expand Up @@ -69,4 +73,12 @@ public SpanTestData setParentId(int parentId) {
return this;
}

public SpanTestData addLogMessage(String mesg) {
logMessages.add(mesg);
return this;
}

public List<String> getLogMessages() {
return logMessages;
}
}