Skip to content

Commit

Permalink
Create a span with EmbraceSpanBuilder from factory (#907)
Browse files Browse the repository at this point in the history
## Goal

Expose an overload of the create method with an `EmbraceSpanBuilder` that gets passed in, rather than just the parameters it supports

## Testing

Added extra unit test because why not
  • Loading branch information
bidetofevil authored May 31, 2024
2 parents 098e74f + 6482c08 commit 1fd9b4b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal interface EmbraceSpanFactory {
private: Boolean = internal,
parent: EmbraceSpan? = null
): PersistableEmbraceSpan

fun create(embraceSpanBuilder: EmbraceSpanBuilder): PersistableEmbraceSpan
}

internal class EmbraceSpanFactoryImpl(
Expand All @@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 1fd9b4b

Please sign in to comment.