Skip to content

Commit

Permalink
fix spring webmvc extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Oct 5, 2021
1 parent d1a76e8 commit 37b7c6c
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@

package io.opentelemetry.instrumentation.spring.webmvc;

import io.opentelemetry.instrumentation.api.instrumenter.http.CapturedHttpHeaders;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.checkerframework.checker.nullness.qual.Nullable;

final class SpringWebMvcHttpAttributesExtractor
extends HttpServerAttributesExtractor<HttpServletRequest, HttpServletResponse> {

// TODO: add support for capturing HTTP headers in library instrumentations
SpringWebMvcHttpAttributesExtractor() {
super(CapturedHttpHeaders.empty());
}

@Override
protected @Nullable String method(HttpServletRequest request) {
return request.getMethod();
Expand All @@ -22,6 +34,12 @@ final class SpringWebMvcHttpAttributesExtractor
return request.getHeader("user-agent");
}

@Override
protected List<String> requestHeader(HttpServletRequest request, String name) {
Enumeration<String> headers = request.getHeaders(name);
return headers == null ? Collections.emptyList() : Collections.list(headers);
}

@Override
protected @Nullable Long requestContentLength(
HttpServletRequest request, @Nullable HttpServletResponse response) {
Expand Down Expand Up @@ -57,6 +75,19 @@ final class SpringWebMvcHttpAttributesExtractor
return null;
}

@Override
protected List<String> responseHeader(
HttpServletRequest request, HttpServletResponse response, String name) {
Collection<String> headers = response.getHeaders(name);
if (headers == null) {
return Collections.emptyList();
}
if (headers instanceof List) {
return (List<String>) headers;
}
return new ArrayList<>(headers);
}

@Override
protected @Nullable String target(HttpServletRequest request) {
String target = request.getRequestURI();
Expand Down

0 comments on commit 37b7c6c

Please sign in to comment.