Skip to content

Commit

Permalink
Only set server.port when server.address is set (open-telemetry#9737
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Mateusz Rzeszutek authored and Abhishekkr3003 committed Nov 7, 2023
1 parent fd259ab commit de6699c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ public InternalClientAttributesExtractor(
public void onStart(AttributesBuilder attributes, REQUEST request) {
AddressAndPort clientAddressAndPort = addressAndPortExtractor.extract(request);

if (emitStableUrlAttributes) {
internalSet(attributes, SemanticAttributes.CLIENT_ADDRESS, clientAddressAndPort.address);
if (clientAddressAndPort.port != null && clientAddressAndPort.port > 0) {
internalSet(attributes, SemanticAttributes.CLIENT_PORT, (long) clientAddressAndPort.port);
if (clientAddressAndPort.address != null) {
if (emitStableUrlAttributes) {
internalSet(attributes, SemanticAttributes.CLIENT_ADDRESS, clientAddressAndPort.address);
if (clientAddressAndPort.port != null && clientAddressAndPort.port > 0) {
internalSet(attributes, SemanticAttributes.CLIENT_PORT, (long) clientAddressAndPort.port);
}
}
if (emitOldHttpAttributes) {
internalSet(attributes, SemanticAttributes.HTTP_CLIENT_IP, clientAddressAndPort.address);
}
}
if (emitOldHttpAttributes) {
internalSet(attributes, SemanticAttributes.HTTP_CLIENT_IP, clientAddressAndPort.address);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ public InternalServerAttributesExtractor(
public void onStart(AttributesBuilder attributes, REQUEST request) {
AddressAndPort serverAddressAndPort = addressAndPortExtractor.extract(request);

if (emitStableUrlAttributes) {
internalSet(attributes, SemanticAttributes.SERVER_ADDRESS, serverAddressAndPort.address);
}
if (emitOldHttpAttributes) {
internalSet(attributes, oldSemconvMode.address, serverAddressAndPort.address);
}

if (serverAddressAndPort.port != null
&& serverAddressAndPort.port > 0
&& captureServerPortCondition.test(serverAddressAndPort.port, request)) {
if (serverAddressAndPort.address != null) {
if (emitStableUrlAttributes) {
internalSet(attributes, SemanticAttributes.SERVER_PORT, (long) serverAddressAndPort.port);
internalSet(attributes, SemanticAttributes.SERVER_ADDRESS, serverAddressAndPort.address);
}
if (emitOldHttpAttributes) {
internalSet(attributes, oldSemconvMode.port, (long) serverAddressAndPort.port);
internalSet(attributes, oldSemconvMode.address, serverAddressAndPort.address);
}

if (serverAddressAndPort.port != null
&& serverAddressAndPort.port > 0
&& captureServerPortCondition.test(serverAddressAndPort.port, request)) {
if (emitStableUrlAttributes) {
internalSet(attributes, SemanticAttributes.SERVER_PORT, (long) serverAddressAndPort.port);
}
if (emitOldHttpAttributes) {
internalSet(attributes, oldSemconvMode.port, (long) serverAddressAndPort.port);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,21 @@ void doesNotSetNegativePortValue() {
extractor.onEnd(endAttributes, Context.root(), request, null, null);
assertThat(endAttributes.build()).isEmpty();
}

@Test
void portWithoutAddress() {
Map<String, String> request = new HashMap<>();
request.put("port", "80");

AttributesExtractor<Map<String, String>, Void> extractor =
ClientAttributesExtractor.create(new TestClientAttributesGetter());

AttributesBuilder startAttributes = Attributes.builder();
extractor.onStart(startAttributes, Context.root(), request);
assertThat(startAttributes.build()).isEmpty();

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), request, null, null);
assertThat(endAttributes.build()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,21 @@ void doesNotSetNegativePortValue() {
assertThat(startAttributes.build())
.containsOnly(entry(SemanticAttributes.SERVER_ADDRESS, "opentelemetry.io"));
}

@Test
void portWithoutAddress() {
Map<String, String> request = new HashMap<>();
request.put("port", "80");

AttributesExtractor<Map<String, String>, Void> extractor =
ServerAttributesExtractor.create(new TestServerAttributesGetter());

AttributesBuilder startAttributes = Attributes.builder();
extractor.onStart(startAttributes, Context.root(), request);
assertThat(startAttributes.build()).isEmpty();

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), request, null, null);
assertThat(endAttributes.build()).isEmpty();
}
}

0 comments on commit de6699c

Please sign in to comment.