Skip to content

Commit

Permalink
SpanContext: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive committed Nov 13, 2023
1 parent 1fd0687 commit 2a1f332
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ object SpanContext {
}

val invalid: SpanContext =
SpanContextImpl(
Impl(
traceId = TraceId.Invalid,
traceIdHex = TraceId.Invalid.toHex,
spanId = SpanId.Invalid,
Expand Down Expand Up @@ -243,7 +243,7 @@ object SpanContext {
remote: Boolean,
isValid: Boolean
): SpanContext =
SpanContextImpl(
Impl(
traceId = traceId,
traceIdHex = traceId.toHex,
spanId = spanId,
Expand All @@ -254,7 +254,7 @@ object SpanContext {
isValid = isValid
)

private final case class SpanContextImpl(
private final case class Impl(
traceId: ByteVector,
traceIdHex: String,
spanId: ByteVector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private[java] object SpanBackendImpl {
def fromJSpan[F[_]: Sync](jSpan: JSpan): SpanBackendImpl[F] =
new SpanBackendImpl(
jSpan,
SpanContextConversion.fromJSpanContext(jSpan.getSpanContext)
SpanContextConversions.toScala(jSpan.getSpanContext)
)

private def toJStatus(status: Status): JStatusCode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private[java] final case class SpanBuilderImpl[F[_]: Sync](
startTimestamp.foreach(d => b.setStartTimestamp(d.length, d.unit))
links.foreach { case (ctx, attributes) =>
b.addLink(
SpanContextConversion.toJSpanContext(ctx),
SpanContextConversions.toJava(ctx),
Conversions.toJAttributes(attributes)
)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ private[java] final case class SpanBuilderImpl[F[_]: Sync](
case Parent.Propagate => underlying
case Parent.Explicit(parent) =>
JSpan
.wrap(SpanContextConversion.toJSpanContext(parent))
.wrap(SpanContextConversions.toJava(parent))
.storeInContext(underlying)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import org.typelevel.otel4s.trace.TraceFlags
import org.typelevel.otel4s.trace.TraceState
import scodec.bits.ByteVector

private[otel4s] object SpanContextConversion {
private[otel4s] object SpanContextConversions {

def fromJSpanContext(context: JSpanContext): SpanContext = {
def toScala(context: JSpanContext): SpanContext = {
val entries = Vector.newBuilder[(String, String)]
context.getTraceState.forEach((k, v) => entries.addOne(k -> v))
val traceState = TraceState.fromVectorUnsafe(entries.result())
Expand All @@ -41,7 +41,7 @@ private[otel4s] object SpanContextConversion {
)
}

def toJSpanContext(context: SpanContext): JSpanContext = {
def toJava(context: SpanContext): JSpanContext = {
val traceId = context.traceIdHex
val spanId = context.spanIdHex
val flags = JTraceFlags.fromByte(context.traceFlags.toByte)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ private[java] class TracerImpl[F[_]: Sync](
case Context.Noop => None
case Context.Wrapped(underlying) =>
Option(JSpan.fromContextOrNull(underlying))
.map(jSpan =>
SpanContextConversion.fromJSpanContext(jSpan.getSpanContext)
)
.map(jSpan => SpanContextConversions.toScala(jSpan.getSpanContext))
}

def currentSpanOrNoop: F[Span[F]] =
Expand All @@ -64,9 +62,7 @@ private[java] class TracerImpl[F[_]: Sync](

def childScope[A](parent: SpanContext)(fa: F[A]): F[A] =
L.local(fa) {
_.map(
JSpan.wrap(SpanContextConversion.toJSpanContext(parent)).storeInContext
)
_.map(JSpan.wrap(SpanContextConversions.toJava(parent)).storeInContext)
}

def rootScope[A](fa: F[A]): F[A] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SpanContextSuite extends ScalaCheckSuite {

test("SpanContext to JSpanContext") {
Prop.forAll(spanContextGen) { ctx =>
val jCtx = SpanContextConversion.toJSpanContext(ctx)
val jCtx = SpanContextConversions.toJava(ctx)

assert(ctx.traceId.toArray.sameElements(jCtx.getTraceIdBytes))
assert(ctx.spanId.toArray.sameElements(jCtx.getSpanIdBytes))
Expand All @@ -76,8 +76,8 @@ class SpanContextSuite extends ScalaCheckSuite {

test("back and forth conversion") {
Prop.forAll(spanContextGen) { ctx =>
val jCtx = SpanContextConversion.toJSpanContext(ctx)
assertEquals(SpanContextConversion.fromJSpanContext(jCtx), ctx)
val jCtx = SpanContextConversions.toJava(ctx)
assertEquals(SpanContextConversions.toScala(jCtx), ctx)
}
}

Expand Down

0 comments on commit 2a1f332

Please sign in to comment.