diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanFactory.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanFactory.kt index 793491535c..7df9c4bf44 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanFactory.kt +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanFactory.kt @@ -18,6 +18,8 @@ internal interface EmbraceSpanFactory { private: Boolean = internal, parent: EmbraceSpan? = null ): PersistableEmbraceSpan + + fun create(embraceSpanBuilder: EmbraceSpanBuilder): PersistableEmbraceSpan } internal class EmbraceSpanFactoryImpl( @@ -32,15 +34,20 @@ internal class EmbraceSpanFactoryImpl( internal: Boolean, private: Boolean, parent: EmbraceSpan? - ): PersistableEmbraceSpan = EmbraceSpanImpl( - spanBuilder = tracer.embraceSpanBuilder( + ): PersistableEmbraceSpan = create( + embraceSpanBuilder = tracer.embraceSpanBuilder( name = name, type = type, internal = internal, private = private, - parent = parent - ), - openTelemetryClock = openTelemetryClock, - spanRepository = spanRepository + parent = parent, + ) ) + + override fun create(embraceSpanBuilder: EmbraceSpanBuilder): PersistableEmbraceSpan = + EmbraceSpanImpl( + spanBuilder = embraceSpanBuilder, + openTelemetryClock = openTelemetryClock, + spanRepository = spanRepository + ) } diff --git a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanFactoryImplTest.kt b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanFactoryImplTest.kt index ff5f9ef8d0..42494f8173 100644 --- a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanFactoryImplTest.kt +++ b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/spans/EmbraceSpanFactoryImplTest.kt @@ -3,7 +3,9 @@ package io.embrace.android.embracesdk.internal.spans import io.embrace.android.embracesdk.arch.schema.EmbType import io.embrace.android.embracesdk.arch.schema.PrivateSpan import io.embrace.android.embracesdk.fakes.FakeClock +import io.embrace.android.embracesdk.fakes.FakePersistableEmbraceSpan import io.embrace.android.embracesdk.fakes.injection.FakeInitModule +import io.opentelemetry.api.trace.Tracer import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull @@ -16,13 +18,15 @@ internal class EmbraceSpanFactoryImplTest { private val clock = FakeClock() private lateinit var embraceSpanFactory: EmbraceSpanFactoryImpl private lateinit var spanRepository: SpanRepository + private lateinit var tracer: Tracer @Before fun setup() { val initModule = FakeInitModule(clock) spanRepository = initModule.openTelemetryModule.spanRepository + tracer = initModule.openTelemetryModule.tracer embraceSpanFactory = EmbraceSpanFactoryImpl( - tracer = initModule.openTelemetryModule.tracer, + tracer = tracer, openTelemetryClock = initModule.openTelemetryClock, spanRepository = spanRepository, ) @@ -64,4 +68,24 @@ internal class EmbraceSpanFactoryImplTest { assertEquals("emb-test", snapshot()?.name) } } + + @Test + fun `span creation with embrace span builder`() { + val spanParent = FakePersistableEmbraceSpan.started() + val spanBuilder = tracer.embraceSpanBuilder( + name = "from-span-builder", + type = EmbType.System.LowPower, + internal = false, + private = false, + parent = spanParent + ) + + with(embraceSpanFactory.create(embraceSpanBuilder = spanBuilder)) { + assertTrue(start(clock.now())) + assertTrue(hasEmbraceAttribute(EmbType.System.LowPower)) + assertEquals(spanParent, parent) + assertFalse(hasEmbraceAttribute(PrivateSpan)) + assertEquals("from-span-builder", snapshot()?.name) + } + } }