Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Use TracingFactory to consolidate constructors
Browse files Browse the repository at this point in the history
Signed-off-by: Isaac Hier <ihier@uber.com>
  • Loading branch information
Isaac Hier committed Aug 7, 2018
1 parent 750d448 commit 848881a
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 118 deletions.
11 changes: 6 additions & 5 deletions jaeger-core/src/main/java/io/jaegertracing/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package io.jaegertracing;

import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.TracingFactory;
import io.jaegertracing.internal.metrics.Metrics;
import io.jaegertracing.internal.metrics.NoopMetricsFactory;
import io.jaegertracing.internal.propagation.B3TextMapCodec;
Expand Down Expand Up @@ -201,6 +202,10 @@ protected void initFromEnv() {
.withCodec(CodecConfiguration.fromEnv());
}

protected TracingFactory tracingFactory() {
return new TracingFactory();
}

public JaegerTracer.Builder getTracerBuilder() {
if (reporterConfig == null) {
reporterConfig = new ReporterConfiguration();
Expand All @@ -217,7 +222,7 @@ public JaegerTracer.Builder getTracerBuilder() {
Metrics metrics = new Metrics(metricsFactory);
Reporter reporter = reporterConfig.getReporter(metrics);
Sampler sampler = samplerConfig.createSampler(serviceName, metrics);
JaegerTracer.Builder builder = createTracerBuilder(serviceName)
JaegerTracer.Builder builder = tracingFactory().createTracerBuilder(serviceName)
.withSampler(sampler)
.withReporter(reporter)
.withMetrics(metrics)
Expand All @@ -226,10 +231,6 @@ public JaegerTracer.Builder getTracerBuilder() {
return builder;
}

protected JaegerTracer.Builder createTracerBuilder(String serviceName) {
return new JaegerTracer.Builder(serviceName);
}

public synchronized JaegerTracer getTracer() {
if (tracer != null) {
return tracer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public long getStart() {
return startTimeMicroseconds;
}

protected long getStartTimeNanoTicks() { return startTimeNanoTicks; }

protected boolean getComputeDurationViaNanoTicks() { return computeDurationViaNanoTicks; }

public long getDuration() {
synchronized (this) {
return durationMicroseconds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public String toString() {
return contextAsString();
}

public static JaegerSpanContext contextFromString(String value)
public static JaegerSpanContext contextFromString(String value) {
return contextFromString(value, new TracingFactory());
}

public static JaegerSpanContext contextFromString(String value, TracingFactory factory)
throws MalformedTracerStateStringException, EmptyTracerStateStringException {
if (value == null || value.equals("")) {
throw new EmptyTracerStateStringException();
Expand All @@ -122,11 +126,17 @@ public static JaegerSpanContext contextFromString(String value)
oibe: because java doesn't like to convert large hex strings to longs
we should write this manually instead of using BigInteger.
*/
return new JaegerSpanContext(
return factory.createSpanContext(
new BigInteger(parts[0], 16).longValue(),
new BigInteger(parts[1], 16).longValue(),
new BigInteger(parts[2], 16).longValue(),
new BigInteger(parts[3], 16).byteValue());
new BigInteger(parts[3], 16).byteValue(),
Collections.<String, String>emptyMap(),
null);
}

protected TracingFactory tracingFactory() {
return new TracingFactory();
}

public JaegerSpanContext withBaggageItem(String key, String val) {
Expand All @@ -136,15 +146,15 @@ public JaegerSpanContext withBaggageItem(String key, String val) {
} else {
newBaggage.put(key, val);
}
return new JaegerSpanContext(traceId, spanId, parentId, flags, newBaggage, debugId);
return tracingFactory().createSpanContext(traceId, spanId, parentId, flags, newBaggage, debugId);
}

public JaegerSpanContext withBaggage(Map<String, String> newBaggage) {
return new JaegerSpanContext(traceId, spanId, parentId, flags, newBaggage, debugId);
return tracingFactory().createSpanContext(traceId, spanId, parentId, flags, newBaggage, debugId);
}

public JaegerSpanContext withFlags(byte flags) {
return new JaegerSpanContext(traceId, spanId, parentId, flags, baggage, debugId);
return tracingFactory().createSpanContext(traceId, spanId, parentId, flags, baggage, debugId);
}

/**
Expand All @@ -166,14 +176,19 @@ boolean isDebugIdContainerOnly() {
* debug trace, and the value of header recorded as a span tag to serve as a searchable
* correlation ID.
*
* @param debugId arbitrary string used as correlation ID
* @param debugId arbitrary string used as correlation ID.
* @param factory tracing object factory.
*
* @return new dummy JaegerSpanContext that serves as a container for debugId only.
*
* @see Constants#DEBUG_ID_HEADER_KEY
*/
public static JaegerSpanContext withDebugId(String debugId, TracingFactory factory) {
return factory.createSpanContext(0, 0, 0, (byte) 0, Collections.<String, String>emptyMap(), debugId);
}

public static JaegerSpanContext withDebugId(String debugId) {
return new JaegerSpanContext(0, 0, 0, (byte) 0, Collections.<String, String>emptyMap(), debugId);
return withDebugId(debugId, new TracingFactory());
}

String getDebugId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
import io.opentracing.tag.Tags;
import io.opentracing.util.ThreadLocalScopeManager;
import java.io.Closeable;
Expand Down Expand Up @@ -161,12 +162,6 @@ Reporter getReporter() {

protected Sampler getSampler() { return sampler; }

protected PropagationRegistry getRegistry() { return registry; }

protected boolean isZipkinSharedRpcSpan() { return zipkinSharedRpcSpan; }

protected BaggageRestrictionManager getBaggageRestrictionManager() { return baggageRestrictionManager; }

void reportSpan(JaegerSpan span) {
reporter.report(span);
metrics.spansFinished.inc(1);
Expand Down Expand Up @@ -194,11 +189,7 @@ public Span activeSpan() {

@Override
public JaegerTracer.SpanBuilder buildSpan(String operationName) {
return createSpanBuilder(operationName);
}

protected JaegerTracer.SpanBuilder createSpanBuilder(String operationName) {
return new SpanBuilder(operationName);
return tracingFactory().createSpanBuilder(this, operationName);
}

@Override
Expand Down Expand Up @@ -228,6 +219,10 @@ public void close() {
sampler.close();
}

protected TracingFactory tracingFactory() {
return new TracingFactory();
}

public class SpanBuilder implements Tracer.SpanBuilder {

private String operationName;
Expand Down Expand Up @@ -329,7 +324,7 @@ private JaegerSpanContext createNewContext(String debugId) {
}
}

return new JaegerSpanContext(id, id, 0, flags);
return JaegerTracer.this.tracingFactory().createSpanContext(id, id, 0, flags, Collections.<String, String>emptyMap(), debugId);
}

private Map<String, String> createChildBaggage() {
Expand Down Expand Up @@ -368,7 +363,7 @@ private JaegerSpanContext createChildContext() {
}
}

return new JaegerSpanContext(
return JaegerTracer.this.tracingFactory().createSpanContext(
preferredReference.getTraceId(),
Utils.uniqueId(),
preferredReference.getSpanId(),
Expand Down Expand Up @@ -443,7 +438,7 @@ public JaegerSpan start() {
}
}

JaegerSpan jaegerSpan = new JaegerSpan(
JaegerSpan jaegerSpan = JaegerTracer.this.tracingFactory().createSpan(
JaegerTracer.this,
operationName,
context,
Expand Down Expand Up @@ -493,18 +488,25 @@ public static class Builder {
private ScopeManager scopeManager = new ThreadLocalScopeManager();
private BaggageRestrictionManager baggageRestrictionManager = new DefaultBaggageRestrictionManager();
private boolean expandExceptionLogs;
private TracingFactory tracingFactory;

public Builder(String serviceName) {
public Builder(String serviceName, TracingFactory tracingFactory) {
this.serviceName = checkValidServiceName(serviceName);
TextMapCodec textMapCodec = new TextMapCodec(false);
this.tracingFactory = tracingFactory;

TextMapCodec textMapCodec = TextMapCodec.builder().withUrlEncoding(false).withTracingFactory(tracingFactory).build();
this.registerInjector(Format.Builtin.TEXT_MAP, textMapCodec);
this.registerExtractor(Format.Builtin.TEXT_MAP, textMapCodec);
TextMapCodec httpCodec = new TextMapCodec(true);
TextMapCodec httpCodec = TextMapCodec.builder().withUrlEncoding(true).withTracingFactory(tracingFactory).build();
this.registerInjector(Format.Builtin.HTTP_HEADERS, httpCodec);
this.registerExtractor(Format.Builtin.HTTP_HEADERS, httpCodec);
// TODO binary codec not implemented
}

public Builder(String serviceName) {
this(serviceName, new TracingFactory());
}

/**
* @param reporter reporter.
*/
Expand Down Expand Up @@ -604,22 +606,7 @@ public JaegerTracer build() {
.withMetrics(metrics)
.build();
}
return createTracer(serviceName, reporter, sampler, registry, clock, metrics, tags,
zipkinSharedRpcSpan, scopeManager, baggageRestrictionManager, expandExceptionLogs);
}

protected JaegerTracer createTracer(String serviceName,
Reporter reporter,
Sampler sampler,
PropagationRegistry registry,
Clock clock,
Metrics metrics,
Map<String, Object> tags,
boolean zipkinSharedRpcSpan,
ScopeManager scopeManager,
BaggageRestrictionManager baggageRestrictionManager,
boolean expandExceptionLogs) {
return new JaegerTracer(serviceName, reporter, sampler, registry, clock, metrics, tags,
return tracingFactory.createTracer(serviceName, reporter, sampler, registry, clock, metrics, tags,
zipkinSharedRpcSpan, scopeManager, baggageRestrictionManager, expandExceptionLogs);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.jaegertracing.internal;

import io.jaegertracing.internal.clock.Clock;
import io.jaegertracing.internal.metrics.Metrics;
import io.jaegertracing.spi.BaggageRestrictionManager;
import io.jaegertracing.spi.Reporter;
import io.jaegertracing.spi.Sampler;
import io.opentracing.ScopeManager;

import java.util.List;
import java.util.Map;

public class TracingFactory {
public JaegerTracer createTracer(
String serviceName,
Reporter reporter,
Sampler sampler,
PropagationRegistry registry,
Clock clock,
Metrics metrics,
Map<String, Object> tags,
boolean zipkinSharedRpcSpan,
ScopeManager scopeManager,
BaggageRestrictionManager baggageRestrictionManager,
boolean expandExceptionLogs) {
return new JaegerTracer(
serviceName,
reporter,
sampler,
registry,
clock,
metrics,
tags,
zipkinSharedRpcSpan,
scopeManager,
baggageRestrictionManager,
expandExceptionLogs);
}

public JaegerSpan createSpan(
JaegerTracer tracer,
String operationName,
JaegerSpanContext context,
long startTimeMicroseconds,
long startTimeNanoTicks,
boolean computeDurationViaNanoTicks,
Map<String, Object> tags,
List<Reference> references) {
return new JaegerSpan(
tracer,
operationName,
context,
startTimeMicroseconds,
startTimeNanoTicks,
computeDurationViaNanoTicks,
tags,
references);
}

public JaegerSpanContext createSpanContext(long traceId,
long spanId,
long parentId,
byte flags,
Map<String, String> baggage,
String debugId) {
return new JaegerSpanContext(traceId, spanId, parentId, flags, baggage, debugId);
}

public JaegerTracer.SpanBuilder createSpanBuilder(JaegerTracer tracer, String operationName) {
return tracer.new SpanBuilder(operationName);
}

public JaegerTracer.Builder createTracerBuilder(String serviceName) {
return new JaegerTracer.Builder(serviceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import io.jaegertracing.internal.JaegerSpanContext;
import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.TracingFactory;
import io.jaegertracing.spi.BaggageRestrictionManager;
import io.jaegertracing.spi.Codec;
import io.opentracing.propagation.TextMap;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class B3TextMapCodec implements Codec<TextMap> {

private static final PrefixedKeys keys = new PrefixedKeys();
private final String baggagePrefix;
private final TracingFactory tracingFactory;

/**
* @deprecated use {@link Builder} instead
Expand All @@ -68,6 +70,7 @@ public B3TextMapCodec() {

private B3TextMapCodec(Builder builder) {
this.baggagePrefix = builder.baggagePrefix;
this.tracingFactory = builder.tracingFactory;
}

@Override
Expand Down Expand Up @@ -129,6 +132,7 @@ public JaegerSpanContext extract(TextMap carrier) {

public static class Builder {
private String baggagePrefix = BAGGAGE_PREFIX;
private TracingFactory tracingFactory = new TracingFactory();

/**
* Specify baggage prefix. The default is {@value B3TextMapCodec#BAGGAGE_PREFIX}
Expand All @@ -138,6 +142,15 @@ public Builder withBaggagePrefix(String baggagePrefix) {
return this;
}

/**
* Specify JaegerSpanContext factory. Used for creating new span contexts. The default factory is an instance of
* {@link TracingFactory}.
*/
public Builder withTracingFactory(TracingFactory tracingFactory) {
this.tracingFactory = tracingFactory;
return this;
}

public B3TextMapCodec build() {
return new B3TextMapCodec(this);
}
Expand Down
Loading

0 comments on commit 848881a

Please sign in to comment.