Vladsz83 commented on a change in pull request #7446: IGNITE-12464 : Service metrics URL: https://github.com/apache/ignite/pull/7446#discussion_r401068478
########## File path: modules/core/src/main/java/org/apache/ignite/internal/processors/service/IgniteServiceProcessor.java ########## @@ -1801,4 +1842,178 @@ private boolean enterBusy() { private void leaveBusy() { opsLock.readLock().unlock(); } + + /** + * Registers metrics to measure durations of service methods. + * + * @param srvc Service for invocations measurement. + * @param srvcName Name of {@code srvc}. + */ + private void registerMetrics(Service srvc, String srvcName) { + getInterfaces(srvc.getClass()).stream().map(Class::getMethods).flatMap(Arrays::stream) + .filter(mtd -> !isMetricIgnoredFor(mtd.getDeclaringClass())) + .forEach(mtd -> { + // All metrics for current service. + Map<String, MethodHistogramHolder> srvcHistograms = + invocationHistograms.computeIfAbsent(srvcName, name -> new HashMap<>(1)); + + // Histograms for this method name. + MethodHistogramHolder mtdHistograms = + srvcHistograms.computeIfAbsent(mtd.getName(), mdtName -> new MethodHistogramHolder()); + + mtdHistograms.addIfAbsent(mtd, () -> createHistogram(srvcName, mtd)); + }); + } + + /** + * Removes metrics for service {@code srvcName}. + * + * @param srvcName Service name. + */ + private void unregisterMetrics(String srvcName) { + ctx.metric().remove(serviceMetricRegistryName(srvcName)); + + invocationHistograms.remove(srvcName); + } + + /** + * Creates histogram for service method. If exist,s considers one or several argument types has same name but + * different package and tries to extend metric name with abbreviation of java package name. + * + * @param srvcName Service name. + * @param method Method to measure. + * @return Histogram of service method timings. + */ + HistogramMetricImpl createHistogram(String srvcName, Method method) { + MetricRegistry metricRegistry = ctx.metric().registry(serviceMetricRegistryName(srvcName)); + + HistogramMetricImpl histogram = null; + + // Find/create histogram. + for (int i = 0; i <= MAX_ABBREVIATE_NAME_LVL; ++i) { + String methodMetricName = methodMetricName(method, i); + + synchronized (metricRegistry) { Review comment: @ivandasch , there is a gap between `metricRegistry.findMetric(methodMetricName)` and `metricRegistry.histogram(methodMetricName, DEFAULT_INVOCATION_BOUNDS, DESCRIPTION_OF_INVOCATION_METRIC)` It is important. If something creates metric after `metricRegistry.findMetric(methodMetricName)`, it would cause incorrent metric naming or NPE. Is it ok to keep synchronized ? ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services