Skip to content

Commit

Permalink
Add baggage API #338 (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
duxet authored Dec 21, 2021
1 parent 130d199 commit 73fc0e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package zio.telemetry.opentelemetry

import io.opentelemetry.api.baggage.Baggage

import java.util.concurrent.TimeUnit
import scala.concurrent.ExecutionContext
import io.opentelemetry.api.common.{ AttributeKey, Attributes }
Expand Down Expand Up @@ -305,6 +307,23 @@ object Tracing {
getCurrentSpan.map(_.setAttribute(AttributeKey.doubleArrayKey(name), v))
}

/**
* Sets a baggage entry in the current context
*/
def setBaggage(name: String, value: String): URIO[Tracing, Context] =
for {
contextRef <- currentContext
context <- contextRef.updateAndGet(context =>
Baggage.fromContext(context).toBuilder.put(name, value).build().storeInContext(context)
)
} yield context

/**
* Gets the baggage from current context
*/
def getCurrentBaggage: URIO[Tracing, Baggage] =
getCurrentContext.map(Baggage.fromContext)

/**
* Gets the current SpanContext
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ object TracingSyntax {
i3: DummyImplicit
): ZIO[Tracing with R, E, A] =
effect <* Tracing.setAttribute(name, values)(i1, i2, i3)

def setBaggage(name: String, value: String): ZIO[Tracing with R, E, A] =
effect <* Tracing.setBaggage(name, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ object TracingTest extends DefaultRunnableSpec {
)
assert(tags)(equalTo(expected))
}
},
testM("baggaging") {
for {
_ <- UIO.unit.setBaggage("some", "thing")
baggage <- Tracing.getCurrentBaggage
entryValue = Option(baggage.getEntryValue("some"))
} yield assert(entryValue)(equalTo(Some("thing")))
}
).provideCustomLayer(tracingMockLayer)
)
Expand Down

0 comments on commit 73fc0e7

Please sign in to comment.