-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement span exporting limits (#45)
* Implement span exporting limits * license * Add demo usage to sample app
- Loading branch information
Mateusz Rzeszutek
authored
Jun 22, 2021
1 parent
c295bb6
commit c7aaf14
Showing
6 changed files
with
464 additions
and
22 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 |
---|---|---|
@@ -1,27 +1,56 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".SecondFragment"> | ||
|
||
<TextView | ||
android:id="@+id/textview_second" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintBottom_toTopOf="@id/button_second" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
<data> | ||
<variable | ||
name="secondFragment" | ||
type="com.splunk.android.sample.SecondFragment" /> | ||
</data> | ||
|
||
<Button | ||
android:id="@+id/button_second" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/previous" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/textview_second" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<TextView | ||
android:id="@+id/textview_second" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintBottom_toTopOf="@id/button_second" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<Button | ||
android:id="@+id/button_second" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/previous" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/textview_second" /> | ||
|
||
<Button | ||
android:id="@+id/button_spam" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/start_spam" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/button_second" /> | ||
|
||
<TextView | ||
android:id="@+id/spam_status" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@{secondFragment.spanCountLabel}" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/button_spam" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</layout> |
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
137 changes: 137 additions & 0 deletions
137
splunk-otel-android/src/main/java/com/splunk/rum/ThrottlingExporter.java
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,137 @@ | ||
/* | ||
* Copyright Splunk Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.splunk.rum; | ||
|
||
import android.util.Log; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Deque; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
|
||
class ThrottlingExporter implements SpanExporter { | ||
private final SpanExporter delegate; | ||
private final Function<SpanData, String> categoryFunction; | ||
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. | ||
private final Map<String, Window> categoryToWindow = new HashMap<>(); | ||
|
||
private ThrottlingExporter(Builder builder) { | ||
this.delegate = builder.delegate; | ||
this.categoryFunction = builder.categoryFunction; | ||
this.windowSizeInNanos = builder.windowSize.toNanos(); | ||
this.maxSpansInWindow = builder.maxSpansInWindow; | ||
} | ||
|
||
static Builder newBuilder(SpanExporter delegate) { | ||
return new Builder(delegate); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode export(Collection<SpanData> spans) { | ||
List<SpanData> spansBelowLimit = new ArrayList<>(); | ||
for (SpanData span : spans) { | ||
String category = categoryFunction.apply(span); | ||
Window window = categoryToWindow.computeIfAbsent(category, k -> new Window()); | ||
if (!window.aboveLimit(span)) { | ||
spansBelowLimit.add(span); | ||
} | ||
} | ||
int dropped = spans.size() - spansBelowLimit.size(); | ||
if (dropped > 0) { | ||
Log.d(SplunkRum.LOG_TAG, "Dropped " + dropped + " spans because of throttling"); | ||
} | ||
return delegate.export(spansBelowLimit); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode flush() { | ||
return delegate.flush(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
return delegate.shutdown(); | ||
} | ||
|
||
class Window { | ||
private final Deque<Long> timestamps = new ArrayDeque<>(); | ||
|
||
// this function assumes that spans are always sorted by their end time (ascending) | ||
boolean aboveLimit(SpanData spanData) { | ||
long endNanos = spanData.getEndEpochNanos(); | ||
timestamps.addLast(endNanos); | ||
|
||
// remove oldest entries until the window shrinks to the configured size | ||
while (true) { | ||
long first = timestamps.peekFirst(); | ||
if (endNanos - first < windowSizeInNanos) { | ||
break; | ||
} | ||
timestamps.removeFirst(); | ||
} | ||
|
||
boolean aboveLimit = timestamps.size() > maxSpansInWindow; | ||
// don't count spans that were throttled | ||
if (aboveLimit) { | ||
timestamps.removeLast(); | ||
} | ||
return aboveLimit; | ||
} | ||
} | ||
|
||
static class Builder { | ||
final SpanExporter delegate; | ||
Function<SpanData, String> categoryFunction = span -> "default"; | ||
Duration windowSize = Duration.ofSeconds(30); | ||
int maxSpansInWindow = 100; | ||
|
||
private Builder(SpanExporter delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
Builder categorizeByAttribute(AttributeKey<String> attributeKey) { | ||
categoryFunction = spanData -> spanData.getAttributes().get(attributeKey); | ||
return this; | ||
} | ||
|
||
Builder windowSize(Duration timeWindow) { | ||
this.windowSize = timeWindow; | ||
return this; | ||
} | ||
|
||
Builder maxSpansInWindow(int maxSpansInWindow) { | ||
this.maxSpansInWindow = maxSpansInWindow; | ||
return this; | ||
} | ||
|
||
ThrottlingExporter build() { | ||
return new ThrottlingExporter(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.