Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is Micrometer and how do you expose metrics to Prometheus? Micrometer क्या है और आप metrics को Prometheus को कैसे expose करते हैं?

Answer

Micrometer is a metrics facade supporting multiple monitoring systems (Prometheus, Datadog, New Relic). Spring Boot Actuator uses Micrometer by default. Export metrics to Prometheus for visualization in Grafana dashboards.

// Micrometer Configuration
// pom.xml
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

// application.yml
management:
  endpoints:
    web:
      exposure:
        include: prometheus
  metrics:
    tags:
      application: order-service
      environment: production

// Custom Metrics
@Service
public class OrderMetricsService {
    private final MeterRegistry meterRegistry;
    private final Counter ordersCreated;
    private final Timer orderProcessingTimer;
    
    public OrderMetricsService(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
        this.ordersCreated = Counter.builder('orders.total')
            .description('Total orders created')
            .tag('type', 'business')
            .register(meterRegistry);
        
        this.orderProcessingTimer = Timer.builder('order.processing')
            .description('Order processing time')
            .register(meterRegistry);
    }
    
    public void processOrder(Order order) {
        orderProcessingTimer.recordCallable(() -> {
            // Process order
            ordersCreated.increment();
            return null;
        });
    }
    
    public void recordInventoryLevel(int level) {
        meterRegistry.gauge('inventory.level', level);
    }
}

// Prometheus Configuration (docker-compose.yml)
version: '3.8'
services:
  app:
    image: app:latest
    ports:
      - '8080:8080'
  
  prometheus:
    image: prom/prometheus:latest
    ports:
      - '9090:9090'
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
  
  grafana:
    image: grafana/grafana:latest
    ports:
      - '3000:3000'
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin

// prometheus.yml
global:
  scrape_interval: 15s
scrape_configs:
  - job_name: 'spring-boot-app'
    static_configs:
      - targets: ['localhost:8080']
    metrics_path: '/actuator/prometheus'

// Grafana Queries
// Total orders: sum(orders_total)
// Order processing rate: rate(order_processing_seconds_sum[5m])
// P95 latency: histogram_quantile(0.95, order_processing_seconds)

// Key Micrometer Metrics:
// 1. Counter - incrementing metric
// 2. Timer - timing operations
// 3. Gauge - current value
// 4. Distribution Summary - distribution of values
Micrometer:

Metrics facade - एक interface से multiple backends

Key Components:
1. Counter - counting events
2. Timer - measure duration
3. Gauge - current value
4. Histogram - distribution

Prometheus Export:
/actuator/prometheus endpoint
Scrape करना Prometheus से

Grafana Visualization:
- Dashboards बनाना
- Alerts configure करना
- Query language: PromQL

Common Metrics:
- http.server.requests
- process.cpu.usage
- jvm.memory.used
- orders.total (custom)

Was this answer clear?