From 37b7c6cb021014dffce58ced9cbfe3103056d340 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Mon, 4 Oct 2021 13:50:30 +0200 Subject: [PATCH] fix spring webmvc extractor --- .../SpringWebMvcHttpAttributesExtractor.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/instrumentation/spring/spring-webmvc-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/SpringWebMvcHttpAttributesExtractor.java b/instrumentation/spring/spring-webmvc-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/SpringWebMvcHttpAttributesExtractor.java index c4776fa0a6ae..44cd00fee0a3 100644 --- a/instrumentation/spring/spring-webmvc-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/SpringWebMvcHttpAttributesExtractor.java +++ b/instrumentation/spring/spring-webmvc-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/SpringWebMvcHttpAttributesExtractor.java @@ -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 { + + // TODO: add support for capturing HTTP headers in library instrumentations + SpringWebMvcHttpAttributesExtractor() { + super(CapturedHttpHeaders.empty()); + } + @Override protected @Nullable String method(HttpServletRequest request) { return request.getMethod(); @@ -22,6 +34,12 @@ final class SpringWebMvcHttpAttributesExtractor return request.getHeader("user-agent"); } + @Override + protected List requestHeader(HttpServletRequest request, String name) { + Enumeration headers = request.getHeaders(name); + return headers == null ? Collections.emptyList() : Collections.list(headers); + } + @Override protected @Nullable Long requestContentLength( HttpServletRequest request, @Nullable HttpServletResponse response) { @@ -57,6 +75,19 @@ final class SpringWebMvcHttpAttributesExtractor return null; } + @Override + protected List responseHeader( + HttpServletRequest request, HttpServletResponse response, String name) { + Collection headers = response.getHeaders(name); + if (headers == null) { + return Collections.emptyList(); + } + if (headers instanceof List) { + return (List) headers; + } + return new ArrayList<>(headers); + } + @Override protected @Nullable String target(HttpServletRequest request) { String target = request.getRequestURI();