reta commented on a change in pull request #642: URL: https://github.com/apache/cxf/pull/642#discussion_r508906820
########## File path: integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/micrometer/provider/SpringBasedTimedAnnotationProvider.java ########## @@ -0,0 +1,97 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.cxf.spring.boot.autoconfigure.micrometer.provider; + +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Method; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.cxf.message.Exchange; +import org.apache.cxf.metrics.micrometer.provider.TimedAnnotationProvider; +import org.apache.cxf.service.Service; +import org.apache.cxf.service.invoker.MethodDispatcher; +import org.apache.cxf.service.model.BindingOperationInfo; +import org.springframework.core.annotation.MergedAnnotationCollectors; +import org.springframework.core.annotation.MergedAnnotations; + +import io.micrometer.core.annotation.Timed; + +public class SpringBasedTimedAnnotationProvider implements TimedAnnotationProvider { + + private ConcurrentHashMap<HandlerMethod, Set<Timed>> timedAnnotationCache = new ConcurrentHashMap<>(); + + @Override + public Set<Timed> getTimedAnnotations(Exchange ex) { + HandlerMethod handlerMethod = new HandlerMethod(ex); + return timedAnnotationCache.computeIfAbsent(handlerMethod, method -> { Review comment: Sorry, a few issues I have run into with this method. 1. The `computeIfAbsent` is known to introduce unnecessary locking on JDK8 in case when entry already exists (see please https://bugs.openjdk.java.net/browse/JDK-8161372). Since we are going to call `getTimedAnnotations` for each invocation, it is better to avoid these kind if issues if possible. 2. We allocate the empty sets every time for each method that does not have `@Timed` annotation, we could use `Collections.emptySet()` singleton instead. The suggested modifications are below, hope they make sense (the same would apply to `DefaultTimedAnnotationProvider`) ``` public Set<Timed> getTimedAnnotations(Exchange ex) { HandlerMethod handlerMethod = new HandlerMethod(ex); final Set<Timed> exists = timedAnnotationCache.get(handlerMethod); if (exists != null) { return exists; } return timedAnnotationCache.computeIfAbsent(handlerMethod, method -> { Set<Timed> timed = findTimedAnnotations(method.getMethod()); if (timed.isEmpty()) { timed = findTimedAnnotations(method.getBeanType()); } return timed.isEmpty() ? Collections.emptySet() : timed;; }); } ``` PS: I am fine doing these changes myself after merge as well, please let me know your preferences. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
