Spring Boot Application Performance Monitoring Using New Relic Tool
Application Monitoring in one place allows you to see your application performance trends at a glance from page load times to error rates, slow transactions, and a list of servers running the app.
Prerequisite
- Spring Boot Application
- AWS EC2-Instance
Setup AWS EC2-Instance:
Step 1: Launch an instance
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2
From the console dashboard, choose Launch Instance
2. The Choose an Amazon Machine Image (AMI) page displays a list of basic configurations, called Amazon Machine Images (AMIs), that serve as templates for your instance.
3. On the Choose an Instance Type page, you can select the hardware configuration of your instance. Select the t2.micro
instance type, which is selected by default.
4. On the Choose an Instance Type page, choose Review and Launch to let the wizard complete the other configuration settings for you.
5. On the Review Instance Launch page, under Security Groups, you’ll see that the wizard created and selected a security group for you.
- Choose Edit security groups.
- On the Configure Security Group page, ensure that Select an existing security group is selected.
- Select your security group from the list of existing security groups, and then choose Review and Launch.
6. On the Review Instance Launch page, choose Launch.
7. When prompted for a key pair, you may use an existing key pair or create a new key pair. When you are ready, select the acknowledgment check box, and then choose Launch Instances.
8. A confirmation page lets you know that your instance is launching. Choose View Instances to close the confirmation page and return to the console.
9. It can take a few minutes for the instance to be ready so that you can connect to it. Check that your instance has passed its status checks; you can view this information in the Status check column
Step 2: Connect to Your Instance
- Copy the server IP from the public IPv4 address
2. Connect the instance via MobaXterm/putty. For the demonstration, I will be using Mobaxtrem. Select the session and select ssh from the popup and enter details as in the popup i.e. remote host, specify username & use the private key then apply OK.
3. Build the Spring Boot application by running mvn clean install then jar will be generated under Spring Boot application project ->target folder
4. Move the jar to the above-created server using WINSCP
5. Once connected to the server we need to run the Spring Boot application. Here I am going to via jar file. Now we install java alone by the following command
1. sudo -i
2. yum install java-1.8.0-openjdk java-1.8.0-openjdk-devel
In case you need to run the Spring Boot maven application from git repo install following in the server
1. sudo -i
2. yum install java-1.8.0-openjdk java-1.8.0-openjdk-devel maven git
6. Move to the folder where the jar has been placed
Run java -jar prometheusspringboot-0.0.1-SNAPSHOT.jar
7. Setup New Relic
Step 1: Go to New Relic Page(https://newrelic.com/) and navigate to the APM tab
Step: 2 Select Add more data from the APM page
Step 3: Then we need to select the type of application that we need to be monitored. In our case, it is Java so I am selecting Java.
Step 4: Give your application a name e.g prometheusspringboot
Step 5: Download your custom configuration file
Step 6: Download and install the agent
Step 7: Go to MobaXterm and install the agent outside the application folder and the agent downloaded successfully.
Step 8: Now we need to unzip the downloaded agent by running the below command
unzip newrelic-java.zip
Step 9: In the unzipped new relic project. We will find the newreclic.jar and newrelic.yml file. Go to Step 5: Download your custom configuration file from the downloaded folder and open the newrelic.yml file
# This file configures the New Relic Agent. New Relic monitors
# Java applications with deep visibility and low overhead. For more details and additional
# configuration options visit https://docs.newrelic.com/docs/agents/java-agent/configuration/java-agent-configuration-config-file.
#
# <%= generated_for_user %>
#
# This section is for settings common to all environments.
# Do not add anything above this next line.
common: &default_settings# ============================== LICENSE KEY ===============================
# You must specify the license key associated with your New Relic
# account. For example, if your license key is 12345 use this:
# license_key: '12345'
# The key binds your Agent's data to your account in the New Relic service.
license_key: b6c0525c1f079ef6a9d03fcdf1b22189224bNRAL# Agent Enabled
# Use this setting to disable the agent instead of removing it from the startup command.
# Default is true.
agent_enabled: true# Set the name of your application as you'd like it show up in New Relic.
# If enable_auto_app_naming is false, the agent reports all data to this application.
# Otherwise, the agent reports only background tasks (transactions for non-web applications)
# to this application. To report data to more than one application
# (useful for rollup reporting), separate the application names with ";".
# For example, to report data to "My Application" and "My Application 2" use this:
# app_name: My Application;My Application 2
# This setting is required. Up to 3 different application names can be specified.
# The first application name must be unique.
app_name: prometheusspringboot# To enable high security, set this property to true. When in high
# security mode, the agent will use SSL and obfuscated SQL. Additionally,
# request parameters and message parameters will not be sent to New Relic.
high_security: false# Set to true to enable support for auto app naming.
# The name of each web app is detected automatically
# and the agent reports data separately for each one.
# This provides a finer-grained performance breakdown for
# web apps in New Relic.
# Default is false.
enable_auto_app_naming: false# Set to true to enable component-based transaction naming.
# Set to false to use the URI of a web request as the name of the transaction.
# Default is true.
enable_auto_transaction_naming: true# The agent uses its own log file to keep its logging
# separate from that of your application. Specify the log level here.
# This setting is dynamic, so changes do not require restarting your application.
# The levels in increasing order of verboseness are:
# off, severe, warning, info, fine, finer, finest
# Default is info.
log_level: info# Log all data sent to and from New Relic in plain text.
# This setting is dynamic, so changes do not require restarting your application.
# Default is false.
audit_mode: false# The number of backup log files to save.
# Default is 1.
log_file_count: 1# The maximum number of kbytes to write to any one log file.
# The log_file_count must be set greater than 1.
# Default is 0 (no limit).
log_limit_in_kbytes: 0# Override other log rolling configuration and roll the logs daily.
# Default is false.
log_daily: false# The name of the log file.
# Default is newrelic_agent.log.
log_file_name: newrelic_agent.log# The log file directory.
# Default is the logs directory in the newrelic.jar parent directory.
#log_file_path:# Proxy settings for connecting to the New Relic server:
# If a proxy is used, the host setting is required. Other settings
# are optional. Default port is 8080. The username and password
# settings will be used to authenticate to Basic Auth challenges
# from a proxy server. Proxy scheme will allow the agent to
# connect through proxies using the HTTPS scheme.
#proxy_host: hostname
#proxy_port: 8080
#proxy_user: username
#proxy_password: password
#proxy_scheme: https# Limits the number of lines to capture for each stack trace.
# Default is 30
max_stack_trace_lines: 30# Provides the ability to configure the attributes sent to New Relic. These
# attributes can be found in transaction traces, traced errors, Insight's
# transaction events, and Insight's page views.
attributes:# When true, attributes will be sent to New Relic. The default is true.
enabled: true#A comma separated list of attribute keys whose values should
# be sent to New Relic.
#include:# A comma separated list of attribute keys whose values should
# not be sent to New Relic.
#exclude:# Transaction tracer captures deep information about slow
# transactions and sends this to the New Relic service once a
# minute. Included in the transaction is the exact call sequence of
# the transactions including any SQL statements issued.
transaction_tracer:# Transaction tracer is enabled by default. Set this to false to turn it off.
# This feature is not available to Lite accounts and is automatically disabled.
# Default is true.
enabled: true# Threshold in seconds for when to collect a transaction
# trace. When the response time of a controller action exceeds
# this threshold, a transaction trace will be recorded and sent to
# New Relic. Valid values are any float value, or (default) "apdex_f",
# which will use the threshold for the "Frustrated" Apdex level
# (greater than four times the apdex_t value).
# Default is apdex_f.
transaction_threshold: apdex_f# When transaction tracer is on, SQL statements can optionally be
# recorded. The recorder has three modes, "off" which sends no
# SQL, "raw" which sends the SQL statement in its original form,
# and "obfuscated", which strips out numeric and string literals.
# Default is obfuscated.
record_sql: obfuscated# Set this to true to log SQL statements instead of recording them.
# SQL is logged using the record_sql mode.
# Default is false.
log_sql: false# Threshold in seconds for when to collect stack trace for a SQL
# call. In other words, when SQL statements exceed this threshold,
# then capture and send to New Relic the current stack trace. This is
# helpful for pinpointing where long SQL calls originate from.
# Default is 0.5 seconds.
stack_trace_threshold: 0.5# Determines whether the agent will capture query plans for slow
# SQL queries. Only supported for MySQL and PostgreSQL.
# Default is true.
explain_enabled: true# Threshold for query execution time below which query plans will not
# not be captured. Relevant only when `explain_enabled` is true.
# Default is 0.5 seconds.
explain_threshold: 0.5# Use this setting to control the variety of transaction traces.
# The higher the setting, the greater the variety.
# Set this to 0 to always report the slowest transaction trace.
# Default is 20.
top_n: 20# Error collector captures information about uncaught exceptions and
# sends them to New Relic for viewing.
error_collector:# This property enables the collection of errors. If the property is not
# set or the property is set to false, then errors will not be collected.
# Default is true.
enabled: true# Use this property to exclude specific exceptions from being reported as errors
# by providing a comma separated list of full class names.
# The default is to exclude akka.actor.ActorKilledException. If you want to override
# this, you must provide any new value as an empty list is ignored.
ignore_errors: akka.actor.ActorKilledException# Use this property to exclude specific http status codes from being reported as errors
# by providing a comma separated list of status codes.
# The default is to exclude 404s. If you want to override
# this, you must provide any new value as an empty list is ignored.
ignore_status_codes: 404# Transaction Events are used for Histograms and Percentiles. Unaggregated data is collected
# for each web transaction and sent to the server on harvest.
transaction_events:# Set to false to disable transaction events.
# Default is true.
enabled: true# Events are collected up to the configured amount. Afterwards, events are sampled to
# maintain an even distribution across the harvest cycle.
# Default is 2000. Setting to 0 will disable.
max_samples_stored: 2000# Distributed tracing lets you see the path that a request takes through your distributed system.
# Enabling distributed tracing changes the behavior of some New Relic features, so carefully consult the transition
# guide before you enable this feature: https://docs.newrelic.com/docs/apm/distributed-tracing/getting-started/transition-guide-distributed-tracing
# Default is false.
distributed_tracing:
enabled: true# Cross Application Tracing adds request and response headers to
# external calls using supported HTTP libraries to provide better
# performance data when calling applications monitored by other New Relic Agents.
cross_application_tracer:# Set to false to disable cross application tracing.
# Default is true.
enabled: false# Thread profiler measures wall clock time, CPU time, and method call counts
# in your application's threads as they run.
# This feature is not available to Lite accounts and is automatically disabled.
thread_profiler:# Set to false to disable the thread profiler.
# Default is true.
enabled: true# New Relic Real User Monitoring gives you insight into the performance real users are
# experiencing with your website. This is accomplished by measuring the time it takes for
# your users' browsers to download and render your web pages by injecting a small amount
# of JavaScript code into the header and footer of each page.
browser_monitoring:# By default the agent automatically inserts API calls in compiled JSPs to
# inject the monitoring JavaScript into web pages. Not all rendering engines are supported.
# See https://docs.newrelic.com/docs/agents/java-agent/instrumentation/new-relic-browser-java-agent#manual_instrumentation
# for instructions to add these manually to your pages.
# Set this attribute to false to turn off this behavior.
auto_instrument: trueclass_transformer:
# This instrumentation reports the name of the user principal returned from
# HttpServletRequest.getUserPrincipal() when servlets and filters are invoked.
com.newrelic.instrumentation.servlet-user:
enabled: falsecom.newrelic.instrumentation.spring-aop-2:
enabled: false# This instrumentation reports metrics for resultset operations.
com.newrelic.instrumentation.jdbc-resultset:
enabled: false# Classes loaded by classloaders in this list will not be instrumented.
# This is a useful optimization for runtimes which use classloaders to
# load dynamic classes which the agent would not instrument.
classloader_excludes:
groovy.lang.GroovyClassLoader$InnerLoader,
org.codehaus.groovy.runtime.callsite.CallSiteClassLoader,
com.collaxa.cube.engine.deployment.BPELClassLoader,
org.springframework.data.convert.ClassGeneratingEntityInstantiator$ObjectInstantiatorClassGenerator,
org.mvel2.optimizers.impl.asm.ASMAccessorOptimizer$ContextClassLoader,
gw.internal.gosu.compiler.SingleServingGosuClassLoader,# User-configurable custom labels for this agent. Labels are name-value pairs.
# There is a maximum of 64 labels per agent. Names and values are limited to 255 characters.
# Names and values may not contain colons (:) or semicolons (;).
labels:# An example label
#label_name: label_value# Application Environments
# ------------------------------------------
# Environment specific settings are in this section.
# You can use the environment to override the default settings.
# For example, to change the app_name setting.
# Use -Dnewrelic.environment=<environment> on the Java startup command line
# to set the environment.
# The default environment is production.# NOTE if your application has other named environments, you should
# provide configuration settings for these environments here.development:
<<: *default_settings
app_name: prometheusspringboot (Development)test:
<<: *default_settings
app_name: prometheusspringboot (Test)production:
<<: *default_settingsstaging:
<<: *default_settings
app_name: prometheusspringboot (Staging)
Note: In this application. To report data to more than one application, separate the application names with “;”.
For example, to report data to “My Application1” and “My Application 2” use this: app_name: My Application1;My Application 2
Here I am using it for a single application.
The above highlighted to be changed according to your configuration. Instead of changing it, I am replacing the newrelic.yml file in the newrelic project folder with the custom configuration file(downloaded file) by running the below command
> newrelic.yml
vi newrelic.yml
Step 10: Then go to the New Relic Page section Get specific instructions for your Java setup. In that, we need to mention the server in our case we are using Spring Boot
Copy the command and run it.
java -javaagent:FULL_PATH_TO/newrelic.jar -jar application.jar
java -javaagent:/root/newrelic/newrelic.jar -jar /root/springboot/prometheusspringboot-0.0.1-SNAPSHOT.jar
The above command will start both the new relic agent as well as the application
Copy the instance IP http://<instance IP>:33333/hello
Step 11: Whatever the API calls we are making we can see it Newrelic server. Go New Relic APM services we can see our service added
Step 12: Select the new service added and select the Distributed Tracing from the left-hand side
Success and error entries of the controller we can view in Distributed Tracing
Step 13: If we want to see the memory we can view it in JVM
Step 14: Select Transactions from the left-hand side. Here we can view all the transactions that have happened
By selecting Show all transactions table we can view all type of transactions
Conclusion:
We have successfully implemented Spring Boot application monitoring with New Relic monitoring. It is important to monitor an application’s performance, manage the application in a better way and notice unoptimized behavior.