It’s a great pleasure for me to get through the second phase of the project and passing the 2nd milestone. Most of the work related to the project is almost done. In the last week, I worked on tickets related to the module and got them merged. I finally got the chance to expose the metrics through a servlet that extends the default jmxservlet class of dropwizards.

Normally it is easy to expose the servlet with dropwizard with their default servlet class it has everything inbuilt. We need to configure a listener that listens to the module startup to register a default metric registry on the application context. But in openmrs we don’t support listeners on servlet context. But we support filters so came up with a class that implements the filter interface. and on the init phase we inject the registry instance which is required by the jmxservlet class.

public class MetricsInitializationFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
final ServletContext context = filterConfig.getServletContext();
context.setAttribute(METRICS_REGISTRY, getMetricRegistry());
context.setAttribute(METRIC_FILTER, getMetricFilter());
if (getDurationUnit() != null) {
context.setInitParameter(MetricsServlet.DURATION_UNIT, getDurationUnit().toString());
}
if (getRateUnit() != null) {
context.setInitParameter(MetricsServlet.RATE_UNIT, getRateUnit().toString());
}
if (getAllowedOrigin() != null) {
context.setInitParameter(MetricsServlet.ALLOWED_ORIGIN, getAllowedOrigin());
}
if (getJsonpCallbackParameter() != null) {
context.setAttribute(CALLBACK_PARAM, getJsonpCallbackParameter());
}
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
chain.doFilter(req, res);
}
}

But it was not a success path it was not injecting the registry instance during the server startup.

Then we went on to the doFilter phase of the Filter and tried to inject the registry object in that method.

public class MetricsInitializationFilter implements Filter {	@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
final ServletContext context = req.getServletContext();
if (context.getAttribute(METRICS_REGISTRY) == null) {
context.setAttribute(METRICS_REGISTRY, getMetricRegistry());
} if (context.getAttribute(METRIC_FILTER) == null) {
context.setAttribute(METRIC_FILTER, getMetricFilter());
} chain.doFilter(req, res);
}
}

Sadly it went on the down path as well. Then thought about injecting the registry in the init method of the servlet. It was not the solution as well.

Finally, I thought to replicate the jmxservlet in a way that we can inject the metric registry with a custom servlet class so it worked. It was the long story short. Yet I didn’t push the changes.

Also, I was been able to merge below PR’s last week

https://github.com/openmrs/openmrs-module-metrics/pull/14

That’s all this week.

Cheers and hoping to work more in the next few weeks thank you.

#apache #opensource #openmrs