Skip to content

Commit a52d607

Browse files
committed
Review fixes
1 parent 8ff0381 commit a52d607

File tree

7 files changed

+112
-151
lines changed

7 files changed

+112
-151
lines changed

exporters/common/src/main/java/io/opentelemetry/exporter/internal/metrics/ExporterInstrumentation.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
package io.opentelemetry.exporter.internal.metrics;
77

88
import io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.api.common.AttributesBuilder;
910
import io.opentelemetry.api.metrics.MeterProvider;
1011
import io.opentelemetry.sdk.common.InternalTelemetryVersion;
1112
import io.opentelemetry.sdk.internal.SemConvAttributes;
1213
import io.opentelemetry.sdk.internal.Signal;
1314
import io.opentelemetry.sdk.internal.StandardComponentId;
15+
import java.net.URI;
16+
import java.net.URISyntaxException;
1417
import java.util.function.Supplier;
1518
import javax.annotation.Nullable;
1619

@@ -42,16 +45,40 @@ public ExporterInstrumentation(
4245
signal == Signal.PROFILE
4346
? NoopExporterMetrics.INSTANCE
4447
: new SemConvExporterMetrics(
45-
meterProviderSupplier,
46-
signal,
47-
componentId,
48-
ServerAttributesUtil.extractServerAttributes(endpoint));
48+
meterProviderSupplier, signal, componentId, extractServerAttributes(endpoint));
4949
break;
5050
default:
5151
throw new IllegalStateException("Unhandled case: " + schema);
5252
}
5353
}
5454

55+
// visible for testing
56+
static Attributes extractServerAttributes(String httpEndpoint) {
57+
try {
58+
URI parsed = new URI(httpEndpoint);
59+
AttributesBuilder builder = Attributes.builder();
60+
String host = parsed.getHost();
61+
if (host != null) {
62+
builder.put(SemConvAttributes.SERVER_ADDRESS, host);
63+
}
64+
int port = parsed.getPort();
65+
if (port == -1) {
66+
String scheme = parsed.getScheme();
67+
if ("https".equals(scheme)) {
68+
port = 443;
69+
} else if ("http".equals(scheme)) {
70+
port = 80;
71+
}
72+
}
73+
if (port != -1) {
74+
builder.put(SemConvAttributes.SERVER_PORT, port);
75+
}
76+
return builder.build();
77+
} catch (URISyntaxException e) {
78+
return Attributes.empty();
79+
}
80+
}
81+
5582
public Recording startRecordingExport(int itemCount) {
5683
return new Recording(implementation.startRecordingExport(itemCount));
5784
}

exporters/common/src/main/java/io/opentelemetry/exporter/internal/metrics/ServerAttributesUtil.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

exporters/common/src/test/java/io/opentelemetry/exporter/internal/grpc/GrpcExporterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import io.opentelemetry.api.common.Attributes;
1414
import io.opentelemetry.exporter.internal.marshal.Marshaler;
15+
import io.opentelemetry.internal.testing.slf4j.SuppressLogger;
1516
import io.opentelemetry.sdk.common.InternalTelemetryVersion;
1617
import io.opentelemetry.sdk.internal.ComponentId;
1718
import io.opentelemetry.sdk.internal.SemConvAttributes;

exporters/common/src/test/java/io/opentelemetry/exporter/internal/http/FakeHttpResponse.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

exporters/common/src/test/java/io/opentelemetry/exporter/internal/http/HttpExporterTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import io.opentelemetry.api.common.Attributes;
1515
import io.opentelemetry.exporter.internal.marshal.Marshaler;
16+
import io.opentelemetry.internal.testing.slf4j.SuppressLogger;
1617
import io.opentelemetry.sdk.common.InternalTelemetryVersion;
1718
import io.opentelemetry.sdk.internal.ComponentId;
1819
import io.opentelemetry.sdk.internal.SemConvAttributes;
@@ -208,4 +209,30 @@ void testInternalTelemetry(StandardComponentId.ExporterType exporterType) {
208209
.hasBucketCounts(1))));
209210
}
210211
}
212+
213+
private static class FakeHttpResponse implements HttpSender.Response {
214+
215+
final int statusCode;
216+
final String statusMessage;
217+
218+
FakeHttpResponse(int statusCode, String statusMessage) {
219+
this.statusCode = statusCode;
220+
this.statusMessage = statusMessage;
221+
}
222+
223+
@Override
224+
public int statusCode() {
225+
return statusCode;
226+
}
227+
228+
@Override
229+
public String statusMessage() {
230+
return statusMessage;
231+
}
232+
233+
@Override
234+
public byte[] responseBody() throws IOException {
235+
return new byte[0];
236+
}
237+
}
211238
}

exporters/common/src/test/java/io/opentelemetry/exporter/internal/metrics/ExporterInstrumentationTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.exporter.internal.metrics;
77

8+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
89
import static org.mockito.Mockito.atLeastOnce;
910
import static org.mockito.Mockito.mock;
1011
import static org.mockito.Mockito.verify;
@@ -16,13 +17,15 @@
1617
import io.opentelemetry.sdk.common.CompletableResultCode;
1718
import io.opentelemetry.sdk.common.InternalTelemetryVersion;
1819
import io.opentelemetry.sdk.internal.ComponentId;
20+
import io.opentelemetry.sdk.internal.SemConvAttributes;
1921
import io.opentelemetry.sdk.internal.StandardComponentId;
2022
import io.opentelemetry.sdk.metrics.InstrumentType;
2123
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
2224
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
2325
import io.opentelemetry.sdk.metrics.export.CollectionRegistration;
2426
import io.opentelemetry.sdk.metrics.export.MetricReader;
2527
import java.util.function.Supplier;
28+
import org.junit.jupiter.api.Test;
2629
import org.junit.jupiter.params.ParameterizedTest;
2730
import org.junit.jupiter.params.provider.EnumSource;
2831
import org.mockito.Mockito;
@@ -103,4 +106,54 @@ void noopMeterProvider(InternalTelemetryVersion schemaVersion) {
103106
instrumentation.startRecordingExport(42).finishSuccessful();
104107
verify(meterProviderSupplier, atLeastOnce()).get();
105108
}
109+
110+
@Test
111+
void serverAttributesInvalidUrl() {
112+
assertThat(ExporterInstrumentation.extractServerAttributes("^")).isEmpty();
113+
}
114+
115+
@Test
116+
void serverAttributesEmptyUrl() {
117+
assertThat(ExporterInstrumentation.extractServerAttributes("")).isEmpty();
118+
}
119+
120+
@Test
121+
void serverAttributesHttps() {
122+
assertThat(ExporterInstrumentation.extractServerAttributes("https://example.com/foo/bar?a=b"))
123+
.hasSize(2)
124+
.containsEntry(SemConvAttributes.SERVER_ADDRESS, "example.com")
125+
.containsEntry(SemConvAttributes.SERVER_PORT, 443);
126+
127+
assertThat(
128+
ExporterInstrumentation.extractServerAttributes("https://example.com:1234/foo/bar?a=b"))
129+
.hasSize(2)
130+
.containsEntry(SemConvAttributes.SERVER_ADDRESS, "example.com")
131+
.containsEntry(SemConvAttributes.SERVER_PORT, 1234);
132+
}
133+
134+
@Test
135+
void serverAttributesHttp() {
136+
assertThat(ExporterInstrumentation.extractServerAttributes("http://example.com/foo/bar?a=b"))
137+
.hasSize(2)
138+
.containsEntry(SemConvAttributes.SERVER_ADDRESS, "example.com")
139+
.containsEntry(SemConvAttributes.SERVER_PORT, 80);
140+
141+
assertThat(
142+
ExporterInstrumentation.extractServerAttributes("http://example.com:1234/foo/bar?a=b"))
143+
.hasSize(2)
144+
.containsEntry(SemConvAttributes.SERVER_ADDRESS, "example.com")
145+
.containsEntry(SemConvAttributes.SERVER_PORT, 1234);
146+
}
147+
148+
@Test
149+
void serverAttributesUnknownScheme() {
150+
assertThat(ExporterInstrumentation.extractServerAttributes("custom://foo"))
151+
.hasSize(1)
152+
.containsEntry(SemConvAttributes.SERVER_ADDRESS, "foo");
153+
154+
assertThat(ExporterInstrumentation.extractServerAttributes("custom://foo:1234"))
155+
.hasSize(2)
156+
.containsEntry(SemConvAttributes.SERVER_ADDRESS, "foo")
157+
.containsEntry(SemConvAttributes.SERVER_PORT, 1234);
158+
}
106159
}

exporters/common/src/test/java/io/opentelemetry/exporter/internal/metrics/ServerAttributesUtilTest.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)