如何获取Actuator Prometheus的自定义指标
Great to hear you’ve got the base setup with Micrometer, Actuator, and the Prometheus endpoint enabled! Let’s walk through exactly how to add and expose your own custom metrics:
1. Inject MeterRegistry into Your Component
First, grab Micrometer’s MeterRegistry in the class where you want to track metrics (a service, controller, or custom component works):
import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.stereotype.Service; @Service public class OrderMetricsService { private final Counter orderCreatedCounter; // Initialize your metric via constructor injection public OrderMetricsService(MeterRegistry meterRegistry) { this.orderCreatedCounter = Counter.builder("orders.created.total") .description("Total number of orders created in the system") .tag("order_type", "online") // Add custom tags for Prometheus filtering .register(meterRegistry); } }
2. Update Metrics in Your Business Logic
Trigger the metric update wherever your relevant business action happens:
public void createOrder(Order order) { // Your core order creation logic here orderCreatedCounter.increment(); // Increment counter each time an order is created }
3. Use Annotations for Quick, Auto-Generated Metrics
If you don’t want to manually register metrics, Micrometer has handy annotations for common use cases like timing method execution:
import io.micrometer.core.annotation.Timed; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class OrderController { @Timed(value = "order.processing.time", description = "Time taken to process an order request") @PostMapping("/orders") public Order createOrder() { // Simulate processing time try { Thread.sleep(150); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return new Order(); } }
Note: For annotations to work automatically in Spring Boot 2.2+, you usually don’t need extra config—but if you run into issues, add
@EnableMetricsand@EnableAspectJAutoProxyto your configuration class.
4. Explore Other Metric Types
Micrometer supports all the metric types you’ll need for monitoring:
- Gauge: Tracks instantaneous values (e.g., current active sessions)
Gauge.builder("active.sessions", this::getActiveSessionCount) .description("Number of currently active user sessions") .register(meterRegistry); - Timer: Manually time code blocks when annotations aren’t feasible
Timer inventoryUpdateTimer = Timer.builder("inventory.update.duration") .description("Time taken to update inventory stock") .register(meterRegistry); // Time a specific task inventoryUpdateTimer.record(() -> { runInventoryUpdateLogic(); });
5. Verify Your Metrics
Once your app is running, hit the /actuator/prometheus endpoint. You’ll see your custom metrics in the plaintext output, like this:
# HELP orders_created_total Total number of orders created in the system # TYPE orders_created_total counter orders_created_total{order_type="online",} 12.0
内容的提问来源于stack exchange,提问作者Dullimeister

