How to set admin port for metrics when using Metrics but not Dropwizard?

864 views
Skip to first unread message

Sreya

unread,
Aug 2, 2013, 6:19:55 PM8/2/13
to dropwiz...@googlegroups.com
Just wondering how I set the admin port to view the metrics when I'm not using Dropwizard but trying to incorporate the metrics. I've got a RESTful API built using Jersey and manually embedded Jetty, I also have the metrics recording statistics on processes within the API but I've yet to figure out how to configure the admin port which happens automatically in Dropwizard. I was hoping someone here could help. Here's my code for the embedded Jetty (I'm assuming it's something you have to add to it)

package pojo;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import com.sun.jersey.spi.container.servlet.ServletContainer;

public class Main {

    public static void main(String[] args) throws Exception {
    Server server = new Server(8112);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        ServletHolder h = new ServletHolder(new ServletContainer());
        h.setInitParameter("com.sun.jersey.config.property.packages", "resources");
        h.setInitOrder(1);
        context.addServlet(h, "/rest/*");
        try
        {
            server.start();
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }
}



Coda Hale

unread,
Aug 3, 2013, 1:30:21 AM8/3/13
to dropwiz...@googlegroups.com
You'll need to use metrics-servlet.

The source code is pretty straight forward, and decent documentation is available at http://metrics.codahale.com.

If you're curious about how Dropwizard configures Jetty, I'd suggest reading the source code.


--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Coda Hale
http://codahale.com
Message has been deleted

Sreya

unread,
Aug 3, 2013, 6:57:58 PM8/3/13
to dropwiz...@googlegroups.com
So I looked through the source code and followed it pretty thoroughly. I set up a class that extended AdminServletContextListener but it seems like it's deprecated. I set it up as follows:

package contextListener;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.servlets.AdminServletContextListener;

@SuppressWarnings("deprecation")
public class MyAdminServletContextListener extends AdminServletContextListener {
    public static final MetricRegistry registry = new MetricRegistry();
    public static final HealthCheckRegistry Hregistry = new HealthCheckRegistry();

    @Override
    protected MetricRegistry getMetricRegistry() {
        return registry;
    }

    @Override
    protected HealthCheckRegistry getHealthCheckRegistry() {
        return Hregistry;
    }
}

Then I tried to follow the rest of the documentation by setting the attributes in the main class:

package pojo;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.servlets.AdminServlet;

import contextListener.MyAdminServletContextListener;

public class Main {

    public static void main(String[] args) throws Exception {
    Server server = new Server(8112);
    AdminServlet admin = new AdminServlet();
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        ServletHolder h = new ServletHolder(new ServletContainer());
        ServletHolder metrics = new ServletHolder(admin);
        h.setInitParameter("com.sun.jersey.config.property.packages", "resources");
        h.setInitOrder(1);
        context.addServlet(h, "/rest/*");
        context.addServlet(metrics, "/metrics");
        context.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry",contextListener.MyAdminServletContextListener.registry);
        context.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.registry", contextListener.MyAdminServletContextListener.Hregistry);

        try
        {
            server.start();
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }
}

Jetty will start up. But when I got to the metrics home page. None of the links load to anything (I'm getting 404 errors). Any suggestions? I'm not very experienced in Jetty so I'll take any pointers I can get. The documentation and source code helped. But after looking at them again, I'm not sure how they'd help solve this problem, they made it seem like as long as I asserted the two registries in the class that extended the now-deprecated AdminServletContextListener it'd take care of the rest

Coda Hale

unread,
Aug 3, 2013, 7:10:18 PM8/3/13
to dropwiz...@googlegroups.com
I'm not sure where to start.

You don't seem familiar with the Servlet API. That will make things hard for you, since what you're doing is trying to hard-code a servlet context.

You may want to read about servlet context listeners, read the Javadocs for AdminServletContextListener, etc.

I wish I could help you more, but I'm afraid I don't have the time.
Message has been deleted

Vijay Polsani

unread,
Jun 9, 2014, 11:52:24 PM6/9/14
to dropwiz...@googlegroups.com
Hi Sreya,

Looks like you need to have something like below: I know it is late but might help someone else too.  I felt it was a bit rude to say someone to read again rather than helping ...


package org.streams.compliance.server;

import java.lang.management.ManagementFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ConnectorStatistics;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;

import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.servlets.AdminServlet;
import com.codahale.metrics.servlets.HealthCheckServlet;
import com.codahale.metrics.servlets.MetricsServlet;
import com.codahale.metrics.servlets.PingServlet;
import com.codahale.metrics.servlets.ThreadDumpServlet;

public class MinimalServer {

private static final Logger log = LoggerFactory.getLogger(MinimalServer.class);
private static final MetricRegistry registry = new MetricRegistry();
private static final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();
private static final ExecutorService threadPoolExecutors = Executors.newCachedThreadPool();

public MinimalServer() {
final JmxReporter reporter = JmxReporter.forRegistry(registry).build();
reporter.start();

}

public static void main(String args[]) throws Exception {
QueuedThreadPool threadPool = new QueuedThreadPool(200, 20, 30000);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");

//CodeHale param
context.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry", registry);
context.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.registry", healthCheckRegistry);
context.setInitParameter("com.codahale.metrics.servlets.MetricsServlet.allowedOrigin", "*");
context.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.executor", threadPoolExecutors);

// Add servlets
context.addServlet(new ServletHolder(new ComplianceStreamingServlet()), "/stream/*");
context.addServlet(new ServletHolder(new ComplainceBatchServlet()), "/batch/*");
context.addServlet(new ServletHolder(new DumpServlet()), "/dump/*");
context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics/*");
context.addServlet(new ServletHolder(new AdminServlet()), "/admin/*");
context.addServlet(new ServletHolder(new HealthCheckServlet()), "/health/*");
context.addServlet(new ServletHolder(new PingServlet()), "/ping/*");
context.addServlet(new ServletHolder(new ThreadDumpServlet()), "/tdump/*");

// Setup Spring context
context.addEventListener(new ContextLoaderListener());
context.setInitParameter("contextConfigLocation", "classpath*:**/appContext.xml");
log.info("Spring Context: " + context.getDisplayName());

Server server = new Server(threadPool);
//Server server = new Server();
server.addBean(new MBeanContainer(ManagementFactory.getPlatformMBeanServer()));
ConnectorStatistics.addToAllConnectors(server);

HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(8443);
http_config.setOutputBufferSize(32768);

Connector[] connectors = new Connector[2];
connectors[0] = setHttpConfiguration(server, http_config);
connectors[1] = setHttpsConfiguration(server, http_config);

ComplianceLifeCycleListener complianceListener = new ComplianceLifeCycleListener();
server.addLifeCycleListener(complianceListener);

server.setHandler(context);
server.setConnectors(connectors);
server.start();
server.join();

}

private static ServerConnector setHttpConfiguration(Server server, HttpConfiguration http_config) {
ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory(http_config));
serverConnector.setPort(8080);
serverConnector.setIdleTimeout(108000);
return serverConnector;
}

private static ServerConnector setHttpsConfiguration(Server server, HttpConfiguration http_config) {
SslContextFactory sslContextFactory = new SslContextFactory();
System.out.println("user.home " + System.getProperty("user.home"));
sslContextFactory.setKeyStorePath(System.getProperty("user.home") + "/keystore.jks");
sslContextFactory.setKeyStorePassword("changeme");
sslContextFactory.setKeyManagerPassword("changeme");

HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer());
ServerConnector serverConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory,
"http/1.1"), new HttpConnectionFactory(https_config));
serverConnector.setPort(8443);
serverConnector.setIdleTimeout(108000);
return serverConnector;
Reply all
Reply to author
Forward
0 new messages