Skip to content

Commit

Permalink
Emit both old and new metrics under dup setting (#9320)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Aug 30, 2023
1 parent f0f86a4 commit e243da4
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMessageBodySizeUtil.getHttpRequestBodySize;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMessageBodySizeUtil.getHttpResponseBodySize;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyClientDurationAndSizeView;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyClientRequestSizeView;
import static java.util.logging.Level.FINE;

import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -81,7 +81,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
return;
}

Attributes sizeAttributes = applyClientDurationAndSizeView(startAttributes, endAttributes);
Attributes sizeAttributes = applyClientRequestSizeView(startAttributes, endAttributes);

Long requestBodySize = getHttpRequestBodySize(endAttributes, startAttributes);
if (requestBodySize != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

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

import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.createDurationHistogram;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.nanosToUnit;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyClientDurationAndSizeView;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.createStableDurationHistogram;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyOldClientDurationView;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyStableClientDurationView;
import static java.util.logging.Level.FINE;

import com.google.auto.value.AutoValue;
Expand All @@ -18,7 +18,10 @@
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/**
* {@link OperationListener} which keeps track of <a
Expand All @@ -27,6 +30,9 @@
*/
public final class HttpClientMetrics implements OperationListener {

private static final double NANOS_PER_MS = TimeUnit.MILLISECONDS.toNanos(1);
private static final double NANOS_PER_S = TimeUnit.SECONDS.toNanos(1);

private static final ContextKey<State> HTTP_CLIENT_REQUEST_METRICS_STATE =
ContextKey.named("http-client-metrics-state");

Expand All @@ -41,16 +47,27 @@ public static OperationMetrics get() {
return HttpClientMetrics::new;
}

private final DoubleHistogram duration;
@Nullable private final DoubleHistogram stableDuration;
@Nullable private final DoubleHistogram oldDuration;

private HttpClientMetrics(Meter meter) {
String durationInstrumentName =
HttpMetricsUtil.emitNewSemconvMetrics
? "http.client.request.duration"
: "http.client.duration";
duration =
createDurationHistogram(
meter, durationInstrumentName, "The duration of the outbound HTTP request");
if (SemconvStability.emitStableHttpSemconv()) {
stableDuration =
createStableDurationHistogram(
meter, "http.client.request.duration", "The duration of the outbound HTTP request");
} else {
stableDuration = null;
}
if (SemconvStability.emitOldHttpSemconv()) {
oldDuration =
meter
.histogramBuilder("http.client.duration")
.setUnit("ms")
.setDescription("The duration of the outbound HTTP request")
.build();
} else {
oldDuration = null;
}
}

@Override
Expand All @@ -71,10 +88,19 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
return;
}

Attributes durationAndSizeAttributes =
applyClientDurationAndSizeView(state.startAttributes(), endAttributes);
duration.record(
nanosToUnit(endNanos - state.startTimeNanos()), durationAndSizeAttributes, context);
if (stableDuration != null) {
Attributes stableDurationAttributes =
applyStableClientDurationView(state.startAttributes(), endAttributes);
stableDuration.record(
(endNanos - state.startTimeNanos()) / NANOS_PER_S, stableDurationAttributes, context);
}

if (oldDuration != null) {
Attributes stableDurationAttributes =
applyOldClientDurationView(state.startAttributes(), endAttributes);
oldDuration.record(
(endNanos - state.startTimeNanos()) / NANOS_PER_MS, stableDurationAttributes, context);
}
}

@AutoValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,27 @@
import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import java.util.List;
import java.util.concurrent.TimeUnit;

final class HttpMetricsUtil {

// we'll use the old unit if the old semconv is in use
static final boolean emitNewSemconvMetrics =
SemconvStability.emitStableHttpSemconv() && !SemconvStability.emitOldHttpSemconv();

static final List<Double> DURATION_SECONDS_BUCKETS =
unmodifiableList(
asList(
0.0, 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5,
10.0));

private static final double NANOS_PER_MS = TimeUnit.MILLISECONDS.toNanos(1);
private static final double NANOS_PER_S = TimeUnit.SECONDS.toNanos(1);

static DoubleHistogram createDurationHistogram(Meter meter, String name, String description) {
static DoubleHistogram createStableDurationHistogram(
Meter meter, String name, String description) {
DoubleHistogramBuilder durationBuilder =
meter
.histogramBuilder(name)
.setUnit(emitNewSemconvMetrics ? "s" : "ms")
.setDescription(description);
meter.histogramBuilder(name).setUnit("s").setDescription(description);
// don't set custom buckets if milliseconds are still used
if (emitNewSemconvMetrics && durationBuilder instanceof ExtendedDoubleHistogramBuilder) {
if (durationBuilder instanceof ExtendedDoubleHistogramBuilder) {
((ExtendedDoubleHistogramBuilder) durationBuilder)
.setAdvice(advice -> advice.setExplicitBucketBoundaries(DURATION_SECONDS_BUCKETS));
}
return durationBuilder.build();
}

static double nanosToUnit(long durationNanos) {
return durationNanos / (emitNewSemconvMetrics ? NANOS_PER_S : NANOS_PER_MS);
}

private HttpMetricsUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMessageBodySizeUtil.getHttpRequestBodySize;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMessageBodySizeUtil.getHttpResponseBodySize;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyActiveRequestsView;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyServerDurationAndSizeView;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyServerRequestSizeView;
import static java.util.logging.Level.FINE;

import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -97,7 +97,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
// request count (otherwise it will split the timeseries)
activeRequests.add(-1, applyActiveRequestsView(startAttributes), context);

Attributes sizeAttributes = applyServerDurationAndSizeView(startAttributes, endAttributes);
Attributes sizeAttributes = applyServerRequestSizeView(startAttributes, endAttributes);

Long requestBodySize = getHttpRequestBodySize(endAttributes, startAttributes);
if (requestBodySize != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

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

import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.createDurationHistogram;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.nanosToUnit;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyServerDurationAndSizeView;
import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpMetricsUtil.createStableDurationHistogram;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyOldServerDurationView;
import static io.opentelemetry.instrumentation.api.instrumenter.http.TemporaryMetricsView.applyStableServerDurationView;
import static java.util.logging.Level.FINE;

import com.google.auto.value.AutoValue;
Expand All @@ -18,7 +18,10 @@
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/**
* {@link OperationListener} which keeps track of <a
Expand All @@ -27,6 +30,9 @@
*/
public final class HttpServerMetrics implements OperationListener {

private static final double NANOS_PER_MS = TimeUnit.MILLISECONDS.toNanos(1);
private static final double NANOS_PER_S = TimeUnit.SECONDS.toNanos(1);

private static final ContextKey<State> HTTP_SERVER_METRICS_STATE =
ContextKey.named("http-server-metrics-state");

Expand All @@ -41,16 +47,27 @@ public static OperationMetrics get() {
return HttpServerMetrics::new;
}

private final DoubleHistogram duration;
@Nullable private final DoubleHistogram stableDuration;
@Nullable private final DoubleHistogram oldDuration;

private HttpServerMetrics(Meter meter) {
String durationInstrumentName =
HttpMetricsUtil.emitNewSemconvMetrics
? "http.server.request.duration"
: "http.server.duration";
duration =
createDurationHistogram(
meter, durationInstrumentName, "The duration of the inbound HTTP request");
if (SemconvStability.emitStableHttpSemconv()) {
stableDuration =
createStableDurationHistogram(
meter, "http.server.request.duration", "The duration of the inbound HTTP request");
} else {
stableDuration = null;
}
if (SemconvStability.emitOldHttpSemconv()) {
oldDuration =
meter
.histogramBuilder("http.server.duration")
.setUnit("ms")
.setDescription("The duration of the inbound HTTP request")
.build();
} else {
oldDuration = null;
}
}

@Override
Expand All @@ -70,10 +87,20 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) {
context);
return;
}
Attributes durationAndSizeAttributes =
applyServerDurationAndSizeView(state.startAttributes(), endAttributes);
duration.record(
nanosToUnit(endNanos - state.startTimeNanos()), durationAndSizeAttributes, context);

if (stableDuration != null) {
Attributes stableDurationAttributes =
applyStableServerDurationView(state.startAttributes(), endAttributes);
stableDuration.record(
(endNanos - state.startTimeNanos()) / NANOS_PER_S, stableDurationAttributes, context);
}

if (oldDuration != null) {
Attributes stableDurationAttributes =
applyOldServerDurationView(state.startAttributes(), endAttributes);
oldDuration.record(
(endNanos - state.startTimeNanos()) / NANOS_PER_MS, stableDurationAttributes, context);
}
}

@AutoValue
Expand Down
Loading

0 comments on commit e243da4

Please sign in to comment.