Skip to content
Permalink
Browse files
Merge pull request #1 from jgallimore/zipkin-v2
GERONIMO-6727 Zipkin v2 changes
  • Loading branch information
jeanouii committed May 3, 2019
2 parents 17fbb28 + d58db42 commit d5fd82cac327d9eaae79e6278586701b80bf49ae
Showing 13 changed files with 480 additions and 15 deletions.
@@ -88,6 +88,7 @@ public void configure(final ResourceInfo resourceInfo, final FeatureContext cont
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.equals("/") ? "" : it)
.map(it -> it.substring(it.startsWith("/") ? 1 : 0, it.endsWith("/") ? it.length() - 1 : it.length()))
.filter(it -> !it.isEmpty())
.collect(joining("/", "/", ""));
@@ -50,6 +50,7 @@ public class ZipkinConverter implements Listener<FinishedSpan> {
private IdGenerator idGenerator;

private String serviceName;
private boolean useV2 = false;

public void setZipkinSpanEvent(final Bus<ZipkinSpan> zipkinSpanEvent) {
this.zipkinSpanEvent = zipkinSpanEvent;
@@ -65,6 +66,7 @@ public void setIdGenerator(final IdGenerator idGenerator) {

public void init() {
serviceName = config.read("zipkin.serviceName", getHostName() + "_" + getPid());
useV2 = Boolean.parseBoolean(config.read("span.converter.zipkin.http.useV2", "false").trim());
}

@Override
@@ -91,7 +93,14 @@ private String getHostName() {
private ZipkinSpan toZipkin(final SpanImpl span) {
final ZipkinSpan.ZipkinEndpoint endpoint = toEndpoint(span);

final ZipkinSpan zipkin = new ZipkinSpan();
final ZipkinSpan zipkin;
if (useV2) {
zipkin = new ZipkinSpan();
} else {
zipkin = new ZipkinV1Span();
((ZipkinV1Span) zipkin).setBinaryAnnotations(toBinaryAnnotations(span.getTags()));
}

if (idGenerator.isCounter()) {
zipkin.setParentId(asLong(span.getParentId()));
zipkin.setTraceId(asLong(span.getTraceId()));
@@ -106,7 +115,7 @@ private ZipkinSpan toZipkin(final SpanImpl span) {
zipkin.setTimestamp(span.getTimestamp());
zipkin.setDuration(span.getDuration());
zipkin.setAnnotations(toAnnotations(span));
zipkin.setBinaryAnnotations(toBinaryAnnotations(span.getTags()));

zipkin.setTags(span.getTags().entrySet().stream().filter(e -> !Tags.SPAN_KIND.getKey().equalsIgnoreCase(e.getKey()))
.collect(toMap(Map.Entry::getKey, e -> String.valueOf(e.getValue()))));

@@ -22,6 +22,7 @@
import static java.util.concurrent.TimeUnit.MINUTES;
import static javax.ws.rs.client.Entity.entity;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static javax.ws.rs.core.MediaType.MEDIA_TYPE_WILDCARD;

import java.util.ArrayList;
import java.util.List;
@@ -38,6 +39,7 @@
import javax.json.bind.JsonbBuilder;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.geronimo.microprofile.opentracing.common.config.GeronimoOpenTracingConfig;
@@ -178,7 +180,7 @@ private void onEmit() {

private void doSend(final List<ZipkinSpan> copy) {
final Response result = client.target(collector)
.request(APPLICATION_JSON_TYPE)
.request()
.post(entity(copy, APPLICATION_JSON_TYPE));
if (result.getStatus() >= 300) {
// todo: better handling but at least log them to not loose them completely or explode in memory
@@ -30,7 +30,6 @@ public class ZipkinSpan {
private ZipkinEndpoint localEndpoint;
private ZipkinEndpoint remoteEndpoint;
private List<ZipkinAnnotation> annotations;
private List<ZipkinBinaryAnnotation> binaryAnnotations;
private Map<String, String> tags;
private Boolean debug;
private Boolean shared;
@@ -91,14 +90,6 @@ public void setAnnotations(final List<ZipkinAnnotation> annotations) {
this.annotations = annotations;
}

public List<ZipkinBinaryAnnotation> getBinaryAnnotations() {
return binaryAnnotations;
}

public void setBinaryAnnotations(final List<ZipkinBinaryAnnotation> binaryAnnotations) {
this.binaryAnnotations = binaryAnnotations;
}

public Boolean getDebug() {
return debug;
}
@@ -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.geronimo.microprofile.opentracing.common.microprofile.zipkin;

import java.util.List;

public class ZipkinV1Span extends ZipkinSpan {
private List<ZipkinBinaryAnnotation> binaryAnnotations;

public List<ZipkinBinaryAnnotation> getBinaryAnnotations() {
return binaryAnnotations;
}

public void setBinaryAnnotations(final List<ZipkinBinaryAnnotation> binaryAnnotations) {
this.binaryAnnotations = binaryAnnotations;
}
}
@@ -122,6 +122,12 @@
<version>1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zipkin.zipkin2</groupId>
<artifactId>zipkin-junit</artifactId>
<version>2.12.9</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
@@ -0,0 +1,100 @@
/*
* 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.tck.setup;


import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import zipkin2.Span;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.net.URL;
import java.util.List;

public class BasicZipkinTest extends Arquillian {

@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(SimpleService.class)
.addAsWebInfResource(new ClassLoaderAsset("test-beans.xml"), "beans.xml")
.addAsServiceProvider(javax.enterprise.inject.spi.Extension.class, UseGeronimoTracerExtension.class);
}

private ZipkinRule zipkin;

@ArquillianResource
private URL serviceUrl;

@BeforeMethod
public void configure() {


}

@BeforeSuite
public void setup() {
zipkin = new ZipkinRule();
System.setProperty("geronimo.opentracing.span.converter.zipkin.sender", "http");
System.setProperty("geronimo.opentracing.span.converter.zipkin.http.collector", zipkin.httpUrl() + "/api/v2/spans");
System.setProperty("geronimo.opentracing.span.converter.zipkin.http.bulkSendInterval", "6000");
System.setProperty("geronimo.opentracing.span.converter.zipkin.http.maxSpansPerBulk", "1");
System.setProperty("geronimo.opentracing.span.converter.zipkin.http.maxSpansIteration","1");
System.setProperty("geronimo.opentracing.span.converter.zipkin.http.bufferSize","1");
System.setProperty("geronimo.opentracing.span.converter.zipkin.http.useV2","true");

}


/**
* Test that server endpoint is adding standard tags
*/
@Test
@RunAsClient
public void testSimpleService() throws Exception {
System.out.println(zipkin.httpUrl());

Client client = ClientBuilder.newClient();
String url = serviceUrl.toExternalForm() + "hello";

WebTarget target = client.target(url);
Response response = target.request().get();
if (response.getStatus() != 200) {
String unexpectedResponse = response.readEntity(String.class);
Assert.fail("Expected HTTP response code 200 but received " + response.getStatus() + "; Response: " + unexpectedResponse);
}

Thread.sleep(10000);

final List<List<Span>> traces = zipkin.getTraces();
Assert.assertTrue(traces.size() > 0, "Expected some traces");
}


}
@@ -0,0 +1,39 @@
/*
* 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.tck.setup;

import org.eclipse.microprofile.opentracing.Traced;

import javax.enterprise.context.RequestScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Traced
@RequestScoped
@Path("hello")
public class SimpleService {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() throws Exception {
return "Hello, world!";
}


}
@@ -21,6 +21,9 @@
import org.jboss.arquillian.core.spi.LoadableExtension;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.Node;

import java.util.Collection;

// the tck put the opentracing-api in the war but not our impl, let's fix it by using the apploader api jar!
public class SkipOpentracingApiSetup implements LoadableExtension {
@@ -34,8 +37,10 @@ public static class Impl {

public void clean(@Observes final BeforeDeploy beforeDeploy) {
final Archive<?> archive = beforeDeploy.getDeployment().getArchive();
archive.delete(archive.getContent(Filters.include("\\/WEB-INF\\/lib\\/opentracing\\-api\\-.*\\.jar")).values()
.iterator().next().getPath());
final Collection<Node> opentracingApi = archive.getContent(Filters.include("\\/WEB-INF\\/lib\\/opentracing\\-api\\-.*\\.jar")).values();
if (opentracingApi != null && !opentracingApi.isEmpty()) {
archive.delete(opentracingApi.iterator().next().getPath());
}
}
}
}
@@ -0,0 +1,29 @@
/*
* 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.tck.setup;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;

public class UseGeronimoTracerExtension implements Extension {

void vetoTckTracerIfScanned(@Observes final ProcessAnnotatedType<TckTracer> tckTracer) {
tckTracer.veto();
}

}

0 comments on commit d5fd82c

Please sign in to comment.