Skip to content

Instantly share code, notes, and snippets.

@pulasthi7
Created June 7, 2020 14:13
Show Gist options
  • Save pulasthi7/3a9c694720c8db80acd46e8e107d6e5d to your computer and use it in GitHub Desktop.
Save pulasthi7/3a9c694720c8db80acd46e8e107d6e5d to your computer and use it in GitHub Desktop.
WSO2 IS - Promethus servlet registration
package org.wso2.carbon.identity.sample.prometheus.internal;
import org.osgi.service.http.HttpService;
public class DataHolder {
private static DataHolder instance = new DataHolder();
private HttpService httpService;
private DataHolder() {
}
public static DataHolder getInstance() {
return instance;
}
public HttpService getHttpService() {
return httpService;
}
public void setHttpService(HttpService httpService) {
this.httpService = httpService;
}
}
package org.wso2.carbon.identity.sample.prometheus.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.http.HttpService;
import org.wso2.carbon.identity.sample.prometheus.servlet.IdentityMetricsServlet;
import javax.servlet.Servlet;
@Component(
name = "identity.sample.prometheus.agent",
immediate = true
)
public class ServiceComponent {
private static final Log LOG = LogFactory.getLog(ServiceComponent.class);
@Activate
protected void activate(ComponentContext context) {
try {
// Register the metrics endpoint as an OSGi Servlet
HttpService httpService = DataHolder.getInstance().getHttpService();
Servlet identityMetricsServlet = new ContextPathServletAdaptor(new IdentityMetricsServlet(),
IdentityMetricsServlet.CONTEXT);
httpService.registerServlet(IdentityMetricsServlet.CONTEXT, identityMetricsServlet, null, null);
if (LOG.isDebugEnabled()) {
LOG.debug("Sample prometheus agent bundle is activated");
}
} catch (Exception e) {
LOG.error("Error while activating sample prometheus agent bundle", e);
}
}
@Deactivate
protected void deactivate(ComponentContext context) {
if (LOG.isDebugEnabled()) {
LOG.debug("Sample prometheus agent bundle is deactivated");
}
}
@Reference(
name = "osgi.httpservice",
service = org.osgi.service.http.HttpService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetHttpService")
protected void setHttpService(HttpService httpService) {
DataHolder.getInstance().setHttpService(httpService);
}
protected void unsetHttpService(HttpService httpService) {
DataHolder.getInstance().setHttpService(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment