Skip to content
Permalink
Browse files
more adapted hex generation
  • Loading branch information
rmannibucau committed May 22, 2018
1 parent 549ae21 commit eb6c67a6b98eef766c24440591ae270538e47ba9
Showing 2 changed files with 16 additions and 13 deletions.
@@ -29,6 +29,7 @@ NOTE: if you are using Microprofile Config it is used, otherwise it uses system
|geronimo.opentracing.propagation.headers.baggagePrefix|Prefix of headers used to host the baggage values|`baggage-`
|geronimo.opentracing.cdi.executorServices.wrappedNames|Name (as CDI names of the beans) of executor services which will get an interceptor propagating the current scope (span)|-
|geronimo.opentracing.id.generator|`counter` (to generate longs), `uuid` (to generate random uuids) or `hex` (to use the hexa representation of the uuid generator). Specifies which kind of trace and span id are in use.|counter
|geronimo.opentracing.id.generator.hex.prefix|For hex generator, a prefix to prepend to all ids.|-
|geronimo.opentracing.span.converter.zipkin.active|Should spans converted to a zipkin representation. True until there is a standard opentracing format.|true
|geronimo.opentracing.span.converter.zipkin.serviceName|The local serviceName.|hostname-jvmid
|geronimo.opentracing.span.converter.zipkin.logger.active|Should a logger named `org.apache.geronimo.opentracing.zipkin` log each span as a Zipkin JSON. This allows to use a logger implementation to push the spans to any backend (like log4j2 kafka appender). It uses JUL as a facade.|true
@@ -16,7 +16,7 @@
*/
package org.apache.geronimo.microprofile.opentracing.impl;

import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
@@ -29,8 +29,6 @@

@ApplicationScoped
public class IdGenerator {
private static final char[] HEXA_MAPPING = "0123456789abcdef".toCharArray();

@Inject
private GeronimoOpenTracingConfig config;

@@ -55,9 +53,21 @@ public Object get() {
case "uuid":
delegate = () -> UUID.randomUUID().toString();
break;
case "hex":
case "hex": // limited to 16 for the length cause of zipkin (see span decoder)
default:
delegate = () -> toHex(UUID.randomUUID().toString().replace("-", "").getBytes(StandardCharsets.UTF_8));
delegate = new Supplier<Object>() {
private final Random random = new Random(System.nanoTime());
private final String constantPart = config.read("id.generator.hex.prefix", "");

@Override
public Object get() {
final StringBuilder sb = new StringBuilder(16).append(constantPart);
for (int i = 0; i < 16 - constantPart.length(); i++) {
sb.append(Integer.toHexString(random.nextInt()));
}
return sb.toString();
}
};
}
}

@@ -68,12 +78,4 @@ public boolean isCounter() {
public Object next() {
return delegate.get();
}

private String toHex(final byte[] uuid) {
final StringBuilder representation = new StringBuilder(uuid.length * 2);
for (final byte current : uuid) {
representation.append(HEXA_MAPPING[(current >> 4) & 0xF]).append(HEXA_MAPPING[(current & 0xF)]);
}
return representation.toString();
}
}

0 comments on commit eb6c67a

Please sign in to comment.