io.opentelemetry.javaagent.instrumentation.servlet.v2_2.Servlet2HttpAttributesGetter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-javaagent-servlet-2.2 Show documentation
Show all versions of opentelemetry-javaagent-servlet-2.2 Show documentation
Instrumentation of Java libraries using OpenTelemetry.
The newest version!
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.servlet.v2_2;
import io.opentelemetry.javaagent.instrumentation.servlet.ServletAccessor;
import io.opentelemetry.javaagent.instrumentation.servlet.ServletHttpAttributesGetter;
import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext;
import io.opentelemetry.javaagent.instrumentation.servlet.ServletResponseContext;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet2HttpAttributesGetter
extends ServletHttpAttributesGetter {
public Servlet2HttpAttributesGetter(
ServletAccessor accessor) {
super(accessor);
}
@Override
@Nullable
public Integer getHttpResponseStatusCode(
ServletRequestContext requestContext,
ServletResponseContext responseContext,
@Nullable Throwable error) {
HttpServletResponse response = responseContext.response();
if (!accessor.isResponseCommitted(response) && error != null) {
// if response is not committed and there is a throwable set status to 500 /
// INTERNAL_SERVER_ERROR, due to servlet spec
// https://javaee.github.io/servlet-spec/downloads/servlet-4.0/servlet-4_0_FINAL.pdf:
// "If a servlet generates an error that is not handled by the error page mechanism as
// described above, the container must ensure to send a response with status 500."
return 500;
}
if (responseContext.hasStatus()) {
return responseContext.getStatus();
}
return null;
}
}