Skip to content

Commit d7a34f9

Browse files
Mateusz Rzeszutektrask
andauthored
Refactor and rename HttpClientResend and HttpRouteHolder (#9280)
Co-authored-by: Trask Stalnaker <[email protected]>
1 parent 2ebf413 commit d7a34f9

File tree

94 files changed

+649
-455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+649
-455
lines changed

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final class HttpClientAttributesExtractorBuilder<REQUEST, RESPONSE> {
3535
List<String> capturedRequestHeaders = emptyList();
3636
List<String> capturedResponseHeaders = emptyList();
3737
Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
38-
ToIntFunction<Context> resendCountIncrementer = HttpClientResend::getAndIncrement;
38+
ToIntFunction<Context> resendCountIncrementer = HttpClientResendCount::getAndIncrement;
3939

4040
HttpClientAttributesExtractorBuilder(
4141
HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter,
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
1212

1313
/** A helper that keeps track of the count of the HTTP request resend attempts. */
14-
public final class HttpClientResend {
14+
public final class HttpClientResendCount {
1515

16-
private static final ContextKey<HttpClientResend> KEY =
16+
private static final ContextKey<HttpClientResendCount> KEY =
1717
ContextKey.named("opentelemetry-http-client-resend-key");
18-
private static final AtomicIntegerFieldUpdater<HttpClientResend> resendsUpdater =
19-
AtomicIntegerFieldUpdater.newUpdater(HttpClientResend.class, "resends");
18+
private static final AtomicIntegerFieldUpdater<HttpClientResendCount> resendsUpdater =
19+
AtomicIntegerFieldUpdater.newUpdater(HttpClientResendCount.class, "resends");
2020

2121
/**
2222
* Initializes the HTTP request resend counter.
@@ -29,16 +29,20 @@ public static Context initialize(Context context) {
2929
if (context.get(KEY) != null) {
3030
return context;
3131
}
32-
return context.with(KEY, new HttpClientResend());
32+
return context.with(KEY, new HttpClientResendCount());
3333
}
3434

35+
/**
36+
* Returns the count of the already made attempts to send an HTTP request; 0 if this is the first
37+
* send attempt.
38+
*/
3539
public static int get(Context context) {
36-
HttpClientResend resend = context.get(KEY);
40+
HttpClientResendCount resend = context.get(KEY);
3741
return resend == null ? 0 : resend.resends;
3842
}
3943

4044
static int getAndIncrement(Context context) {
41-
HttpClientResend resend = context.get(KEY);
45+
HttpClientResendCount resend = context.get(KEY);
4246
if (resend == null) {
4347
return 0;
4448
}
@@ -48,5 +52,5 @@ static int getAndIncrement(Context context) {
4852
@SuppressWarnings("unused") // it actually is used by the resendsUpdater
4953
private volatile int resends = 0;
5054

51-
private HttpClientResend() {}
55+
private HttpClientResendCount() {}
5256
}

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpRouteBiGetter.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55

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

8-
import io.opentelemetry.context.Context;
9-
import javax.annotation.Nullable;
10-
11-
/** An interface for getting the {@code http.route} attribute. */
8+
/**
9+
* An interface for getting the {@code http.route} attribute.
10+
*
11+
* @deprecated This class is deprecated and will be removed in the 2.0 release. Use {@link
12+
* HttpServerRouteBiGetter} instead.
13+
*/
14+
@Deprecated
1215
@FunctionalInterface
13-
public interface HttpRouteBiGetter<T, U> {
14-
15-
/**
16-
* Returns the {@code http.route} attribute extracted from {@code context}, {@code arg1} and
17-
* {@code arg2}; or {@code null} if it was not found.
18-
*/
19-
@Nullable
20-
String get(Context context, T arg1, U arg2);
21-
}
16+
public interface HttpRouteBiGetter<T, U> extends HttpServerRouteBiGetter<T, U> {}

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpRouteGetter.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55

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

8-
import io.opentelemetry.context.Context;
9-
import javax.annotation.Nullable;
10-
11-
/** An interface for getting the {@code http.route} attribute. */
8+
/**
9+
* An interface for getting the {@code http.route} attribute.
10+
*
11+
* @deprecated This class is deprecated and will be removed in the 2.0 release. Use {@link
12+
* HttpServerRouteGetter} instead.
13+
*/
14+
@Deprecated
1215
@FunctionalInterface
13-
public interface HttpRouteGetter<T> {
14-
15-
/**
16-
* Returns the {@code http.route} attribute extracted from {@code context} and {@code arg}; or
17-
* {@code null} if it was not found.
18-
*/
19-
@Nullable
20-
String get(Context context, T arg);
21-
}
16+
public interface HttpRouteGetter<T> extends HttpServerRouteGetter<T> {}

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpRouteHolder.java

Lines changed: 8 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55

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

8-
import io.opentelemetry.api.trace.Span;
98
import io.opentelemetry.context.Context;
109
import io.opentelemetry.instrumentation.api.instrumenter.ContextCustomizer;
1110
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
12-
import io.opentelemetry.instrumentation.api.instrumenter.LocalRootSpan;
13-
import io.opentelemetry.instrumentation.api.internal.HttpRouteState;
14-
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
1511
import javax.annotation.Nullable;
1612

1713
/**
@@ -22,7 +18,11 @@
2218
* later, after the instrumented operation starts. This class provides several static methods that
2319
* allow the instrumentation author to provide the matching HTTP route to the instrumentation when
2420
* it is discovered.
21+
*
22+
* @deprecated This class is deprecated and will be removed in the 2.0 release. Use {@link
23+
* HttpServerRoute} instead.
2524
*/
25+
@Deprecated
2626
public final class HttpRouteHolder {
2727

2828
/**
@@ -31,13 +31,7 @@ public final class HttpRouteHolder {
3131
*/
3232
public static <REQUEST> ContextCustomizer<REQUEST> create(
3333
HttpServerAttributesGetter<REQUEST, ?> getter) {
34-
return (context, request, startAttributes) -> {
35-
if (HttpRouteState.fromContextOrNull(context) != null) {
36-
return context;
37-
}
38-
String method = getter.getHttpRequestMethod(request);
39-
return context.with(HttpRouteState.create(method, null, 0));
40-
};
34+
return HttpServerRoute.create(getter);
4135
}
4236

4337
private HttpRouteHolder() {}
@@ -57,7 +51,7 @@ private HttpRouteHolder() {}
5751
*/
5852
public static void updateHttpRoute(
5953
Context context, HttpRouteSource source, @Nullable String httpRoute) {
60-
updateHttpRoute(context, source, ConstantAdapter.INSTANCE, httpRoute);
54+
HttpServerRoute.update(context, source.toHttpServerRouteSource(), httpRoute);
6155
}
6256

6357
/**
@@ -75,7 +69,7 @@ public static void updateHttpRoute(
7569
*/
7670
public static <T> void updateHttpRoute(
7771
Context context, HttpRouteSource source, HttpRouteGetter<T> httpRouteGetter, T arg1) {
78-
updateHttpRoute(context, source, OneArgAdapter.getInstance(), arg1, httpRouteGetter);
72+
HttpServerRoute.update(context, source.toHttpServerRouteSource(), httpRouteGetter, arg1);
7973
}
8074

8175
/**
@@ -97,92 +91,6 @@ public static <T, U> void updateHttpRoute(
9791
HttpRouteBiGetter<T, U> httpRouteGetter,
9892
T arg1,
9993
U arg2) {
100-
Span serverSpan = LocalRootSpan.fromContextOrNull(context);
101-
// even if the server span is not sampled, we have to continue - we need to compute the
102-
// http.route properly so that it can be captured by the server metrics
103-
if (serverSpan == null) {
104-
return;
105-
}
106-
HttpRouteState httpRouteState = HttpRouteState.fromContextOrNull(context);
107-
if (httpRouteState == null) {
108-
// TODO: remove this branch?
109-
String httpRoute = httpRouteGetter.get(context, arg1, arg2);
110-
if (httpRoute != null && !httpRoute.isEmpty()) {
111-
// update just the attribute - without http.method we can't compute a proper span name here
112-
serverSpan.setAttribute(SemanticAttributes.HTTP_ROUTE, httpRoute);
113-
}
114-
return;
115-
}
116-
// special case for servlet filters, even when we have a route from previous filter see whether
117-
// the new route is better and if so use it instead
118-
boolean onlyIfBetterRoute =
119-
!source.useFirst && source.order == httpRouteState.getUpdatedBySourceOrder();
120-
if (source.order > httpRouteState.getUpdatedBySourceOrder() || onlyIfBetterRoute) {
121-
String route = httpRouteGetter.get(context, arg1, arg2);
122-
if (route != null
123-
&& !route.isEmpty()
124-
&& (!onlyIfBetterRoute || isBetterRoute(httpRouteState, route))) {
125-
126-
// update just the span name - the attribute will be picked up by the
127-
// HttpServerAttributesExtractor at the end of request processing
128-
updateSpanName(serverSpan, httpRouteState, route);
129-
130-
httpRouteState.update(context, source.order, route);
131-
}
132-
}
133-
}
134-
135-
// This is used when setting route from a servlet filter to pick the most descriptive (longest)
136-
// route.
137-
private static boolean isBetterRoute(HttpRouteState httpRouteState, String name) {
138-
String route = httpRouteState.getRoute();
139-
int routeLength = route == null ? 0 : route.length();
140-
return name.length() > routeLength;
141-
}
142-
143-
private static void updateSpanName(Span serverSpan, HttpRouteState httpRouteState, String route) {
144-
String method = httpRouteState.getMethod();
145-
// method should never really be null - but in case it for some reason is, we'll rely on the
146-
// span name extractor behavior
147-
if (method != null) {
148-
serverSpan.updateName(method + " " + route);
149-
}
150-
}
151-
152-
/**
153-
* Returns the {@code http.route} attribute value that's stored in the {@code context}, or null if
154-
* it was not set before.
155-
*/
156-
@Nullable
157-
static String getRoute(Context context) {
158-
HttpRouteState httpRouteState = HttpRouteState.fromContextOrNull(context);
159-
return httpRouteState == null ? null : httpRouteState.getRoute();
160-
}
161-
162-
private static final class OneArgAdapter<T> implements HttpRouteBiGetter<T, HttpRouteGetter<T>> {
163-
164-
private static final OneArgAdapter<Object> INSTANCE = new OneArgAdapter<>();
165-
166-
@SuppressWarnings("unchecked")
167-
static <T> OneArgAdapter<T> getInstance() {
168-
return (OneArgAdapter<T>) INSTANCE;
169-
}
170-
171-
@Override
172-
@Nullable
173-
public String get(Context context, T arg, HttpRouteGetter<T> httpRouteGetter) {
174-
return httpRouteGetter.get(context, arg);
175-
}
176-
}
177-
178-
private static final class ConstantAdapter implements HttpRouteGetter<String> {
179-
180-
private static final ConstantAdapter INSTANCE = new ConstantAdapter();
181-
182-
@Nullable
183-
@Override
184-
public String get(Context context, String route) {
185-
return route;
186-
}
94+
HttpServerRoute.update(context, source.toHttpServerRouteSource(), httpRouteGetter, arg1, arg2);
18795
}
18896
}

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpRouteSource.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55

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

8-
/** Represents the source that provided the {@code http.route} attribute. */
8+
/**
9+
* Represents the source that provided the {@code http.route} attribute.
10+
*
11+
* @deprecated This class is deprecated and will be removed in the 2.0 release. Use {@link
12+
* HttpServerRouteSource} instead.
13+
*/
14+
@Deprecated
915
public enum HttpRouteSource {
1016
// for servlet filters we try to find the best name which isn't necessarily from the first
1117
// filter that is called
@@ -27,4 +33,18 @@ public enum HttpRouteSource {
2733
this.order = order;
2834
this.useFirst = useFirst;
2935
}
36+
37+
HttpServerRouteSource toHttpServerRouteSource() {
38+
switch (this) {
39+
case FILTER:
40+
return HttpServerRouteSource.SERVER_FILTER;
41+
case SERVLET:
42+
return HttpServerRouteSource.SERVER;
43+
case CONTROLLER:
44+
return HttpServerRouteSource.CONTROLLER;
45+
case NESTED_CONTROLLER:
46+
return HttpServerRouteSource.NESTED_CONTROLLER;
47+
}
48+
throw new IllegalStateException("Unsupported value " + this);
49+
}
3050
}

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpServerAttributesExtractorBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final class HttpServerAttributesExtractorBuilder<REQUEST, RESPONSE> {
3737
List<String> capturedRequestHeaders = emptyList();
3838
List<String> capturedResponseHeaders = emptyList();
3939
Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
40-
Function<Context, String> httpRouteGetter = HttpRouteHolder::getRoute;
40+
Function<Context, String> httpRouteGetter = HttpServerRoute::get;
4141

4242
HttpServerAttributesExtractorBuilder(
4343
HttpServerAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter,

0 commit comments

Comments
 (0)