reka-core-20240722· unknown
from LMArena Human Preference 100K
приведи пример использования micrometer в spring с помощью аннотаций и ручной…
- User
приведи пример использования micrometer в spring с помощью аннотаций и ручной регистрации метрик
- Assistant
Вот пример использования Micrometer в Spring с помощью аннотаций и ручной регистрации метрик:
-
Добавьте зависимости в
pom.xml:xml<dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Micrometer Registry Prometheus --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <!-- Micrometer Spring Boot Starter --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-spring-boot-starter</artifactId> </dependency> </dependencies> -
Настройте Micrometer для использования Prometheus реестра в
application.properties:propertiesmanagement.endpoints.web.exposure.include=* micrometer.registry.prometheus.enabled=true -
Создайте контроллер с аннотациями Micrometer:
javaimport io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.binder.http.HttpClientMetrics; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { private final MeterRegistry meterRegistry; @Autowired public MyController(MeterRegistry meterRegistry) { this.meterRegistry = meterRegistry; // Регистрируем метрики для HTTP клиента HttpClientMetrics.register(meterRegistry, "my-http-client"); } @GetMapping("/my-endpoint") public String myEndpoint() { // Создаем и регистрируем счетчик вызовов метода meterRegistry.counter("my-endpoint.calls").increment(); // Возвращаем ответ return "Hello, World!"; } }
-