Skip to content

Commit

Permalink
fixup! Redesign context implementation
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
NthPortal committed Aug 21, 2023
1 parent b4b53c5 commit b501b98
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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

import cats.effect.SyncIO
import munit.FunSuite
import org.typelevel.otel4s.context.Context.Implicits._
import org.typelevel.otel4s.context.vault.VaultContext

class ContextSuite extends FunSuite {
test("implicit syntax") {
def check[C, K[_]](implicit
c: Context.Keyed[C, K],
kp: Key.Provider[SyncIO, K]
): Unit = {
val key1 = kp.uniqueKey[String]("key1").unsafeRunSync()
val key2 = kp.uniqueKey[Int]("key2").unsafeRunSync()

var ctx = c.root
assertEquals(ctx.get(key1), None)
assertEquals(ctx.get(key2), None)

ctx = ctx.updated(key1, "1")
assertEquals(ctx.get(key1), Some("1"))
assertEquals(ctx.get(key2), None)

ctx = ctx.updated(key1, "2")
assertEquals(ctx.get(key1), Some("2"))
assertEquals(ctx.get(key2), None)

ctx = ctx.updated(key2, 1)
assertEquals(ctx.get(key1), Some("2"))
assertEquals(ctx.get(key2), Some(1))
}

check[VaultContext, VaultContext.Key]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.trace.StatusCode
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator
import io.opentelemetry.context.propagation.{
TextMapPropagator,
ContextPropagators => JContextPropagators
}
import io.opentelemetry.extension.incubator.propagation.PassThroughPropagator
import io.opentelemetry.sdk.common.InstrumentationScopeInfo
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter
import io.opentelemetry.sdk.testing.time.TestClock
Expand Down Expand Up @@ -140,6 +142,9 @@ class TracerSuite extends CatsEffectSuite {
} yield {
assertEquals(spans.map(_.getTraceId), List(span.context.traceIdHex))
assertEquals(spans.map(_.getSpanId), List(span.context.spanIdHex))
val key = JAttributeKey.stringKey("string-attribute")
val attr = spans.map(data => Option(data.getAttributes.get(key)))
assertEquals(attr.flatten, List("value"))
}
}

Expand All @@ -162,6 +167,26 @@ class TracerSuite extends CatsEffectSuite {
}
}

// typelevel/otel4s#277
test("retain all of a provided context through propagation") {
TestControl.executeEmbed {
for {
sdk <- makeSdk(additionalPropagators =
Seq(PassThroughPropagator.create("foo", "bar"))
)
tracer <- sdk.provider.get("tracer")
_ <- tracer.joinOrRoot(Map("foo" -> "1", "baz" -> "2")) {
for {
carrier <- tracer.propagate(Map.empty[String, String])
} yield {
assertEquals(carrier.size, 1)
assertEquals(carrier.get("foo"), Some("1"))
}
}
} yield ()
}
}

test("automatically start and stop span") {
val sleepDuration = 500.millis

Expand Down Expand Up @@ -859,7 +884,9 @@ class TracerSuite extends CatsEffectSuite {
}

private def makeSdk(
customize: SdkTracerProviderBuilder => SdkTracerProviderBuilder = identity
customize: SdkTracerProviderBuilder => SdkTracerProviderBuilder =
identity,
additionalPropagators: Seq[TextMapPropagator] = Nil,
): IO[TracerSuite.Sdk] = {
val exporter = InMemorySpanExporter.create()

Expand All @@ -871,8 +898,12 @@ class TracerSuite extends CatsEffectSuite {
customize(builder).build()

IOLocal(Context.root).map { implicit ioLocal: IOLocal[Context] =>
val textMapPropagators =
W3CTraceContextPropagator.getInstance() +: additionalPropagators
val propagators = new ContextPropagatorsImpl[IO](
JContextPropagators.create(W3CTraceContextPropagator.getInstance())
JContextPropagators.create(
TextMapPropagator.composite(textMapPropagators.asJava)
)
)

val provider = TracerProviderImpl.local[IO](tracerProvider, propagators)
Expand Down

0 comments on commit b501b98

Please sign in to comment.