[英]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:问题:
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'
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.任何形式的帮助将不胜感激。
What are the next steps?什么是下一步?
It seems you are done and successfully shipped metrics to NewRelic.您似乎已完成并成功将指标发送到 NewRelic。
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 产品。
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).看来你已经找到了(截图)。
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 种方法-
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
Using New Relic's Micrometer Dependency is the preferred way to do it.使用 New Relic 的 Micrometer Dependency 是首选方法。
<dependency>
<groupId>com.newrelic.telemetry</groupId>
<artifactId>micrometer-registry-new-relic</artifactId>
<version>0.7.0</version>
</dependency>
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.