Skip to content

Commit

Permalink
improve how the SpanFilterBuilder delegates to the SpanDataModifier. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk authored Aug 23, 2023
1 parent fc1f2ff commit 4142246
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.rum.internal.export.SpanDataModifier;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

/** Delegating wrapper around otel SpanDataModifier. */
public final class SpanFilterBuilder {

// TODO: Use pure upstream mechanism for this
private Predicate<String> rejectSpanNamesPredicate = spanName -> false;
private final Map<AttributeKey<?>, Predicate<?>> rejectSpanAttributesPredicates =
new HashMap<>();
private final Map<AttributeKey<?>, Function<?, ?>> spanAttributeReplacements = new HashMap<>();
private final SpanDataModifier spanDataModifier;

public SpanFilterBuilder(SpanExporter exporter) {
this(SpanDataModifier.builder(exporter));
}

public SpanFilterBuilder(SpanDataModifier spanDataModifier) {
this.spanDataModifier = spanDataModifier;
}

/**
* Remove matching spans from the exporter pipeline.
Expand All @@ -43,7 +45,8 @@ public final class SpanFilterBuilder {
* @return {@code this}.
*/
public SpanFilterBuilder rejectSpansByName(Predicate<String> spanNamePredicate) {
this.rejectSpanNamesPredicate = rejectSpanNamesPredicate.or(spanNamePredicate);
// TODO: Use pure upstream mechanism for this
spanDataModifier.rejectSpansByName(spanNamePredicate);
return this;
}

Expand All @@ -60,12 +63,7 @@ public SpanFilterBuilder rejectSpansByName(Predicate<String> spanNamePredicate)
*/
public <T> SpanFilterBuilder rejectSpansByAttributeValue(
AttributeKey<T> attributeKey, Predicate<? super T> attributeValuePredicate) {
rejectSpanAttributesPredicates.compute(
attributeKey,
(k, oldValue) ->
oldValue == null
? attributeValuePredicate
: ((Predicate<T>) oldValue).or(attributeValuePredicate));
spanDataModifier.rejectSpansByAttributeValue(attributeKey, attributeValuePredicate);
return this;
}

Expand Down Expand Up @@ -115,31 +113,11 @@ public <T> SpanFilterBuilder removeSpanAttribute(
*/
public <T> SpanFilterBuilder replaceSpanAttribute(
AttributeKey<T> attributeKey, Function<? super T, ? extends T> attributeValueModifier) {
spanAttributeReplacements.compute(
attributeKey,
(k, oldValue) ->
oldValue == null
? attributeValueModifier
: ((Function<T, T>) oldValue).andThen(attributeValueModifier));
spanDataModifier.replaceSpanAttribute(attributeKey, attributeValueModifier);
return this;
}

SpanExporter build(SpanExporter exporter) {
SpanDataModifier builder =
SpanDataModifier.builder(exporter).rejectSpansByName(rejectSpanNamesPredicate);
spanAttributeReplacements.forEach(
(attributeKey, function) -> {
AttributeKey<? super Object> key = (AttributeKey<? super Object>) attributeKey;
Function<? super Object, ? super Object> fn =
(Function<? super Object, ? super Object>) function;
builder.replaceSpanAttribute(key, fn);
});
rejectSpanAttributesPredicates.forEach(
((attributeKey, predicate) -> {
AttributeKey<? super Object> kk = (AttributeKey<? super Object>) attributeKey;
Predicate<? super Object> vv = (Predicate<? super Object>) predicate;
builder.rejectSpansByAttributeValue(kk, vv);
}));
return builder.build();
SpanExporter build() {
return spanDataModifier.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class SplunkRumBuilder {
Duration slowRenderingDetectionPollInterval = DEFAULT_SLOW_RENDERING_DETECTION_POLL_INTERVAL;
Attributes globalAttributes = Attributes.empty();
@Nullable String deploymentEnvironment;
private final SpanFilterBuilder spanFilterBuilder = new SpanFilterBuilder();
private Consumer<SpanFilterBuilder> spanFilterConfigurer = x -> {};
int maxUsageMegabytes = DEFAULT_MAX_STORAGE_USE_MB;
boolean sessionBasedSamplerEnabled = false;
double sessionBasedSamplerRatio = 1.0;
Expand Down Expand Up @@ -242,7 +242,7 @@ public SplunkRumBuilder setDeploymentEnvironment(String environment) {
* @return {@code this}
*/
public SplunkRumBuilder filterSpans(Consumer<SpanFilterBuilder> configurer) {
configurer.accept(spanFilterBuilder);
this.spanFilterConfigurer = configurer;
return this;
}

Expand Down Expand Up @@ -320,7 +320,9 @@ ConfigFlags getConfigFlags() {
}

SpanExporter decorateWithSpanFilter(SpanExporter exporter) {
return spanFilterBuilder.build(exporter);
SpanFilterBuilder spanFilterBuilder = new SpanFilterBuilder(exporter);
this.spanFilterConfigurer.accept(spanFilterBuilder);
return spanFilterBuilder.build();
}

boolean isDebugEnabled() {
Expand Down

0 comments on commit 4142246

Please sign in to comment.