avatar

Bhuwan Upadhyay

Talks all about software engineering

Published on

Performance Benchmarking with Tomcat

Authors

Introduction

This article demonstrates, how we can use Elastic Apm with tomcat web application for monitoring. We will create project from scratch and run with elastic apm.

Just as athletes can’t win without a sophisticated mixture of strategy, form, attitude, tactics, and speed, performance engineering requires a good collection of metrics and tools to deliver the desired business results. - Todd DeCapua

On this article, I am going to explain how to use Elastic APM with tomcat.

The Dockerfile for application to run elastic-apm-agent.jar with tomcat.

FROM tomcat:8.5.34-jre8-alpine
ARG WAR_FILE
ARG ELASTIC_APM_AGEN_VERSION=0.7.0
COPY ${WAR_FILE} /usr/local/tomcat/app.war
RUN cd /usr/local/tomcat && \
    rm -rf webapps/* && \
    mkdir -p webapps/ROOT && \
    unzip app.war -d webapps/ROOT > /dev/null && \
    rm /usr/local/tomcat/app.war
RUN wget https://search.maven.org/remotecontent?filepath=co/elastic/apm/elastic-apm-agent/${ELASTIC_APM_AGEN_VERSION}/elastic-apm-agent-${ELASTIC_APM_AGEN_VERSION}.jar \
    -O ${HOME}/elastic-apm-agent.jar
ADD bin /usr/local/tomcat/bin
WORKDIR /usr/local/tomcat/bin
ENV JPDA_ADDRESS 8000
CMD ["catalina.sh", "jpda", "run"]
mkdir bin && cd bin
touch setenv.sh
chmod +x setenv.sh

setenv.sh

#!/usr/bin/env bash
export CATALINA_OPTS="
       $CATALINA_OPTS -javaagent:${HOME}/elastic-apm-agent.jar
       -Delastic.apm.service_name=${ELASTIC_APM_SERVICE_NAME}
       -Delastic.apm.application_packages=${ELASTIC_APM_APPLICATION_PACKAGES}
       -Delastic.apm.server_urls=${ELASTIC_APM_SERVER_URLS}
"
echo ""
echo "--------------------------------------------------------------"
echo "CATALINA_OPTS: ${CATALINA_OPTS}"
echo "--------------------------------------------------------------"
echo ""

docker-compose.yml

version: '3'
services:
  apm-elasticsearch:
    ports:
      - 9200
  kibana:
    environment:
      ELASTICSEARCH_URL: http://apm-elasticsearch:9200
    ports:
      - 5601:5601
    links:
      - apm-elasticsearch
  elastic-apm:
    command: [
        'bash',
        '-c',
        "apm-server run -e -E output.elasticsearch.hosts=['http://apm-elasticsearch:9200']",
      ]
    links:
      - apm-elasticsearch
    ports:
      - 8200
  performance-benchmarking-with-tomcat:
    build:
      context: cicd/docker
      dockerfile: Dockerfile
      args:
        - WAR_FILE=Spring Boot-order-crud-service.war
    ports:
      - 8000:8000
      - 8080:8080
    environment:
      ELASTIC_APM_SERVICE_NAME: performance-benchmarking-with-tomcat
      ELASTIC_APM_APPLICATION_PACKAGES: io.github.bhuwanupadhyay
      ELASTIC_APM_SERVER_URLS: http://elastic-apm:8200
    depends_on:
      - apm-elasticsearch
      - elastic-apm
      - kibana
    links:
      - elastic-apm

Take a look at this repository Github to see how it works.