001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.metrics; 020 021import io.micrometer.core.instrument.MeterRegistry; 022import org.jpos.q2.Q2; 023 024/** 025 * Utility class for accessing Micrometer metrics in jPOS components. 026 * 027 * <p>This class provides convenient static access to the MeterRegistry for components 028 * that don't extend QBeanSupport or otherwise have direct access to the Q2 container.</p> 029 * 030 * <h2>Usage Examples</h2> 031 * 032 * <h3>For QBeans (components extending QBeanSupport)</h3> 033 * <pre> 034 * MeterRegistry registry = getServer().getMeterRegistry(); 035 * </pre> 036 * 037 * <h3>For Non-QBean components (e.g., providers, participants)</h3> 038 * <pre> 039 * import org.jpos.metrics.Metrics; 040 * 041 * MeterRegistry registry = Metrics.getMeterRegistry(); 042 * Counter counter = Counter.builder("my.metric") 043 * .description("My metric description") 044 * .register(registry); 045 * </pre> 046 * 047 * <h3>Using MeterFactory for consistent metrics</h3> 048 * <pre> 049 * MeterRegistry registry = Metrics.getMeterRegistry(); 050 * Counter counter = MeterFactory.counter(registry, myMeterInfo, Tags.of("key", "value")); 051 * </pre> 052 * 053 * @see MeterFactory 054 * @see MeterInfo 055 * @see Q2#getMeterRegistry() 056 */ 057public class Metrics { 058 /** Default constructor; no instance state to initialise. */ 059 public Metrics() {} 060 061 /** 062 * Get the MeterRegistry from the default Q2 instance. 063 * 064 * <p>For use by components that don't have direct access to Q2 (non-QBeans). 065 * This method looks up the Q2 instance registered as "Q2" in NameRegistrar 066 * and returns its MeterRegistry, or null if Q2 is not available.</p> 067 * 068 * <p>Note: Q2.getQ2() returns null if Q2 is not found (uses getIfExists internally).</p> 069 * 070 * @return MeterRegistry from the default Q2 instance, or null if Q2 not available 071 */ 072 public static MeterRegistry getMeterRegistry() { 073 Q2 q2 = Q2.getQ2(); 074 return q2 != null ? q2.getMeterRegistry() : null; 075 } 076 077 /** 078 * Get the MeterRegistry from the default Q2 instance, waiting up to timeout milliseconds. 079 * 080 * <p>This variant blocks until Q2 is available or the timeout expires. 081 * Useful during initialization when Q2 might not be fully started yet.</p> 082 * 083 * <p>Throws {@link org.jpos.util.NameRegistrar.NotFoundException} (unchecked) 084 * if Q2 is not registered before the timeout elapses. 085 * 086 * @param timeout maximum time to wait in milliseconds 087 * @return MeterRegistry from Q2 088 */ 089 public static MeterRegistry getMeterRegistry(long timeout) { 090 return Q2.getQ2(timeout).getMeterRegistry(); 091 } 092 093 /** 094 * Get the MeterRegistry from a named Q2 instance. 095 * 096 * <p>Use this when multiple Q2 instances are running and you need to access 097 * a specific instance's MeterRegistry.</p> 098 * 099 * @param q2Name Q2 instance name (e.g., "Q2", "Q2-1", etc.) 100 * @return MeterRegistry from the named Q2 instance, or null if not found 101 */ 102 public static MeterRegistry getMeterRegistry(String q2Name) { 103 Q2 q2 = org.jpos.util.NameRegistrar.getIfExists(q2Name); 104 return q2 != null ? q2.getMeterRegistry() : null; 105 } 106 107 /** 108 * Get the MeterRegistry from a named Q2 instance, waiting up to timeout milliseconds. 109 * 110 * <p>Throws {@link org.jpos.util.NameRegistrar.NotFoundException} (unchecked) 111 * if the named Q2 is not registered before the timeout elapses. 112 * 113 * @param q2Name Q2 instance name 114 * @param timeout maximum time to wait in milliseconds 115 * @return MeterRegistry from named Q2 instance 116 */ 117 public static MeterRegistry getMeterRegistry(String q2Name, long timeout) { 118 return org.jpos.util.NameRegistrar.<Q2>get(q2Name, timeout).getMeterRegistry(); 119 } 120}