-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to throttle exports when reading from disk. #663
base: main
Are you sure you want to change the base?
Changes from 5 commits
ec58878
d6b4d5c
42a434a
ffcdf3c
6493988
8a7a07f
adadd52
0837057
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
demo-app/local.properties | ||
.DS_Store | ||
**/build/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"java.configuration.updateBuildConfiguration": "automatic" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.opentelemetry.android; | ||
|
||
import android.util.Log; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
class BandwidthThrottlingExporter implements SpanExporter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we would also need to see a unit test for this class. |
||
private final SpanExporter delegate; | ||
private final Function<SpanData, String> categoryFunction; | ||
private final long maxBytesPerSecond; | ||
private final long timeWindowInMillis; | ||
private long lastExportTime; | ||
private long bytesExportedInWindow; | ||
|
||
private BandwidthThrottlingExporter(Builder builder) { | ||
this.delegate = builder.delegate; | ||
this.categoryFunction = builder.categoryFunction; | ||
this.maxBytesPerSecond = builder.maxBytesPerSecond; | ||
this.timeWindowInMillis = builder.timeWindow.toMillis(); | ||
this.lastExportTime = SystemTime.get().getCurrentTimeMillis(); | ||
this.bytesExportedInWindow = 0; | ||
} | ||
|
||
static Builder newBuilder(SpanExporter delegate) { | ||
return new Builder(delegate); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode export(Collection<SpanData> spans) { | ||
List<SpanData> spansToExport = new ArrayList<>(); | ||
long totalBytes = 0; | ||
|
||
for (SpanData span : spans) { | ||
// Estimate the size of the span (this can be adjusted based on actual size) | ||
long spanSize = estimateSpanSize(span); | ||
totalBytes += spanSize; | ||
|
||
// Check if we can export this span based on the current bandwidth limit | ||
if (canExport(spanSize)) { | ||
spansToExport.add(span); | ||
bytesExportedInWindow += spanSize; | ||
} else { | ||
Log.d("BandwidthThrottlingExporter", "Throttled span: " + span.getName()); | ||
} | ||
} | ||
|
||
return delegate.export(spansToExport); | ||
} | ||
|
||
private boolean canExport(long spanSize) { | ||
long currentTime = System.currentTimeMillis(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should use |
||
if (currentTime - lastExportTime > timeWindowInMillis) { | ||
// Reset the window | ||
bytesExportedInWindow = 0; | ||
lastExportTime = currentTime; | ||
} | ||
|
||
return (bytesExportedInWindow + spanSize) | ||
<= maxBytesPerSecond * (timeWindowInMillis / 1000); | ||
} | ||
|
||
private long estimateSpanSize(SpanData span) { | ||
// This is a placeholder for actual size estimation logic | ||
return span.getAttributes().size() * 8; // Example: 8 bytes per attribute | ||
} | ||
|
||
@Override | ||
public CompletableResultCode flush() { | ||
return delegate.flush(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
return delegate.shutdown(); | ||
} | ||
|
||
static class Builder { | ||
final SpanExporter delegate; | ||
CategoryFunction categoryFunction = span -> "default"; | ||
long maxBytesPerSecond = 1024; // Default to 1 KB/s | ||
long timeWindowInMillis = 1000; // Default to 1 second | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should they be private since they have a setter anyway (using the builder pattern)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the fields are intended to be set only through the builder methods i have tried to add and make them private so they can only be modified through it's provided builder methods. I'm sure it will help enhance encapsulation and help maintain the integrity of it's object's state. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
private Builder(SpanExporter delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
Builder maxBytesPerSecond(long maxBytesPerSecond) { | ||
this.maxBytesPerSecond = maxBytesPerSecond; | ||
return this; | ||
} | ||
|
||
Builder timeWindow(Duration timeWindow) { | ||
this.timeWindow = timeWindow; | ||
return this; | ||
} | ||
|
||
BandwidthThrottlingExporter build() { | ||
return new BandwidthThrottlingExporter(this); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the IDE specific configurations out of the repo please.