-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract HTTP server experimental metrics to a separate class (#9259)
- Loading branch information
Mateusz Rzeszutek
committed
Aug 30, 2023
1 parent
bcd693f
commit 133de24
Showing
30 changed files
with
956 additions
and
447 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
112 changes: 112 additions & 0 deletions
112
...io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerExperimentalMetrics.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,112 @@ | ||
/* | ||
* 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.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 java.util.logging.Level.FINE; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.LongHistogram; | ||
import io.opentelemetry.api.metrics.LongUpDownCounter; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; | ||
import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* {@link OperationListener} which keeps track of <a | ||
* href="https://github.com/open-telemetry/semantic-conventions/blob/main/specification/metrics/semantic_conventions/http-metrics.md#http-server">non-stable | ||
* HTTP server metrics</a>: <a | ||
* href="https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-metrics.md#metric-httpserveractive_requests">the | ||
* number of in-flight request</a>, <a | ||
* href="https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-metrics.md#metric-httpserverrequestsize">the | ||
* request size</a> and <a | ||
* href="https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-metrics.md#metric-httpserverresponsesize">the | ||
* response size</a>. | ||
*/ | ||
public final class HttpServerExperimentalMetrics implements OperationListener { | ||
|
||
private static final ContextKey<Attributes> HTTP_SERVER_EXPERIMENTAL_METRICS_START_ATTRIBUTES = | ||
ContextKey.named("http-server-experimental-metrics-start-attributes"); | ||
|
||
private static final Logger logger = | ||
Logger.getLogger(HttpServerExperimentalMetrics.class.getName()); | ||
|
||
/** | ||
* Returns a {@link OperationMetrics} which can be used to enable recording of {@link | ||
* HttpServerExperimentalMetrics} on an {@link | ||
* io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder}. | ||
*/ | ||
public static OperationMetrics get() { | ||
return HttpServerExperimentalMetrics::new; | ||
} | ||
|
||
private final LongUpDownCounter activeRequests; | ||
private final LongHistogram requestSize; | ||
private final LongHistogram responseSize; | ||
|
||
private HttpServerExperimentalMetrics(Meter meter) { | ||
activeRequests = | ||
meter | ||
.upDownCounterBuilder("http.server.active_requests") | ||
.setUnit("{requests}") | ||
.setDescription("The number of concurrent HTTP requests that are currently in-flight") | ||
.build(); | ||
requestSize = | ||
meter | ||
.histogramBuilder("http.server.request.size") | ||
.setUnit("By") | ||
.setDescription("The size of HTTP request messages") | ||
.ofLongs() | ||
.build(); | ||
responseSize = | ||
meter | ||
.histogramBuilder("http.server.response.size") | ||
.setUnit("By") | ||
.setDescription("The size of HTTP response messages") | ||
.ofLongs() | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Context onStart(Context context, Attributes startAttributes, long startNanos) { | ||
activeRequests.add(1, applyActiveRequestsView(startAttributes), context); | ||
|
||
return context.with(HTTP_SERVER_EXPERIMENTAL_METRICS_START_ATTRIBUTES, startAttributes); | ||
} | ||
|
||
@Override | ||
public void onEnd(Context context, Attributes endAttributes, long endNanos) { | ||
Attributes startAttributes = context.get(HTTP_SERVER_EXPERIMENTAL_METRICS_START_ATTRIBUTES); | ||
if (startAttributes == null) { | ||
logger.log( | ||
FINE, | ||
"No state present when ending context {0}. Cannot record HTTP request metrics.", | ||
context); | ||
return; | ||
} | ||
// it's important to use exactly the same attributes that were used when incrementing the active | ||
// request count (otherwise it will split the timeseries) | ||
activeRequests.add(-1, applyActiveRequestsView(startAttributes), context); | ||
|
||
Attributes sizeAttributes = applyServerDurationAndSizeView(startAttributes, endAttributes); | ||
|
||
Long requestBodySize = getHttpRequestBodySize(endAttributes, startAttributes); | ||
if (requestBodySize != null) { | ||
requestSize.record(requestBodySize, sizeAttributes, context); | ||
} | ||
|
||
Long responseBodySize = getHttpResponseBodySize(endAttributes, startAttributes); | ||
if (responseBodySize != null) { | ||
responseSize.record(responseBodySize, sizeAttributes, context); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.