Skip to content

Commit

Permalink
[#142] Initial implementation of span duration histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Feb 5, 2016
1 parent bc48a00 commit a8e8080
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package org.springframework.cloud.sleuth.autoconfig;

import java.util.Random;

import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -28,15 +27,20 @@
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.metric.CounterServiceBasedSpanReporterService;
import org.springframework.cloud.sleuth.metric.GaugeServiceBasedSpanDurationReporterService;
import org.springframework.cloud.sleuth.metric.NoOpSpanReporterService;
import org.springframework.cloud.sleuth.metric.NoOpSpanDurationReporterService;
import org.springframework.cloud.sleuth.metric.SleuthMetricProperties;
import org.springframework.cloud.sleuth.metric.SpanDurationReporterService;
import org.springframework.cloud.sleuth.metric.SpanReporterService;
import org.springframework.cloud.sleuth.sampler.NeverSampler;
import org.springframework.cloud.sleuth.trace.DefaultTracer;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Random;

/**
* @author Spencer Gibb
*/
Expand All @@ -60,8 +64,9 @@ public Sampler defaultTraceSampler() {
@Bean
@ConditionalOnMissingBean(Tracer.class)
public DefaultTracer traceManager(Sampler sampler, Random random,
ApplicationEventPublisher publisher) {
return new DefaultTracer(sampler, random, publisher);
ApplicationEventPublisher publisher,
SpanDurationReporterService spanDurationReporterService) {
return new DefaultTracer(sampler, random, publisher, spanDurationReporterService);
}

@Bean
Expand All @@ -82,11 +87,26 @@ public SpanReporterService spanReporterCounterService(CounterService counterServ
sleuthMetricProperties.getSpan().getDroppedName(), counterService);
}

@Bean
@ConditionalOnBean(GaugeService.class)
public SpanDurationReporterService spanDurationReporterService(GaugeService gaugeService,
SleuthMetricProperties sleuthMetricProperties) {
return new GaugeServiceBasedSpanDurationReporterService(
sleuthMetricProperties.getSpan().getDurationPrefixName(),
gaugeService);
}

@Bean
@ConditionalOnMissingBean(CounterService.class)
public SpanReporterService noOpSpanReporterCounterService() {
return new NoOpSpanReporterService();
}

@Bean
@ConditionalOnMissingBean(SpanDurationReporterService.class)
public SpanDurationReporterService noOpSpanDurationReporterService() {
return new NoOpSpanDurationReporterService();
}
}

@Bean
Expand All @@ -96,4 +116,11 @@ public SpanReporterService noOpSpanReporterCounterService() {
return new NoOpSpanReporterService();
}

@Bean
@ConditionalOnMissingClass("org.springframework.boot.actuate.metrics.GaugeService")
@ConditionalOnMissingBean(SpanDurationReporterService.class)
public SpanDurationReporterService noOpSpanDurationReporterService() {
return new NoOpSpanDurationReporterService();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.metric;

import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.util.StringUtils;

/**
* Service to operate on span duration statistics.
*
* @author Marcin Grzejszczak
*/
public class GaugeServiceBasedSpanDurationReporterService implements SpanDurationReporterService {
private final String metricPrefix;
private final GaugeService gaugeService;

public GaugeServiceBasedSpanDurationReporterService(String metricPrefix,
GaugeService gaugeService) {
this.metricPrefix = metricPrefix;
this.gaugeService = gaugeService;
}

@Override
public void submitDuration(String metricName, double duration) {
String metricFullName = metricName;
if (StringUtils.hasText(this.metricPrefix)) {
metricFullName = this.metricPrefix + "." + metricName;
}
this.gaugeService.submit(metricFullName, duration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.metric;

/**
* @author Marcin Grzejszczak
*/
public class NoOpSpanDurationReporterService implements SpanDurationReporterService {

@Override
public void submitDuration(String metricName, double duration) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static class Span {

private String droppedName = "counter.span.dropped";

private String durationPrefixName = "histogram.span";

public String getAcceptedName() {
return this.acceptedName;
}
Expand All @@ -41,5 +43,13 @@ public String getDroppedName() {
public void setDroppedName(String droppedName) {
this.droppedName = droppedName;
}

public String getDurationPrefixName() {
return this.durationPrefixName;
}

public void setDurationPrefixName(String durationPrefixName) {
this.durationPrefixName = durationPrefixName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.metric;

/**
* @author Marcin Grzejszczak
*/
public interface SpanDurationReporterService {

/**
* Once the span is closed - it's duration will be submitted for metric aggregation
*
* @param metricName the name of the metric
* @param duration the duration of a span
*/
void submitDuration(String metricName, double duration);

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.metric;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.springframework.cloud.sleuth.event.SpanReleasedEvent;
import org.springframework.cloud.sleuth.instrument.TraceCallable;
import org.springframework.cloud.sleuth.instrument.TraceRunnable;
import org.springframework.cloud.sleuth.metric.NoOpSpanDurationReporterService;
import org.springframework.cloud.sleuth.metric.SpanDurationReporterService;
import org.springframework.cloud.sleuth.util.ExceptionUtils;
import org.springframework.context.ApplicationEventPublisher;

Expand All @@ -41,11 +43,20 @@ public class DefaultTracer implements Tracer {

private final Random random;

private final SpanDurationReporterService spanDurationReporterService;

public DefaultTracer(Sampler defaultSampler, Random random,
ApplicationEventPublisher publisher) {
ApplicationEventPublisher publisher,
SpanDurationReporterService spanDurationReporterService) {
this.defaultSampler = defaultSampler;
this.random = random;
this.publisher = publisher;
this.spanDurationReporterService = spanDurationReporterService;
}

public DefaultTracer(Sampler defaultSampler, Random random,
ApplicationEventPublisher publisher) {
this(defaultSampler, random, publisher, new NoOpSpanDurationReporterService());
}

@Override
Expand Down Expand Up @@ -125,6 +136,7 @@ public Span close(Span span) {
}
SpanContextHolder.close();
}
this.spanDurationReporterService.submitDuration(span.getName(), span.getAccumulatedMillis());
return savedSpan;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@

package org.springframework.cloud.sleuth.instrument.web;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import java.util.Random;

import lombok.SneakyThrows;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -46,7 +37,15 @@
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

import lombok.SneakyThrows;
import java.util.Random;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

/**
* @author Spencer Gibb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

package org.springframework.cloud.sleuth.instrument.zuul;

import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;

import java.util.Random;

import com.netflix.zuul.context.RequestContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -31,7 +27,10 @@
import org.springframework.cloud.sleuth.trace.TestSpanContextHolder;
import org.springframework.context.ApplicationEventPublisher;

import com.netflix.zuul.context.RequestContext;
import java.util.Random;

import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;

/**
* @author Dave Syer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@

package org.springframework.cloud.sleuth.instrument.zuul;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import java.util.Random;

import com.netflix.zuul.context.RequestContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -34,7 +28,12 @@
import org.springframework.cloud.sleuth.trace.TestSpanContextHolder;
import org.springframework.context.ApplicationEventPublisher;

import com.netflix.zuul.context.RequestContext;
import java.util.Random;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

/**
* @author Dave Syer
Expand Down
Loading

0 comments on commit a8e8080

Please sign in to comment.