Expand my Community achievements bar.

AEM + Prometheus client_java = servlet endpoint

Avatar

Level 2

Does anyone tried to use Prometheus client_java (GitHub - prometheus/client_java) in AEM sling servlet?


I am trying to create a servlet where prometheus data will be displayed from a custom URL.

This is my simple servlet, but I am experiencing some issues with it.

 

 

import io.prometheus.client.Counter;


import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;


@Component(service = Servlet.class, immediate = true, property = {
    "sling.servlet.methods=GET",
    "sling.servlet.paths=/services/metrics"
})

public class PrometheusServlet extends SlingSafeMethodsServlet {

    static final Counter counter =
        Counter.build().name("Total_number_of_sample_counter").help("Total number of counter").register();

    void processCounter() {
        counter.inc();
    }


    @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws IOException, ServletException {

            processCounter();
            response.getWriter().println(counter);

    }
}

 

 

 

Error Message:

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: No use provider could resolve identifier

Any help will be appreciated.


Thanks!

3 Replies

Avatar

Community Advisor

@jnikolainemo 

 

    Is this happening because of the missing @Override ?

 

Thanks

Veena ✌

Avatar

Level 2

I pasted the wrong code, but the one I am running on my machine has @Override

Avatar

Community Advisor

Is your servlet in active state ? The error makes me think that your servlet is not yet active. Also how are you accessing the servlet ?