Skip to content

Commit

Permalink
Merge pull request #327 from iRevive/span-update-name
Browse files Browse the repository at this point in the history
Add `updateName` to `Span`
  • Loading branch information
iRevive authored Oct 2, 2023
2 parents 7b29e59 + e725ca1 commit e9683d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/trace/src/main/scala/org/typelevel/otel4s/trace/Span.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ trait Span[F[_]] extends SpanMacro[F] {
final def context: SpanContext =
backend.context

/** Updates the name of the [[Span]].
*
* '''Note''': if used, this will override the name provided via the
* [[SpanBuilder]].
*
* '''Caution''': upon this update, any sampling behavior based on span's
* name will depend on the implementation.
*
* @param name
* the new name of the span
*/
final def updateName(name: String): F[Unit] =
backend.updateName(name)

/** Marks the end of [[Span]] execution.
*
* Only the timing of the first end call for a given span will be recorded,
Expand Down Expand Up @@ -102,6 +116,8 @@ object Span {
def meta: InstrumentMeta[F]
def context: SpanContext

def updateName(name: String): F[Unit]

def addAttributes(attributes: Attribute[_]*): F[Unit]
def addEvent(name: String, attributes: Attribute[_]*): F[Unit]

Expand Down Expand Up @@ -131,6 +147,8 @@ object Span {
val meta: InstrumentMeta[F] = InstrumentMeta.disabled
val context: SpanContext = SpanContext.invalid

def updateName(name: String): F[Unit] = unit

def addAttributes(attributes: Attribute[_]*): F[Unit] = unit
def addEvent(name: String, attributes: Attribute[_]*): F[Unit] = unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ private[java] class SpanBackendImpl[F[_]: Sync](
def context: SpanContext =
spanContext

def updateName(name: String): F[Unit] =
Sync[F].delay {
jSpan.updateName(name)
()
}

def addAttributes(attributes: Attribute[_]*): F[Unit] =
Sync[F].delay {
jSpan.setAllAttributes(Conversions.toJAttributes(attributes))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ class TracerSuite extends CatsEffectSuite {
)
}

test("update span name") {
for {
sdk <- makeSdk()
tracer <- sdk.provider.get("tracer")
_ <- tracer.span("span").use(span => span.updateName("span-use"))
spans <- sdk.finishedSpans
} yield assertEquals(spans.map(_.getName), List("span-use"))
}

test("set attributes only once") {
val key = "string-attribute"
val value = "value"
Expand Down

0 comments on commit e9683d1

Please sign in to comment.