Skip to content

Commit

Permalink
Create Timer.start() and remove Timer.Sample.start(), polish
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jan 16, 2018
1 parent 90f206b commit 661e540
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package io.micrometer.prometheus;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.config.NamingConvention;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.config.NamingConvention;
import io.prometheus.client.Collector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ default Type type() {
return Type.Timer;
}

static Sample start(MeterRegistry registry) {
return new Sample(registry.config().clock());
}

static Sample start(Clock clock) {
return new Sample(clock);
}

class Sample {
private final long startTime;
private final Clock clock;
Expand All @@ -147,18 +155,10 @@ class Sample {
this.startTime = clock.monotonicTime();
}

public static Sample start(Clock clock) {
return new Sample(clock);
}

public static Sample start(MeterRegistry registry) {
return start(registry.config().clock());
}

/**
* Records the duration of the operation
*
* @return The duration that was stop in nanoseconds
* @return The total duration of the sample in nanoseconds
*/
public long stop(Timer timer){
long durationNs = clock.monotonicTime() - startTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void onEvent(RequestEvent event) {
timedAnnotations = annotations(event);

timedAnnotationsOnRequest.put(containerRequest, timedAnnotations);
shortTaskSample.put(containerRequest, Timer.Sample.start(registry));
shortTaskSample.put(containerRequest, Timer.start(registry));

List<LongTaskTimer.Sample> longTaskSamples = longTaskTimers(timedAnnotations, event).stream().map(LongTaskTimer::start).collect(Collectors.toList());
if (!longTaskSamples.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private class TimingSampleContext {

TimingSampleContext(HttpServletRequest request, Object handlerObject) {
timedAnnotations = annotations(handlerObject);
timerSample = Timer.Sample.start(registry);
timerSample = Timer.start(registry);
longTaskTimerSamples = timedAnnotations.stream()
.filter(Timed::longTask)
.map(t -> LongTaskTimer.builder(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ default void recordWithRunnable(MeterRegistry registry) throws Exception {

@Test
@DisplayName("record with stateful Sample instance")
default void recordWithTiming(MeterRegistry registry) throws Exception {
default void recordWithSample(MeterRegistry registry) throws Exception {
Timer timer = registry.timer("myTimer");
Timer.Sample sample = Timer.Sample.start(registry);
Timer.Sample sample = Timer.start(registry);

clock(registry).add(10, TimeUnit.NANOSECONDS);
sample.stop(timer);
Expand Down

0 comments on commit 661e540

Please sign in to comment.