Java Code Examples for io.micrometer.core.instrument.Timer#start()

The following examples show how to use io.micrometer.core.instrument.Timer#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractConnectOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
Future<Void> reconcileConnectorAndHandleResult(Reconciliation reconciliation, String host, KafkaConnectApi apiClient,
                                         boolean useResources, String connectorName, KafkaConnector connector) {
    Promise<Void> reconciliationResult = Promise.promise();

    connectorsReconciliationsCounter.increment();
    Timer.Sample connectorsReconciliationsTimerSample = Timer.start(metrics.meterRegistry());

    reconcileConnector(reconciliation, host, apiClient, useResources, connectorName, connector)
            .onComplete(result -> {
                connectorsReconciliationsTimerSample.stop(connectorsReconciliationsTimer);

                if (result.succeeded())    {
                    connectorsSuccessfulReconciliationsCounter.increment();
                    reconciliationResult.complete();
                } else {
                    connectorsFailedReconciliationsCounter.increment();
                    reconciliationResult.fail(result.cause());
                }
            });

    return reconciliationResult.future();
}
 
Example 2
Source File: AbstractBatchMetricsAspect.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
protected Object profileMethod(ProceedingJoinPoint pjp) throws Throwable {
	Timer.Sample sample = Timer.start(meterRegistry);
	try {
		return pjp.proceed();
	} finally {
		sample.stop(meterRegistry.timer(MetricsListener.METRIC_NAME, Arrays.asList(//
				new ImmutableTag("context", getStepIdentifier()), //
				new ImmutableTag("method", ClassUtils.getShortName(pjp.getTarget().getClass()) + "."
						+ pjp.getSignature().getName()))));
	}
}
 
Example 3
Source File: MetricCollectingClientCallListener.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Creates a new delegating ClientCallListener that will wrap the given client call listener to collect metrics.
 *
 * @param delegate The original call to wrap.
 * @param registry The registry to save the metrics to.
 * @param responseCounter The counter for incoming responses.
 * @param timerFunction A function that will return a timer for a given status code.
 */
public MetricCollectingClientCallListener(
        final ClientCall.Listener<A> delegate,
        final MeterRegistry registry,
        final Counter responseCounter,
        final Function<Code, Timer> timerFunction) {
    super(delegate);
    this.responseCounter = responseCounter;
    this.timerFunction = timerFunction;
    this.timerSample = Timer.start(registry);
}
 
Example 4
Source File: MetricCollectingServerCall.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Creates a new delegating ServerCall that will wrap the given server call to collect metrics.
 *
 * @param delegate The original call to wrap.
 * @param registry The registry to save the metrics to.
 * @param responseCounter The counter for incoming responses.
 * @param timerFunction A function that will return a timer for a given status code.
 */
public MetricCollectingServerCall(final ServerCall<Q, A> delegate, final MeterRegistry registry,
        final Counter responseCounter,
        final Function<Code, Timer> timerFunction) {
    super(delegate);
    this.responseCounter = responseCounter;
    this.timerFunction = timerFunction;
    this.timerSample = Timer.start(registry);
}
 
Example 5
Source File: TimerBenchmark.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Benchmark
public int sumTimedWithSample() {
    Timer.Sample sample = Timer.start(registry);
    int sum = sum();
    sample.stop(timer);
    return sum;
}
 
Example 6
Source File: MetricCollectingServerCall.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Creates a new delegating ServerCall that will wrap the given server call to collect metrics.
 *
 * @param delegate The original call to wrap.
 * @param registry The registry to save the metrics to.
 * @param responseCounter The counter for incoming responses.
 * @param timerFunction A function that will return a timer for a given status code.
 */
public MetricCollectingServerCall(final ServerCall<Q, A> delegate, final MeterRegistry registry,
        final Counter responseCounter,
        final Function<Code, Timer> timerFunction) {
    super(delegate);
    this.responseCounter = responseCounter;
    this.timerFunction = timerFunction;
    this.timerSample = Timer.start(registry);
}
 
Example 7
Source File: TimedCallable.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Override
public V call() throws Exception {
    idleSample.stop(idleTimer);
    Timer.Sample executionSample = Timer.start(registry);
    try {
        return callable.call();
    } finally {
        executionSample.stop(executionTimer);
    }
}
 
Example 8
Source File: TimedCallable.java    From micrometer with Apache License 2.0 5 votes vote down vote up
TimedCallable(MeterRegistry registry, Timer executionTimer, Timer idleTimer, Callable<V> callable) {
    this.registry = registry;
    this.executionTimer = executionTimer;
    this.idleTimer = idleTimer;
    this.callable = callable;
    this.idleSample = Timer.start(registry);
}
 
Example 9
Source File: TimedRunnable.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    idleSample.stop(idleTimer);
    Timer.Sample executionSample = Timer.start(registry);
    try {
        command.run();
    } finally {
        executionSample.stop(executionTimer);
    }
}
 
Example 10
Source File: MetricCollectingClientCallListener.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Creates a new delegating ClientCallListener that will wrap the given client call listener to collect metrics.
 *
 * @param delegate The original call to wrap.
 * @param registry The registry to save the metrics to.
 * @param responseCounter The counter for incoming responses.
 * @param timerFunction A function that will return a timer for a given status code.
 */
public MetricCollectingClientCallListener(
        final ClientCall.Listener<A> delegate,
        final MeterRegistry registry,
        final Counter responseCounter,
        final Function<Code, Timer> timerFunction) {
    super(delegate);
    this.responseCounter = responseCounter;
    this.timerFunction = timerFunction;
    this.timerSample = Timer.start(registry);
}
 
Example 11
Source File: MicrometerHttpRequestExecutor.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse execute(HttpRequest request, HttpClientConnection conn, HttpContext context) throws IOException, HttpException {
    Timer.Sample timerSample = Timer.start(registry);

    Tag method = Tag.of("method", request.getRequestLine().getMethod());
    Tag uri = Tag.of("uri", uriMapper.apply(request));
    Tag status = STATUS_UNKNOWN;

    Tags routeTags = exportTagsForRoute ? HttpContextUtils.generateTagsForRoute(context) : Tags.empty();

    try {
        HttpResponse response = super.execute(request, conn, context);
        status = response != null ? Tag.of("status", Integer.toString(response.getStatusLine().getStatusCode())) : STATUS_CLIENT_ERROR;
        return response;
    } catch (IOException | HttpException | RuntimeException e) {
        status = STATUS_IO_ERROR;
        throw e;
    } finally {
        Iterable<Tag> tags = Tags.of(extraTags)
                .and(routeTags)
                .and(uri, method, status);

        timerSample.stop(Timer.builder(METER_NAME)
                .description("Duration of Apache HttpClient request execution")
                .tags(tags)
                .register(registry));
    }
}
 
Example 12
Source File: RntbdRequestArgs.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
public RntbdRequestArgs(final RxDocumentServiceRequest serviceRequest, final URI physicalAddress) {
    this.sample = Timer.start();
    this.activityId = UUID.fromString(serviceRequest.getActivityId());
    this.timeCreated = OffsetDateTime.now();
    this.nanoTimeCreated = System.nanoTime();
    this.lifetime = Stopwatch.createStarted();
    this.origin = physicalAddress.getScheme() + "://" + physicalAddress.getAuthority();
    this.physicalAddress = physicalAddress;
    this.replicaPath = StringUtils.stripEnd(physicalAddress.getPath(), "/");
    this.serviceRequest = serviceRequest;
    this.transportRequestId = instanceCount.incrementAndGet();
}
 
Example 13
Source File: WebMvcMetricsFilter.java    From foremast with Apache License 2.0 5 votes vote down vote up
TimingSampleContext(HttpServletRequest request, Object handlerObject) {
    timedAnnotations = annotations(handlerObject);
    timerSample = Timer.start(registry);
    longTaskTimerSamples = timedAnnotations.stream()
        .filter(Timed::longTask)
        .map(t -> LongTaskTimer.builder(t)
            .tags(tagsProvider.httpLongRequestTags(request, handlerObject))
            .register(registry)
            .start())
        .collect(Collectors.toList());
}
 
Example 14
Source File: ReactiveTimedAspect.java    From spring-rdbms-cdc-kafka-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Around("execution (@io.micrometer.core.annotation.Timed * *.*(..))")
public Object timedMethod(ProceedingJoinPoint joinPoint) throws Throwable {

  MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

  String className = methodSignature.getDeclaringType().getSimpleName();
  String methodName = methodSignature.getName();
  String metricName = String.format("%s.%s", className, methodName);

  Timer.Sample timer = Timer.start(registry);

  //@formatter:off
  return Try.of(joinPoint::proceed)
            .map(value ->
                Match(value).of(
                    Case($(instanceOf(Mono.class)),
                        m -> m.doOnTerminate(() -> stopTimer(className, methodName, metricName, timer))),

                    Case($(instanceOf(Flux.class)),
                        f -> f.doOnTerminate(() -> stopTimer(className, methodName, metricName, timer))),

                    Case($(), res -> {
                      stopTimer(className, methodName, metricName, timer);
                      return res;
                    })
                ))
            .get();
  //@formatter:on
}
 
Example 15
Source File: ReactiveTimedAspect.java    From spring-rdbms-cdc-kafka-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Around("execution (@io.micrometer.core.annotation.Timed * *.*(..))")
public Object timedMethod(ProceedingJoinPoint joinPoint) throws Throwable {

  MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

  String className = methodSignature.getDeclaringType().getSimpleName();
  String methodName = methodSignature.getName();
  String metricName = String.format("%s.%s", className, methodName);

  Timer.Sample timer = Timer.start(registry);

  //@formatter:off
  return Try.of(joinPoint::proceed)
            .map(value ->
                Match(value).of(
                    Case($(instanceOf(Mono.class)),
                        m -> m.doOnTerminate(() -> stopTimer(className, methodName, metricName, timer))),

                    Case($(instanceOf(Flux.class)),
                        f -> f.doOnTerminate(() -> stopTimer(className, methodName, metricName, timer))),

                    Case($(), res -> {
                      stopTimer(className, methodName, metricName, timer);
                      return res;
                    })
                ))
            .get();
  //@formatter:on
}
 
Example 16
Source File: JooqExecuteListener.java    From micrometer with Apache License 2.0 4 votes vote down vote up
private void startTimer(ExecuteContext ctx) {
    Timer.Sample started = Timer.start(registry);
    synchronized (sampleLock) {
        sampleByExecuteContext.put(ctx, started);
    }
}
 
Example 17
Source File: MicrometerBasedMetrics.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Sample startTimer() {
    return Timer.start(registry);
}
 
Example 18
Source File: EndpointMetricsFilter.java    From syndesis with Apache License 2.0 4 votes vote down vote up
/**
 * Called before the resource method.
 */
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    Sample sample = Timer.start(registry);
    requestContext.setProperty(TIMING_SAMPLE, sample);
}
 
Example 19
Source File: EndpointMetricsFilter.java    From syndesis with Apache License 2.0 4 votes vote down vote up
/**
 * Called before the resource method.
 */
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    Sample sample = Timer.start(registry);
    requestContext.setProperty(TIMING_SAMPLE, sample);
}
 
Example 20
Source File: MicrometerResponderRSocket.java    From spring-cloud-rsocket with Apache License 2.0 4 votes vote down vote up
Timer.Sample start() {
	return Timer.start(meterRegistry);
}