简体   繁体   English

使用 Micrometer 将 New Relic 与 Spring Boot 集成

[英]Integrate New Relic with Spring Boot using Micrometer

I am trying to integrate a simple Spring Boot Application with New Relic using Micrometer.我正在尝试使用千分尺将一个简单的 Spring Boot 应用程序与 New Relic 集成。

Here are the configurations details:-以下是配置详细信息:-

application.properties应用程序属性

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

management.metrics.export.newrelic.enabled=true
management.metrics.export.newrelic.api-key:MY_API_KEY // Have added the API key here
management.metrics.export.newrelic.account-id: MY_ACCOUNT_ID // Have added the account id here
logging.level.io.micrometer.newrelic=TRACE

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>springboot.micrometer.demo</groupId>
    <artifactId>micrometer-new-relic</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>micrometer-new-relic</name>
    <description>Demo project for actuator integration with new relic using micrometer</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-new-relic</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I was able to integrate Prometheus with this application using micrometer-registry-prometheus dependency.我能够使用micrometer-registry-prometheus依赖micrometer-registry-prometheus将 Prometheus 与此应用程序集成。 I had set up Prometheus to run in a Docker container in my local system.我已将 Prometheus 设置为在本地系统的 Docker 容器中运行。 I used the following set of commands-我使用了以下一组命令-

docker pull prom/prometheus
docker run -p 9090:9090 -v D:/Workspaces/STS/server_sent_events_blog/micrometer-new-relic/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

prometheus.yml普罗米修斯.yml

global:
  scrape_interval: 4s
  evaluation_interval: 4s

scrape_configs:
  - job_name: 'spring_micrometer'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['my_ip_address:8080']

When I navigated to localhost:9090/targets I can see that Prometheus dashboard shows my application details and that it can scrape data from it.当我导航到localhost:9090/targets我可以看到 Prometheus 仪表板显示了我的应用程序详细信息,并且可以从中抓取数据。 And in the dashboard, I can see my custom metrics as well along with other metrics.在仪表板中,我可以看到我的自定义指标以及其他指标。

So my question is I want to achieve the same thing using New Relic.所以我的问题是我想用 New Relic 实现同样的目标。 I have added the micrometer-registry-new-relic pom dependency.我添加了micrometer-registry-new-relic pom 依赖项。 I have shared the application.properties file as well.我也共享了application.properties文件。 I can see logs in my console saying it is sending data to New Relic-我可以在控制台中看到日志说它正在向 New Relic- 发送数据

2021-10-24 12:42:04.889 DEBUG 2672 --- [trics-publisher] imnNewRelicInsightsApiClientProvider : successfully sent 58 metrics to New Relic.

Questions:问题:

  1. What are the next steps?什么是下一个步骤?
  2. Do I need a local running server of New Relic as I did for Prometheus?我是否需要像 Prometheus 一样在本地运行 New Relic 的服务器?
  3. Where can I visualize this data?我在哪里可以可视化这些数据? I have an account in New Relic, I see nothing there我在 New Relic 有一个帐户,我在那里什么都看不到

在此处输入图像描述

https://discuss.newrelic.com/t/integrate-spring-boot-actuator-with-new-relic/126732 As per the above link, Spring Bootctuator pushes metric as an event type “SpringBootSample”. https://discuss.newrelic.com/t/integrate-spring-boot-actuator-with-new-relic/126732根据上面的链接,Spring Bootctuator 将指标作为事件类型“SpringBootSample”推送。 With NRQL query we can confirm this-通过 NRQL 查询,我们可以确认这一点——

FROM SpringBootSample SELECT max(value) TIMESERIES 1 minute WHERE metricName = 'jvmMemoryCommitted'

在此处输入图像描述

  1. What does the result of this query indicate?这个查询的结果说明了什么? Is it a metric related to my application?它是与我的应用程序相关的指标吗?

Here is the GitHub link to the demo that I have shared here.这是我在此处共享的演示的GitHub 链接 I did not find any clear instructions on this, there are some examples out there but that uses Java agent.我没有找到任何明确的说明,那里有一些示例,但使用了 Java 代理。

Any kind of help will be highly appreciated.任何形式的帮助将不胜感激。

  1. What are the next steps?什么是下一步?
    It seems you are done and successfully shipped metrics to NewRelic.您似乎已完成并成功将指标发送到 NewRelic。

  2. Do I need a local running server of New Relic as I did for Prometheus?我是否需要像 Prometheus 那样在本地运行 New Relic 服务器?
    No, NewRelic is a SaaS offering.不,NewRelic 是一种 SaaS 产品。

  3. Where can I visualize this data?我在哪里可以可视化这些数据? I have an account in New Relic, I see nothing there我在 New Relic 有一个帐户,我在那里什么也看不到
    It seems you already found it (screenshot).看来你已经找到了(截图)。

  4. What does the result of this query indicate?这个查询的结果表明什么? Is it a metric related to my application?它是与我的应用程序相关的指标吗?
    From the screenshot, I can't tell if it is your application but this seems to be the jvm.memory.committed metric pushed by a Spring Boot app (so likely).从屏幕截图中,我无法判断它是否是您的应用程序,但这似乎是 Spring Boot 应用程序推送的jvm.memory.committed指标(很有可能)。

In order to see if this is your app or not, you can add common tags which can tell the name of the app and some kind of an instance ID (or hostname?) in case you have multiple instances from the same app, see:为了查看这是否是您的应用程序,您可以添加通用标签,这些标签可以告诉应用程序的名称和某种实例 ID(或主机名?),以防您有来自同一应用程序的多个实例,请参阅:

From what I have learned so far.从我到目前为止所学到的。 There are 3 ways to integrate New Relic with a Spring Boot Application-将 New Relic 与 Spring Boot 应用程序集成有 3 种方法-

  1. Using the Java Agent provided by New Relic使用 New Relic 提供的 Java Agent
  2. Using New Relic's Micrometer Dependency使用 New Relic 的千分尺依赖性
  3. Micormeter's New Relic Dependency Micormeter 的新 Relic 依赖项

1. Configuration using Java Agent Provided By New Relic 1.使用New Relic提供的Java Agent进行配置

  1. Download the Java Agent from this URL- https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes/从此 URL 下载 Java 代理 - https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes/
  2. Extract it.提取它。
  3. Modify the newrelic.yml file inside the extracted folder to inlcude your license_key: app_name:修改解压缩文件夹中的 newrelic.yml 文件以包含您的 license_key: app_name:
  4. Create a SpringBoot application with some REST endpoints.创建一个带有一些 REST 端点的 SpringBoot 应用程序。
  5. Build the application.构建应用程序。
  6. Navigate to the root path where you have extracted the newrelic java agent.导航到您提取 newrelic java 代理的根路径。
  7. Enter this command java -javagent:<path to your new relic jar>\\newrelic.jar -jar <path to your application jar>\\<you rapplication jar name>.jar输入这个命令java -javagent:<path to your new relic jar>\\newrelic.jar -jar <path to your application jar>\\<you rapplication jar name>.jar

To view the application metrics-要查看应用程序指标 -

  1. Log in to your New Relic account.登录到您的 New Relic 帐户。
  2. Go to Explorer Tab.转到资源管理器选项卡。
  3. Click on Services-APM点击服务-APM
  4. You can see the name of your application(which you had mentioned in the newrelic.yml file) listed there.您可以在此处看到您的应用程序的名称(您在 newrelic.yml 文件中提到过)。
  5. Click on the application name.单击应用程序名称。
  6. The dashboard should look something like this.仪表板应该看起来像这样。

图片

Using New Relic's Micrometer Dependency is the preferred way to do it.使用 New Relic 的 Micrometer Dependency 是首选方法。

2. Configuration using New Relic's Micrometer Dependency 2.使用New Relic的千分尺依赖配置

  1. Add this dependency添加此依赖项
<dependency>
        <groupId>com.newrelic.telemetry</groupId>
        <artifactId>micrometer-registry-new-relic</artifactId>
        <version>0.7.0</version>
    </dependency>
  1. Modify the MicrometerConfig.java class to add your API Key and Application name.修改MicrometerConfig.java类以添加您的 API 密钥和应用程序名称。
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.newrelic.telemetry.Attributes;
import com.newrelic.telemetry.micrometer.NewRelicRegistry;
import com.newrelic.telemetry.micrometer.NewRelicRegistryConfig;

import java.time.Duration;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.util.NamedThreadFactory;

@Configuration
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnClass(NewRelicRegistry.class)
public class MicrometerConfig {

    @Bean
    public NewRelicRegistryConfig newRelicConfig() {
        return new NewRelicRegistryConfig() {
            @Override
            public String get(String key) {
                return null;
            }

            @Override
            public String apiKey() {
                return "your_api_key"; // for production purposes take it from config file
            }

            @Override
            public Duration step() {
                return Duration.ofSeconds(5);
            }

            @Override
            public String serviceName() {
                return "your_service_name"; // take it from config file
            }

        };
    }

    @Bean
    public NewRelicRegistry newRelicMeterRegistry(NewRelicRegistryConfig config) throws UnknownHostException {
        NewRelicRegistry newRelicRegistry = NewRelicRegistry.builder(config)
                .commonAttributes(new Attributes().put("host", InetAddress.getLocalHost().getHostName())).build();
        newRelicRegistry.config().meterFilter(MeterFilter.ignoreTags("plz_ignore_me"));
        newRelicRegistry.config().meterFilter(MeterFilter.denyNameStartsWith("jvm.threads"));
        newRelicRegistry.start(new NamedThreadFactory("newrelic.micrometer.registry"));
        return newRelicRegistry;
    }
}
  1. Run the application.运行应用程序。

To view the Application metrics-要查看应用程序指标 -

  1. Log in to your New Relic account.登录到您的 New Relic 帐户。
  2. Go to Explorer Tab.转到资源管理器选项卡。
  3. Click on Services-OpenTelemetry点击服务-OpenTelemetry
  4. You can see the name of your application(which you had mentioned in the MicrometerConfig file) listed there.您可以在此处看到您的应用程序的名称(您在 MicrometerConfig 文件中提到过)。
  5. Click on the application name.单击应用程序名称。
  6. The dashboard should look something like this.仪表板应该看起来像这样。

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM