Skip to content

Commit

Permalink
Make server.socket.* attributes on the HTTP server side opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Jul 17, 2023
1 parent cc8160c commit b45ffd7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public static <REQUEST, RESPONSE> HttpClientAttributesExtractorBuilder<REQUEST,
addressPortExtractor,
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
InternalServerAttributesExtractor.Mode.PEER);
InternalServerAttributesExtractor.Mode.PEER,
/* captureServerSocketAttributes= */ true);
this.resendCountIncrementer = resendCountIncrementer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ public static <REQUEST, RESPONSE> HttpServerAttributesExtractorBuilder<REQUEST,
NetServerAttributesGetter<REQUEST, RESPONSE> netAttributesGetter,
List<String> capturedRequestHeaders,
List<String> capturedResponseHeaders,
Set<String> knownMethods) {
Set<String> knownMethods,
boolean captureServerSocketAttributes) {
this(
httpAttributesGetter,
netAttributesGetter,
capturedRequestHeaders,
capturedResponseHeaders,
knownMethods,
captureServerSocketAttributes,
HttpRouteHolder::getRoute);
}

Expand All @@ -97,6 +99,7 @@ public static <REQUEST, RESPONSE> HttpServerAttributesExtractorBuilder<REQUEST,
List<String> capturedRequestHeaders,
List<String> capturedResponseHeaders,
Set<String> knownMethods,
boolean captureServerSocketAttributes,
Function<Context, String> httpRouteHolderGetter) {
super(httpAttributesGetter, capturedRequestHeaders, capturedResponseHeaders, knownMethods);
HttpNetAddressPortExtractor<REQUEST> addressPortExtractor =
Expand All @@ -123,7 +126,10 @@ public static <REQUEST, RESPONSE> HttpServerAttributesExtractorBuilder<REQUEST,
addressPortExtractor,
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
InternalServerAttributesExtractor.Mode.HOST);
InternalServerAttributesExtractor.Mode.HOST,
// we're not capturing these by default, since they're opt-in
// we'll add a configuration flag if someone happens to request them
/* captureServerSocketAttributes= */ false);
internalClientExtractor =
new InternalClientAttributesExtractor<>(
netAttributesGetter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public final class HttpServerAttributesExtractorBuilder<REQUEST, RESPONSE> {
List<String> capturedRequestHeaders = emptyList();
List<String> capturedResponseHeaders = emptyList();
Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
boolean captureServerSocketAttributes = false;

HttpServerAttributesExtractorBuilder(
HttpServerAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter,
Expand Down Expand Up @@ -89,6 +90,20 @@ public HttpServerAttributesExtractorBuilder<REQUEST, RESPONSE> setKnownMethods(
return this;
}

/**
* Configures the extractor to capture the optional {@code server.socket.address} and {@code
* server.socket.port} attributes, which are not collected by default.
*
* @param captureServerSocketAttributes {@code true} if the extractor should collect the optional
* {@code server.socket.address} and {@code server.socket.port} attributes.
*/
@CanIgnoreReturnValue
public HttpServerAttributesExtractorBuilder<REQUEST, RESPONSE> setCaptureServerSocketAttributes(
boolean captureServerSocketAttributes) {
this.captureServerSocketAttributes = captureServerSocketAttributes;
return this;
}

/**
* Returns a new {@link HttpServerAttributesExtractor} with the settings of this {@link
* HttpServerAttributesExtractorBuilder}.
Expand All @@ -99,6 +114,7 @@ public AttributesExtractor<REQUEST, RESPONSE> build() {
netAttributesGetter,
capturedRequestHeaders,
capturedResponseHeaders,
knownMethods);
knownMethods,
captureServerSocketAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ private NetClientAttributesExtractor(NetClientAttributesGetter<REQUEST, RESPONSE
FallbackAddressPortExtractor.noop(),
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
InternalServerAttributesExtractor.Mode.PEER);
InternalServerAttributesExtractor.Mode.PEER,
/* captureServerSocketAttributes= */ true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private NetServerAttributesExtractor(NetServerAttributesGetter<REQUEST, RESPONSE
FallbackAddressPortExtractor.noop(),
SemconvStability.emitStableHttpSemconv(),
SemconvStability.emitOldHttpSemconv(),
InternalServerAttributesExtractor.Mode.HOST);
InternalServerAttributesExtractor.Mode.HOST,
/* captureServerSocketAttributes= */ true);
internalClientExtractor =
new InternalClientAttributesExtractor<>(
getter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public static <REQUEST, RESPONSE> ServerAttributesExtractor<REQUEST, RESPONSE> c
/* emitStableUrlAttributes= */ true,
/* emitOldHttpAttributes= */ false,
// this param does not matter when old semconv is off
InternalServerAttributesExtractor.Mode.HOST);
InternalServerAttributesExtractor.Mode.HOST,
/* captureServerSocketAttributes= */ true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ public final class InternalServerAttributesExtractor<REQUEST, RESPONSE> {
private final boolean emitStableUrlAttributes;
private final boolean emitOldHttpAttributes;
private final Mode oldSemconvMode;
private final boolean captureServerSocketAttributes;

public InternalServerAttributesExtractor(
ServerAttributesGetter<REQUEST, RESPONSE> getter,
BiPredicate<Integer, REQUEST> captureServerPortCondition,
FallbackAddressPortExtractor<REQUEST> fallbackAddressPortExtractor,
boolean emitStableUrlAttributes,
boolean emitOldHttpAttributes,
Mode oldSemconvMode) {
Mode oldSemconvMode,
boolean captureServerSocketAttributes) {
this.getter = getter;
this.captureServerPortCondition = captureServerPortCondition;
this.fallbackAddressPortExtractor = fallbackAddressPortExtractor;
this.emitStableUrlAttributes = emitStableUrlAttributes;
this.emitOldHttpAttributes = emitOldHttpAttributes;
this.oldSemconvMode = oldSemconvMode;
this.captureServerSocketAttributes = captureServerSocketAttributes;
}

public void onStart(AttributesBuilder attributes, REQUEST request) {
Expand Down Expand Up @@ -69,7 +72,7 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO

String serverSocketAddress = getter.getServerSocketAddress(request, response);
if (serverSocketAddress != null && !serverSocketAddress.equals(serverAddressAndPort.address)) {
if (emitStableUrlAttributes) {
if (emitStableUrlAttributes && captureServerSocketAttributes) {
internalSet(attributes, NetworkAttributes.SERVER_SOCKET_ADDRESS, serverSocketAddress);
}
if (emitOldHttpAttributes) {
Expand All @@ -81,7 +84,7 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO
if (serverSocketPort != null
&& serverSocketPort > 0
&& !serverSocketPort.equals(serverAddressAndPort.port)) {
if (emitStableUrlAttributes) {
if (emitStableUrlAttributes && captureServerSocketAttributes) {
internalSet(attributes, NetworkAttributes.SERVER_SOCKET_PORT, (long) serverSocketPort);
}
if (emitOldHttpAttributes) {
Expand All @@ -91,7 +94,7 @@ public void onEnd(AttributesBuilder attributes, REQUEST request, @Nullable RESPO

String serverSocketDomain = getter.getServerSocketDomain(request, response);
if (serverSocketDomain != null && !serverSocketDomain.equals(serverAddressAndPort.address)) {
if (emitStableUrlAttributes) {
if (emitStableUrlAttributes && captureServerSocketAttributes) {
internalSet(attributes, NetworkAttributes.SERVER_SOCKET_DOMAIN, serverSocketDomain);
}
if (emitOldHttpAttributes && oldSemconvMode.socketDomain != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ void normal() {
singletonList("Custom-Request-Header"),
singletonList("Custom-Response-Header"),
HttpConstants.KNOWN_METHODS,
false,
routeFromContext);

AttributesBuilder startAttributes = Attributes.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ void normal() {
singletonList("Custom-Request-Header"),
singletonList("Custom-Response-Header"),
HttpConstants.KNOWN_METHODS,
false,
routeFromContext);

AttributesBuilder startAttributes = Attributes.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ public Integer getServerPort(Map<String, String> request) {
String value = request.get("hostPort");
return value == null ? null : Integer.parseInt(value);
}

@Nullable
@Override
public String getServerSocketAddress(
Map<String, String> request, @Nullable Map<String, String> response) {
return request.get("serverSocketAddress");
}

@Nullable
@Override
public Integer getServerSocketPort(
Map<String, String> request, @Nullable Map<String, String> response) {
String value = request.get("serverSocketPort");
return value == null ? null : Integer.parseInt(value);
}
}

@Test
Expand All @@ -154,6 +169,8 @@ void normal() {
request.put("type", "ipv4");
request.put("protocolName", "http");
request.put("protocolVersion", "2.0");
request.put("serverSocketAddress", "1.2.3.4");
request.put("serverSocketPort", "42");

Map<String, String> response = new HashMap<>();
response.put("statusCode", "202");
Expand All @@ -169,6 +186,7 @@ void normal() {
singletonList("Custom-Request-Header"),
singletonList("Custom-Response-Header"),
HttpConstants.KNOWN_METHODS,
false,
routeFromContext);

AttributesBuilder startAttributes = Attributes.builder();
Expand Down

0 comments on commit b45ffd7

Please sign in to comment.