|
17 | 17 |
|
18 | 18 | import io.github.microcks.event.TraceEvent; |
19 | 19 | import io.opentelemetry.api.common.AttributeKey; |
| 20 | +import io.opentelemetry.sdk.trace.ReadableSpan; |
20 | 21 | import io.opentelemetry.sdk.trace.data.EventData; |
21 | | -import io.opentelemetry.sdk.trace.data.SpanData; |
22 | 22 |
|
23 | 23 | import java.util.List; |
24 | 24 | import java.util.Map; |
@@ -89,40 +89,58 @@ && matchesWildcard(operationName, traceEvent.operation()) |
89 | 89 | * @param spans the list of spans |
90 | 90 | * @return the TraceEvent |
91 | 91 | */ |
92 | | - public static TraceEvent extractTraceEvent(String traceId, List<SpanData> spans) { |
| 92 | + public static TraceEvent extractTraceEvent(String traceId, List<ReadableSpan> spans) { |
93 | 93 | if (spans == null || spans.isEmpty()) { |
94 | 94 | return null; |
95 | 95 | } |
| 96 | + |
96 | 97 | String service = null; |
97 | 98 | String operation = null; |
98 | 99 | 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; |
119 | 111 | } |
120 | 112 | } |
| 113 | + } |
| 114 | + |
| 115 | + return new TraceEvent(traceId, service, operation, clientAddress); |
| 116 | + } |
121 | 117 |
|
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; |
124 | 137 | } |
125 | 138 | } |
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; |
127 | 145 | } |
128 | 146 | } |
0 commit comments