-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a new wrapper for SpanBuilder and persist all attributes in a …
…snapshot there (#611) ## Goal The SpanBuilder and Span OTel API interfaces are a one-way, write-only street. The previous snapshotting saved span data in EmbraceSpan, but there is data only set in the SpanBuilder that we also have to include on the snapshot, like the attributes that were set on the builder alone. As as result, we have to wrap THAT too, and ensure what is set on the wrapper builder eventually is set on the span that is created. With this, we're basically storing everything in the wrapper and only feeding the OTel span/builder what is necessary when it's necessary. I also took this opportunity to refactor some of the extensions to make them less complex and easier for use going forward. ## Testing Added appropriate tests but largely leveraging existing tests to validate the refactoring is a-ok.
- Loading branch information
Showing
8 changed files
with
159 additions
and
108 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
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
68 changes: 68 additions & 0 deletions
68
...roid-sdk/src/main/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanBuilder.kt
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,68 @@ | ||
package io.embrace.android.embracesdk.internal.spans | ||
|
||
import io.embrace.android.embracesdk.arch.schema.EmbType | ||
import io.embrace.android.embracesdk.arch.schema.EmbraceAttribute | ||
import io.embrace.android.embracesdk.arch.schema.KeySpan | ||
import io.embrace.android.embracesdk.arch.schema.PrivateSpan | ||
import io.embrace.android.embracesdk.arch.schema.TelemetryType | ||
import io.embrace.android.embracesdk.spans.EmbraceSpan | ||
import io.opentelemetry.api.trace.Span | ||
import io.opentelemetry.api.trace.SpanBuilder | ||
import io.opentelemetry.context.Context | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal class EmbraceSpanBuilder( | ||
private val otelSpanBuilder: SpanBuilder, | ||
telemetryType: TelemetryType, | ||
parent: EmbraceSpan? | ||
) { | ||
val embraceAttributes = mutableListOf<EmbraceAttribute>(telemetryType) | ||
|
||
/** | ||
* Extract the parent span from an [EmbraceSpan] and set it as the parent | ||
*/ | ||
init { | ||
if (parent == null) { | ||
otelSpanBuilder.setNoParent() | ||
if (telemetryType == EmbType.Performance.Default) { | ||
makeKey() | ||
} | ||
} else if (parent is EmbraceSpanImpl) { | ||
parent.wrappedSpan()?.let { | ||
otelSpanBuilder.setParent(Context.current().with(it)) | ||
} | ||
} | ||
} | ||
|
||
fun startSpan(startTimeMs: Long): Span { | ||
val startedSpan = otelSpanBuilder.setStartTimestamp(startTimeMs, TimeUnit.MILLISECONDS).startSpan() | ||
embraceAttributes.forEach { embraceAttribute -> | ||
startedSpan.setEmbraceAttribute(embraceAttribute) | ||
} | ||
return startedSpan | ||
} | ||
|
||
/** | ||
* Mark the span generated by this builder as [PrivateSpan] | ||
*/ | ||
fun makePrivate(): EmbraceSpanBuilder { | ||
setEmbraceAttribute(PrivateSpan) | ||
return this | ||
} | ||
|
||
/** | ||
* Mark the span generated by this builder as a [KeySpan] | ||
*/ | ||
private fun makeKey(): EmbraceSpanBuilder { | ||
setEmbraceAttribute(KeySpan) | ||
return this | ||
} | ||
|
||
/** | ||
* Sets an [EmbraceAttribute] on the given [SpanBuilder] and return it | ||
*/ | ||
private fun setEmbraceAttribute(embraceAttribute: EmbraceAttribute): EmbraceSpanBuilder { | ||
embraceAttributes.add(embraceAttribute) | ||
return this | ||
} | ||
} |
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
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
Oops, something went wrong.