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
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapGetter;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.opentelemetry2.decorators.OpenTelemetryHttpSpanDecorator;
import org.apache.camel.opentelemetry2.decorators.OpenTelemetryMessagingSpanDecorator;
import org.apache.camel.spi.Configurer;
import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.telemetry.Op;
import org.apache.camel.telemetry.Span;
import org.apache.camel.telemetry.SpanContextPropagationExtractor;
import org.apache.camel.telemetry.SpanContextPropagationInjector;
import org.apache.camel.telemetry.SpanDecorator;
import org.apache.camel.telemetry.SpanDecoratorManager;
import org.apache.camel.telemetry.SpanDecoratorManagerImpl;
import org.apache.camel.telemetry.SpanLifecycleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -44,6 +53,20 @@ public class OpenTelemetryTracer extends org.apache.camel.telemetry.Tracer {

private Tracer tracer;
private ContextPropagators contextPropagators;
private final SpanDecoratorManager spanDecoratorManager = new SpanDecoratorManagerImpl();

// ThreadLocal storage for passing decorator and operation context to SpanLifecycleManager
private static final ThreadLocal<SpanCreationContext> spanCreationContext = new ThreadLocal<>();

private static class SpanCreationContext {
final SpanDecorator decorator;
final Op operation;

SpanCreationContext(SpanDecorator decorator, Op operation) {
this.decorator = decorator;
this.operation = operation;
}
}

@Override
protected void initTracer() {
Expand Down Expand Up @@ -79,6 +102,32 @@ void setContextPropagators(ContextPropagators cp) {
this.contextPropagators = cp;
}

@Override
protected void beginEventSpan(Exchange exchange, Endpoint endpoint, Op op) throws Exception {
// Store decorator and operation in ThreadLocal so SpanLifecycleManager can access it
SpanDecorator decorator = spanDecoratorManager.get(endpoint);
spanCreationContext.set(new SpanCreationContext(decorator, op));
try {
super.beginEventSpan(exchange, endpoint, op);
} finally {
// Clean up ThreadLocal to avoid memory leaks
spanCreationContext.remove();
}
}

@Override
protected void beginProcessorSpan(Exchange exchange, String processorName) throws Exception {
// Store decorator and operation in ThreadLocal so SpanLifecycleManager can access it
SpanDecorator decorator = spanDecoratorManager.get(processorName);
spanCreationContext.set(new SpanCreationContext(decorator, Op.EVENT_PROCESS));
try {
super.beginProcessorSpan(exchange, processorName);
} finally {
// Clean up ThreadLocal to avoid memory leaks
spanCreationContext.remove();
}
}

@Override
protected void doStart() throws Exception {
super.doStart();
Expand Down Expand Up @@ -135,9 +184,39 @@ public String get(SpanContextPropagationExtractor carrier, String key) {
baggage = Baggage.fromContext(ctx);
}

// Set SpanKind based on decorator and operation type
SpanKind spanKind = determineSpanKind();
if (spanKind != null) {
builder.setSpanKind(spanKind);
}

return new OpenTelemetrySpanAdapter(builder.startSpan(), baggage);
}

private SpanKind determineSpanKind() {
SpanCreationContext context = spanCreationContext.get();
if (context == null) {
return SpanKind.INTERNAL;
} else if (context.decorator instanceof OpenTelemetryHttpSpanDecorator http) {
return getSpanKindForOp(context.operation, http::getInitiatorSpanKind, http::getReceiverSpanKind);
} else if (context.decorator instanceof OpenTelemetryMessagingSpanDecorator msg) {
return getSpanKindForOp(context.operation, msg::getInitiatorSpanKind, msg::getReceiverSpanKind);
}

return SpanKind.INTERNAL;
}

private SpanKind getSpanKindForOp(
Op operation,
java.util.function.Supplier<SpanKind> initiator,
java.util.function.Supplier<SpanKind> receiver) {
return switch (operation) {
case EVENT_SENT -> initiator.get();
case EVENT_RECEIVED -> receiver.get();
default -> SpanKind.INTERNAL;
};
}

@Override
public void activate(Span span) {
OpenTelemetrySpanAdapter otelSpan = (OpenTelemetrySpanAdapter) span;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.opentelemetry2.decorators;

/**
* OpenTelemetry-aware ActiveMQ 6 span decorator. Extends the camel-telemetry ActiveMQ6 decorator and adds SpanKind.
*/
public class ActiveMQ6SpanDecorator extends org.apache.camel.telemetry.decorators.ActiveMQ6SpanDecorator
implements OpenTelemetryMessagingSpanDecorator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.opentelemetry2.decorators;

/**
* OpenTelemetry-aware ActiveMQ span decorator. Extends the camel-telemetry ActiveMQ decorator and adds SpanKind.
*/
public class ActiveMQSpanDecorator extends org.apache.camel.telemetry.decorators.ActiveMQSpanDecorator
implements OpenTelemetryMessagingSpanDecorator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.opentelemetry2.decorators;

/**
* OpenTelemetry-aware AMQP span decorator. Extends the camel-telemetry AMQP decorator and adds SpanKind.
*/
public class AmqpSpanDecorator extends org.apache.camel.telemetry.decorators.AmqpSpanDecorator
implements OpenTelemetryMessagingSpanDecorator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.opentelemetry2.decorators;

/**
* OpenTelemetry-aware HTTP span decorator. Extends the camel-telemetry HTTP decorator to inherit all HTTP-specific
* behavior, and implements the OpenTelemetry interface to add SpanKind.
*/
public class HttpSpanDecorator extends org.apache.camel.telemetry.decorators.HttpSpanDecorator
implements OpenTelemetryHttpSpanDecorator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.opentelemetry2.decorators;

/**
* OpenTelemetry-aware HTTPS span decorator. Extends the camel-telemetry HTTPS decorator and adds SpanKind.
*/
public class HttpsSpanDecorator extends org.apache.camel.telemetry.decorators.HttpsSpanDecorator
implements OpenTelemetryHttpSpanDecorator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.opentelemetry2.decorators;

/**
* OpenTelemetry-aware JMS span decorator. Extends the camel-telemetry JMS decorator and adds SpanKind.
*/
public class JmsSpanDecorator extends org.apache.camel.telemetry.decorators.JmsSpanDecorator
implements OpenTelemetryMessagingSpanDecorator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.opentelemetry2.decorators;

/**
* OpenTelemetry-aware Kafka span decorator. Extends the camel-telemetry Kafka decorator to inherit all Kafka-specific
* behavior (partition, offset, key tags), and implements the OpenTelemetry interface to add SpanKind.
*/
public class KafkaSpanDecorator extends org.apache.camel.telemetry.decorators.KafkaSpanDecorator
implements OpenTelemetryMessagingSpanDecorator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.opentelemetry2.decorators;

/**
* OpenTelemetry-aware Netty HTTP span decorator. Extends the camel-telemetry Netty HTTP decorator and adds SpanKind.
*/
public class NettyHttpSpanDecorator extends org.apache.camel.telemetry.decorators.NettyHttpSpanDecorator
implements OpenTelemetryHttpSpanDecorator {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.opentelemetry2.decorators;

import io.opentelemetry.api.trace.SpanKind;
import org.apache.camel.telemetry.SpanDecorator;

/**
* Interface for OpenTelemetry HTTP span decorators.
* <p>
* HTTP client calls use SpanKind.CLIENT and HTTP server endpoints use SpanKind.SERVER.
* </p>
* <p>
* Concrete decorators should extend the corresponding camel-telemetry decorator and implement this interface.
* </p>
*/
public interface OpenTelemetryHttpSpanDecorator extends SpanDecorator {

default SpanKind getInitiatorSpanKind() {
return SpanKind.CLIENT;
}

default SpanKind getReceiverSpanKind() {
return SpanKind.SERVER;
}

}
Loading
Loading