-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from NthPortal/scala-propagator-support/PR
Support using Scala propagators with Java backend
- Loading branch information
Showing
25 changed files
with
1,249 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
otel4s | ||
Copyright 2022-2023 Typelevel | ||
Licensed under Apache License 2.0 (see LICENSE) | ||
|
||
This software contains portions of code derived from scala | ||
https://github.com/scala/scala | ||
Scala | ||
Copyright (c) 2002-2023 EPFL | ||
Copyright (c) 2011-2023 Lightbend, Inc. | ||
|
||
Scala includes software developed at | ||
LAMP/EPFL (https://lamp.epfl.ch/) and | ||
Lightbend, Inc. (https://www.lightbend.com/). | ||
|
||
Licensed under the Apache License, Version 2.0 (see licenses/LICENSE_scala). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ommon/src/main/scala/org/typelevel/otel4s/context/propagation/PassThroughPropagator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.context | ||
package propagation | ||
|
||
import cats.effect.SyncIO | ||
import org.typelevel.otel4s.context.syntax._ | ||
|
||
/** A [[TextMapPropagator]] that extracts a specified collection of fields and | ||
* stores them in a context, and extracts them from a context later for | ||
* injection. It does not interact with telemetry. | ||
*/ | ||
final class PassThroughPropagator[Ctx, K[X] <: Key[X]] private ( | ||
val fields: Iterable[String], | ||
entriesKey: K[List[(String, String)]] | ||
)(implicit c: Contextual.Keyed[Ctx, K]) | ||
extends TextMapPropagator[Ctx] { | ||
|
||
def extract[A](ctx: Ctx, carrier: A)(implicit | ||
getter: TextMapGetter[A] | ||
): Ctx = { | ||
val list = fields.view | ||
.flatMap(k => getter.get(carrier, k).map(k -> _)) | ||
.toList | ||
if (list.isEmpty) ctx else ctx.updated(entriesKey, list) | ||
} | ||
|
||
def inject[A](ctx: Ctx, carrier: A)(implicit updater: TextMapUpdater[A]): A = | ||
ctx | ||
.getOrElse(entriesKey, Nil) | ||
.foldLeft(carrier) { case (c, k -> v) => updater.updated(c, k, v) } | ||
|
||
override def toString: String = | ||
s"PassThroughPropagator{fields=${fields.mkString("[", ", ", "]")}}" | ||
} | ||
|
||
object PassThroughPropagator { | ||
private def forDistinctFields[Ctx, K[X] <: Key[X]](fields: Seq[String])( | ||
implicit | ||
c: Contextual.Keyed[Ctx, K], | ||
kp: Key.Provider[SyncIO, K] | ||
): TextMapPropagator[Ctx] = | ||
if (fields.isEmpty) TextMapPropagator.noop | ||
else { | ||
new PassThroughPropagator( | ||
fields, | ||
kp.uniqueKey[List[(String, String)]]( | ||
"otel4s-PassThroughPropagator-entries" | ||
).unsafeRunSync() | ||
) | ||
} | ||
|
||
/** Creates a `PassThroughPropagator` that propagates the given fields. */ | ||
def apply[Ctx, K[X] <: Key[X]](fields: String*)(implicit | ||
c: Contextual.Keyed[Ctx, K], | ||
kp: Key.Provider[SyncIO, K] | ||
): TextMapPropagator[Ctx] = | ||
forDistinctFields(fields.distinct) | ||
|
||
/** Creates a `PassThroughPropagator` that propagates the given fields. */ | ||
def apply[Ctx, K[X] <: Key[X]](fields: Iterable[String])(implicit | ||
c: Contextual.Keyed[Ctx, K], | ||
kp: Key.Provider[SyncIO, K] | ||
): TextMapPropagator[Ctx] = | ||
forDistinctFields(fields.iterator.distinct.toSeq) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 0 additions & 68 deletions
68
core/common/src/main/scala/org/typelevel/otel4s/context/propagation/TextMapSetter.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../src/test/scala/org/typelevel/otel4s/context/propagation/PassThroughPropagatorProps.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.context.propagation | ||
|
||
import munit.ScalaCheckSuite | ||
import org.scalacheck.Prop.forAll | ||
import org.typelevel.otel4s.context.vault.VaultContext | ||
|
||
class PassThroughPropagatorProps extends ScalaCheckSuite { | ||
private[this] def vaultPropagator( | ||
fields: Iterable[String] | ||
): TextMapPropagator[VaultContext] = | ||
PassThroughPropagator(fields) | ||
|
||
property("retrieves all stored values matching fields and no others") { | ||
forAll { | ||
( | ||
entries: Map[String, String], | ||
extraFields: Set[String], | ||
unpropagated: Map[String, String] | ||
) => | ||
val propagator = vaultPropagator(entries.keys.view ++ extraFields) | ||
val toSkip = unpropagated.view | ||
.filterKeys(s => !entries.contains(s) && !extraFields.contains(s)) | ||
.toMap | ||
val extracted = propagator.extract(VaultContext.root, entries ++ toSkip) | ||
val injected = propagator.inject(extracted, Map.empty[String, String]) | ||
assertEquals(injected, entries) | ||
for (key -> _ <- toSkip) assert(!injected.contains(key)) // redundant | ||
} | ||
} | ||
|
||
property("retrieves no values when none were stored") { | ||
forAll { (entries: Map[String, String], extraFields: Seq[String]) => | ||
val propagator = vaultPropagator(entries.keys.view ++ extraFields) | ||
val injected = | ||
propagator.inject(VaultContext.root, Map.empty[String, String]) | ||
assert(injected.isEmpty) | ||
} | ||
} | ||
|
||
property("retrieves no values when created with no fields") { | ||
forAll { (entries: Map[String, String]) => | ||
val propagator = vaultPropagator(Nil) | ||
val extracted = propagator.extract(VaultContext.root, entries) | ||
val injected = propagator.inject(extracted, Map.empty[String, String]) | ||
assertEquals(extracted, VaultContext.root) | ||
assert(injected.isEmpty) | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../src/test/scala/org/typelevel/otel4s/context/propagation/PassThroughPropagatorSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.context.propagation | ||
|
||
import munit.FunSuite | ||
import org.typelevel.otel4s.context.vault.VaultContext | ||
|
||
class PassThroughPropagatorSuite extends FunSuite { | ||
private val propagator: TextMapPropagator[VaultContext] = | ||
PassThroughPropagator("foo", "bar") | ||
|
||
test("propagates only fields given to constructor") { | ||
val entries = Map("foo" -> "0", "bar" -> "1", "baz" -> "2", "qux" -> "3") | ||
val extracted = propagator.extract(VaultContext.root, entries) | ||
val injected = propagator.inject(extracted, Map.empty[String, String]) | ||
assertEquals(injected, Map("foo" -> "0", "bar" -> "1")) | ||
} | ||
|
||
test("propagates nothing when no matching fields") { | ||
val entries = Map("baz" -> "2", "qux" -> "3") | ||
val extracted = propagator.extract(VaultContext.root, entries) | ||
val injected = propagator.inject(extracted, Map.empty[String, String]) | ||
assert(injected.isEmpty) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
core/common/src/test/scala/org/typelevel/otel4s/context/propagation/TextMapSetterProps.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.