You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Actuator Prometheus的自定义指标

Adding Custom Metrics to Your Spring Actuator Prometheus Endpoint

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 @EnableMetrics and @EnableAspectJAutoProxy to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:21:42