-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract HTTP request/response headers as span attributes (#4237)
* Extract HTTP request/response headers as span attributes * fix muzzle * code review comments * fix compilation failure after merge conflict * avoid using streams API when transforming the headers list * fix liberty extractor * fix spring webmvc extractor
- Loading branch information
Mateusz Rzeszutek
authored
Oct 5, 2021
1 parent
2a76d41
commit 7473eff
Showing
56 changed files
with
1,071 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...main/java/io/opentelemetry/instrumentation/api/instrumenter/http/CapturedHttpHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter.http; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Represents the configuration that specifies which HTTP request/response headers should be | ||
* captured as span attributes. | ||
*/ | ||
@AutoValue | ||
public abstract class CapturedHttpHeaders { | ||
|
||
private static final CapturedHttpHeaders EMPTY = create(emptyList(), emptyList()); | ||
|
||
/** Don't capture any HTTP headers as span attributes. */ | ||
public static CapturedHttpHeaders empty() { | ||
return EMPTY; | ||
} | ||
|
||
/** | ||
* Captures the configured HTTP request and response headers as span attributes as described in <a | ||
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers">HTTP | ||
* semantic conventions</a>. | ||
* | ||
* <p>The HTTP request header values will be captured under the {@code http.request.header.<name>} | ||
* attribute key. The HTTP response header values will be captured under the {@code | ||
* http.response.header.<name>} attribute key. The {@code <name>} part in the attribute key is the | ||
* normalized header name: lowercase, with dashes replaced by underscores. | ||
* | ||
* @param capturedRequestHeaders A list of HTTP request header names that are to be captured as | ||
* span attributes. | ||
* @param capturedResponseHeaders A list of HTTP response header names that are to be captured as | ||
* span attributes. | ||
*/ | ||
public static CapturedHttpHeaders create( | ||
List<String> capturedRequestHeaders, List<String> capturedResponseHeaders) { | ||
return new AutoValue_CapturedHttpHeaders( | ||
lowercase(capturedRequestHeaders), lowercase(capturedResponseHeaders)); | ||
} | ||
|
||
private static List<String> lowercase(List<String> names) { | ||
return names.stream().map(s -> s.toLowerCase(Locale.ROOT)).collect(Collectors.toList()); | ||
} | ||
|
||
/** Returns the list of HTTP request header names that are to be captured as span attributes. */ | ||
public abstract List<String> requestHeaders(); | ||
|
||
/** Returns the list of HTTP response header names that are to be captured as span attributes. */ | ||
public abstract List<String> responseHeaders(); | ||
|
||
CapturedHttpHeaders() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ain/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpHeaderAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter.http; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.instrumentation.api.caching.Cache; | ||
import java.util.List; | ||
|
||
final class HttpHeaderAttributes { | ||
|
||
private static final Cache<String, AttributeKey<List<String>>> requestKeysCache = | ||
Cache.newBuilder().setMaximumSize(32).build(); | ||
private static final Cache<String, AttributeKey<List<String>>> responseKeysCache = | ||
Cache.newBuilder().setMaximumSize(32).build(); | ||
|
||
static AttributeKey<List<String>> requestAttributeKey(String headerName) { | ||
return requestKeysCache.computeIfAbsent(headerName, n -> createKey("request", n)); | ||
} | ||
|
||
static AttributeKey<List<String>> responseAttributeKey(String headerName) { | ||
return responseKeysCache.computeIfAbsent(headerName, n -> createKey("response", n)); | ||
} | ||
|
||
private static AttributeKey<List<String>> createKey(String type, String headerName) { | ||
// headerName is always lowercase, see CapturedHttpHeaders | ||
String key = "http." + type + ".header." + headerName.replace('-', '_'); | ||
return AttributeKey.stringArrayKey(key); | ||
} | ||
|
||
private HttpHeaderAttributes() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.