Introduction
可观察性是涉及测量,收集和分析来自系统的各种诊断信号的活动。 这些信号可能包括度量,跟踪,日志,事件,配置文件等等。
特别是在DevOps文化中,自动化是保持生产力的关键,可观察性起着重要作用。 您的团队应根据相关系统指标定义警报,以确保达到服务水平目标。 但是,大多数现代应用程序都是非常复杂的分布式系统,因此很难衡量所有内容。
幸运的是,如果您使用的是托管平台,则会自动为您收集许多指标。 AWS,Azure和Google Cloud等许多云平台已经收集了负载均衡器,应用程序容器,应用程序请求,数据库等的指标。 但是,云提供商无法提供的是特定于应用程序的指标,因为它们取决于您的应用程序逻辑。
千分尺为许多流行的监视系统的JVM提供了一个简单的外观,以收集特定于应用程序的指标。 当前,它支持以下监视系统: Azure监视器,Netflix Atlas,CloudWatch,Datadog,Dynatrace,New Relic,Prometheus和许多其他提供商。 Check this documentation for all available.
Micrometer
MeterRegistry
仪表是有关您的应用程序的一组测量的抽象。 仪表通过其名称和标签唯一标识。 电表注册表保存电表。 在Micrometer中,MeterRegistry是用于注册仪表的核心组件。
注册表的最简单形式是SimpleMeterRegistry。 但是在大多数情况下,我们应该使用为监视系统明确设计的MeterRegistry。 适用于Azure Monitor(AzureMonitorMeterRegistry),Prometheus(PrometheusMeterRegistry),Atlas(AtlasMeterRegistry)。
在本文中,我们将介绍Micrometer的基本用法及其与Spring boot 2的集成。
Basic Meters
Counter
计数器报告一个指标,一个计数。 计数器使您可以增加固定数量,该数量必须为正。
建立图表并从柜台发出警报时,通常您应该最感兴趣的是测量在给定时间间隔内某些事件的发生率。 考虑一个简单的队列。 计数器可用于测量诸如插入和移除项目的速率之类的事情。
Gauge
量规用于在特定时间报告数字状态. 与可以增加的计数器不同,无论何时导出度量,量规都会监视对象的状态并报告当前状态. 一个常见的示例是队列中的消息数或连接池中的连接数.
Other metrics
计时器,长任务计时器,功能跟踪计数器,功能跟踪计时器. I recommend read this article of Rosner and the official documentation.
Azure Application Insights with Spring Boot 2 using Micrometer Registry Azure
本节说明了如何使用Micrometer Azure注册表将指标导出到Azure Monitor。
Set up Azure Application Insights
First, we need to create an Application Insights Resource.
访问azure门户并创建资源。
下一步是定义订阅和实例详细信息。
访问创建的资源并获取工具密钥。
Configuring your Spring boot 2 project
在项目中添加Azure依赖项:
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-spring-boot-starter</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>天蓝色弹簧启动指标启动器</artifactId>
<version>2.2.0</version>
</dependency>
当前,微软提供了一个Spring Boot Starter,用于自动配置Azure Application Insights:applicationinsights-spring-boot-starter
.
请注意,Microsoft还提供了azure-spring-boot-metrics-starter
,用于在先前的启动器之上添加Micrometer监视支持。 在撰写本文时,其当前版本使用的配置密钥与applicationinsights-spring-boot-starter
.
Add instrumentation key
更新资料application.properties
文件并在两个属性中使用相同的检测键:
management.metrics.export.azuremonitor.instrumentation-key=XXXXXXXXXX
azure.application-insights.instrumentation-key=XXXXXXXXXX
Add custom Micrometer metric
这是使用计数器的示例:
下面的示例项目是带有HTTP操作的简单用户CRUD,在这种情况下,自定义指标是报告电话的创建用户数。
Azure Application Insights with Spring Boot 2 using Micrometer Registry Azure
You can see more about this case in https://dev.to/silviobuss/publishing-application-metrics-to-azure-monitor-using-micrometer-plk.
This project uses a database, if you already have mysql installed on the machine, you can use it by changing the settings in the application.properties
file.
Start Mysql with Docker (OPTIONAL)
To initialize a docker container with mysql, use the command below:
泊坞窗运行--name mysql57 -p 3306: 3306 -e MYSQL_ROOT_PASSWORD =根-e MYSQL_USER =用户-e MYSQL_PASSWORD =用户1234 -e MYSQL_DATABASE = demo_app -d mysql / mysql-server: 5.7
If you want to access the container to make any query:
docker exec -it mysql57 bash
and login to the mysql instance:
mysql -h localhost -u root -p
Getting Started
Just update the database properties in application.properties
and run the DemoApplication.IDE中的Java.
To generate the custom metric, just perform a POST request to endpoint http://localhost:8080/users with the JSON below:
Testing Azure Application Insights
请注意,不必将应用程序托管在azure上才能访问Application Insights和监视器。
如果一切设置正确,则在“实时指标流”中,您应该看到正在运行的方案:
我们可以在“指标”中看到我们的自定义指标:
Conclusion
在这篇文章中,我们已经看到Micrometer如何在代码和监视系统之间充当灵活的抽象层。 我们可以看到它是如何工作的,以及如何监视带有Micrometer Azure层的Spring Boot 2中实现的Java应用程序。
References
Microsoft. https://docs.microsoft.com/en-us/azure/azure-monitor/app/micrometer-java
弗兰克·罗斯纳. https://dev.to/frosnerd/publishing-application-metrics-to-cloudwatch-using-micrometer-343f