-
Notifications
You must be signed in to change notification settings - Fork 75
Convert AttributeModifyingSpanExporter to Kotlin #1334
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.export | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey | ||
| import io.opentelemetry.api.common.Attributes | ||
| import io.opentelemetry.sdk.common.CompletableResultCode | ||
| import io.opentelemetry.sdk.trace.data.SpanData | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter | ||
|
|
||
| typealias SpanAttributeReplacement = (AttributeKey<*>, Any?) -> Any? | ||
|
|
||
| /** | ||
| * A SpanExporter that is configured to modify some of its attributes at export time. | ||
| */ | ||
| class AttributeModifyingSpanExporter( | ||
| private val delegate: SpanExporter, | ||
| private val spanAttributeReplacements: SpanAttributeReplacement, | ||
| ) : SpanExporter { | ||
| override fun export(spans: Collection<SpanData>): CompletableResultCode = delegate.export(spans.map(::buildModifiedAttributes)) | ||
|
|
||
| private fun buildModifiedAttributes(span: SpanData): ModifiedSpanData { | ||
| val originals = span.attributes.asMap() | ||
| val modified = | ||
| originals.mapValues { entry -> | ||
| spanAttributeReplacements(entry.key, entry.value) | ||
| } | ||
|
|
||
| val builder = Attributes.builder() | ||
| modified.forEach { | ||
| @Suppress("UNCHECKED_CAST") | ||
| builder.put(it.key as AttributeKey<Any>, it.value) | ||
| } | ||
| return ModifiedSpanData(span, builder.build()) | ||
| } | ||
|
|
||
| override fun flush(): CompletableResultCode = delegate.flush() | ||
|
|
||
| override fun shutdown(): CompletableResultCode = delegate.shutdown() | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.android.export | ||
|
|
||
| import io.opentelemetry.api.common.Attributes | ||
| import io.opentelemetry.sdk.trace.data.DelegatingSpanData | ||
| import io.opentelemetry.sdk.trace.data.SpanData | ||
|
|
||
| internal class ModifiedSpanData( | ||
| original: SpanData, | ||
| private val modifiedAttributes: Attributes, | ||
| ) : DelegatingSpanData(original) { | ||
| override fun getAttributes(): Attributes = modifiedAttributes | ||
|
|
||
| override fun getTotalAttributeCount(): Int = modifiedAttributes.size() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,7 +132,18 @@ public SpanExporter build() { | |
| if (!spanAttributeReplacements.isEmpty()) { | ||
| modifier = | ||
| new AttributeModifyingSpanExporter( | ||
| delegate, new HashMap<>(spanAttributeReplacements)); | ||
| delegate, | ||
| (attributeKey, value) -> { | ||
| //noinspection unchecked | ||
| Function<Object, Object> function = | ||
| (Function<Object, Object>) | ||
| spanAttributeReplacements.get(attributeKey); | ||
| if (function != null) { | ||
| return function.apply(value); | ||
| } else { | ||
| return value; | ||
|
Comment on lines
+143
to
+144
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. nit: Prefer omitting the redundant else (and yeah, if it was kotlin it would be even cleaner to return from the 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 don't see how this else is redundant as the lambda requires a return statement if 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 he's talking about writing it like this: if (function != null) {
return function.apply(value);
}
return value;Maybe it sounded like omitting the "whole else", which might be confusing. |
||
| } | ||
| }); | ||
| } | ||
| return FilteringSpanExporter.builder(modifier) | ||
| .rejectSpansWithAttributesMatching(new HashMap<>(rejectSpanAttributesPredicates)) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.