1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| @Component @Slf4j public class CustomMetricsCollector { private final MeterRegistry meterRegistry; private final Timer.Sample startSample; private final Counter orderCreatedCounter; private final Counter paymentSuccessCounter; private final Counter userLoginCounter; private final Timer databaseQueryTimer; private final Timer externalApiTimer; private final Gauge activeConnectionGauge; private final Gauge cacheHitRateGauge; public CustomMetricsCollector(MeterRegistry meterRegistry) { this.meterRegistry = meterRegistry; this.orderCreatedCounter = Counter.builder("business.order.created.total") .description("Total number of orders created") .tag("service", "order-service") .register(meterRegistry); this.paymentSuccessCounter = Counter.builder("business.payment.success.total") .description("Total number of successful payments") .tag("service", "payment-service") .register(meterRegistry); this.databaseQueryTimer = Timer.builder("database.query.duration") .description("Database query execution time") .register(meterRegistry); this.externalApiTimer = Timer.builder("external.api.duration") .description("External API call duration") .register(meterRegistry); this.activeConnectionGauge = Gauge.builder("system.connections.active") .description("Number of active database connections") .register(meterRegistry, this, CustomMetricsCollector::getActiveConnections); this.cacheHitRateGauge = Gauge.builder("cache.hit.rate") .description("Cache hit rate percentage") .register(meterRegistry, this, CustomMetricsCollector::getCacheHitRate); }
public void recordBusinessOperation(String operation, Map<String, String> tags) { Timer.Sample sample = Timer.start(meterRegistry); try { switch (operation) { case "order.created": orderCreatedCounter.increment( Tags.of("status", tags.getOrDefault("status", "unknown")) ); break; case "payment.success": paymentSuccessCounter.increment( Tags.of("method", tags.getOrDefault("method", "unknown")) ); break; default: log.warn("Unknown business operation: {}", operation); } } finally { sample.stop(Timer.builder("business.operation.duration") .tag("operation", operation) .register(meterRegistry)); } }
public <T> T recordDatabaseQuery(String queryType, Supplier<T> queryOperation) { return databaseQueryTimer.recordCallable(() -> { Timer.Sample sample = Timer.start(meterRegistry); try { T result = queryOperation.get(); meterRegistry.counter("database.query.total", "type", queryType, "status", "success" ).increment(); return result; } catch (Exception e) { meterRegistry.counter("database.query.total", "type", queryType, "status", "error", "error", e.getClass().getSimpleName() ).increment(); throw e; } finally { sample.stop(Timer.builder("database.query.detailed.duration") .tag("type", queryType) .register(meterRegistry)); } }); }
public <T> T recordExternalApiCall(String apiName, String method, Supplier<T> apiCall) { return Timer.Sample.start(meterRegistry) .stop(externalApiTimer.wrap(() -> { long startTime = System.currentTimeMillis(); try { T result = apiCall.get(); meterRegistry.counter("external.api.calls.total", "api", apiName, "method", method, "status", "success" ).increment(); return result; } catch (Exception e) { meterRegistry.counter("external.api.calls.total", "api", apiName, "method", method, "status", "error", "error", e.getClass().getSimpleName() ).increment(); throw e; } finally { long duration = System.currentTimeMillis() - startTime; log.debug("External API call completed: {} {} in {}ms", method, apiName, duration); } })); } private Number getActiveConnections() { return 42; } private Number getCacheHitRate() { return 0.85; } }
|