Skip to content

Commit

Permalink
Add InstrumenterBuilder.addRequestListener (open-telemetry#5655)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored and RashmiRam committed May 23, 2022
1 parent 8f7e3bb commit 4adae5d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,11 @@ public void end(

if (!requestListeners.isEmpty() || !requestMetricListeners.isEmpty()) {
long endNanos = getNanos(endTime);
for (RequestListener requestListener : requestListeners) {
// TODO (trask) call end in the reverse order that start was called?
for (RequestListener requestListener : requestMetricListeners) {
requestListener.end(context, attributes, endNanos);
}
for (RequestListener requestListener : requestMetricListeners) {
for (RequestListener requestListener : requestListeners) {
requestListener.end(context, attributes, endNanos);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ public InstrumenterBuilder<REQUEST, RESPONSE> addContextCustomizer(
return this;
}

/** Adds a {@link RequestMetrics} whose metrics will be recorded for request start and stop. */
/** Adds a {@link RequestListener} which will be called for request start and end. */
public InstrumenterBuilder<REQUEST, RESPONSE> addRequestListener(RequestListener listener) {
requestListeners.add(listener);
return this;
}

/** Adds a {@link RequestMetrics} whose metrics will be recorded for request start and end. */
@UnstableApi
public InstrumenterBuilder<REQUEST, RESPONSE> addRequestMetrics(RequestMetrics factory) {
requestMetricListeners.add(factory.create(meter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,38 @@ void client_parent() {
.hasParentSpanId("090a0b0c0d0e0f00")));
}

@Test
void requestListeners() {
AtomicReference<Boolean> startContext = new AtomicReference<>();
AtomicReference<Boolean> endContext = new AtomicReference<>();

RequestListener requestListener =
new RequestListener() {
@Override
public Context start(Context context, Attributes startAttributes, long startNanos) {
startContext.set(true);
return context;
}

@Override
public void end(Context context, Attributes endAttributes, long endNanos) {
endContext.set(true);
}
};

Instrumenter<Map<String, String>, Map<String, String>> instrumenter =
Instrumenter.<Map<String, String>, Map<String, String>>builder(
otelTesting.getOpenTelemetry(), "test", unused -> "span")
.addRequestListener(requestListener)
.newServerInstrumenter(new MapGetter());

Context context = instrumenter.start(Context.root(), REQUEST);
instrumenter.end(context, REQUEST, RESPONSE, null);

assertThat(startContext.get()).isTrue();
assertThat(endContext.get()).isTrue();
}

@Test
void requestMetrics() {
AtomicReference<Context> startContext = new AtomicReference<>();
Expand Down

0 comments on commit 4adae5d

Please sign in to comment.