Skip to content

Commit

Permalink
CXF-8457 Provide OpenTelemetry support
Browse files Browse the repository at this point in the history
A new module under integration/tracing is introduced to add support for tracing with OpenTelemetry (short OTel).

For few changes have been done:

* Add the OTel BOMs in the parent/pom.xml dependency management (too much? should keep only the dependencies that are used?)
* The new module is strongly inspired by the integration/tracing/tracing-opentracing module
* The tracing systest was enriched with tests dedicated to OpenTelemetry in the JaxRS and JaxWS contexts
  • Loading branch information
mederel committed Jul 12, 2023
1 parent 285c671 commit f72c483
Show file tree
Hide file tree
Showing 34 changed files with 2,370 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ derby.log
.sts4-cache/
**/.factorypath
.vscode/
*.xml.bak
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<artifactId>cxf-integration-tracing-opentracing</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-integration-tracing-opentelemetry</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-coloc</artifactId>
Expand Down
1 change: 1 addition & 0 deletions integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<module>cdi</module>
<module>tracing/tracing-brave</module>
<module>tracing/tracing-opentracing</module>
<module>tracing/tracing-opentelemetry</module>
<module>spring-boot</module>
</modules>
</project>
101 changes: 101 additions & 0 deletions integration/tracing/tracing-opentelemetry/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cxf-integration-tracing-opentelemetry</artifactId>
<packaging>bundle</packaging>
<name>Apache CXF Distributed Tracing using OpenTelemetry</name>
<description>Apache CXF Distributed using OpenTelemetry</description>
<url>https://cxf.apache.org</url>
<parent>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-parent</artifactId>
<version>4.0.3-SNAPSHOT</version>
<relativePath>../../../parent/pom.xml</relativePath>
</parent>

<properties>
<cxf.module.name>org.apache.cxf.tracing.opentelemetry</cxf.module.name>
<cxf.osgi.export>
org.apache.cxf.tracing,
org.apache.cxf.tracing.opentelemetry,
org.apache.cxf.tracing.opentelemetry.jaxrs
</cxf.osgi.export>
</properties>

<dependencies>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-management</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-semconv</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* 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.cxf.tracing.opentelemetry;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import io.opentelemetry.api.trace.Tracer;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptor;

public abstract class AbstractOpenTelemetryClientInterceptor extends AbstractOpenTelemetryClientProvider
implements PhaseInterceptor<Message> {

private String phase;

protected AbstractOpenTelemetryClientInterceptor(final String phase, final Tracer tracer) {
super(tracer);
this.phase = phase;
}

public Collection<PhaseInterceptor<? extends Message>> getAdditionalInterceptors() {
return null;
}

public Set<String> getAfter() {
return Collections.emptySet();
}

public Set<String> getBefore() {
return Collections.emptySet();
}

public String getId() {
return getClass().getName();
}

public String getPhase() {
return phase;
}

public void handleFault(Message message) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.cxf.tracing.opentelemetry;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.tracing.AbstractTracingProvider;
import org.apache.cxf.tracing.opentelemetry.internal.TextMapInjectAdapter;

public abstract class AbstractOpenTelemetryClientProvider extends AbstractTracingProvider {
protected static final Logger LOG = LogUtils.getL7dLogger(AbstractOpenTelemetryClientProvider.class);
protected static final String TRACE_SPAN = "org.apache.cxf.tracing.client.opentelemetry.span";

private final Tracer tracer;

public AbstractOpenTelemetryClientProvider(final Tracer tracer) {
this.tracer = tracer;
}

protected TraceScopeHolder<TraceScope> startTraceSpan(final Map<String, List<String>> requestHeaders,
URI uri, String method) {
Context parentContext = Context.current();
Span activeSpan = tracer.spanBuilder(buildSpanDescription(uri.toString(), method))
.setParent(parentContext).setSpanKind(SpanKind.CLIENT)
.setAttribute(SemanticAttributes.HTTP_METHOD, method)
.setAttribute(SemanticAttributes.HTTP_URL, uri.toString())
// TODO: Enhance with semantics from request
.startSpan();
Scope scope = activeSpan.makeCurrent();

GlobalOpenTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), requestHeaders,
TextMapInjectAdapter.get());

// In case of asynchronous client invocation, the span should be detached as JAX-RS
// client request / response filters are going to be executed in different threads.
Span span = null;
if (isAsyncInvocation()) {
span = activeSpan;
scope.close();
}

return new TraceScopeHolder<TraceScope>(new TraceScope(activeSpan, scope, null),
span != null /* detached */);
}

private boolean isAsyncInvocation() {
return !PhaseInterceptorChain.getCurrentMessage().getExchange().isSynchronous();
}

protected void stopTraceSpan(final TraceScopeHolder<TraceScope> holder, final int responseStatus) {
if (holder == null) {
return;
}

final TraceScope traceScope = holder.getScope();
if (traceScope != null) {
Span span = traceScope.getSpan();
Scope scope = traceScope.getScope();

// If the client invocation was asynchronous , the trace span has been created
// in another thread and should be re-attached to the current one.
if (holder.isDetached()) {
scope = span.makeCurrent();
}

span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE.getKey(), responseStatus);
// TODO: enrich with other semantics

span.end();

scope.close();
}
}

protected void stopTraceSpan(final TraceScopeHolder<TraceScope> holder, final Throwable ex) {
if (holder == null) {
return;
}

final TraceScope traceScope = holder.getScope();
if (traceScope != null) {
Span span = traceScope.getSpan();
Scope scope = traceScope.getScope();

// If the client invocation was asynchronous , the trace span has been created
// in another thread and should be re-attached to the current one.
if (holder.isDetached()) {
scope = span.makeCurrent();
}

span.setStatus(StatusCode.ERROR);
if (ex != null) {
span.recordException(ex, Attributes.of(SemanticAttributes.EXCEPTION_ESCAPED, true));
}
span.end();

scope.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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.cxf.tracing.opentelemetry;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import io.opentelemetry.api.trace.Tracer;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptor;

public abstract class AbstractOpenTelemetryInterceptor extends AbstractOpenTelemetryProvider
implements PhaseInterceptor<Message> {

private final String phase;

protected AbstractOpenTelemetryInterceptor(String phase, Tracer tracer) {
super(tracer);
this.phase = phase;
}

@Override
public Set<String> getAfter() {
return Collections.emptySet();
}

@Override
public Set<String> getBefore() {
return Collections.emptySet();
}

@Override
public String getId() {
return getClass().getName();
}

@Override
public String getPhase() {
return phase;
}

@Override
public Collection<PhaseInterceptor<? extends Message>> getAdditionalInterceptors() {
return null;
}

@Override
public void handleFault(Message message) {
}
}
Loading

0 comments on commit f72c483

Please sign in to comment.