1
- package datadog .trace .common . metrics ;
1
+ package datadog .trace .core . tagprocessor ;
2
2
3
3
import static datadog .trace .bootstrap .instrumentation .api .Tags .HTTP_ENDPOINT ;
4
4
import static datadog .trace .bootstrap .instrumentation .api .Tags .HTTP_ROUTE ;
5
5
import static datadog .trace .bootstrap .instrumentation .api .Tags .HTTP_URL ;
6
6
7
+ import datadog .trace .api .Config ;
7
8
import datadog .trace .core .CoreSpan ;
9
+ import datadog .trace .core .DDSpanContext ;
8
10
import java .util .ArrayList ;
9
11
import java .util .List ;
12
+ import java .util .regex .Matcher ;
10
13
import java .util .regex .Pattern ;
11
14
import org .slf4j .Logger ;
12
15
import org .slf4j .LoggerFactory ;
13
16
14
17
/**
15
18
* Utility class for HTTP endpoint tagging logic. Handles route eligibility checks and URL path
16
- * parameterization for trace metrics .
19
+ * parameterization for spans .
17
20
*
18
21
* <p>This implementation ensures: 1. Only applies to HTTP service entry spans (server spans) 2.
19
22
* Limits cardinality through URL path parameterization 3. Uses http.route when available and
@@ -48,12 +51,16 @@ private HttpEndpointTagging() {
48
51
* @return true if route is eligible, false otherwise
49
52
*/
50
53
public static boolean isRouteEligible (String route ) {
51
- if (route == null || route . trim (). isEmpty () ) {
54
+ if (route == null ) {
52
55
return false ;
53
56
}
54
57
55
58
route = route .trim ();
56
59
60
+ if (route .isEmpty ()) {
61
+ return false ;
62
+ }
63
+
57
64
// Route must start with / to be a valid path
58
65
if (!route .startsWith ("/" )) {
59
66
return false ;
@@ -64,11 +71,6 @@ public static boolean isRouteEligible(String route) {
64
71
return false ;
65
72
}
66
73
67
- // Reject routes that are just wildcards
68
- if (route .matches ("^[*/]+$" )) {
69
- return false ;
70
- }
71
-
72
74
// Route is eligible for endpoint tagging
73
75
return true ;
74
76
}
@@ -145,11 +147,16 @@ public static String parameterizeUrlPath(String path) {
145
147
* @return parameterized endpoint path or '/'
146
148
*/
147
149
public static String computeEndpointFromUrl (String url ) {
148
- if (url == null || url . trim (). isEmpty () ) {
150
+ if (url == null ) {
149
151
return "/" ;
150
152
}
151
153
152
- java .util .regex .Matcher matcher = URL_PATTERN .matcher (url .trim ());
154
+ url = url .trim ();
155
+ if (url .isEmpty ()) {
156
+ return "/" ;
157
+ }
158
+
159
+ Matcher matcher = URL_PATTERN .matcher (url );
153
160
if (!matcher .matches ()) {
154
161
log .debug ("Failed to parse URL for endpoint computation: {}" , url );
155
162
return "/" ;
@@ -193,29 +200,22 @@ public static void setEndpointTag(CoreSpan<?> span) {
193
200
194
201
/**
195
202
* Sets the HTTP endpoint tag on a span context based on configuration flags. This overload
196
- * accepts DDSpanContext for use in TagInterceptor and other core components.
203
+ * accepts DDSpanContext for use in tag post-processors and other core components.
197
204
*
198
205
* @param spanContext The span context to potentially tag
199
206
* @param config The tracer configuration containing feature flags
200
207
*/
201
- public static void setEndpointTag (
202
- datadog .trace .core .DDSpanContext spanContext , datadog .trace .api .Config config ) {
208
+ public static void setEndpointTag (DDSpanContext spanContext , Config config ) {
203
209
if (!config .isResourceRenamingEnabled ()) {
204
210
return ;
205
211
}
206
212
207
213
Object route = spanContext .unsafeGetTag (HTTP_ROUTE );
208
- boolean shouldUseRoute = false ;
209
214
210
215
// Check if we should use route (when not forcing simplified endpoints)
211
216
if (!config .isResourceRenamingAlwaysSimplifiedEndpoint ()
212
217
&& route != null
213
218
&& isRouteEligible (route .toString ())) {
214
- shouldUseRoute = true ;
215
- }
216
-
217
- // If we should use route and not set endpoint tag, return early
218
- if (shouldUseRoute ) {
219
219
return ;
220
220
}
221
221
@@ -236,23 +236,17 @@ && isRouteEligible(route.toString())) {
236
236
* @param span The span to potentially tag
237
237
* @param config The tracer configuration containing feature flags
238
238
*/
239
- public static void setEndpointTag (CoreSpan <?> span , datadog . trace . api . Config config ) {
239
+ public static void setEndpointTag (CoreSpan <?> span , Config config ) {
240
240
if (!config .isResourceRenamingEnabled ()) {
241
241
return ;
242
242
}
243
243
244
244
Object route = span .getTag (HTTP_ROUTE );
245
- boolean shouldUseRoute = false ;
246
245
247
246
// Check if we should use route (when not forcing simplified endpoints)
248
247
if (!config .isResourceRenamingAlwaysSimplifiedEndpoint ()
249
248
&& route != null
250
249
&& isRouteEligible (route .toString ())) {
251
- shouldUseRoute = true ;
252
- }
253
-
254
- // If we should use route and not set endpoint tag, return early
255
- if (shouldUseRoute ) {
256
250
return ;
257
251
}
258
252
0 commit comments