Skip to content

Commit eb38ab8

Browse files
committed
comments
Signed-off-by: sezen.leblay <[email protected]>
1 parent 1c2c74a commit eb38ab8

File tree

7 files changed

+56
-35
lines changed

7 files changed

+56
-35
lines changed

dd-trace-core/src/main/java/datadog/trace/common/metrics/MetricKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public MetricKey(
6161
+ 29791 * this.operationName.hashCode()
6262
+ 961 * this.type.hashCode()
6363
+ 31 * httpStatusCode
64-
+ 29 * this.httpMethod.hashCode()
65-
+ 27 * this.httpEndpoint.hashCode()
64+
+ -1796951359 * this.httpMethod.hashCode()
65+
+ 129082719 * this.httpEndpoint.hashCode()
6666
+ (this.synthetics ? 1 : 0);
6767
}
6868

dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ private void finishAndAddToTrace(final long durationNano) {
150150
setLongRunningVersion(-this.longRunningVersion);
151151
this.metrics.onSpanFinished();
152152

153-
// Apply HTTP endpoint tagging for HTTP server spans before publishing
154-
// This ensures endpoint tagging is applied when all tags are available
155-
datadog.trace.common.metrics.HttpEndpointTagging.setEndpointTag(
156-
context, datadog.trace.api.Config.get());
157-
158153
TraceCollector.PublishState publishState = context.getTraceCollector().onPublish(this);
159154
log.debug("Finished span ({}): {}", publishState, this);
160155
} else {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package datadog.trace.core.tagprocessor;
2+
3+
import datadog.trace.api.Config;
4+
import datadog.trace.api.TagMap;
5+
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
6+
import datadog.trace.core.DDSpanContext;
7+
import java.util.List;
8+
9+
/**
10+
* TagsPostProcessor that applies HTTP endpoint tagging logic to spans. This processor computes and
11+
* sets the http.endpoint tag based on http.route and http.url tags when appropriate.
12+
*/
13+
public final class HttpEndpointPostProcessor extends TagsPostProcessor {
14+
private final Config config;
15+
16+
public HttpEndpointPostProcessor() {
17+
this(Config.get());
18+
}
19+
20+
// Visible for testing
21+
HttpEndpointPostProcessor(Config config) {
22+
this.config = config;
23+
}
24+
25+
@Override
26+
public void processTags(
27+
TagMap unsafeTags, DDSpanContext spanContext, List<AgentSpanLink> spanLinks) {
28+
HttpEndpointTagging.setEndpointTag(spanContext, config);
29+
}
30+
}

dd-trace-core/src/main/java/datadog/trace/common/metrics/HttpEndpointTagging.java renamed to dd-trace-core/src/main/java/datadog/trace/core/tagprocessor/HttpEndpointTagging.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
package datadog.trace.common.metrics;
1+
package datadog.trace.core.tagprocessor;
22

33
import static datadog.trace.bootstrap.instrumentation.api.Tags.HTTP_ENDPOINT;
44
import static datadog.trace.bootstrap.instrumentation.api.Tags.HTTP_ROUTE;
55
import static datadog.trace.bootstrap.instrumentation.api.Tags.HTTP_URL;
66

7+
import datadog.trace.api.Config;
78
import datadog.trace.core.CoreSpan;
9+
import datadog.trace.core.DDSpanContext;
810
import java.util.ArrayList;
911
import java.util.List;
12+
import java.util.regex.Matcher;
1013
import java.util.regex.Pattern;
1114
import org.slf4j.Logger;
1215
import org.slf4j.LoggerFactory;
1316

1417
/**
1518
* Utility class for HTTP endpoint tagging logic. Handles route eligibility checks and URL path
16-
* parameterization for trace metrics.
19+
* parameterization for spans.
1720
*
1821
* <p>This implementation ensures: 1. Only applies to HTTP service entry spans (server spans) 2.
1922
* Limits cardinality through URL path parameterization 3. Uses http.route when available and
@@ -48,12 +51,16 @@ private HttpEndpointTagging() {
4851
* @return true if route is eligible, false otherwise
4952
*/
5053
public static boolean isRouteEligible(String route) {
51-
if (route == null || route.trim().isEmpty()) {
54+
if (route == null) {
5255
return false;
5356
}
5457

5558
route = route.trim();
5659

60+
if (route.isEmpty()) {
61+
return false;
62+
}
63+
5764
// Route must start with / to be a valid path
5865
if (!route.startsWith("/")) {
5966
return false;
@@ -64,11 +71,6 @@ public static boolean isRouteEligible(String route) {
6471
return false;
6572
}
6673

67-
// Reject routes that are just wildcards
68-
if (route.matches("^[*/]+$")) {
69-
return false;
70-
}
71-
7274
// Route is eligible for endpoint tagging
7375
return true;
7476
}
@@ -145,11 +147,16 @@ public static String parameterizeUrlPath(String path) {
145147
* @return parameterized endpoint path or '/'
146148
*/
147149
public static String computeEndpointFromUrl(String url) {
148-
if (url == null || url.trim().isEmpty()) {
150+
if (url == null) {
149151
return "/";
150152
}
151153

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);
153160
if (!matcher.matches()) {
154161
log.debug("Failed to parse URL for endpoint computation: {}", url);
155162
return "/";
@@ -193,29 +200,22 @@ public static void setEndpointTag(CoreSpan<?> span) {
193200

194201
/**
195202
* 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.
197204
*
198205
* @param spanContext The span context to potentially tag
199206
* @param config The tracer configuration containing feature flags
200207
*/
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) {
203209
if (!config.isResourceRenamingEnabled()) {
204210
return;
205211
}
206212

207213
Object route = spanContext.unsafeGetTag(HTTP_ROUTE);
208-
boolean shouldUseRoute = false;
209214

210215
// Check if we should use route (when not forcing simplified endpoints)
211216
if (!config.isResourceRenamingAlwaysSimplifiedEndpoint()
212217
&& route != null
213218
&& isRouteEligible(route.toString())) {
214-
shouldUseRoute = true;
215-
}
216-
217-
// If we should use route and not set endpoint tag, return early
218-
if (shouldUseRoute) {
219219
return;
220220
}
221221

@@ -236,23 +236,17 @@ && isRouteEligible(route.toString())) {
236236
* @param span The span to potentially tag
237237
* @param config The tracer configuration containing feature flags
238238
*/
239-
public static void setEndpointTag(CoreSpan<?> span, datadog.trace.api.Config config) {
239+
public static void setEndpointTag(CoreSpan<?> span, Config config) {
240240
if (!config.isResourceRenamingEnabled()) {
241241
return;
242242
}
243243

244244
Object route = span.getTag(HTTP_ROUTE);
245-
boolean shouldUseRoute = false;
246245

247246
// Check if we should use route (when not forcing simplified endpoints)
248247
if (!config.isResourceRenamingAlwaysSimplifiedEndpoint()
249248
&& route != null
250249
&& isRouteEligible(route.toString())) {
251-
shouldUseRoute = true;
252-
}
253-
254-
// If we should use route and not set endpoint tag, return early
255-
if (shouldUseRoute) {
256250
return;
257251
}
258252

dd-trace-core/src/main/java/datadog/trace/core/tagprocessor/TagsPostProcessorFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ private static class Lazy {
1313
private static TagsPostProcessor lazyProcessor = createLazyChain();
1414

1515
private static TagsPostProcessor createEagerChain() {
16-
final List<TagsPostProcessor> processors = new ArrayList<>(2);
16+
final List<TagsPostProcessor> processors = new ArrayList<>(3);
1717
processors.add(new PeerServiceCalculator());
18+
processors.add(new HttpEndpointPostProcessor());
1819
if (addBaseService) {
1920
processors.add(new BaseServiceAdder(Config.get().getServiceName()));
2021
}

dd-trace-core/src/test/groovy/datadog/trace/common/metrics/HttpEndpointTaggingIntegrationTest.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.common.metrics
22

3+
import datadog.trace.core.tagprocessor.HttpEndpointTagging
34
import datadog.trace.test.util.DDSpecification
45

56
class HttpEndpointTaggingIntegrationTest extends DDSpecification {

dd-trace-core/src/test/groovy/datadog/trace/common/metrics/HttpEndpointTaggingTest.groovy renamed to dd-trace-core/src/test/groovy/datadog/trace/core/tagprocessor/HttpEndpointTaggingTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package datadog.trace.common.metrics
1+
package datadog.trace.core.tagprocessor
22

33
import datadog.trace.test.util.DDSpecification
44

0 commit comments

Comments
 (0)