Add ELK APM agent in Java project

Even APM agent can add parameter -javaAgent, but it will more flexible if embed in jar file. This demo will show how to add ELK APM agent inside Java gradle project.

Prerequisites

  1. Ensure network connectivity between source and destination.

Steps

  1. Add ELK stack APM agent.
    In build.gradle, add dependencies as below.

    dependencies {
      implementation 'co.elastic.apm:apm-agent-attach:1.+'
    }
  2. Externalize APM agent configuration.
    Create file name elasticapm.properties and add content below.

    # ELK stack APM agent for Java properties.
    # Config reference: https://www.elastic.co/guide/en/apm/agent/java/current/configuration.html
    
    # Enable ELK Agent.
    enabled=true
    
    # Allow ELK Agent collect data from JVM.
    #recording=true
    
    # Service setting.
    service_name=testing-project
    application_packages=local.project.testing
    
    # APM server settings.
    server_url=https://elk-server:8200
    
    # Metric collection scale and interval.
    capture_body=all
    transaction_sample_rate=0.2
    metrics_interval=5m
    transaction_ignore_urls=/status,/actuator/*
    enable_log_correlation=true
  3. Alter execution command.
    In dockerfile / service file, alter command as below.

    /usr/bin/java -Delastic.apm.config_file=elasticapm.properties -jar test-service.jar

Reminding

After run dependency check, found in latest version (1.22.0), there are several vulnerabilities found. So split to use -javaagent still a valid option to prevent package polluted.

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.