Building Search API with ElasticSearch — Part 11: Elastic APM

André Coelho
3 min readJul 16, 2022

--

Nowadays collecting metrics and tracking application performance is a fundamental item that every developer should think about when building an application.

Carrying out this monitoring will help to identify problems of slowness, errors and other things that can impact performance and from this identification we can act with greater speed in incidents preventing the user from being impacted by a failure.

There are several APM tools on the market, Application performance monitoring, and in our project we are going to use Elastic APM.

Elastic APM

Elastic APM is one of Elastic’s products that will help us collect metrics, logs and traces. Our project is running in a local environment, so we need to create an APM server for that, add the service in docker-compose.

apm-server:
image: docker.elastic.co/apm/apm-server:7.17.1
cap_add: ["CHOWN", "DAC_OVERRIDE", "SETGID", "SETUID"]
cap_drop: ["ALL"]
ports:
- 8200:8200
command: >
apm-server -e
-E apm-server.rum.enabled=true
-E setup.kibana.host=kibana:5601
-E setup.template.settings.index.number_of_replicas=0
-E apm-server.kibana.enabled=true
-E apm-server.kibana.host=kibana:5601
-E output.elasticsearch.hosts=["elasticsearch:9200"]
healthcheck:
interval: 10s
retries: 12
test: curl --write-out 'HTTP %{http_code}' --fail --silent --output /dev/null http://localhost:8200/

After creating the APM server, we need to define the APM Agent, which is responsible for collecting the application data and sending it to the APM server.
Our application is developed in Java so our APM Agent must be compatible with the language.
To learn more about the other APM Agents, click in Elastic Docs APM Agents.

Integration APM

Access Kibana and click on the Observability tab > User Experience > Add APM. On the screen you will follow the instructions to download the agent, remember that we are going to choose Java technology.

Integration APM

Download the Jar and leave it at the root of the project, in our project the jar is the elastic-apm-agent-1.32.0.jar.

Just below the next instruction is how to start the application with the agent.
The command for the project looks like this:

java -javaagent:./elastic-apm-agent-1.32.0.jar \
-Delastic.apm.service_name=search-store \
-Delastic.apm.server_urls=http://localhost:8200 \
-Delastic.apm.environment=dev \
-Delastic.apm.application_packages=com.company.searchstore \
-jar ./target/search-store-1.0.0.jar

When the application starts, go back to kibana > Observability
> APM > Services.
If everything goes correctly, our application is already collecting data and sent to the server.

Application observability

When accessing the service, we can explore the dashboard.

Application Search-Store Dashboard APM

From now on we can visualize several metrics of our application such as Latency, Transaction and Errors. With this data we can create alerts and detect behaviors that can impact performance in advance.

Start by reading the Get started with the APM app and understand each metrics offered and thus make better use of all Elastic APM.

The application source code is available in git. The source code: search-store.

Building Search API with ElasticSearch — Part 01: Preparing the environment
Building Search API with ElasticSearch — Part 02: Pagination
Building Search API with ElasticSearch — Part 03: Autocomplete
Building Search API with ElasticSearch — Part 04: Search Query
Building Search API with ElasticSearch — Part 05: Simple Facets
Building Search API with ElasticSearch — Part 06: Filters
Building Search API with ElasticSearch — Part 07: “Did you mean”
Building Search API with ElasticSearch — Part 08: Sort results
Building Search API with ElasticSearch — Part 09: Recommendation with More Like This Query
Building Search API with ElasticSearch — Part 10: Front-End
Building Search API with ElasticSearch — Part 11: Elastic APM

--

--

André Coelho

Developer of web and mobile systems. Enthusiast in the area of ​​automation and electronics and I have hobbie music.