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
| @Component public class ServiceMetrics { private final Counter requestCounter; private final Timer requestTimer; private final Gauge activeConnections; private final Histogram requestSize; public ServiceMetrics(MeterRegistry meterRegistry) { this.requestCounter = Counter.builder("service.requests.total") .description("Total number of requests") .register(meterRegistry); this.requestTimer = Timer.builder("service.request.duration") .description("Request processing time") .register(meterRegistry); this.activeConnections = Gauge.builder("service.connections.active") .description("Number of active connections") .register(meterRegistry, this, ServiceMetrics::getActiveConnectionCount); this.requestSize = Histogram.builder("service.request.size") .description("Request size in bytes") .buckets(100, 1000, 10000, 100000) .register(meterRegistry); } public void recordRequest(String method, String endpoint, int status, long duration, long size) { requestCounter.increment( Tags.of( Tag.of("method", method), Tag.of("endpoint", endpoint), Tag.of("status", String.valueOf(status)) ) ); requestTimer.record(duration, TimeUnit.MILLISECONDS, Tags.of( Tag.of("method", method), Tag.of("endpoint", endpoint) ) ); requestSize.record(size); } private double getActiveConnectionCount() { return connectionManager.getActiveConnectionCount(); } }
@Component public class BusinessMetrics { private final Counter orderCounter; private final Timer orderProcessingTime; private final Gauge inventoryLevel; public BusinessMetrics(MeterRegistry meterRegistry) { this.orderCounter = Counter.builder("business.orders.total") .description("Total number of orders") .register(meterRegistry); this.orderProcessingTime = Timer.builder("business.order.processing.time") .description("Order processing time") .register(meterRegistry); this.inventoryLevel = Gauge.builder("business.inventory.level") .description("Current inventory level") .register(meterRegistry, this, BusinessMetrics::getCurrentInventoryLevel); } public void recordOrder(String productCategory, String paymentMethod, long processingTime) { orderCounter.increment( Tags.of( Tag.of("category", productCategory), Tag.of("payment_method", paymentMethod) ) ); orderProcessingTime.record(processingTime, TimeUnit.MILLISECONDS, Tags.of(Tag.of("category", productCategory)) ); } private double getCurrentInventoryLevel() { return inventoryService.getTotalInventoryValue(); } }
|