-
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.
* Fix metrics cardinality * Add test
- Loading branch information
Showing
5 changed files
with
134 additions
and
7 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
77 changes: 77 additions & 0 deletions
77
...ain/java/io/opentelemetry/instrumentation/api/instrumenter/http/TemporaryMetricsView.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,77 @@ | ||
/* | ||
* 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.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
|
||
// this is temporary, see | ||
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/3962#issuecomment-906606325 | ||
@SuppressWarnings("rawtypes") | ||
final class TemporaryMetricsView { | ||
|
||
private static final Set<AttributeKey> durationView = buildDurationView(); | ||
|
||
private static final Set<AttributeKey> activeRequestsView = buildActiveRequestsView(); | ||
|
||
private static Set<AttributeKey> buildDurationView() { | ||
// the list of included metrics is from | ||
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/http-metrics.md#attributes | ||
Set<AttributeKey> view = new HashSet<>(); | ||
view.add(SemanticAttributes.HTTP_METHOD); | ||
view.add(SemanticAttributes.HTTP_HOST); | ||
view.add(SemanticAttributes.HTTP_SCHEME); | ||
view.add(SemanticAttributes.HTTP_STATUS_CODE); | ||
view.add(SemanticAttributes.HTTP_FLAVOR); | ||
view.add(SemanticAttributes.NET_PEER_NAME); | ||
view.add(SemanticAttributes.NET_PEER_PORT); | ||
view.add(SemanticAttributes.NET_PEER_IP); | ||
view.add(SemanticAttributes.HTTP_SERVER_NAME); | ||
view.add(SemanticAttributes.NET_HOST_NAME); | ||
view.add(SemanticAttributes.NET_HOST_PORT); | ||
return view; | ||
} | ||
|
||
private static Set<AttributeKey> buildActiveRequestsView() { | ||
// the list of included metrics is from | ||
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/http-metrics.md#attributes | ||
Set<AttributeKey> view = new HashSet<>(); | ||
view.add(SemanticAttributes.HTTP_METHOD); | ||
view.add(SemanticAttributes.HTTP_HOST); | ||
view.add(SemanticAttributes.HTTP_SCHEME); | ||
view.add(SemanticAttributes.HTTP_FLAVOR); | ||
view.add(SemanticAttributes.HTTP_SERVER_NAME); | ||
return view; | ||
} | ||
|
||
static Attributes applyDurationView(Attributes attributes) { | ||
return applyView(attributes, durationView); | ||
} | ||
|
||
static Attributes applyActiveRequestsView(Attributes attributes) { | ||
return applyView(attributes, activeRequestsView); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static Attributes applyView(Attributes attributes, Set<AttributeKey> view) { | ||
AttributesBuilder filtered = Attributes.builder(); | ||
attributes.forEach( | ||
(BiConsumer<AttributeKey, Object>) | ||
(key, value) -> { | ||
if (view.contains(key)) { | ||
filtered.put(key, value); | ||
} | ||
}); | ||
return filtered.build(); | ||
} | ||
|
||
private TemporaryMetricsView() {} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
...java/io/opentelemetry/instrumentation/api/instrumenter/http/TemporaryMetricsViewTest.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,45 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter.http; | ||
|
||
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyActiveRequestsView; | ||
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyDurationView; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions; | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TemporaryMetricsViewTest { | ||
|
||
@Test | ||
public void shouldApplyDurationView() { | ||
Attributes attributes = | ||
Attributes.builder() | ||
.put(SemanticAttributes.HTTP_METHOD, "GET") | ||
.put(SemanticAttributes.HTTP_URL, "http://somehost/high/cardinality/12345") | ||
.put(SemanticAttributes.NET_PEER_NAME, "somehost") | ||
.build(); | ||
|
||
OpenTelemetryAssertions.assertThat(applyDurationView(attributes)) | ||
.containsOnly( | ||
attributeEntry("http.method", "GET"), attributeEntry("net.peer.name", "somehost")); | ||
} | ||
|
||
@Test | ||
public void shouldApplyActiveRequestsView() { | ||
Attributes attributes = | ||
Attributes.builder() | ||
.put(SemanticAttributes.HTTP_METHOD, "GET") | ||
.put(SemanticAttributes.HTTP_URL, "/high/cardinality/12345") | ||
.put(SemanticAttributes.NET_PEER_NAME, "somehost") | ||
.build(); | ||
|
||
OpenTelemetryAssertions.assertThat(applyActiveRequestsView(attributes)) | ||
.containsOnly(attributeEntry("http.method", "GET")); | ||
} | ||
} |