Skip to content

Commit

Permalink
make sure we can handle a null component when throttling. (#47)
Browse files Browse the repository at this point in the history
* make sure we can handle a null component when throttling.

* Update splunk-otel-android/src/test/java/com/splunk/rum/ThrottlingExporterTest.java

* inline an unused test method
  • Loading branch information
jkwatson authored Jun 23, 2021
1 parent c7aaf14 commit 68d2949
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ThrottlingExporter implements SpanExporter {
private final long windowSizeInNanos;
private final int maxSpansInWindow;
// note: no need to make this thread-safe since it will only ever be called from the BatchSpanProcessor worker thread.
// the implementation here needs to support null keys, or we'd need to use a default component value.
private final Map<String, Window> categoryToWindow = new HashMap<>();

private ThrottlingExporter(Builder builder) {
Expand Down Expand Up @@ -89,7 +90,12 @@ boolean aboveLimit(SpanData spanData) {

// remove oldest entries until the window shrinks to the configured size
while (true) {
long first = timestamps.peekFirst();
Long first = timestamps.peekFirst();
//this shouldn't happen, due to the single-threaded nature of things here, but
//just to be on the safe side, don't blow up if something has cleared out the window.
if (first == null) {
break;
}
if (endNanos - first < windowSizeInNanos) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ public void shouldCountDifferentComponentsSeparately() {
span("ui", now.plus(10, ChronoUnit.SECONDS)),
span("ui", now.plus(15, ChronoUnit.SECONDS)),
span("ui", now.plus(20, ChronoUnit.SECONDS)),
span("error", now.plus(25, ChronoUnit.SECONDS))
span("error", now.plus(25, ChronoUnit.SECONDS)),
//user-generated spans probably won't have a "component" attribute at all.
span(null, now.plus(30, ChronoUnit.SECONDS)),
span(null, now.plus(35, ChronoUnit.SECONDS)),
span(null, now.plus(40, ChronoUnit.SECONDS)),
span(null, now.plus(45, ChronoUnit.SECONDS))
);

// when
Expand All @@ -130,7 +135,11 @@ public void shouldCountDifferentComponentsSeparately() {
spanData.get(2),
spanData.get(3),
// idx=4 will be skipped because it's the 3rd component=ui span in the last 15 secs
spanData.get(5)
spanData.get(5),
spanData.get(6),
spanData.get(7),
// idx = 8 will be skipped because it's the 3rd no-component span in the 2-span, 15s window
spanData.get(9)
));
}

Expand Down Expand Up @@ -210,4 +219,5 @@ private static SpanData span(String component, Instant endTime) {
.setAttributes(Attributes.of(SplunkRum.COMPONENT_KEY, component))
.build();
}
}

}

0 comments on commit 68d2949

Please sign in to comment.