Skip to content

Commit 0ad2069

Browse files
committed
Tweaks
1 parent a20b8b3 commit 0ad2069

5 files changed

Lines changed: 28 additions & 35 deletions

File tree

src/main/java/io/apitally/common/SpanCollector.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public SpanHandle startCollection() {
4444
SpanContext spanContext = span.getSpanContext();
4545
String traceId = spanContext.getTraceId();
4646

47-
includedSpanIds.put(traceId, ConcurrentHashMap.newKeySet());
48-
includedSpanIds.get(traceId).add(spanContext.getSpanId());
47+
Set<String> spanIds = ConcurrentHashMap.newKeySet();
48+
spanIds.add(spanContext.getSpanId());
49+
includedSpanIds.put(traceId, spanIds);
4950
collectedSpans.put(traceId, new ConcurrentLinkedQueue<>());
5051

5152
return new SpanHandle(traceId, span, scope, this);
@@ -119,15 +120,11 @@ private SpanData serializeSpan(ReadableSpan span) {
119120
SpanContext spanContext = spanData.getSpanContext();
120121
SpanContext parentSpanContext = spanData.getParentSpanContext();
121122

122-
String parentSpanId = null;
123-
if (parentSpanContext.isValid()) {
124-
parentSpanId = parentSpanContext.getSpanId();
125-
}
126-
127-
String status = null;
128-
if (spanData.getStatus().getStatusCode() != StatusCode.UNSET) {
129-
status = spanData.getStatus().getStatusCode().name();
130-
}
123+
String parentSpanId = parentSpanContext.isValid() ? parentSpanContext.getSpanId() : null;
124+
String status =
125+
spanData.getStatus().getStatusCode() != StatusCode.UNSET
126+
? spanData.getStatus().getStatusCode().name()
127+
: null;
131128

132129
Map<String, Object> attributes = null;
133130
if (!spanData.getAttributes().isEmpty()) {
@@ -165,18 +162,12 @@ public String getTraceId() {
165162
}
166163

167164
public void setName(String name) {
168-
if (span != null) {
169-
span.updateName(name);
170-
}
165+
span.updateName(name);
171166
}
172167

173168
public List<SpanData> end() {
174-
if (scope != null) {
175-
scope.close();
176-
}
177-
if (span != null) {
178-
span.end();
179-
}
169+
scope.close();
170+
span.end();
180171
return collector.getAndClearSpans(traceId);
181172
}
182173
}

src/main/java/io/apitally/spring/ApitallyFilter.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.apitally.common.ConsumerRegistry;
55
import io.apitally.common.LogAppender;
66
import io.apitally.common.RequestLogger;
7+
import io.apitally.common.RequestLoggingConfig;
78
import io.apitally.common.SpanCollector;
89
import io.apitally.common.dto.Consumer;
910
import io.apitally.common.dto.Header;
@@ -54,18 +55,20 @@ protected void doFilterInternal(
5455
return;
5556
}
5657

58+
RequestLoggingConfig requestLoggingConfig = client.requestLogger.getConfig();
59+
final boolean requestLoggingEnabled = requestLoggingConfig.isEnabled();
60+
5761
String requestContentType = request.getContentType();
5862
final boolean shouldCacheRequest =
59-
client.requestLogger.getConfig().isEnabled()
60-
&& client.requestLogger.getConfig().isRequestBodyIncluded()
63+
requestLoggingEnabled
64+
&& requestLoggingConfig.isRequestBodyIncluded()
6165
&& requestContentType != null
6266
&& RequestLogger.ALLOWED_CONTENT_TYPES.stream()
6367
.anyMatch(
6468
allowedContentType ->
6569
requestContentType.startsWith(allowedContentType));
6670
final boolean shouldCacheResponse =
67-
client.requestLogger.getConfig().isEnabled()
68-
&& client.requestLogger.getConfig().isResponseBodyIncluded();
71+
requestLoggingEnabled && requestLoggingConfig.isResponseBodyIncluded();
6972
ContentCachingRequestWrapper cachingRequest =
7073
shouldCacheRequest ? new ContentCachingRequestWrapper(request) : null;
7174
ContentCachingResponseWrapper cachingResponse =
@@ -74,11 +77,9 @@ protected void doFilterInternal(
7477
cachingResponse == null ? new CountingResponseWrapper(response) : null;
7578

7679
final boolean shouldCaptureLogs =
77-
client.requestLogger.getConfig().isEnabled()
78-
&& client.requestLogger.getConfig().isLogCaptureEnabled();
80+
requestLoggingEnabled && requestLoggingConfig.isLogCaptureEnabled();
7981
final boolean shouldCaptureSpans =
80-
client.requestLogger.getConfig().isEnabled()
81-
&& client.requestLogger.getConfig().isTracingEnabled();
82+
requestLoggingEnabled && requestLoggingConfig.isTracingEnabled();
8283

8384
Exception exception = null;
8485
final long startTime = System.currentTimeMillis();

src/main/java/io/apitally/spring/ApitallySpanCollector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void onStart(Context parentContext, ReadWriteSpan span) {
5656

5757
@Override
5858
public boolean isStartRequired() {
59-
return true;
59+
return delegate != null && delegate.isStartRequired();
6060
}
6161

6262
@Override
@@ -68,7 +68,7 @@ public void onEnd(ReadableSpan span) {
6868

6969
@Override
7070
public boolean isEndRequired() {
71-
return true;
71+
return delegate != null && delegate.isEndRequired();
7272
}
7373

7474
public SpanCollector.SpanHandle startCollection() {

src/test/java/io/apitally/common/SpanCollectorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.apitally.spring.ApitallySpanCollector;
1111
import io.opentelemetry.api.GlobalOpenTelemetry;
1212
import io.opentelemetry.api.trace.Span;
13+
import io.opentelemetry.api.trace.StatusCode;
1314
import io.opentelemetry.api.trace.Tracer;
1415
import java.util.List;
1516
import java.util.Set;
@@ -108,11 +109,8 @@ void testCollectorWithChildSpans() {
108109
@Test
109110
void testDoesNotCollectUnrelatedSpans() {
110111
SpanCollector collector = createAndRegisterCollector(true);
111-
112-
// Trigger initialization first by starting and ending a collection
113-
collector.startCollection().end();
114-
115112
Tracer tracer = GlobalOpenTelemetry.getTracer("test");
113+
116114
Span outsideSpan = tracer.spanBuilder("outsideSpan").startSpan();
117115
outsideSpan.end();
118116

@@ -140,6 +138,7 @@ void testSpanDataSerialization() {
140138
Span span = tracer.spanBuilder("testSpan").startSpan();
141139
span.setAttribute("http.method", "GET");
142140
span.setAttribute("http.status_code", 200);
141+
span.setStatus(StatusCode.OK);
143142
span.end();
144143

145144
List<SpanData> spans = handle.end();
@@ -154,6 +153,7 @@ void testSpanDataSerialization() {
154153
assertTrue(testSpan.getEndTime() > 0);
155154
assertTrue(testSpan.getEndTime() >= testSpan.getStartTime());
156155

156+
assertEquals("OK", testSpan.getStatus());
157157
assertNotNull(testSpan.getAttributes());
158158
assertEquals("GET", testSpan.getAttributes().get("http.method"));
159159
assertEquals(200L, testSpan.getAttributes().get("http.status_code"));

src/test/java/io/apitally/spring/ApitallyFilterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Base64;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.stream.StreamSupport;
2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.Test;
2425
import org.slf4j.Logger;
@@ -332,7 +333,7 @@ void testRequestLogger() {
332333
assertTrue(firstItem.get("spans").isArray());
333334
assertTrue(firstItem.get("spans").size() >= 2); // root span + child span
334335
assertTrue(
335-
java.util.stream.StreamSupport.stream(firstItem.get("spans").spliterator(), false)
336+
StreamSupport.stream(firstItem.get("spans").spliterator(), false)
336337
.anyMatch(span -> span.get("name").asText().equals("fetchItems")));
337338

338339
// Verify POST request logging with request body

0 commit comments

Comments
 (0)