Skip to content

Commit

Permalink
cleanup HTTP propagation, introduce a new Binary propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantopo committed Sep 28, 2018
1 parent 6941f53 commit b61c92e
Show file tree
Hide file tree
Showing 22 changed files with 817 additions and 723 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.scalatest.time.SpanSugar._

class KamonLifecycleSpec extends WordSpec with Matchers with Eventually{

"the Kamon lifecycle" should {
"the Kamon lifecycle" ignore {
"keep the JVM running if reporters are running" in {
val process = Runtime.getRuntime.exec(createProcessCommand("kamon.KamonWithRunningReporter"))
Thread.sleep(5000)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package kamon.context

import java.io.ByteArrayOutputStream

import com.typesafe.config.ConfigFactory
import kamon.Kamon
import kamon.context.BinaryPropagation.{ByteStreamReader, ByteStreamWriter}
import kamon.context.Propagation.{EntryReader, EntryWriter}
import org.scalatest.{Matchers, OptionValues, WordSpec}

class BinaryPropagationSpec extends WordSpec with Matchers with OptionValues {

"The Binary Context Propagation" should {
"return an empty context if there is no data to read from" in {
val context = binaryPropagation.read(ByteStreamReader.of(Array.ofDim[Byte](0)))
context.isEmpty() shouldBe true
}

"not write any data to the medium if the context is empty" in {
val writer = inspectableByteStreamWriter()
binaryPropagation.write(Context.Empty, writer)
writer.size() shouldBe 0
}

"round trip a Context that only has tags" in {
val context = Context.of(Map("hello" -> "world", "kamon" -> "rulez"))
val writer = inspectableByteStreamWriter()
binaryPropagation.write(context, writer)

val rtContext = binaryPropagation.read(ByteStreamReader.of(writer.toByteArray))
rtContext.entries shouldBe empty
rtContext.tags should contain theSameElementsAs (context.tags)
}

"round trip a Context that only has entries" in {
val context = Context.of(BinaryPropagationSpec.StringKey, "string-value", BinaryPropagationSpec.IntegerKey, 42)
val writer = inspectableByteStreamWriter()
binaryPropagation.write(context, writer)

val rtContext = binaryPropagation.read(ByteStreamReader.of(writer.toByteArray))
rtContext.tags shouldBe empty
rtContext.get(BinaryPropagationSpec.StringKey) shouldBe "string-value"
rtContext.get(BinaryPropagationSpec.IntegerKey) shouldBe 0 // there is no entry configuration for the integer key
}

"round trip a Context that with tags and entries" in {
val context = Context.of(Map("hello" -> "world", "kamon" -> "rulez"))
.withKey(BinaryPropagationSpec.StringKey, "string-value")
.withKey(BinaryPropagationSpec.IntegerKey, 42)

val writer = inspectableByteStreamWriter()
binaryPropagation.write(context, writer)
val rtContext = binaryPropagation.read(ByteStreamReader.of(writer.toByteArray))

rtContext.tags should contain theSameElementsAs (context.tags)
rtContext.get(BinaryPropagationSpec.StringKey) shouldBe "string-value"
rtContext.get(BinaryPropagationSpec.IntegerKey) shouldBe 0 // there is no entry configuration for the integer key
}
}

val binaryPropagation = BinaryPropagation.from(
ConfigFactory.parseString(
"""
|
|entries.incoming.string = "kamon.context.BinaryPropagationSpec$StringEntryCodec"
|entries.outgoing.string = "kamon.context.BinaryPropagationSpec$StringEntryCodec"
|
""".stripMargin
).withFallback(ConfigFactory.load().getConfig("kamon.propagation")), Kamon)


def inspectableByteStreamWriter() = new ByteArrayOutputStream(32) with ByteStreamWriter

}

object BinaryPropagationSpec {

val StringKey = Context.key[String]("string", null)
val IntegerKey = Context.key[Int]("integer", 0)

class StringEntryCodec extends EntryReader[ByteStreamReader] with EntryWriter[ByteStreamWriter] {

override def read(medium: ByteStreamReader, context: Context): Context = {
val valueData = medium.readAll()

if(valueData.length > 0) {
context.withKey(StringKey, new String(valueData))
} else context
}

override def write(context: Context, medium: ByteStreamWriter): Unit = {
val value = context.get(StringKey)
if(value != null) {
medium.write(value.getBytes)
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ import kamon.testkit.ContextTesting
import org.scalatest.{Matchers, OptionValues, WordSpec}

class ContextSerializationSpec extends WordSpec with Matchers with ContextTesting with OptionValues {
"the Context is Serializable" ignore {
"empty " in {
val bos = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(bos)
oos.writeObject(Context.Empty)

val ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray))
val ctx = ois.readObject().asInstanceOf[Context]
ctx shouldBe Context.Empty
}

"full" in {
val sCtx = Context.of(StringBroadcastKey, Some("disi"))
val bos = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(bos)
oos.writeObject(sCtx)

val ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray))
val rCtx = ois.readObject().asInstanceOf[Context]
rCtx shouldBe sCtx
}

}

val ContextCodec = new Codecs(Kamon.config())
// "the Context is Serializable" ignore {
// "empty " in {
// val bos = new ByteArrayOutputStream()
// val oos = new ObjectOutputStream(bos)
// oos.writeObject(Context.Empty)
//
// val ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray))
// val ctx = ois.readObject().asInstanceOf[Context]
// ctx shouldBe Context.Empty
// }
//
// "full" in {
// val sCtx = Context.of(StringBroadcastKey, Some("disi"))
// val bos = new ByteArrayOutputStream()
// val oos = new ObjectOutputStream(bos)
// oos.writeObject(sCtx)
//
// val ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray))
// val rCtx = ois.readObject().asInstanceOf[Context]
// rCtx shouldBe sCtx
// }
//
// }
//
// val ContextCodec = new Codecs(Kamon.config())
}
Loading

0 comments on commit b61c92e

Please sign in to comment.