Skip to content

Commit

Permalink
Update to SDK 1.5.0 (#3847)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuraag Agrawal authored Aug 17, 2021
1 parent 706bfa2 commit d8eae49
Show file tree
Hide file tree
Showing 51 changed files with 1,226 additions and 1,750 deletions.
2 changes: 1 addition & 1 deletion dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class DependencySet(val group: String, val version: String, val modules: Li
val dependencyVersions = hashMapOf<String, String>()
rootProject.extra["versions"] = dependencyVersions

val otelVersion = "1.4.1"
val otelVersion = "1.5.0"
rootProject.extra["otelVersion"] = otelVersion

// Need both BOM and -all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.DoubleValueRecorder;
import io.opentelemetry.api.metrics.DoubleHistogram;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.common.Labels;
import io.opentelemetry.api.metrics.common.LabelsBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.annotations.UnstableApi;
Expand Down Expand Up @@ -48,12 +46,12 @@ public static RequestMetrics get() {
return HttpClientMetrics::new;
}

private final DoubleValueRecorder duration;
private final DoubleHistogram duration;

private HttpClientMetrics(Meter meter) {
duration =
meter
.doubleValueRecorderBuilder("http.client.duration")
.histogramBuilder("http.client.duration")
.setUnit("milliseconds")
.setDescription("The duration of the outbound HTTP request")
.build();
Expand All @@ -62,11 +60,10 @@ private HttpClientMetrics(Meter meter) {
@Override
public Context start(Context context, Attributes requestAttributes) {
long startTimeNanos = System.nanoTime();
Labels durationLabels = durationLabels(requestAttributes);

return context.with(
HTTP_CLIENT_REQUEST_METRICS_STATE,
new AutoValue_HttpClientMetrics_State(durationLabels, startTimeNanos));
new AutoValue_HttpClientMetrics_State(requestAttributes, startTimeNanos));
}

@Override
Expand All @@ -78,41 +75,13 @@ public void end(Context context, Attributes responseAttributes) {
return;
}
duration.record(
(System.nanoTime() - state.startTimeNanos()) / NANOS_PER_MS, state.durationLabels());
}

private static Labels durationLabels(Attributes attributes) {
LabelsBuilder labels = Labels.builder();
attributes.forEach(
(key, value) -> {
switch (key.getKey()) {
case "http.method":
case "http.host":
case "http.scheme":
case "http.flavor":
case "http.server_name":
case "net.host.name":
if (value instanceof String) {
labels.put(key.getKey(), (String) value);
}
break;
case "http.status_code":
case "net.host.port":
if (value instanceof Long) {
labels.put(key.getKey(), Long.toString((long) value));
}
break;
default:
// fall through
}
});
return labels.build();
(System.nanoTime() - state.startTimeNanos()) / NANOS_PER_MS, state.startAttributes());
}

@AutoValue
abstract static class State {

abstract Labels durationLabels();
abstract Attributes startAttributes();

abstract long startTimeNanos();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
package io.opentelemetry.instrumentation.api.instrumenter.http;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.DoubleValueRecorder;
import io.opentelemetry.api.metrics.DoubleHistogram;
import io.opentelemetry.api.metrics.LongUpDownCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.common.Labels;
import io.opentelemetry.api.metrics.common.LabelsBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.annotations.UnstableApi;
Expand Down Expand Up @@ -51,19 +48,19 @@ public static RequestMetrics get() {
}

private final LongUpDownCounter activeRequests;
private final DoubleValueRecorder duration;
private final DoubleHistogram duration;

private HttpServerMetrics(Meter meter) {
activeRequests =
meter
.longUpDownCounterBuilder("http.server.active_requests")
.upDownCounterBuilder("http.server.active_requests")
.setUnit("requests")
.setDescription("The number of concurrent HTTP requests that are currently in-flight")
.build();

duration =
meter
.doubleValueRecorderBuilder("http.server.duration")
.histogramBuilder("http.server.duration")
.setUnit("milliseconds")
.setDescription("The duration of the inbound HTTP request")
.build();
Expand All @@ -72,13 +69,11 @@ private HttpServerMetrics(Meter meter) {
@Override
public Context start(Context context, Attributes requestAttributes) {
long startTimeNanos = System.nanoTime();
Labels activeRequestLabels = activeRequestLabels(requestAttributes);
Labels durationLabels = durationLabels(requestAttributes);
activeRequests.add(1, activeRequestLabels);
activeRequests.add(1, requestAttributes);

return context.with(
HTTP_SERVER_REQUEST_METRICS_STATE,
new AutoValue_HttpServerMetrics_State(activeRequestLabels, durationLabels, startTimeNanos));
new AutoValue_HttpServerMetrics_State(requestAttributes, startTimeNanos));
}

@Override
Expand All @@ -89,67 +84,15 @@ public void end(Context context, Attributes responseAttributes) {
"No state present when ending context {}. Cannot reset HTTP request metrics.", context);
return;
}
activeRequests.add(-1, state.activeRequestLabels());
activeRequests.add(-1, state.startAttributes());
duration.record(
(System.nanoTime() - state.startTimeNanos()) / NANOS_PER_MS, state.durationLabels());
}

private static Labels activeRequestLabels(Attributes attributes) {
LabelsBuilder labels = Labels.builder();
attributes.forEach(
(key, value) -> {
if (key.getType() != AttributeType.STRING) {
return;
}
switch (key.getKey()) {
case "http.method":
case "http.host":
case "http.scheme":
case "http.flavor":
case "http.server_name":
labels.put(key.getKey(), (String) value);
break;
default:
// fall through
}
});
return labels.build();
}

private static Labels durationLabels(Attributes attributes) {
LabelsBuilder labels = Labels.builder();
attributes.forEach(
(key, value) -> {
switch (key.getKey()) {
case "http.method":
case "http.host":
case "http.scheme":
case "http.flavor":
case "http.server_name":
case "net.host.name":
if (value instanceof String) {
labels.put(key.getKey(), (String) value);
}
break;
case "http.status_code":
case "net.host.port":
if (value instanceof Long) {
labels.put(key.getKey(), Long.toString((long) value));
}
break;
default:
// fall through
}
});
return labels.build();
(System.nanoTime() - state.startTimeNanos()) / NANOS_PER_MS, state.startAttributes());
}

@AutoValue
abstract static class State {

abstract Labels activeRequestLabels();

abstract Labels durationLabels();
abstract Attributes startAttributes();

abstract long startTimeNanos();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

package io.opentelemetry.instrumentation.api.instrumenter.http;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry;
import static io.opentelemetry.sdk.testing.assertj.metrics.MetricAssertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.RequestListener;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.data.DoubleSummaryPointData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import java.util.Collection;
import org.junit.jupiter.api.Test;
Expand All @@ -33,8 +31,6 @@ void collectsMetrics() {
.put("http.scheme", "https")
.put("net.host.name", "localhost")
.put("net.host.port", 1234)
.put("rpc.service", "unused")
.put("rpc.method", "unused")
.build();

// Currently ignored.
Expand All @@ -61,33 +57,36 @@ void collectsMetrics() {
assertThat(metrics).hasSize(1);
assertThat(metrics)
.anySatisfy(
metric -> {
assertThat(metric.getName()).isEqualTo("http.client.duration");
assertThat(metric.getDoubleSummaryData().getPoints()).hasSize(1);
DoubleSummaryPointData data =
metric.getDoubleSummaryData().getPoints().stream().findFirst().get();
assertThat(data.getAttributes().asMap())
.containsOnly(
entry(stringKey("http.host"), "host"),
entry(stringKey("http.method"), "GET"),
entry(stringKey("http.scheme"), "https"),
entry(stringKey("net.host.name"), "localhost"),
entry(stringKey("net.host.port"), "1234"));
assertThat(data.getPercentileValues()).isNotEmpty();
});
metric ->
assertThat(metric)
.hasName("http.client.duration")
.hasDoubleSummary()
.points()
.satisfiesExactly(
point -> {
assertThat(point.getPercentileValues()).isNotEmpty();
assertThat(point)
.attributes()
.containsOnly(
attributeEntry("http.host", "host"),
attributeEntry("http.method", "GET"),
attributeEntry("http.scheme", "https"),
attributeEntry("net.host.name", "localhost"),
attributeEntry("net.host.port", 1234L));
}));

listener.end(context2, responseAttributes);

metrics = meterProvider.collectAllMetrics();
assertThat(metrics).hasSize(1);
assertThat(metrics)
.anySatisfy(
metric -> {
assertThat(metric.getName()).isEqualTo("http.client.duration");
assertThat(metric.getDoubleSummaryData().getPoints()).hasSize(1);
DoubleSummaryPointData data =
metric.getDoubleSummaryData().getPoints().stream().findFirst().get();
assertThat(data.getPercentileValues()).isNotEmpty();
});
metric ->
assertThat(metric)
.hasName("http.client.duration")
.hasDoubleSummary()
.points()
.satisfiesExactly(
point -> assertThat(point.getPercentileValues()).isNotEmpty()));
}
}
Loading

0 comments on commit d8eae49

Please sign in to comment.