Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more args to RequestListener #4102

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> newBuil
private final List<? extends SpanLinksExtractor<? super REQUEST>> spanLinksExtractors;
private final List<? extends AttributesExtractor<? super REQUEST, ? super RESPONSE>>
attributesExtractors;
private final List<? extends RequestListener> requestListeners;
private final List<? extends RequestListener<REQUEST, RESPONSE>> requestListeners;
private final ErrorCauseExtractor errorCauseExtractor;
@Nullable private final StartTimeExtractor<REQUEST> startTimeExtractor;
@Nullable private final EndTimeExtractor<REQUEST, RESPONSE> endTimeExtractor;
Expand Down Expand Up @@ -144,8 +144,8 @@ public Context start(Context parentContext, REQUEST request) {

Context context = parentContext;

for (RequestListener requestListener : requestListeners) {
context = requestListener.start(context, attributes);
for (RequestListener<REQUEST, RESPONSE> requestListener : requestListeners) {
context = requestListener.start(context, attributes, request);
}

spanBuilder.setAllAttributes(attributes);
Expand Down Expand Up @@ -177,8 +177,8 @@ public void end(
Attributes attributes = attributesBuilder;
span.setAllAttributes(attributes);

for (RequestListener requestListener : requestListeners) {
requestListener.end(context, attributes);
for (RequestListener<REQUEST, RESPONSE> requestListener : requestListeners) {
requestListener.end(context, attributes, request, response, error);
}

StatusCode statusCode = spanStatusExtractor.extract(request, response, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class InstrumenterBuilder<REQUEST, RESPONSE> {
final List<SpanLinksExtractor<? super REQUEST>> spanLinksExtractors = new ArrayList<>();
final List<AttributesExtractor<? super REQUEST, ? super RESPONSE>> attributesExtractors =
new ArrayList<>();
final List<RequestListener> requestListeners = new ArrayList<>();
final List<RequestListener<REQUEST, RESPONSE>> requestListeners = new ArrayList<>();

SpanKindExtractor<? super REQUEST> spanKindExtractor = SpanKindExtractor.alwaysInternal();
SpanStatusExtractor<? super REQUEST, ? super RESPONSE> spanStatusExtractor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.context.Context;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A listener of the start and end of a request. Instrumented libraries will call {@link
* #start(Context, Attributes)} as early as possible in the processing of a request and {@link
* #end(Context, Attributes)} as late as possible when finishing the request. These correspond to
* the start and end of a span when tracing.
* #start(Context, Attributes, Object)} as early as possible in the processing of a request and
* {@link #end(Context, Attributes, Object, Object, Throwable)} as late as possible when finishing
* the request. These correspond to the start and end of a span when tracing.
*/
public interface RequestListener {
public interface RequestListener<REQUEST, RESPONSE> {

/**
* Listener method that is called at the start of a request. If any state needs to be kept between
* the start and end of the request, e.g., an in-progress span, it should be added to the passed
* in {@link Context} and returned.
*/
Context start(Context context, Attributes startAttributes);
Context start(Context context, Attributes startAttributes, REQUEST request);

/** Listener method that is called at the end of a request. */
void end(Context context, Attributes endAttributes);
void end(
Context context,
Attributes endAttributes,
REQUEST request,
@Nullable RESPONSE response,
@Nullable Throwable error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
@UnstableApi
public interface RequestMetrics {
/** Returns a {@link RequestListener} for recording metrics using the given {@link Meter}. */
RequestListener create(Meter meter);
<REQUEST, RESPONSE> RequestListener<REQUEST, RESPONSE> create(Meter meter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.RequestListener;
import io.opentelemetry.instrumentation.api.instrumenter.RequestMetrics;
import java.util.concurrent.TimeUnit;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,7 +30,8 @@
* dependencies.
*/
@UnstableApi
public final class HttpClientMetrics implements RequestListener {
public final class HttpClientMetrics<REQUEST, RESPONSE>
implements RequestListener<REQUEST, RESPONSE> {

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

Expand Down Expand Up @@ -60,7 +62,7 @@ private HttpClientMetrics(Meter meter) {
}

@Override
public Context start(Context context, Attributes startAttributes) {
public Context start(Context context, Attributes startAttributes, REQUEST request) {
long startTimeNanos = System.nanoTime();

return context.with(
Expand All @@ -69,7 +71,12 @@ public Context start(Context context, Attributes startAttributes) {
}

@Override
public void end(Context context, Attributes endAttributes) {
public void end(
Context context,
Attributes endAttributes,
REQUEST request,
@Nullable RESPONSE response,
@Nullable Throwable error) {
State state = context.get(HTTP_CLIENT_REQUEST_METRICS_STATE);
if (state == null) {
logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.RequestListener;
import io.opentelemetry.instrumentation.api.instrumenter.RequestMetrics;
import java.util.concurrent.TimeUnit;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,7 +32,8 @@
* dependencies.
*/
@UnstableApi
public final class HttpServerMetrics implements RequestListener {
public final class HttpServerMetrics<REQUEST, RESPONSE>
implements RequestListener<REQUEST, RESPONSE> {

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

Expand Down Expand Up @@ -70,7 +72,7 @@ private HttpServerMetrics(Meter meter) {
}

@Override
public Context start(Context context, Attributes startAttributes) {
public Context start(Context context, Attributes startAttributes, REQUEST request) {
long startTimeNanos = System.nanoTime();
activeRequests.add(1, applyActiveRequestsView(startAttributes));

Expand All @@ -80,7 +82,12 @@ public Context start(Context context, Attributes startAttributes) {
}

@Override
public void end(Context context, Attributes endAttributes) {
public void end(
Context context,
Attributes endAttributes,
REQUEST request,
@Nullable RESPONSE response,
@Nullable Throwable error) {
State state = context.get(HTTP_SERVER_REQUEST_METRICS_STATE);
if (state == null) {
logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ void collectsMetrics() {
.put("http.status_code", 200)
.build();

Context context1 = listener.start(Context.current(), requestAttributes);
Context context1 = listener.start(Context.current(), requestAttributes, null);

Collection<MetricData> metrics = meterProvider.collectAllMetrics();
assertThat(metrics).isEmpty();

Context context2 = listener.start(Context.current(), requestAttributes);
Context context2 = listener.start(Context.current(), requestAttributes, null);

metrics = meterProvider.collectAllMetrics();
assertThat(metrics).isEmpty();

listener.end(context1, responseAttributes);
listener.end(context1, responseAttributes, null, null, null);

metrics = meterProvider.collectAllMetrics();
assertThat(metrics).hasSize(1);
Expand All @@ -75,7 +75,7 @@ void collectsMetrics() {
attributeEntry("net.host.port", 1234L));
}));

listener.end(context2, responseAttributes);
listener.end(context2, responseAttributes, null, null, null);

metrics = meterProvider.collectAllMetrics();
assertThat(metrics).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void collectsMetrics() {
.put("http.status_code", 200)
.build();

Context context1 = listener.start(Context.current(), requestAttributes);
Context context1 = listener.start(Context.current(), requestAttributes, null);

Collection<MetricData> metrics = meterProvider.collectAllMetrics();
assertThat(metrics).hasSize(1);
Expand All @@ -65,7 +65,7 @@ void collectsMetrics() {
attributeEntry("http.method", "GET"),
attributeEntry("http.scheme", "https"))));

Context context2 = listener.start(Context.current(), requestAttributes);
Context context2 = listener.start(Context.current(), requestAttributes, null);

metrics = meterProvider.collectAllMetrics();
assertThat(metrics).hasSize(1);
Expand All @@ -78,7 +78,7 @@ void collectsMetrics() {
.points()
.satisfiesExactly(point -> assertThat(point).hasValue(2)));

listener.end(context1, responseAttributes);
listener.end(context1, responseAttributes, null, null, null);

metrics = meterProvider.collectAllMetrics();
assertThat(metrics).hasSize(2);
Expand Down Expand Up @@ -110,7 +110,7 @@ void collectsMetrics() {
attributeEntry("net.host.port", 1234L));
}));

listener.end(context2, responseAttributes);
listener.end(context2, responseAttributes, null, null, null);

metrics = meterProvider.collectAllMetrics();
assertThat(metrics).hasSize(2);
Expand Down