-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: specify span name based on matching controller
- Loading branch information
1 parent
df98208
commit 7a8e5a4
Showing
2 changed files
with
64 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.epam.aidial.core.util; | ||
|
||
import io.opencensus.contrib.http.util.HttpTraceAttributeConstants; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import lombok.experimental.UtilityClass; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
|
||
@UtilityClass | ||
public class SpanUtil { | ||
|
||
private static final AttributeKey<String> ATTR_HTTP_METHOD = AttributeKey.stringKey(HttpTraceAttributeConstants.HTTP_METHOD); | ||
private static final List<String> GROUPS = List.of("id", "bucket", "path"); | ||
|
||
public void updateName(String path, Matcher matcher) { | ||
String spanName = path; | ||
if (Span.current() instanceof ReadableSpan span) { | ||
String method = span.getAttribute(ATTR_HTTP_METHOD); | ||
if (method != null) { | ||
spanName = method + " " + spanName; | ||
} | ||
} | ||
if (matcher.groupCount() > 0) { | ||
for (String group : GROUPS) { | ||
spanName = replace(spanName, group, matcher); | ||
} | ||
} | ||
Span.current().updateName(spanName); | ||
} | ||
|
||
private static String replace(String spanName, String group, Matcher matcher) { | ||
try { | ||
return spanName.replaceAll(matcher.group(group), "{%s}".formatted(group)); | ||
} catch (IllegalArgumentException ignored) { | ||
return spanName; | ||
} | ||
} | ||
} |