Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.mycompany.servlet.filterlistener;
import io.honeycomb.beeline.DefaultBeeline;
import io.honeycomb.beeline.tracing.Span;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Example of how to use generated Beeline spans within the request itself.
*/
@SuppressWarnings("serial")
@WebServlet("/FilterExample")
public class FilterExampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// To add more spans or custom fields get the Beeline object from the request.
// This was added by the Filter.
DefaultBeeline beeline = (DefaultBeeline) req.getAttribute("beeline");
// Add fields based on things like user input, SQL calls, or anything you can imagine.
beeline.getActiveSpan().addField("fooParameter", req.getParameter("foo"));
// More spans can be generated to time / gain insight into whatever you need.
Span midSpan = beeline.startSpan("sleep");
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
midSpan.close();
}
Span sleepAgainSpan = beeline.startSpan("sleepAgain");
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
sleepAgainSpan.close();
}
// close HTTP writer
}
}
package com.mycompany.servlet.filterlistener;
import io.honeycomb.beeline.DefaultBeeline;
import io.honeycomb.beeline.tracing.Span;
import io.honeycomb.libhoney.shaded.org.apache.http.HttpHeaders;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter("/*")
public class HoneycombServletFilter implements Filter {
protected ServletContext servletContext;
DefaultBeeline beeline;
@Override
public void init(FilterConfig filterConfig) {
servletContext = filterConfig.getServletContext();
beeline = DefaultBeeline.getInstance("yourname.jboss_servlet_filter", "FilterExampleService", "WRITEKEY");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
final HttpServletResponse httpServletResponse = (HttpServletResponse) response;
final Span rootSpan = beeline.startSpan("ServletRequest");
rootSpan.addField("request.content_length", request.getContentLength());
rootSpan.addField("request.host", request.getServerName());
rootSpan.addField("request.http_version", request.getProtocol());
rootSpan.addField("request.method", httpServletRequest.getMethod());
rootSpan.addField("request.path", httpServletRequest.getRequestURL().toString());
rootSpan.addField("request.query", httpServletRequest.getQueryString());
rootSpan.addField("request.header.accept", httpServletRequest.getHeader(HttpHeaders.ACCEPT));
rootSpan.addField("request.header.user_agent", httpServletRequest.getHeader(HttpHeaders.USER_AGENT));
try {
request.setAttribute("beeline", beeline);
chain.doFilter(request, response);
rootSpan.addField("response.header_content_type", httpServletResponse.getHeader(HttpHeaders.CONTENT_TYPE));
rootSpan.addField("response.status_code", httpServletResponse.getStatus());
} finally {
rootSpan.close();
beeline.getBeeline().getTracer().endTrace();
}
}
@Override
public void destroy() {
servletContext = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment