Skip to content
Closed
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 @@ -5,9 +5,14 @@

package io.opentelemetry.javaagent.bootstrap.executors;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.DoubleHistogram;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.internal.ContextPropagationDebug;
import java.util.concurrent.TimeUnit;

public final class ContextPropagatingRunnable implements Runnable {

Expand All @@ -23,17 +28,31 @@ public static Runnable propagateContext(Runnable task, Context context) {
return new ContextPropagatingRunnable(task, context);
}

private static final double NANOS_PER_S = TimeUnit.SECONDS.toNanos(1);
private final Runnable delegate;
private final Context context;
private final Long startObservation;
private final DoubleHistogram pendingTimeHistogram;

private ContextPropagatingRunnable(Runnable delegate, Context context) {
this.delegate = delegate;
this.context = ContextPropagationDebug.addDebugInfo(context, delegate);
this.startObservation = System.nanoTime();

this.pendingTimeHistogram =
GlobalOpenTelemetry.getMeter("thread.pending.duration")
.histogramBuilder("thread.pending.duration")
.setUnit("s")
.setDescription("Duration of HTTP client requests.")
.build();
}

@Override
public void run() {
try (Scope ignored = context.makeCurrent()) {
pendingTimeHistogram.record(
System.nanoTime() - startObservation / NANOS_PER_S,
Attributes.of(AttributeKey.stringKey("thread"), Thread.currentThread().getName()));
delegate.run();
}
}
Expand Down
Loading