-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TracingTimer #207
Closed
Closed
Add TracingTimer #207
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
micrometer-core/src/main/java/io/micrometer/core/instrument/tracing/OpenCensusTimer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* Copyright 2017 Pivotal Software, Inc. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 io.micrometer.core.instrument.tracing; | ||
|
||
import io.micrometer.core.instrument.HistogramSnapshot; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Timer; | ||
import io.opencensus.common.Scope; | ||
import io.opencensus.trace.AttributeValue; | ||
import io.opencensus.trace.Tracer; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
|
||
public class OpenCensusTimer implements Timer { | ||
|
||
public static Builder builder(String name, Tracer tracer) { | ||
return new Builder(name, tracer); | ||
} | ||
|
||
public static class Builder extends Timer.Builder{ | ||
private final Tracer tracer; | ||
|
||
Builder(String name, Tracer tracer) { | ||
super(name); | ||
this.tracer = tracer; | ||
} | ||
|
||
@Override | ||
public OpenCensusTimer register(MeterRegistry registry) { | ||
return new OpenCensusTimer(super.register(registry), tracer); | ||
} | ||
} | ||
|
||
private final Timer delegate; | ||
private final Tracer tracer; | ||
|
||
OpenCensusTimer(Timer delegate, Tracer tracer) { | ||
this.delegate = delegate; | ||
this.tracer = tracer; | ||
} | ||
|
||
private Scope createSpan() { | ||
Scope spanBuilder = tracer.spanBuilder(getId().getName()).startScopedSpan(); | ||
getId().getTags().forEach(t -> tracer.getCurrentSpan().putAttribute(t.getKey(), AttributeValue.stringAttributeValue(t.getValue()))); | ||
return spanBuilder; | ||
} | ||
|
||
|
||
@Override | ||
public <T> T record(Supplier<T> f) { | ||
try(@SuppressWarnings("unused") Scope span = createSpan()){ | ||
return delegate.record(f); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T recordCallable(Callable<T> f) throws Exception { | ||
try(@SuppressWarnings("unused") Scope span = createSpan()) { | ||
return delegate.recordCallable(f); | ||
} | ||
} | ||
|
||
@Override | ||
public void record(Runnable f) { | ||
try(@SuppressWarnings("unused") Scope span = createSpan()) { | ||
delegate.record(f); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> Callable<T> wrap(Callable<T> f) { | ||
return () -> { | ||
try(@SuppressWarnings("unused") Scope span = createSpan()) { | ||
return delegate.wrap(f).call(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void record(long amount, TimeUnit unit) { | ||
delegate.record(amount, unit); | ||
} | ||
|
||
@Override | ||
public long count() { | ||
return delegate.count(); | ||
} | ||
|
||
@Override | ||
public double totalTime(TimeUnit unit) { | ||
return delegate.totalTime(unit); | ||
} | ||
|
||
@Override | ||
public double max(TimeUnit unit) { | ||
return delegate.max(unit); | ||
} | ||
|
||
@Override | ||
public double percentile(double percentile, TimeUnit unit) { | ||
return delegate.percentile(percentile, unit); | ||
} | ||
|
||
@Override | ||
public double histogramCountAtValue(long valueNanos) { | ||
return delegate.histogramCountAtValue(valueNanos); | ||
} | ||
|
||
@Override | ||
public HistogramSnapshot takeSnapshot(boolean supportsAggregablePercentiles) { | ||
return delegate.takeSnapshot(supportsAggregablePercentiles); | ||
} | ||
|
||
@Override | ||
public Id getId() { | ||
return delegate.getId(); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
micrometer-core/src/main/java/io/micrometer/core/instrument/tracing/OpenTracingTimer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* Copyright 2017 Pivotal Software, Inc. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 io.micrometer.core.instrument.tracing; | ||
|
||
import io.micrometer.core.instrument.HistogramSnapshot; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Timer; | ||
import io.opentracing.ActiveSpan; | ||
import io.opentracing.Tracer; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
|
||
public class OpenTracingTimer implements Timer { | ||
|
||
public static Builder builder(String name, Tracer tracer) { | ||
return new Builder(name, tracer); | ||
} | ||
|
||
public static class Builder extends Timer.Builder{ | ||
private final Tracer tracer; | ||
|
||
Builder(String name, Tracer tracer) { | ||
super(name); | ||
this.tracer = tracer; | ||
} | ||
|
||
@Override | ||
public OpenTracingTimer register(MeterRegistry registry) { | ||
return new OpenTracingTimer(super.register(registry), tracer); | ||
} | ||
} | ||
|
||
private final Timer delegate; | ||
private final Tracer tracer; | ||
|
||
OpenTracingTimer(Timer delegate, Tracer tracer) { | ||
this.delegate = delegate; | ||
this.tracer = tracer; | ||
} | ||
|
||
private ActiveSpan createSpan() { | ||
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(getId().getName()); | ||
getId().getTags().forEach(t -> spanBuilder.withTag(t.getKey(), t.getValue())); | ||
return spanBuilder.startActive(); | ||
} | ||
|
||
|
||
@Override | ||
public <T> T record(Supplier<T> f) { | ||
try(@SuppressWarnings("unused") ActiveSpan span = createSpan()){ | ||
return delegate.record(f); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T recordCallable(Callable<T> f) throws Exception { | ||
try(@SuppressWarnings("unused") ActiveSpan span = createSpan()) { | ||
return delegate.recordCallable(f); | ||
} | ||
} | ||
|
||
@Override | ||
public void record(Runnable f) { | ||
try(@SuppressWarnings("unused") ActiveSpan span = createSpan()) { | ||
delegate.record(f); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> Callable<T> wrap(Callable<T> f) { | ||
return () -> { | ||
try(@SuppressWarnings("unused") ActiveSpan span = createSpan()) { | ||
return delegate.wrap(f).call(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void record(long amount, TimeUnit unit) { | ||
delegate.record(amount, unit); | ||
} | ||
|
||
@Override | ||
public long count() { | ||
return delegate.count(); | ||
} | ||
|
||
@Override | ||
public double totalTime(TimeUnit unit) { | ||
return delegate.totalTime(unit); | ||
} | ||
|
||
@Override | ||
public double max(TimeUnit unit) { | ||
return delegate.max(unit); | ||
} | ||
|
||
@Override | ||
public double percentile(double percentile, TimeUnit unit) { | ||
return delegate.percentile(percentile, unit); | ||
} | ||
|
||
@Override | ||
public double histogramCountAtValue(long valueNanos) { | ||
return delegate.histogramCountAtValue(valueNanos); | ||
} | ||
|
||
@Override | ||
public HistogramSnapshot takeSnapshot(boolean supportsAggregablePercentiles) { | ||
return delegate.takeSnapshot(supportsAggregablePercentiles); | ||
} | ||
|
||
@Override | ||
public Id getId() { | ||
return delegate.getId(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
micrometer-core/src/test/java/io/micrometer/core/instrument/tracing/OpenCensusTimerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.micrometer.core.instrument.tracing; | ||
|
||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import io.opencensus.trace.AttributeValue; | ||
import io.opencensus.trace.Tracer; | ||
import io.opencensus.trace.Tracing; | ||
import io.opencensus.trace.config.TraceParams; | ||
import io.opencensus.trace.export.SpanData; | ||
import io.opencensus.trace.export.SpanExporter; | ||
import io.opencensus.trace.samplers.Samplers; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class OpenCensusTimerTest { | ||
|
||
private Tracer tracer; | ||
private SimpleMeterRegistry registry; | ||
private ArrayList<SpanData> exportedSpans; | ||
|
||
@BeforeEach | ||
void setup(){ | ||
tracer = Tracing.getTracer(); | ||
registry = new SimpleMeterRegistry(); | ||
registry.config().commonTags("commonTag1", "commonVal1"); | ||
|
||
exportedSpans = new ArrayList<>(); | ||
Tracing.getTraceConfig().updateActiveTraceParams(TraceParams.DEFAULT.toBuilder().setSampler(Samplers.probabilitySampler(1.0)).build()); | ||
Tracing.getExportComponent().getSpanExporter().registerHandler("test", new SpanExporter.Handler() { | ||
@Override | ||
public void export(Collection<SpanData> spanDataList) { | ||
exportedSpans.addAll(spanDataList); | ||
} | ||
}); | ||
} | ||
|
||
@AfterEach | ||
void cleanup(){ | ||
Tracing.getExportComponent().getSpanExporter().unregisterHandler("test"); | ||
} | ||
|
||
@Test | ||
void tracingTimer() throws InterruptedException { | ||
|
||
|
||
OpenCensusTimer.builder("trace.outer.timer", tracer).tags("testTag1","testVal1").register(registry).record(() -> { | ||
OpenCensusTimer.builder("trace.inner.timer", tracer).tags("testTag2","testVal2").register(registry).record(() -> { | ||
//Nothing to do here | ||
}); | ||
}); | ||
|
||
registry.getMeters(); | ||
|
||
waitForSpans(Duration.ofSeconds(5), 2); | ||
|
||
SpanData innerSpan = exportedSpans.get(0); | ||
SpanData outerSpan = exportedSpans.get(1); | ||
assertThat(innerSpan.getName()).isEqualTo("trace.inner.timer"); | ||
assertThat(innerSpan.getAttributes().getAttributeMap().get("commonTag1")).describedAs("Common tags are applied").isEqualTo(AttributeValue.stringAttributeValue("commonVal1")); | ||
assertThat(innerSpan.getParentSpanId()).isNotNull(); | ||
|
||
|
||
assertThat(outerSpan.getName()).isEqualTo("trace.outer.timer"); | ||
assertThat(outerSpan.getAttributes().getAttributeMap()).hasSize(2); | ||
assertThat(outerSpan.getAttributes().getAttributeMap().get("testTag1")).isEqualTo(AttributeValue.stringAttributeValue("testVal1")); | ||
assertThat(outerSpan.getParentSpanId()).isNull(); | ||
} | ||
|
||
private void waitForSpans(Duration duration, int spanCount) { | ||
long endTime = System.currentTimeMillis() + duration.toMillis(); | ||
while(endTime > System.currentTimeMillis()) { | ||
if (exportedSpans.size() == spanCount) { | ||
return; | ||
} | ||
try { | ||
Thread.sleep(100L); | ||
} catch (InterruptedException e) { | ||
//ignored | ||
} | ||
} | ||
throw new AssertionError("Expected spans of "+spanCount+" not found after waiting "+duration.toMillis()+"ms Spans Found:"+exportedSpans.size()); | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jkschneider This single line change is the only really 'required' change in Micrometer that will allow creating the other Tracing timer classes in other repos. I could remove the rest of the PR if needed.
I would love to leave the Opentracing/OpenCensus implementations in Micrometer as Optional deps. I did research and the breaking API change that Adrian brought up will be properly shimmed, and should actually behave as a deprecation for a good while.
Alternatively I could add an abstraction so the
Tracer
is an interface that could be implemented with a few lines of code that is added to a typical timer.