diff --git a/sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/baggage/SdkBaggageManager.scala b/sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/baggage/SdkBaggageManager.scala index d9bd2ca57..8ef418328 100644 --- a/sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/baggage/SdkBaggageManager.scala +++ b/sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/baggage/SdkBaggageManager.scala @@ -42,7 +42,7 @@ private[sdk] object SdkBaggageManager { .unsafeRunSync() private def baggageFromContext(context: Context): Baggage = - context.get(BaggageKey).getOrElse(Baggage.empty) + context.getOrElse(BaggageKey, Baggage.empty) def fromLocal[F[_]: LocalContext]: BaggageManager[F] = new SdkBaggageManager[F] diff --git a/sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/context/Context.scala b/sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/context/Context.scala index 72842ca27..7ec3a3db7 100644 --- a/sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/context/Context.scala +++ b/sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/context/Context.scala @@ -32,6 +32,12 @@ sealed trait Context { */ def get[A](key: Context.Key[A]): Option[A] + /** Retrieves the value associated with the given key from the context, if such a value exists; otherwise, returns the + * provided default value. + */ + def getOrElse[A](key: Context.Key[A], default: => A): A = + get(key).getOrElse(default) + /** Creates a copy of this context with the given `value` associated with the given `key`. */ def updated[A](key: Context.Key[A], value: A): Context