|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.android.export |
| 7 | + |
| 8 | +import io.opentelemetry.api.common.AttributeKey |
| 9 | +import io.opentelemetry.contrib.interceptor.InterceptableSpanExporter |
| 10 | +import io.opentelemetry.contrib.interceptor.api.Interceptor |
| 11 | +import io.opentelemetry.sdk.trace.data.SpanData |
| 12 | +import io.opentelemetry.sdk.trace.export.SpanExporter |
| 13 | +import java.util.function.Predicate |
| 14 | + |
| 15 | +@Suppress("UNCHECKED_CAST") |
| 16 | +class FilteringSpanExporterBuilder internal constructor( |
| 17 | + private val delegate: SpanExporter, |
| 18 | +) { |
| 19 | + private var predicate = Predicate { _: SpanData -> false } |
| 20 | + private val interceptor: Interceptor<SpanData> = |
| 21 | + Interceptor<SpanData> { item -> |
| 22 | + when { |
| 23 | + predicate.test(item) -> null |
| 24 | + else -> item |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates a SpanExporter that will not export any spans whose name matches the given name. All |
| 30 | + * other spans will be exported by the delegate. |
| 31 | + * |
| 32 | + * @param name - Entire case sensitive span name to match for exclusion |
| 33 | + * @return a SpanExporter |
| 34 | + */ |
| 35 | + fun rejectSpansNamed(name: String): FilteringSpanExporterBuilder = rejecting { span: SpanData -> name == span.name } |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a SpanExporter that will not export any spans whose name matches the given predicate. |
| 39 | + * All other spans will be exported by the delegate. |
| 40 | + * |
| 41 | + * @param spanNamePredicate - predicate to test the span name atainst |
| 42 | + * @return a SpanExporter |
| 43 | + */ |
| 44 | + fun rejectSpansNamed(spanNamePredicate: Predicate<String>): FilteringSpanExporterBuilder = |
| 45 | + rejecting { span: SpanData -> spanNamePredicate.test(span.name) } |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a SpanExporter that will not export any spans whose name contains the given |
| 49 | + * substring. All other spans will be exported by the delegate. |
| 50 | + * |
| 51 | + * @param substring - Substring go match within the span name |
| 52 | + * @return a SpanExporter |
| 53 | + */ |
| 54 | + fun rejectSpansWithNameContaining(substring: String): FilteringSpanExporterBuilder = |
| 55 | + rejecting { span: SpanData -> span.name.contains(substring) } |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a span exporter that will not export any spans whose SpanData matches the rejecting |
| 59 | + * predicate. |
| 60 | + * |
| 61 | + * @param predicate A predicate that returns true when a span is to be rejected |
| 62 | + * @return this |
| 63 | + */ |
| 64 | + fun rejecting(predicate: Predicate<SpanData>): FilteringSpanExporterBuilder { |
| 65 | + this.predicate = this.predicate.or(predicate) |
| 66 | + return this |
| 67 | + } |
| 68 | + |
| 69 | + fun rejectSpansWithAttributesMatching(attrRejection: MutableMap<AttributeKey<*>, Predicate<*>>): FilteringSpanExporterBuilder { |
| 70 | + if (attrRejection.isEmpty()) { |
| 71 | + return this |
| 72 | + } |
| 73 | + val spanRejecter = |
| 74 | + Predicate { spanData: SpanData -> |
| 75 | + val attributes = spanData.attributes |
| 76 | + attrRejection.entries |
| 77 | + .stream() |
| 78 | + .anyMatch { e: MutableMap.MutableEntry<AttributeKey<*>, Predicate<*>> -> |
| 79 | + val key: AttributeKey<*> = e.key |
| 80 | + val valuePredicate = |
| 81 | + e.value as Predicate<in Any?> |
| 82 | + val attributeValue: Any? = attributes.get(key) |
| 83 | + ( |
| 84 | + attributeValue != null && |
| 85 | + valuePredicate.test(attributeValue) |
| 86 | + ) |
| 87 | + } |
| 88 | + } |
| 89 | + this.predicate = this.predicate.or(spanRejecter) |
| 90 | + return this |
| 91 | + } |
| 92 | + |
| 93 | + fun build(): SpanExporter = InterceptableSpanExporter(delegate, interceptor) |
| 94 | +} |
0 commit comments