Switch to Spring Core IdGenerator

This commit is contained in:
Dave Syer
2015-12-01 16:44:48 +00:00
parent c0e5c865dd
commit fcb12c8f5e
13 changed files with 70 additions and 99 deletions

View File

@@ -1,24 +0,0 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed 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.springframework.cloud.sleuth;
/**
* @author Spencer Gibb
*/
public interface IdGenerator {
String create();
}

View File

@@ -1,32 +0,0 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed 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.springframework.cloud.sleuth.autoconfig;
import java.util.UUID;
import org.springframework.cloud.sleuth.IdGenerator;
/**
* @author Spencer Gibb
*/
public class RandomUuidGenerator implements IdGenerator {
@Override
public String create() {
return UUID.randomUUID().toString();
}
}

View File

@@ -18,13 +18,14 @@ package org.springframework.cloud.sleuth.autoconfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.sleuth.IdGenerator;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.sampler.IsTracingSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.IdGenerator;
import org.springframework.util.JdkIdGenerator;
/**
* @author Spencer Gibb
@@ -36,7 +37,7 @@ public class TraceAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public IdGenerator traceIdGenerator() {
return new RandomUuidGenerator();
return new JdkIdGenerator();
}
@Bean

View File

@@ -19,12 +19,12 @@ package org.springframework.cloud.sleuth.instrument.scheduling;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.cloud.sleuth.IdGenerator;
import org.springframework.cloud.sleuth.MilliSpan;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.IdGenerator;
/**
* Aspect that creates a new Span for running threads executing methods annotated with
@@ -53,7 +53,7 @@ public class TraceSchedulingAspect {
public Object traceBackgroundThread(final ProceedingJoinPoint pjp) throws Throwable {
final Span span = this.trace.isTracing() ? this.trace.getCurrentSpan()
: MilliSpan.builder().begin(System.currentTimeMillis())
.traceId(this.idGenerator.create()).spanId(this.idGenerator.create())
.traceId(createId()).spanId(createId())
.build();
Trace scope = this.trace.startSpan(pjp.toShortString(), span);
try {
@@ -63,4 +63,8 @@ public class TraceSchedulingAspect {
this.trace.close(scope);
}
}
private String createId() {
return this.idGenerator.generateId().toString();
}
}

View File

@@ -25,12 +25,12 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.sleuth.IdGenerator;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.util.IdGenerator;
/**
* Registers beans related to task scheduling.

View File

@@ -20,7 +20,6 @@ import static org.springframework.cloud.sleuth.util.ExceptionUtils.error;
import java.util.concurrent.Callable;
import org.springframework.cloud.sleuth.IdGenerator;
import org.springframework.cloud.sleuth.MilliSpan;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.Span;
@@ -33,6 +32,7 @@ import org.springframework.cloud.sleuth.instrument.TraceCallable;
import org.springframework.cloud.sleuth.instrument.TraceRunnable;
import org.springframework.cloud.sleuth.util.ExceptionUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.IdGenerator;
/**
* @author Spencer Gibb
@@ -137,8 +137,8 @@ public class DefaultTraceManager implements TraceManager {
protected Span createChild(Span parent, String name) {
if (parent == null) {
MilliSpan span = MilliSpan.builder().begin(System.currentTimeMillis())
.name(name).traceId(this.idGenerator.create())
.spanId(this.idGenerator.create()).build();
.name(name).traceId(createId())
.spanId(createId()).build();
this.publisher.publishEvent(new SpanAcquiredEvent(this, span));
return span;
}
@@ -149,13 +149,17 @@ public class DefaultTraceManager implements TraceManager {
}
MilliSpan span = MilliSpan.builder().begin(System.currentTimeMillis())
.name(name).traceId(parent.getTraceId()).parent(parent.getSpanId())
.spanId(this.idGenerator.create()).processId(parent.getProcessId())
.spanId(createId()).processId(parent.getProcessId())
.build();
this.publisher.publishEvent(new SpanAcquiredEvent(this, parent, span));
return span;
}
}
private String createId() {
return this.idGenerator.generateId().toString();
}
@Override
public Trace continueSpan(Span span) {
// Return an empty TraceScope that does nothing on close