2
votes

I am currently doing a PoC to integrate Elastic APM into my spring application. I was following this page :- https://www.elastic.co/guide/en/apm/agent/java/1.x/setup-attach-api.html to programatically attach elastic-apm jar.

I have added the required jar into pom.xml but i am not getting how should i attach Elastic Apm (ElasticApmAttacher.attach())into my normal spring code. Example given is for SpringBoot. But my application is on Spring core ( spring-core, spring-web ..) with rest services exposed using Jax-Rs.

2

2 Answers

0
votes

You can attach your ElasticApmAttacher.attach() in the Spring Application main class

For a SpringBootApplication packaged as a war file, and deployed to Tomcat server, this can be added to the configure method

Below code might help:

package com.test.main

import co.elastic.apm.attach.ElasticApmAttacher
import org.springframework.boot.Banner
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer


@SpringBootApplication(scanBasePackages = [ "com.test" ])
class Application extends SpringBootServletInitializer{

    static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class)
        app.setBannerMode(Banner.Mode.OFF)
        ElasticApmAttacher.attach();
        app.run(args)
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        ElasticApmAttacher.attach();
        return application.sources(Application.class)
    }
}
0
votes
    Map<String, String> apmConfiguration = new HashMap<>();
    apmConfiguration.put("server_urls", "http://localhost:8200");
    apmConfiguration.put("service_name", "SpringBootApp");
    ElasticApmAttacher.attach(apmConfiguration);