使用Spring Boot 2和Micrometer将应用程序度量发布到Azure Monitor

Introduction

可观察性是涉及测量,收集和分析来自系统的各种诊断信号的活动。 这些信号可能包括度量,跟踪,日志,事件,配置文件等等。

特别是在DevOps文化中,自动化是保持生产力的关键,可观察性起着重要作用。 您的团队应根据相关系统指标定义警报,以确保达到服务水平目标。 但是,大多数现代应用程序都是非常复杂的分布式系统,因此很难衡量所有内容。

幸运的是,如果您使用的是托管平台,则会自动为您收集许多指标。 AWS,Azure和Google Cloud等许多云平台已经收集了负载均衡器,应用程序容器,应用程序请求,数据库等的指标。 但是,云提供商无法提供的是特定于应用程序的指标,因为它们取决于您的应用程序逻辑。

Micrometer provides a simple facade for the JVM for a number of popular monitoring systems to collect application specific metrics. Currently, it supports the following monitoring systems: Azure Monitor, Netflix Atlas, CloudWatch, Datadog, Dynatrace, New Relic, Prometheus, And many other providers. 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

Alt Text

计数器报告一个指标,一个计数。 计数器使您可以增加固定数量,该数量必须为正。

建立图表并从柜台发出警报时,通常您应该最感兴趣的是测量在给定时间间隔内某些事件的发生率。 考虑一个简单的队列。 计数器可用于测量诸如插入和移除项目的速率之类的事情。

Gauge

Alt Text
Gauges are used to report a numeric state at a certain time. In contrast to a counter which you can increment, a gauge watches the state of an object and reports the current state whenever the metric is exported. A common example is the number of messages in a queue, or the number of connections in your connection pool.

Other metrics

Timer, Long Task Timer, Function-tracking counters, Function-tracking timers. 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门户并创建资源。

Alt Text

下一步是定义订阅和实例详细信息。

Alt Text

访问创建的资源并获取工具密钥。

Alt Text

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>azure-spring-boot-metrics-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

当前,Microsoft提供了一个Spring Boot Starter,用于自动配置Azure Application Insights:applicationinsights-spring-boot-starter。

请注意,Microsoft还提供了天蓝色弹簧启动指标启动器,用于在先前的启动器之上添加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

这是使用计数器的示例:

Alt Text

下面的示例项目是带有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:

docker run --name mysql57 -p 3306: 3306 -e MYSQL_ROOT_PASSWORD = root -e MYSQL_USER = user -e MYSQL_PASSWORD = user1234 -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.java in your IDE.

To generate the custom metric, just perform a POST request to endpoint http://localhost:8080/users with the JSON below:

Alt Text

Testing Azure Application Insights

请注意,不必将应用程序托管在azure上才能访问Application Insights和监视器。

如果一切设置正确,则在“实时指标流”中,您应该看到正在运行的方案:

Alt Text

我们可以在“指标”中看到我们的自定义指标:

Alt Text

Conclusion

在这篇文章中,我们已经看到Micrometer如何在代码和监视系统之间充当灵活的抽象层。 我们可以看到它是如何工作的,以及如何监视带有Micrometer Azure层的Spring Boot 2中实现的Java应用程序。

References

Microsoft. https://docs.microsoft.com/en-us/azure/azure-monitor/app/micrometer-java

Frank Rosner. https://dev.to/frosnerd/publishing-application-metrics-to-cloudwatch-using-micrometer-343f

from: https://dev.to//silviobuss/publishing-application-metrics-to-azure-monitor-using-micrometer-plk

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值