Skip to content

Commit 0febad8

Browse files
committed
refactor: reduce cognitive complexity and simplify control flow in SpanFilterUtil for sonarqube errors
1 parent 6c046a6 commit 0febad8

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

webapp/src/main/java/io/github/microcks/util/tracing/SpanFilterUtil.java

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import io.github.microcks.event.TraceEvent;
1919
import io.opentelemetry.api.common.AttributeKey;
20+
import io.opentelemetry.sdk.trace.ReadableSpan;
2021
import io.opentelemetry.sdk.trace.data.EventData;
21-
import io.opentelemetry.sdk.trace.data.SpanData;
2222

2323
import java.util.List;
2424
import java.util.Map;
@@ -89,40 +89,58 @@ && matchesWildcard(operationName, traceEvent.operation())
8989
* @param spans the list of spans
9090
* @return the TraceEvent
9191
*/
92-
public static TraceEvent extractTraceEvent(String traceId, List<SpanData> spans) {
92+
public static TraceEvent extractTraceEvent(String traceId, List<ReadableSpan> spans) {
9393
if (spans == null || spans.isEmpty()) {
9494
return null;
9595
}
96+
9697
String service = null;
9798
String operation = null;
9899
String clientAddress = null;
99-
for (SpanData s : spans) {
100-
if (s == null) {
101-
continue;
102-
}
103-
Map<AttributeKey<?>, Object> attributes = s.getAttributes().asMap();
104-
105-
String serviceAttribute = (String) attributes.get(CommonAttributes.SERVICE_NAME);
106-
String operationAttribute = (String) attributes.get(CommonAttributes.OPERATION_NAME);
107-
if (serviceAttribute != null)
108-
service = serviceAttribute;
109-
if (operationAttribute != null)
110-
operation = operationAttribute;
111-
112-
Optional<EventData> invocationReceivedEvent = s.getEvents().stream()
113-
.filter(e -> CommonEvents.INVOCATION_RECEIVED.getEventName().equals(e.getName())).findFirst();
114-
if (invocationReceivedEvent.isPresent()) {
115-
String clientAddressAttribute = invocationReceivedEvent.get().getAttributes()
116-
.get(CommonAttributes.CLIENT_ADDRESS);
117-
if (clientAddressAttribute != null) {
118-
clientAddress = clientAddressAttribute;
100+
101+
for (ReadableSpan s : spans) {
102+
if (isValidSpan(s)) {
103+
Map<AttributeKey<?>, Object> attributes = s.toSpanData().getAttributes().asMap();
104+
105+
service = extractAttributeIfPresent(attributes, CommonAttributes.SERVICE_NAME, service);
106+
operation = extractAttributeIfPresent(attributes, CommonAttributes.OPERATION_NAME, operation);
107+
clientAddress = extractClientAddress(s, clientAddress);
108+
109+
if (allAttributesFound(service, operation, clientAddress)) {
110+
break;
119111
}
120112
}
113+
}
114+
115+
return new TraceEvent(traceId, service, operation, clientAddress);
116+
}
121117

122-
if (service != null && operation != null && clientAddress != null) {
123-
break;
118+
private static boolean isValidSpan(ReadableSpan span) {
119+
return span != null && span.toSpanData() != null;
120+
}
121+
122+
private static String extractAttributeIfPresent(Map<AttributeKey<?>, Object> attributes, AttributeKey<String> key,
123+
String currentValue) {
124+
String attributeValue = (String) attributes.get(key);
125+
return attributeValue != null ? attributeValue : currentValue;
126+
}
127+
128+
private static String extractClientAddress(ReadableSpan span, String currentClientAddress) {
129+
Optional<EventData> invocationReceivedEvent = span.toSpanData().getEvents().stream()
130+
.filter(e -> CommonEvents.INVOCATION_RECEIVED.getEventName().equals(e.getName())).findFirst();
131+
132+
if (invocationReceivedEvent.isPresent()) {
133+
String clientAddressAttribute = invocationReceivedEvent.get().getAttributes()
134+
.get(CommonAttributes.CLIENT_ADDRESS);
135+
if (clientAddressAttribute != null) {
136+
return clientAddressAttribute;
124137
}
125138
}
126-
return new TraceEvent(traceId, service, operation, clientAddress);
139+
140+
return currentClientAddress;
141+
}
142+
143+
private static boolean allAttributesFound(String service, String operation, String clientAddress) {
144+
return service != null && operation != null && clientAddress != null;
127145
}
128146
}

0 commit comments

Comments
 (0)