diff --git a/micrometer-core/build.gradle b/micrometer-core/build.gradle index 3423c48a57..0971554690 100644 --- a/micrometer-core/build.gradle +++ b/micrometer-core/build.gradle @@ -36,6 +36,13 @@ dependencies { compile 'com.squareup.okhttp3:okhttp:3.9.0',optional + //tracing + compile 'io.opencensus:opencensus-api:0.9.1', optional + testCompile 'io.opencensus:opencensus-impl:0.9.1' + compile 'io.opentracing:opentracing-api:0.30.0', optional + testCompile 'io.opentracing:opentracing-mock:0.30.0' + testCompile 'io.opentracing:opentracing-util:0.30.0' + testCompile 'io.projectreactor:reactor-test:3.1.0.RELEASE' testCompile 'io.projectreactor.ipc:reactor-netty:0.7.1.RELEASE' diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/Timer.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/Timer.java index 5455b2acf8..08da42a0f9 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/Timer.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/Timer.java @@ -143,7 +143,7 @@ class Builder { private String description; private final HistogramConfig.Builder histogramConfigBuilder; - private Builder(String name) { + protected Builder(String name) { this.name = name; this.histogramConfigBuilder = new HistogramConfig.Builder(); minimumExpectedValue(Duration.ofMillis(1)); diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/tracing/OpenCensusTimer.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/tracing/OpenCensusTimer.java new file mode 100644 index 0000000000..75d2ae4433 --- /dev/null +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/tracing/OpenCensusTimer.java @@ -0,0 +1,133 @@ +/** + * Copyright 2017 Pivotal Software, Inc. + *
+ * 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 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
+ * 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 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
+ * 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 io.micrometer.core.instrument.tracing;
+
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+import io.opentracing.mock.MockSpan;
+import io.opentracing.mock.MockTracer;
+import io.opentracing.util.ThreadLocalActiveSpanSource;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class OpenTracingTimerTest {
+
+ private MockTracer tracer;
+ private SimpleMeterRegistry registry;
+
+ @BeforeEach
+ void setup(){
+ tracer = new MockTracer(new ThreadLocalActiveSpanSource(), MockTracer.Propagator.TEXT_MAP);
+ registry = new SimpleMeterRegistry();
+ registry.config().commonTags("commonTag1", "commonVal1");
+ }
+
+ @Test
+ void tracingTimer() {
+ OpenTracingTimer.builder("trace.outer.timer", tracer).tags("testTag1","testVal1").register(registry).record(() -> {
+ OpenTracingTimer.builder("trace.inner.timer", tracer).tags("testTag2","testVal2").register(registry).record(() -> {
+ //Nothing to do here
+ });
+ });
+
+ List