Skip to content
Permalink
Browse files
GERONIMO-6651 GERONIMO-6652 osgi bundle - tests to do + spec 1.2 upgrade
  • Loading branch information
rmannibucau committed Oct 22, 2018
1 parent 33d97ce commit 94e01f1a4bbce8437a4a461112fbaa9ce95ff802
Showing 22 changed files with 669 additions and 106 deletions.
@@ -35,15 +35,4 @@
<properties>
<geronimo-opentracing.Automatic-Module-Name>org.apache.geronimo.opentracing.common</geronimo-opentracing.Automatic-Module-Name>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>4.0.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
@@ -55,8 +55,7 @@ public interface GeronimoOpenTracingConfig {

static GeronimoOpenTracingConfig create() {
try {
final Iterator<GeronimoOpenTracingConfig> iterator = ServiceLoader.load(GeronimoOpenTracingConfig.class)
.iterator();
final Iterator<GeronimoOpenTracingConfig> iterator = ServiceLoader.load(GeronimoOpenTracingConfig.class).iterator();
if (iterator.hasNext()) {
return new PrefixedConfig(iterator.next());
}
@@ -17,8 +17,13 @@
package org.apache.geronimo.microprofile.opentracing.common.microprofile.server;

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import javax.ws.rs.HttpMethod;
@@ -40,6 +45,7 @@ public class GeronimoOpenTracingFeature implements DynamicFeature {
private Tracer tracer;
private GeronimoOpenTracingConfig config;
private Container container;
private Collection<Pattern> skipPatterns;

public void setTracer(final Tracer tracer) {
this.tracer = tracer;
@@ -64,22 +70,32 @@ public void configure(final ResourceInfo resourceInfo, final FeatureContext cont
if (config == null) {
config = container.lookup(GeronimoOpenTracingConfig.class);
}
if (skipPatterns == null) {
skipPatterns = ofNullable(config.read("mp.opentracing.server.skip-pattern", null))
.map(it -> Stream.of(it.split("\\|")).map(String::trim).filter(p -> !p.isEmpty()).map(Pattern::compile).collect(toList()))
.orElseGet(Collections::emptyList);
}

final Optional<Traced> traced = ofNullable(ofNullable(resourceInfo.getResourceMethod().getAnnotation(Traced.class))
.orElseGet(() -> resourceInfo.getResourceClass().getAnnotation(Traced.class)));
if (!traced.map(Traced::value).orElse(true)) {
return;
}
final String path = Stream.of(
ofNullable(resourceInfo.getResourceClass().getAnnotation(Path.class)).map(Path::value).orElse(""),
ofNullable(resourceInfo.getResourceMethod().getAnnotation(Path.class)).map(Path::value).orElse(""))
.map(it -> it.substring(it.startsWith("/") ? 1 : 0, it.endsWith("/") ? it.length() - 1 : it.length()))
.filter(it -> !it.isEmpty())
.collect(joining("/", "/", ""));
if (skipPatterns.stream().anyMatch(it -> it.matcher(path).matches())) {
return;
}

final String operationName = traced.map(Traced::operationName).filter(v -> !v.trim().isEmpty()).orElseGet(() -> {
final boolean usePath = Boolean.parseBoolean(config.read("server.filter.request.operationName.usePath", "false"));
if (usePath) {
final String classPath = ofNullable(resourceInfo.getResourceClass().getAnnotation(Path.class)).map(Path::value)
.orElse("");
final String methodPath = ofNullable(resourceInfo.getResourceMethod().getAnnotation(Path.class)).map(Path::value)
.orElse("");
return getHttpMethod(resourceInfo) + ':' + classPath
+ (!classPath.isEmpty() && !methodPath.isEmpty() && !classPath.endsWith("/") ? "/" : "") + methodPath;
if (Boolean.parseBoolean(config.read("server.filter.request.operationName.usePath", "false"))) {
return getHttpMethod(resourceInfo) + ':' + getMethodPath(resourceInfo);
} else if ("http-path".equals(config.read("mp.opentracing.server.operation-name-provider", null))) {
return getMethodPath(resourceInfo);
}
return buildDefaultName(resourceInfo);
});
@@ -92,6 +108,17 @@ public void configure(final ResourceInfo resourceInfo, final FeatureContext cont
Boolean.parseBoolean(config.read("server.filter.request.skipDefaultTags", "false"))));
}

private String getMethodPath(final ResourceInfo resourceInfo) {
final String classPath = ofNullable(resourceInfo.getResourceClass().getAnnotation(Path.class))
.map(Path::value).orElse("");
final String methodPath = ofNullable(resourceInfo.getResourceMethod().getAnnotation(Path.class))
.map(Path::value).orElse("");
return Stream.of(classPath, methodPath)
.map(it -> it.substring(it.startsWith("/") ? 1 : 0, it.length() - (it.endsWith("/") ? 1 : 0)))
.filter(it -> !it.isEmpty())
.collect(joining("/", "/", ""));
}

private String buildDefaultName(final ResourceInfo resourceInfo) {
return getHttpMethod(resourceInfo) + ':' + resourceInfo.getResourceClass().getName() + "."
+ resourceInfo.getResourceMethod().getName();
@@ -4,9 +4,13 @@
import static java.util.stream.Collectors.toList;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -30,6 +34,7 @@
import org.apache.geronimo.microprofile.opentracing.common.spi.Container;

import io.opentracing.Scope;
import io.opentracing.ScopeManager;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
@@ -38,14 +43,15 @@
public class OpenTracingFilter implements Filter {
private Tracer tracer;
private GeronimoOpenTracingConfig config;
private ScopeManagerImpl manager;
private ScopeManager manager;
private Container container;

private Collection<Predicate<String>> forcedUrls;

private List<Predicate<String>> skipUrls;

private boolean skipDefaultTags;
private boolean forceStackLog;

public void setTracer(final Tracer tracer) {
this.tracer = tracer;
@@ -55,7 +61,7 @@ public void setConfig(final GeronimoOpenTracingConfig config) {
this.config = config;
}

public void setManager(final ScopeManagerImpl manager) {
public void setManager(final ScopeManager manager) {
this.manager = manager;
}

@@ -72,12 +78,13 @@ public void init(final FilterConfig filterConfig) {
tracer = container.lookup(Tracer.class);
}
if (manager == null) {
manager = container.lookup(ScopeManagerImpl.class);
manager = container.lookup(ScopeManager.class);
}
if (config == null) {
config = container.lookup(GeronimoOpenTracingConfig.class);
}
skipDefaultTags = Boolean.parseBoolean(config.read("filter.forcedTracing.skipDefaultTags", "false"));
forceStackLog = Boolean.parseBoolean(config.read("filter.error.forceStackLog", "false"));
forcedUrls = ofNullable(config.read("filter.forcedTracing.urls", null))
.map(String::trim).filter(v -> !v.isEmpty())
.map(v -> toMatchingPredicates(v, "forcedTracing"))
@@ -153,14 +160,13 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
final Span span = scope.span();
Tags.HTTP_STATUS.set(span,
status == HttpServletResponse.SC_OK ? HttpServletResponse.SC_INTERNAL_SERVER_ERROR : status);
Tags.ERROR.set(span, true);
span.log(new HashMap<String, Object>() {

{
put("event", Tags.ERROR.getKey());
put("event.object", ex);
}
});
if (forceStackLog) {
Tags.ERROR.set(span, true);
final Map<String, Object> logs = new LinkedHashMap<>();
logs.put("event", Tags.ERROR.getKey());
logs.put("error.object", ex);
span.log(logs);
}
});
throw ex;
} finally {
@@ -188,7 +194,17 @@ public void onStartAsync(final AsyncEvent event) {
// no-op
}
});
manager.clear();

ScopeManager managerImpl = manager;
if (!ScopeManagerImpl.class.isInstance(managerImpl) && Proxy.isProxyClass(manager.getClass())) {
final InvocationHandler handler = Proxy.getInvocationHandler(manager);
if (Container.Unwrappable.class.isInstance(handler)) {
managerImpl = ScopeManager.class.cast(Container.Unwrappable.class.cast(handler).unwrap());
}
}
if (ScopeManagerImpl.class.isInstance(managerImpl)) {
ScopeManagerImpl.class.cast(managerImpl).clear();
}
} else {
scope.close();
}
@@ -18,7 +18,6 @@

import java.util.logging.Logger;

import javax.inject.Inject;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;

@@ -31,7 +30,6 @@ public class ZipkinLogger implements Listener<ZipkinSpan> {

private final Logger spanLogger = Logger.getLogger("org.apache.geronimo.opentracing.zipkin");

@Inject
private GeronimoOpenTracingConfig config;

private Jsonb jsonb;
@@ -53,6 +51,10 @@ public void init() {
wrapAsList = Boolean.parseBoolean(config.read("span.converter.zipkin.logger.wrapAsList", "true"));
}

public Jsonb getJsonb() {
return jsonb;
}

public void destroy() {
try {
jsonb.close();
@@ -29,4 +29,9 @@ static Container get() {
}
return iterator.next();
}

@FunctionalInterface
interface Unwrappable {
Object unwrap();
}
}
@@ -0,0 +1,91 @@
<?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="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>geronimo-opentracing-parent</artifactId>
<groupId>org.apache.geronimo</groupId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>geronimo-opentracing-osgi</artifactId>
<packaging>bundle</packaging>
<name>Geronimo OpenTracing :: OSGi</name>

<properties>
<geronimo-opentracing.Automatic-Module-Name>org.apache.geronimo.opentracing.osgi</geronimo-opentracing.Automatic-Module-Name>

<pax.exam.version>4.12.0</pax.exam.version>
<slf4j.version>1.7.25</slf4j.version>
</properties>

<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.cmpn</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo</groupId>
<artifactId>geronimo-opentracing-common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-karaf</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-aether</artifactId>
<version>1.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,46 @@
/*
* 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.geronimo.microprofile.opentracing.osgi;

import static java.util.Optional.ofNullable;

import java.io.IOException;

import org.apache.geronimo.microprofile.opentracing.common.config.GeronimoOpenTracingConfig;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

public class ConfigAdminOpenTracingConfig implements GeronimoOpenTracingConfig {
private volatile Configuration delegate;

public ConfigAdminOpenTracingConfig() {
ofNullable(OpenTracingActivator.INSTANCES.get(ConfigurationAdmin.class))
.map(ConfigurationAdmin.class::cast)
.ifPresent(admin -> {
try {
delegate = admin.getConfiguration("geronimo.opentracing");
} catch (final IOException e) {
throw new IllegalArgumentException(e);
}
});
}

@Override
public String read(final String value, final String def) {
return ofNullable(delegate).map(c -> c.getProperties().get(value)).map(String::valueOf).orElse(def);
}
}

0 comments on commit 94e01f1

Please sign in to comment.