aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2019-02-25 00:21:40 +0100
committerIvan Topolnjak <ivantopo@gmail.com>2019-03-27 13:56:30 +0100
commit91575b8db60ca4fc6df9f44fa720929d8c3868ac (patch)
tree983f8f6a7e9c7e71930f7930fadaf6ae4bffd2a4
parentff7c7b89335e3d3c463a57cd24321a1419a587ed (diff)
downloadKamon-91575b8db60ca4fc6df9f44fa720929d8c3868ac.tar.gz
Kamon-91575b8db60ca4fc6df9f44fa720929d8c3868ac.tar.bz2
Kamon-91575b8db60ca4fc6df9f44fa720929d8c3868ac.zip
use TagSet as the implementation for Context tags
-rw-r--r--kamon-core-tests/src/test/scala/kamon/context/BinaryPropagationSpec.scala12
-rw-r--r--kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala21
-rw-r--r--kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala13
-rw-r--r--kamon-core-tests/src/test/scala/kamon/tag/TagSetSpec.scala (renamed from kamon-core-tests/src/test/scala/kamon/tag/TagsSpec.scala)54
-rw-r--r--kamon-core/src/main/colfer/Context.colf27
-rw-r--r--kamon-core/src/main/java/kamon/context/generated/binary/context/BooleanTag.java412
-rw-r--r--kamon-core/src/main/java/kamon/context/generated/binary/context/Context.java107
-rw-r--r--kamon-core/src/main/java/kamon/context/generated/binary/context/Entry.java76
-rw-r--r--kamon-core/src/main/java/kamon/context/generated/binary/context/LongTag.java443
-rw-r--r--kamon-core/src/main/java/kamon/context/generated/binary/context/StringTag.java466
-rw-r--r--kamon-core/src/main/java/kamon/context/generated/binary/context/Tags.java513
-rw-r--r--kamon-core/src/main/scala/kamon/context/BinaryPropagation.scala81
-rw-r--r--kamon-core/src/main/scala/kamon/context/Context.scala126
-rw-r--r--kamon-core/src/main/scala/kamon/context/HttpPropagation.scala65
-rw-r--r--kamon-core/src/main/scala/kamon/instrumentation/HttpServer.scala24
-rw-r--r--kamon-core/src/main/scala/kamon/metric/Accumulator.scala6
-rw-r--r--kamon-core/src/main/scala/kamon/metric/Metric.scala24
-rw-r--r--kamon-core/src/main/scala/kamon/metric/PeriodSnapshot.scala4
-rw-r--r--kamon-core/src/main/scala/kamon/metric/Timer.scala6
-rw-r--r--kamon-core/src/main/scala/kamon/package.scala2
-rw-r--r--kamon-core/src/main/scala/kamon/tag/Lookups.scala32
-rw-r--r--kamon-core/src/main/scala/kamon/tag/Tag.scala50
-rw-r--r--kamon-core/src/main/scala/kamon/tag/TagSet.scala (renamed from kamon-core/src/main/scala/kamon/tag/Tags.scala)160
23 files changed, 2341 insertions, 383 deletions
diff --git a/kamon-core-tests/src/test/scala/kamon/context/BinaryPropagationSpec.scala b/kamon-core-tests/src/test/scala/kamon/context/BinaryPropagationSpec.scala
index 5681d300..4fa7116d 100644
--- a/kamon-core-tests/src/test/scala/kamon/context/BinaryPropagationSpec.scala
+++ b/kamon-core-tests/src/test/scala/kamon/context/BinaryPropagationSpec.scala
@@ -6,6 +6,8 @@ import com.typesafe.config.ConfigFactory
import kamon.Kamon
import kamon.context.BinaryPropagation.{ByteStreamReader, ByteStreamWriter}
import kamon.context.Propagation.{EntryReader, EntryWriter}
+import kamon.tag.TagSet
+import kamon.tag.Lookups._
import org.scalatest.{Matchers, OptionValues, WordSpec}
import scala.util.Random
@@ -70,13 +72,14 @@ class BinaryPropagationSpec extends WordSpec with Matchers with OptionValues {
}
"round trip a Context that only has tags" in {
- val context = Context.of(Map("hello" -> "world", "kamon" -> "rulez"))
+ val context = Context.of(TagSet.from(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)
+ rtContext.tags.get(plain("hello")) shouldBe "world"
+ rtContext.tags.get(plain("kamon")) shouldBe "rulez"
}
"round trip a Context that only has entries" in {
@@ -91,7 +94,7 @@ class BinaryPropagationSpec extends WordSpec with Matchers with OptionValues {
}
"round trip a Context that with tags and entries" in {
- val context = Context.of(Map("hello" -> "world", "kamon" -> "rulez"))
+ val context = Context.of(TagSet.from(Map("hello" -> "world", "kamon" -> "rulez")))
.withKey(BinaryPropagationSpec.StringKey, "string-value")
.withKey(BinaryPropagationSpec.IntegerKey, 42)
@@ -99,7 +102,8 @@ class BinaryPropagationSpec extends WordSpec with Matchers with OptionValues {
binaryPropagation.write(context, writer)
val rtContext = binaryPropagation.read(ByteStreamReader.of(writer.toByteArray))
- rtContext.tags should contain theSameElementsAs (context.tags)
+ rtContext.tags.get(plain("hello")) shouldBe "world"
+ rtContext.tags.get(plain("kamon")) shouldBe "rulez"
rtContext.get(BinaryPropagationSpec.StringKey) shouldBe "string-value"
rtContext.get(BinaryPropagationSpec.IntegerKey) shouldBe 0 // there is no entry configuration for the integer key
}
diff --git a/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala b/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala
index fcddfe24..0cd10672 100644
--- a/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala
+++ b/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala
@@ -5,6 +5,8 @@ import kamon.Kamon
import kamon.context.HttpPropagation.{HeaderReader, HeaderWriter}
import kamon.context.Propagation.{EntryReader, EntryWriter}
import org.scalatest.{Matchers, OptionValues, WordSpec}
+import kamon.tag.Lookups._
+import kamon.tag.TagSet
import scala.collection.mutable
@@ -22,12 +24,11 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues {
"x-content-tags" -> "hello=world;correlation=1234",
"x-mapped-tag" -> "value"
)
+
val context = httpPropagation.read(headerReaderFromMap(headers))
- context.tags should contain only(
- "hello" -> "world",
- "correlation" -> "1234",
- "mappedTag" -> "value"
- )
+ context.tags.get(plain("hello")) shouldBe "world"
+ context.tags.get(plain("correlation")) shouldBe "1234"
+ context.tags.get(plain("mappedTag")) shouldBe "value"
}
"handle errors when reading HTTP headers" in {
@@ -48,9 +49,9 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues {
context.get(HttpPropagationSpec.StringKey) shouldBe "hey"
context.get(HttpPropagationSpec.IntegerKey) shouldBe 123
context.get(HttpPropagationSpec.OptionalKey) shouldBe empty
- context.getTag("hello").value shouldBe "world"
- context.getTag("correlation").value shouldBe "1234"
- context.getTag("unknown") shouldBe empty
+ context.getTag(plain("hello")) shouldBe "world"
+ context.getTag(option("correlation")).value shouldBe "1234"
+ context.getTag(option("unknown")) shouldBe empty
}
}
@@ -64,10 +65,10 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues {
"write context tags when available" in {
val headers = mutable.Map.empty[String, String]
- val context = Context.of(Map(
+ val context = Context.of(TagSet.from(Map(
"hello" -> "world",
"mappedTag" -> "value"
- ))
+ )))
httpPropagation.write(context, headerWriterFromMap(headers))
headers should contain only(
diff --git a/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala b/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala
index 62eae45b..c7e856d0 100644
--- a/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala
+++ b/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala
@@ -4,6 +4,7 @@ import java.time.Duration
import kamon.context.Context
import kamon.metric.{Counter, Histogram, RangeSampler}
+import kamon.tag.Lookups._
import kamon.testkit.{MetricInspection, SpanInspection}
import org.scalatest.concurrent.Eventually
import org.scalatest.{Matchers, OptionValues, WordSpec}
@@ -20,10 +21,8 @@ class HttpServerInstrumentationSpec extends WordSpec with Matchers with SpanInsp
"custom-trace-id" -> "0011223344556677"
)))
- handler.context.tags should contain only(
- "tag" -> "value",
- "none" -> "0011223344556677"
- )
+ handler.context.tags.get(plain("tag")) shouldBe "value"
+ handler.context.tags.get(plain("none")) shouldBe "0011223344556677"
handler.send(fakeResponse(200, mutable.Map.empty), Context.Empty)
handler.doneSending(0L)
@@ -35,10 +34,8 @@ class HttpServerInstrumentationSpec extends WordSpec with Matchers with SpanInsp
"custom-trace-id" -> "0011223344556677"
)))
- handler.context.tags should contain only(
- "tag" -> "value",
- "none" -> "0011223344556677"
- )
+ handler.context.tags.get(plain("tag")) shouldBe "value"
+ handler.context.tags.get(plain("none")) shouldBe "0011223344556677"
val span = inspect(handler.span)
span.context().traceID.string shouldNot be("0011223344556677")
diff --git a/kamon-core-tests/src/test/scala/kamon/tag/TagsSpec.scala b/kamon-core-tests/src/test/scala/kamon/tag/TagSetSpec.scala
index 1a83e1c9..cd23c58d 100644
--- a/kamon-core-tests/src/test/scala/kamon/tag/TagsSpec.scala
+++ b/kamon-core-tests/src/test/scala/kamon/tag/TagSetSpec.scala
@@ -6,25 +6,25 @@ import org.scalatest.{Matchers, WordSpec}
import scala.collection.JavaConverters.mapAsJavaMapConverter
-class TagsSpec extends WordSpec with Matchers {
+class TagSetSpec extends WordSpec with Matchers {
import Lookups._
"Tags" should {
"silently drop null and unacceptable keys and/or values when constructed from the companion object builders" in {
- Tags.from(NullString, NullString).all().size shouldBe 0
- Tags.from(EmptyString, NullString).all().size shouldBe 0
- Tags.from(EmptyString, "value").all().size shouldBe 0
- Tags.from(NullString, "value").all().size shouldBe 0
- Tags.from("key", NullString).all().size shouldBe 0
- Tags.from("key", NullBoolean).all().size shouldBe 0
- Tags.from("key", NullLong).all().size shouldBe 0
-
- Tags.from(BadScalaTagMap).all().size shouldBe 0
- Tags.from(BadJavaTagMap).all().size shouldBe 0
+ TagSet.from(NullString, NullString).all().size shouldBe 0
+ TagSet.from(EmptyString, NullString).all().size shouldBe 0
+ TagSet.from(EmptyString, "value").all().size shouldBe 0
+ TagSet.from(NullString, "value").all().size shouldBe 0
+ TagSet.from("key", NullString).all().size shouldBe 0
+ TagSet.from("key", NullBoolean).all().size shouldBe 0
+ TagSet.from("key", NullLong).all().size shouldBe 0
+
+ TagSet.from(BadScalaTagMap).all().size shouldBe 0
+ TagSet.from(BadJavaTagMap).all().size shouldBe 0
}
"silently drop null keys and/or values when created with the .withTag, withTags or .and methods" in {
- val tags = Tags.from("initialKey", "initialValue")
+ val tags = TagSet.from("initialKey", "initialValue")
.withTag(NullString, NullString)
.withTag(EmptyString, NullString)
.withTag(EmptyString, "value")
@@ -46,14 +46,14 @@ class TagsSpec extends WordSpec with Matchers {
}
"create a properly populated instance when valid pairs are provided" in {
- Tags.from("isAwesome", true).all().size shouldBe 1
- Tags.from("name", "kamon").all().size shouldBe 1
- Tags.from("age", 5L).all().size shouldBe 1
+ TagSet.from("isAwesome", true).all().size shouldBe 1
+ TagSet.from("name", "kamon").all().size shouldBe 1
+ TagSet.from("age", 5L).all().size shouldBe 1
- Tags.from(GoodScalaTagMap).all().size shouldBe 3
- Tags.from(GoodJavaTagMap).all().size shouldBe 3
+ TagSet.from(GoodScalaTagMap).all().size shouldBe 3
+ TagSet.from(GoodJavaTagMap).all().size shouldBe 3
- Tags.from("initial", "initial")
+ TagSet.from("initial", "initial")
.withTag("isAwesome", true)
.withTag("name", "Kamon")
.withTag("age", 5L)
@@ -64,8 +64,8 @@ class TagsSpec extends WordSpec with Matchers {
}
"override pre-existent tags when merging with other Tags instance" in {
- val leftTags = Tags.from(GoodScalaTagMap)
- val rightTags = Tags
+ val leftTags = TagSet.from(GoodScalaTagMap)
+ val rightTags = TagSet
.from("name", "New Kamon")
.and("age", 42L)
.and("isAwesome", false) // just for testing :)
@@ -82,7 +82,7 @@ class TagsSpec extends WordSpec with Matchers {
}
"provide typed access to the contained pairs when looking up values" in {
- val tags = Tags.from(GoodScalaTagMap)
+ val tags = TagSet.from(GoodScalaTagMap)
tags.get(plain("name")) shouldBe "Kamon"
tags.get(plain("none")) shouldBe null
@@ -111,7 +111,7 @@ class TagsSpec extends WordSpec with Matchers {
}
"allow iterating over all contained tags" in {
- val tags = Tags.from(Map(
+ val tags = TagSet.from(Map(
"age" -> 5L,
"name" -> "Kamon",
"isAwesome" -> true,
@@ -130,14 +130,14 @@ class TagsSpec extends WordSpec with Matchers {
}
"be equal to other Tags instance with the same tags" in {
- Tags.from(GoodScalaTagMap) shouldBe Tags.from(GoodScalaTagMap)
- Tags.from(GoodJavaTagMap) shouldBe Tags.from(GoodJavaTagMap)
+ TagSet.from(GoodScalaTagMap) shouldBe TagSet.from(GoodScalaTagMap)
+ TagSet.from(GoodJavaTagMap) shouldBe TagSet.from(GoodJavaTagMap)
}
"have a readable toString implementation" in {
- Tags.from(GoodScalaTagMap).toString() should include("age=5")
- Tags.from(GoodScalaTagMap).toString() should include("name=Kamon")
- Tags.from(GoodScalaTagMap).toString() should include("isAwesome=true")
+ TagSet.from(GoodScalaTagMap).toString() should include("age=5")
+ TagSet.from(GoodScalaTagMap).toString() should include("name=Kamon")
+ TagSet.from(GoodScalaTagMap).toString() should include("isAwesome=true")
}
}
diff --git a/kamon-core/src/main/colfer/Context.colf b/kamon-core/src/main/colfer/Context.colf
index f5d9a80b..9bd9ec76 100644
--- a/kamon-core/src/main/colfer/Context.colf
+++ b/kamon-core/src/main/colfer/Context.colf
@@ -1,11 +1,32 @@
package context
type Entry struct {
- name text
- content binary
+ key text
+ value binary
+}
+
+type StringTag struct {
+ key text
+ value text
+}
+
+type LongTag struct {
+ key text
+ value int64
+}
+
+type BooleanTag struct {
+ key text
+ value bool
+}
+
+type Tags struct {
+ strings []StringTag
+ longs []LongTag
+ booleans []BooleanTag
}
type Context struct {
- tags []text
+ tags Tags
entries []Entry
} \ No newline at end of file
diff --git a/kamon-core/src/main/java/kamon/context/generated/binary/context/BooleanTag.java b/kamon-core/src/main/java/kamon/context/generated/binary/context/BooleanTag.java
new file mode 100644
index 00000000..d8caad09
--- /dev/null
+++ b/kamon-core/src/main/java/kamon/context/generated/binary/context/BooleanTag.java
@@ -0,0 +1,412 @@
+package kamon.context.generated.binary.context;
+
+
+// Code generated by colf(1); DO NOT EDIT.
+
+
+import static java.lang.String.format;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.util.InputMismatchException;
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
+
+
+/**
+ * Data bean with built-in serialization support.
+
+ * @author generated by colf(1)
+ * @see <a href="https://github.com/pascaldekloe/colfer">Colfer's home</a>
+ */
+@javax.annotation.Generated(value="colf(1)", comments="Colfer from schema file Context.colf")
+public class BooleanTag implements Serializable {
+
+ /** The upper limit for serial byte sizes. */
+ public static int colferSizeMax = 16 * 1024 * 1024;
+
+
+
+
+ public String key;
+
+ public boolean value;
+
+
+ /** Default constructor */
+ public BooleanTag() {
+ init();
+ }
+
+
+ /** Colfer zero values. */
+ private void init() {
+ key = "";
+ }
+
+ /**
+ * {@link #reset(InputStream) Reusable} deserialization of Colfer streams.
+ */
+ public static class Unmarshaller {
+
+ /** The data source. */
+ protected InputStream in;
+
+ /** The read buffer. */
+ public byte[] buf;
+
+ /** The {@link #buf buffer}'s data start index, inclusive. */
+ protected int offset;
+
+ /** The {@link #buf buffer}'s data end index, exclusive. */
+ protected int i;
+
+
+ /**
+ * @param in the data source or {@code null}.
+ * @param buf the initial buffer or {@code null}.
+ */
+ public Unmarshaller(InputStream in, byte[] buf) {
+ // TODO: better size estimation
+ if (buf == null || buf.length == 0)
+ buf = new byte[Math.min(BooleanTag.colferSizeMax, 2048)];
+ this.buf = buf;
+ reset(in);
+ }
+
+ /**
+ * Reuses the marshaller.
+ * @param in the data source or {@code null}.
+ * @throws IllegalStateException on pending data.
+ */
+ public void reset(InputStream in) {
+ if (this.i != this.offset) throw new IllegalStateException("colfer: pending data");
+ this.in = in;
+ this.offset = 0;
+ this.i = 0;
+ }
+
+ /**
+ * Deserializes the following object.
+ * @return the result or {@code null} when EOF.
+ * @throws IOException from the input stream.
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public BooleanTag next() throws IOException {
+ if (in == null) return null;
+
+ while (true) {
+ if (this.i > this.offset) {
+ try {
+ BooleanTag o = new BooleanTag();
+ this.offset = o.unmarshal(this.buf, this.offset, this.i);
+ return o;
+ } catch (BufferUnderflowException e) {
+ }
+ }
+ // not enough data
+
+ if (this.i <= this.offset) {
+ this.offset = 0;
+ this.i = 0;
+ } else if (i == buf.length) {
+ byte[] src = this.buf;
+ // TODO: better size estimation
+ if (offset == 0) this.buf = new byte[Math.min(BooleanTag.colferSizeMax, this.buf.length * 4)];
+ System.arraycopy(src, this.offset, this.buf, 0, this.i - this.offset);
+ this.i -= this.offset;
+ this.offset = 0;
+ }
+ assert this.i < this.buf.length;
+
+ int n = in.read(buf, i, buf.length - i);
+ if (n < 0) {
+ if (this.i > this.offset)
+ throw new InputMismatchException("colfer: pending data with EOF");
+ return null;
+ }
+ assert n > 0;
+ i += n;
+ }
+ }
+
+ }
+
+
+ /**
+ * Serializes the object.
+ * @param out the data destination.
+ * @param buf the initial buffer or {@code null}.
+ * @return the final buffer. When the serial fits into {@code buf} then the return is {@code buf}.
+ * Otherwise the return is a new buffer, large enough to hold the whole serial.
+ * @throws IOException from {@code out}.
+ * @throws IllegalStateException on an upper limit breach defined by {@link #colferSizeMax}.
+ */
+ public byte[] marshal(OutputStream out, byte[] buf) throws IOException {
+ // TODO: better size estimation
+ if (buf == null || buf.length == 0)
+ buf = new byte[Math.min(BooleanTag.colferSizeMax, 2048)];
+
+ while (true) {
+ int i;
+ try {
+ i = marshal(buf, 0);
+ } catch (BufferOverflowException e) {
+ buf = new byte[Math.min(BooleanTag.colferSizeMax, buf.length * 4)];
+ continue;
+ }
+
+ out.write(buf, 0, i);
+ return buf;
+ }
+ }
+
+ /**
+ * Serializes the object.
+ * @param buf the data destination.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferOverflowException when {@code buf} is too small.
+ * @throws IllegalStateException on an upper limit breach defined by {@link #colferSizeMax}.
+ */
+ public int marshal(byte[] buf, int offset) {
+ int i = offset;
+
+ try {
+ if (! this.key.isEmpty()) {
+ buf[i++] = (byte) 0;
+ int start = ++i;
+
+ String s = this.key;
+ for (int sIndex = 0, sLength = s.length(); sIndex < sLength; sIndex++) {
+ char c = s.charAt(sIndex);
+ if (c < '\u0080') {
+ buf[i++] = (byte) c;
+ } else if (c < '\u0800') {
+ buf[i++] = (byte) (192 | c >>> 6);
+ buf[i++] = (byte) (128 | c & 63);
+ } else if (c < '\ud800' || c > '\udfff') {
+ buf[i++] = (byte) (224 | c >>> 12);
+ buf[i++] = (byte) (128 | c >>> 6 & 63);
+ buf[i++] = (byte) (128 | c & 63);
+ } else {
+ int cp = 0;
+ if (++sIndex < sLength) cp = Character.toCodePoint(c, s.charAt(sIndex));
+ if ((cp >= 1 << 16) && (cp < 1 << 21)) {
+ buf[i++] = (byte) (240 | cp >>> 18);
+ buf[i++] = (byte) (128 | cp >>> 12 & 63);
+ buf[i++] = (byte) (128 | cp >>> 6 & 63);
+ buf[i++] = (byte) (128 | cp & 63);
+ } else
+ buf[i++] = (byte) '?';
+ }
+ }
+ int size = i - start;
+ if (size > BooleanTag.colferSizeMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.BooleanTag.key size %d exceeds %d UTF-8 bytes", size, BooleanTag.colferSizeMax));
+
+ int ii = start - 1;
+ if (size > 0x7f) {
+ i++;
+ for (int x = size; x >= 1 << 14; x >>>= 7) i++;
+ System.arraycopy(buf, start, buf, i - size, size);
+
+ do {
+ buf[ii++] = (byte) (size | 0x80);
+ size >>>= 7;
+ } while (size > 0x7f);
+ }
+ buf[ii] = (byte) size;
+ }
+
+ if (this.value) {
+ buf[i++] = (byte) 1;
+ }
+
+ buf[i++] = (byte) 0x7f;
+ return i;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ if (i - offset > BooleanTag.colferSizeMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.BooleanTag exceeds %d bytes", BooleanTag.colferSizeMax));
+ if (i > buf.length) throw new BufferOverflowException();
+ throw e;
+ }
+ }
+
+ /**
+ * Deserializes the object.
+ * @param buf the data source.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public int unmarshal(byte[] buf, int offset) {
+ return unmarshal(buf, offset, buf.length);
+ }
+
+ /**
+ * Deserializes the object.
+ * @param buf the data source.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @param end the index limit for {@code buf}, exclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public int unmarshal(byte[] buf, int offset, int end) {
+ if (end > buf.length) end = buf.length;
+ int i = offset;
+
+ try {
+ byte header = buf[i++];
+
+ if (header == (byte) 0) {
+ int size = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ size |= (b & 0x7f) << shift;
+ if (shift == 28 || b >= 0) break;
+ }
+ if (size < 0 || size > BooleanTag.colferSizeMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.BooleanTag.key size %d exceeds %d UTF-8 bytes", size, BooleanTag.colferSizeMax));
+
+ int start = i;
+ i += size;
+ this.key = new String(buf, start, size, StandardCharsets.UTF_8);
+ header = buf[i++];
+ }
+
+ if (header == (byte) 1) {
+ this.value = true;
+ header = buf[i++];
+ }
+
+ if (header != (byte) 0x7f)
+ throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
+ } finally {
+ if (i > end && end - offset < BooleanTag.colferSizeMax) throw new BufferUnderflowException();
+ if (i < 0 || i - offset > BooleanTag.colferSizeMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.BooleanTag exceeds %d bytes", BooleanTag.colferSizeMax));
+ if (i > end) throw new BufferUnderflowException();
+ }
+
+ return i;
+ }
+
+ // {@link Serializable} version number.
+ private static final long serialVersionUID = 2L;
+
+ // {@link Serializable} Colfer extension.
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ // TODO: better size estimation
+ byte[] buf = new byte[1024];
+ int n;
+ while (true) try {
+ n = marshal(buf, 0);
+ break;
+ } catch (BufferUnderflowException e) {
+ buf = new byte[4 * buf.length];
+ }
+
+ out.writeInt(n);
+ out.write(buf, 0, n);
+ }
+
+ // {@link Serializable} Colfer extension.
+ private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
+ init();
+
+ int n = in.readInt();
+ byte[] buf = new byte[n];
+ in.readFully(buf);
+ unmarshal(buf, 0);
+ }
+
+ // {@link Serializable} Colfer extension.
+ private void readObjectNoData() throws ObjectStreamException {
+ init();
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.BooleanTag.key.
+ * @return the value.
+ */
+ public String getKey() {
+ return this.key;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.BooleanTag.key.
+ * @param value the replacement.
+ */
+ public void setKey(String value) {
+ this.key = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.BooleanTag.key.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public BooleanTag withKey(String value) {
+ this.key = value;
+ return this;
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.BooleanTag.value.
+ * @return the value.
+ */
+ public boolean getValue() {
+ return this.value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.BooleanTag.value.
+ * @param value the replacement.
+ */
+ public void setValue(boolean value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.BooleanTag.value.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public BooleanTag withValue(boolean value) {
+ this.value = value;
+ return this;
+ }
+
+ @Override
+ public final int hashCode() {
+ int h = 1;
+ if (this.key != null) h = 31 * h + this.key.hashCode();
+ h = 31 * h + (this.value ? 1231 : 1237);
+ return h;
+ }
+
+ @Override
+ public final boolean equals(Object o) {
+ return o instanceof BooleanTag && equals((BooleanTag) o);
+ }
+
+ public final boolean equals(BooleanTag o) {
+ if (o == null) return false;
+ if (o == this) return true;
+ return o.getClass() == BooleanTag.class
+ && (this.key == null ? o.key == null : this.key.equals(o.key))
+ && this.value == o.value;
+ }
+
+}
diff --git a/kamon-core/src/main/java/kamon/context/generated/binary/context/Context.java b/kamon-core/src/main/java/kamon/context/generated/binary/context/Context.java
index 4be6d630..3582bfa2 100644
--- a/kamon-core/src/main/java/kamon/context/generated/binary/context/Context.java
+++ b/kamon-core/src/main/java/kamon/context/generated/binary/context/Context.java
@@ -12,7 +12,6 @@ import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.OutputStream;
import java.io.Serializable;
-import java.nio.charset.StandardCharsets;
import java.util.InputMismatchException;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
@@ -36,7 +35,7 @@ public class Context implements Serializable {
- public String[] tags;
+ public Tags tags;
public Entry[] entries;
@@ -46,12 +45,10 @@ public class Context implements Serializable {
init();
}
- private static final String[] _zeroTags = new String[0];
private static final Entry[] _zeroEntries = new Entry[0];
/** Colfer zero values. */
private void init() {
- tags = _zeroTags;
entries = _zeroEntries;
}
@@ -147,7 +144,6 @@ public class Context implements Serializable {
/**
* Serializes the object.
- * All {@code null} elements in {@link #tags} will be replaced with {@code ""}.
* All {@code null} elements in {@link #entries} will be replaced with a {@code new} value.
* @param out the data destination.
* @param buf the initial buffer or {@code null}.
@@ -177,7 +173,6 @@ public class Context implements Serializable {
/**
* Serializes the object.
- * All {@code null} elements in {@link #tags} will be replaced with {@code ""}.
* All {@code null} elements in {@link #entries} will be replaced with a {@code new} value.
* @param buf the data destination.
* @param offset the initial index for {@code buf}, inclusive.
@@ -189,68 +184,9 @@ public class Context implements Serializable {
int i = offset;
try {
- if (this.tags.length != 0) {
+ if (this.tags != null) {
buf[i++] = (byte) 0;
- String[] a = this.tags;
-
- int x = a.length;
- if (x > Context.colferListMax)
- throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Context.tags length %d exceeds %d elements", x, Context.colferListMax));
- while (x > 0x7f) {
- buf[i++] = (byte) (x | 0x80);
- x >>>= 7;
- }
- buf[i++] = (byte) x;
-
- for (int ai = 0; ai < a.length; ai++) {
- String s = a[ai];
- if (s == null) {
- s = "";
- a[ai] = s;
- }
-
- int start = ++i;
-
- for (int sIndex = 0, sLength = s.length(); sIndex < sLength; sIndex++) {
- char c = s.charAt(sIndex);
- if (c < '\u0080') {
- buf[i++] = (byte) c;
- } else if (c < '\u0800') {
- buf[i++] = (byte) (192 | c >>> 6);
- buf[i++] = (byte) (128 | c & 63);
- } else if (c < '\ud800' || c > '\udfff') {
- buf[i++] = (byte) (224 | c >>> 12);
- buf[i++] = (byte) (128 | c >>> 6 & 63);
- buf[i++] = (byte) (128 | c & 63);
- } else {
- int cp = 0;
- if (++sIndex < sLength) cp = Character.toCodePoint(c, s.charAt(sIndex));
- if ((cp >= 1 << 16) && (cp < 1 << 21)) {
- buf[i++] = (byte) (240 | cp >>> 18);
- buf[i++] = (byte) (128 | cp >>> 12 & 63);
- buf[i++] = (byte) (128 | cp >>> 6 & 63);
- buf[i++] = (byte) (128 | cp & 63);
- } else
- buf[i++] = (byte) '?';
- }
- }
- int size = i - start;
- if (size > Context.colferSizeMax)
- throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Context.tags[%d] size %d exceeds %d UTF-8 bytes", ai, size, Context.colferSizeMax));
-
- int ii = start - 1;
- if (size > 0x7f) {
- i++;
- for (int y = size; y >= 1 << 14; y >>>= 7) i++;
- System.arraycopy(buf, start, buf, i - size, size);
-
- do {
- buf[ii++] = (byte) (size | 0x80);
- size >>>= 7;
- } while (size > 0x7f);
- }
- buf[ii] = (byte) size;
- }
+ i = this.tags.marshal(buf, i);
}
if (this.entries.length != 0) {
@@ -317,31 +253,8 @@ public class Context implements Serializable {
byte header = buf[i++];
if (header == (byte) 0) {
- int length = 0;
- for (int shift = 0; true; shift += 7) {
- byte b = buf[i++];
- length |= (b & 0x7f) << shift;
- if (shift == 28 || b >= 0) break;
- }
- if (length < 0 || length > Context.colferListMax)
- throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Context.tags length %d exceeds %d elements", length, Context.colferListMax));
-
- String[] a = new String[length];
- for (int ai = 0; ai < length; ai++) {
- int size = 0;
- for (int shift = 0; true; shift += 7) {
- byte b = buf[i++];
- size |= (b & 0x7f) << shift;
- if (shift == 28 || b >= 0) break;
- }
- if (size < 0 || size > Context.colferSizeMax)
- throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Context.tags[%d] size %d exceeds %d UTF-8 bytes", ai, size, Context.colferSizeMax));
-
- int start = i;
- i += size;
- a[ai] = new String(buf, start, size, StandardCharsets.UTF_8);
- }
- this.tags = a;
+ this.tags = new Tags();
+ i = this.tags.unmarshal(buf, i, end);
header = buf[i++];
}
@@ -415,7 +328,7 @@ public class Context implements Serializable {
* Gets kamon/context/generated/binary/context.Context.tags.
* @return the value.
*/
- public String[] getTags() {
+ public Tags getTags() {
return this.tags;
}
@@ -423,7 +336,7 @@ public class Context implements Serializable {
* Sets kamon/context/generated/binary/context.Context.tags.
* @param value the replacement.
*/
- public void setTags(String[] value) {
+ public void setTags(Tags value) {
this.tags = value;
}
@@ -432,7 +345,7 @@ public class Context implements Serializable {
* @param value the replacement.
* @return {link this}.
*/
- public Context withTags(String[] value) {
+ public Context withTags(Tags value) {
this.tags = value;
return this;
}
@@ -466,7 +379,7 @@ public class Context implements Serializable {
@Override
public final int hashCode() {
int h = 1;
- for (String o : this.tags) h = 31 * h + (o == null ? 0 : o.hashCode());
+ if (this.tags != null) h = 31 * h + this.tags.hashCode();
for (Entry o : this.entries) h = 31 * h + (o == null ? 0 : o.hashCode());
return h;
}
@@ -480,7 +393,7 @@ public class Context implements Serializable {
if (o == null) return false;
if (o == this) return true;
return o.getClass() == Context.class
- && java.util.Arrays.equals(this.tags, o.tags)
+ && (this.tags == null ? o.tags == null : this.tags.equals(o.tags))
&& java.util.Arrays.equals(this.entries, o.entries);
}
diff --git a/kamon-core/src/main/java/kamon/context/generated/binary/context/Entry.java b/kamon-core/src/main/java/kamon/context/generated/binary/context/Entry.java
index dc75b10d..32213c79 100644
--- a/kamon-core/src/main/java/kamon/context/generated/binary/context/Entry.java
+++ b/kamon-core/src/main/java/kamon/context/generated/binary/context/Entry.java
@@ -33,9 +33,9 @@ public class Entry implements Serializable {
- public String name;
+ public String key;
- public byte[] content;
+ public byte[] value;
/** Default constructor */
@@ -47,8 +47,8 @@ public class Entry implements Serializable {
/** Colfer zero values. */
private void init() {
- name = "";
- content = _zeroBytes;
+ key = "";
+ value = _zeroBytes;
}
/**
@@ -181,11 +181,11 @@ public class Entry implements Serializable {
int i = offset;
try {
- if (! this.name.isEmpty()) {
+ if (! this.key.isEmpty()) {
buf[i++] = (byte) 0;
int start = ++i;
- String s = this.name;
+ String s = this.key;
for (int sIndex = 0, sLength = s.length(); sIndex < sLength; sIndex++) {
char c = s.charAt(sIndex);
if (c < '\u0080') {
@@ -211,7 +211,7 @@ public class Entry implements Serializable {
}
int size = i - start;
if (size > Entry.colferSizeMax)
- throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Entry.name size %d exceeds %d UTF-8 bytes", size, Entry.colferSizeMax));
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Entry.key size %d exceeds %d UTF-8 bytes", size, Entry.colferSizeMax));
int ii = start - 1;
if (size > 0x7f) {
@@ -227,12 +227,12 @@ public class Entry implements Serializable {
buf[ii] = (byte) size;
}
- if (this.content.length != 0) {
+ if (this.value.length != 0) {
buf[i++] = (byte) 1;
- int size = this.content.length;
+ int size = this.value.length;
if (size > Entry.colferSizeMax)
- throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Entry.content size %d exceeds %d bytes", size, Entry.colferSizeMax));
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Entry.value size %d exceeds %d bytes", size, Entry.colferSizeMax));
int x = size;
while (x > 0x7f) {
@@ -243,7 +243,7 @@ public class Entry implements Serializable {
int start = i;
i += size;
- System.arraycopy(this.content, 0, buf, start, size);
+ System.arraycopy(this.value, 0, buf, start, size);
}
buf[i++] = (byte) 0x7f;
@@ -294,11 +294,11 @@ public class Entry implements Serializable {
if (shift == 28 || b >= 0) break;
}
if (size < 0 || size > Entry.colferSizeMax)
- throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Entry.name size %d exceeds %d UTF-8 bytes", size, Entry.colferSizeMax));
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Entry.key size %d exceeds %d UTF-8 bytes", size, Entry.colferSizeMax));
int start = i;
i += size;
- this.name = new String(buf, start, size, StandardCharsets.UTF_8);
+ this.key = new String(buf, start, size, StandardCharsets.UTF_8);
header = buf[i++];
}
@@ -310,12 +310,12 @@ public class Entry implements Serializable {
if (shift == 28 || b >= 0) break;
}
if (size < 0 || size > Entry.colferSizeMax)
- throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Entry.content size %d exceeds %d bytes", size, Entry.colferSizeMax));
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Entry.value size %d exceeds %d bytes", size, Entry.colferSizeMax));
- this.content = new byte[size];
+ this.value = new byte[size];
int start = i;
i += size;
- System.arraycopy(buf, start, this.content, 0, size);
+ System.arraycopy(buf, start, this.value, 0, size);
header = buf[i++];
}
@@ -367,62 +367,62 @@ public class Entry implements Serializable {
}
/**
- * Gets kamon/context/generated/binary/context.Entry.name.
+ * Gets kamon/context/generated/binary/context.Entry.key.
* @return the value.
*/
- public String getName() {
- return this.name;
+ public String getKey() {
+ return this.key;
}
/**
- * Sets kamon/context/generated/binary/context.Entry.name.
+ * Sets kamon/context/generated/binary/context.Entry.key.
* @param value the replacement.
*/
- public void setName(String value) {
- this.name = value;
+ public void setKey(String value) {
+ this.key = value;
}
/**
- * Sets kamon/context/generated/binary/context.Entry.name.
+ * Sets kamon/context/generated/binary/context.Entry.key.
* @param value the replacement.
* @return {link this}.
*/
- public Entry withName(String value) {
- this.name = value;
+ public Entry withKey(String value) {
+ this.key = value;
return this;
}
/**
- * Gets kamon/context/generated/binary/context.Entry.content.
+ * Gets kamon/context/generated/binary/context.Entry.value.
* @return the value.
*/
- public byte[] getContent() {
- return this.content;
+ public byte[] getValue() {
+ return this.value;
}
/**
- * Sets kamon/context/generated/binary/context.Entry.content.
+ * Sets kamon/context/generated/binary/context.Entry.value.
* @param value the replacement.
*/
- public void setContent(byte[] value) {
- this.content = value;
+ public void setValue(byte[] value) {
+ this.value = value;
}
/**
- * Sets kamon/context/generated/binary/context.Entry.content.
+ * Sets kamon/context/generated/binary/context.Entry.value.
* @param value the replacement.
* @return {link this}.
*/
- public Entry withContent(byte[] value) {
- this.content = value;
+ public Entry withValue(byte[] value) {
+ this.value = value;
return this;
}
@Override
public final int hashCode() {
int h = 1;
- if (this.name != null) h = 31 * h + this.name.hashCode();
- for (byte b : this.content) h = 31 * h + b;
+ if (this.key != null) h = 31 * h + this.key.hashCode();
+ for (byte b : this.value) h = 31 * h + b;
return h;
}
@@ -435,8 +435,8 @@ public class Entry implements Serializable {
if (o == null) return false;
if (o == this) return true;
return o.getClass() == Entry.class
- && (this.name == null ? o.name == null : this.name.equals(o.name))
- && java.util.Arrays.equals(this.content, o.content);
+ && (this.key == null ? o.key == null : this.key.equals(o.key))
+ && java.util.Arrays.equals(this.value, o.value);
}
}
diff --git a/kamon-core/src/main/java/kamon/context/generated/binary/context/LongTag.java b/kamon-core/src/main/java/kamon/context/generated/binary/context/LongTag.java
new file mode 100644
index 00000000..505ba2f0
--- /dev/null
+++ b/kamon-core/src/main/java/kamon/context/generated/binary/context/LongTag.java
@@ -0,0 +1,443 @@
+package kamon.context.generated.binary.context;
+
+
+// Code generated by colf(1); DO NOT EDIT.
+
+
+import static java.lang.String.format;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.util.InputMismatchException;
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
+
+
+/**
+ * Data bean with built-in serialization support.
+
+ * @author generated by colf(1)
+ * @see <a href="https://github.com/pascaldekloe/colfer">Colfer's home</a>
+ */
+@javax.annotation.Generated(value="colf(1)", comments="Colfer from schema file Context.colf")
+public class LongTag implements Serializable {
+
+ /** The upper limit for serial byte sizes. */
+ public static int colferSizeMax = 16 * 1024 * 1024;
+
+
+
+
+ public String key;
+
+ public long value;
+
+
+ /** Default constructor */
+ public LongTag() {
+ init();
+ }
+
+
+ /** Colfer zero values. */
+ private void init() {
+ key = "";
+ }
+
+ /**
+ * {@link #reset(InputStream) Reusable} deserialization of Colfer streams.
+ */
+ public static class Unmarshaller {
+
+ /** The data source. */
+ protected InputStream in;
+
+ /** The read buffer. */
+ public byte[] buf;
+
+ /** The {@link #buf buffer}'s data start index, inclusive. */
+ protected int offset;
+
+ /** The {@link #buf buffer}'s data end index, exclusive. */
+ protected int i;
+
+
+ /**
+ * @param in the data source or {@code null}.
+ * @param buf the initial buffer or {@code null}.
+ */
+ public Unmarshaller(InputStream in, byte[] buf) {
+ // TODO: better size estimation
+ if (buf == null || buf.length == 0)
+ buf = new byte[Math.min(LongTag.colferSizeMax, 2048)];
+ this.buf = buf;
+ reset(in);
+ }
+
+ /**
+ * Reuses the marshaller.
+ * @param in the data source or {@code null}.
+ * @throws IllegalStateException on pending data.
+ */
+ public void reset(InputStream in) {
+ if (this.i != this.offset) throw new IllegalStateException("colfer: pending data");
+ this.in = in;
+ this.offset = 0;
+ this.i = 0;
+ }
+
+ /**
+ * Deserializes the following object.
+ * @return the result or {@code null} when EOF.
+ * @throws IOException from the input stream.
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public LongTag next() throws IOException {
+ if (in == null) return null;
+
+ while (true) {
+ if (this.i > this.offset) {
+ try {
+ LongTag o = new LongTag();
+ this.offset = o.unmarshal(this.buf, this.offset, this.i);
+ return o;
+ } catch (BufferUnderflowException e) {
+ }
+ }
+ // not enough data
+
+ if (this.i <= this.offset) {
+ this.offset = 0;
+ this.i = 0;
+ } else if (i == buf.length) {
+ byte[] src = this.buf;
+ // TODO: better size estimation
+ if (offset == 0) this.buf = new byte[Math.min(LongTag.colferSizeMax, this.buf.length * 4)];
+ System.arraycopy(src, this.offset, this.buf, 0, this.i - this.offset);
+ this.i -= this.offset;
+ this.offset = 0;
+ }
+ assert this.i < this.buf.length;
+
+ int n = in.read(buf, i, buf.length - i);
+ if (n < 0) {
+ if (this.i > this.offset)
+ throw new InputMismatchException("colfer: pending data with EOF");
+ return null;
+ }
+ assert n > 0;
+ i += n;
+ }
+ }
+
+ }
+
+
+ /**
+ * Serializes the object.
+ * @param out the data destination.
+ * @param buf the initial buffer or {@code null}.
+ * @return the final buffer. When the serial fits into {@code buf} then the return is {@code buf}.
+ * Otherwise the return is a new buffer, large enough to hold the whole serial.
+ * @throws IOException from {@code out}.
+ * @throws IllegalStateException on an upper limit breach defined by {@link #colferSizeMax}.
+ */
+ public byte[] marshal(OutputStream out, byte[] buf) throws IOException {
+ // TODO: better size estimation
+ if (buf == null || buf.length == 0)
+ buf = new byte[Math.min(LongTag.colferSizeMax, 2048)];
+
+ while (true) {
+ int i;
+ try {
+ i = marshal(buf, 0);
+ } catch (BufferOverflowException e) {
+ buf = new byte[Math.min(LongTag.colferSizeMax, buf.length * 4)];
+ continue;
+ }
+
+ out.write(buf, 0, i);
+ return buf;
+ }
+ }
+
+ /**
+ * Serializes the object.
+ * @param buf the data destination.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferOverflowException when {@code buf} is too small.
+ * @throws IllegalStateException on an upper limit breach defined by {@link #colferSizeMax}.
+ */
+ public int marshal(byte[] buf, int offset) {
+ int i = offset;
+
+ try {
+ if (! this.key.isEmpty()) {
+ buf[i++] = (byte) 0;
+ int start = ++i;
+
+ String s = this.key;
+ for (int sIndex = 0, sLength = s.length(); sIndex < sLength; sIndex++) {
+ char c = s.charAt(sIndex);
+ if (c < '\u0080') {
+ buf[i++] = (byte) c;
+ } else if (c < '\u0800') {
+ buf[i++] = (byte) (192 | c >>> 6);
+ buf[i++] = (byte) (128 | c & 63);
+ } else if (c < '\ud800' || c > '\udfff') {
+ buf[i++] = (byte) (224 | c >>> 12);
+ buf[i++] = (byte) (128 | c >>> 6 & 63);
+ buf[i++] = (byte) (128 | c & 63);
+ } else {
+ int cp = 0;
+ if (++sIndex < sLength) cp = Character.toCodePoint(c, s.charAt(sIndex));
+ if ((cp >= 1 << 16) && (cp < 1 << 21)) {
+ buf[i++] = (byte) (240 | cp >>> 18);
+ buf[i++] = (byte) (128 | cp >>> 12 & 63);
+ buf[i++] = (byte) (128 | cp >>> 6 & 63);
+ buf[i++] = (byte) (128 | cp & 63);
+ } else
+ buf[i++] = (byte) '?';
+ }
+ }
+ int size = i - start;
+ if (size > LongTag.colferSizeMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.LongTag.key size %d exceeds %d UTF-8 bytes", size, LongTag.colferSizeMax));
+
+ int ii = start - 1;
+ if (size > 0x7f) {
+ i++;
+ for (int x = size; x >= 1 << 14; x >>>= 7) i++;
+ System.arraycopy(buf, start, buf, i - size, size);
+
+ do {
+ buf[ii++] = (byte) (size | 0x80);
+ size >>>= 7;
+ } while (size > 0x7f);
+ }
+ buf[ii] = (byte) size;
+ }
+
+ if (this.value != 0) {
+ long x = this.value;
+ if (x < 0) {
+ x = -x;
+ buf[i++] = (byte) (1 | 0x80);
+ } else
+ buf[i++] = (byte) 1;
+ for (int n = 0; n < 8 && (x & ~0x7fL) != 0; n++) {
+ buf[i++] = (byte) (x | 0x80);
+ x >>>= 7;
+ }
+ buf[i++] = (byte) x;
+ }
+
+ buf[i++] = (byte) 0x7f;
+ return i;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ if (i - offset > LongTag.colferSizeMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.LongTag exceeds %d bytes", LongTag.colferSizeMax));
+ if (i > buf.length) throw new BufferOverflowException();
+ throw e;
+ }
+ }
+
+ /**
+ * Deserializes the object.
+ * @param buf the data source.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public int unmarshal(byte[] buf, int offset) {
+ return unmarshal(buf, offset, buf.length);
+ }
+
+ /**
+ * Deserializes the object.
+ * @param buf the data source.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @param end the index limit for {@code buf}, exclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public int unmarshal(byte[] buf, int offset, int end) {
+ if (end > buf.length) end = buf.length;
+ int i = offset;
+
+ try {
+ byte header = buf[i++];
+
+ if (header == (byte) 0) {
+ int size = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ size |= (b & 0x7f) << shift;
+ if (shift == 28 || b >= 0) break;
+ }
+ if (size < 0 || size > LongTag.colferSizeMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.LongTag.key size %d exceeds %d UTF-8 bytes", size, LongTag.colferSizeMax));
+
+ int start = i;
+ i += size;
+ this.key = new String(buf, start, size, StandardCharsets.UTF_8);
+ header = buf[i++];
+ }
+
+ if (header == (byte) 1) {
+ long x = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ if (shift == 56 || b >= 0) {
+ x |= (b & 0xffL) << shift;
+ break;
+ }
+ x |= (b & 0x7fL) << shift;
+ }
+ this.value = x;
+ header = buf[i++];
+ } else if (header == (byte) (1 | 0x80)) {
+ long x = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ if (shift == 56 || b >= 0) {
+ x |= (b & 0xffL) << shift;
+ break;
+ }
+ x |= (b & 0x7fL) << shift;
+ }
+ this.value = -x;
+ header = buf[i++];
+ }
+
+ if (header != (byte) 0x7f)
+ throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
+ } finally {
+ if (i > end && end - offset < LongTag.colferSizeMax) throw new BufferUnderflowException();
+ if (i < 0 || i - offset > LongTag.colferSizeMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.LongTag exceeds %d bytes", LongTag.colferSizeMax));
+ if (i > end) throw new BufferUnderflowException();
+ }
+
+ return i;
+ }
+
+ // {@link Serializable} version number.
+ private static final long serialVersionUID = 2L;
+
+ // {@link Serializable} Colfer extension.
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ // TODO: better size estimation
+ byte[] buf = new byte[1024];
+ int n;
+ while (true) try {
+ n = marshal(buf, 0);
+ break;
+ } catch (BufferUnderflowException e) {
+ buf = new byte[4 * buf.length];
+ }
+
+ out.writeInt(n);
+ out.write(buf, 0, n);
+ }
+
+ // {@link Serializable} Colfer extension.
+ private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
+ init();
+
+ int n = in.readInt();
+ byte[] buf = new byte[n];
+ in.readFully(buf);
+ unmarshal(buf, 0);
+ }
+
+ // {@link Serializable} Colfer extension.
+ private void readObjectNoData() throws ObjectStreamException {
+ init();
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.LongTag.key.
+ * @return the value.
+ */
+ public String getKey() {
+ return this.key;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.LongTag.key.
+ * @param value the replacement.
+ */
+ public void setKey(String value) {
+ this.key = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.LongTag.key.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public LongTag withKey(String value) {
+ this.key = value;
+ return this;
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.LongTag.value.
+ * @return the value.
+ */
+ public long getValue() {
+ return this.value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.LongTag.value.
+ * @param value the replacement.
+ */
+ public void setValue(long value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.LongTag.value.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public LongTag withValue(long value) {
+ this.value = value;
+ return this;
+ }
+
+ @Override
+ public final int hashCode() {
+ int h = 1;
+ if (this.key != null) h = 31 * h + this.key.hashCode();
+ h = 31 * h + (int)(this.value ^ this.value >>> 32);
+ return h;
+ }
+
+ @Override
+ public final boolean equals(Object o) {
+ return o instanceof LongTag && equals((LongTag) o);
+ }
+
+ public final boolean equals(LongTag o) {
+ if (o == null) return false;
+ if (o == this) return true;
+ return o.getClass() == LongTag.class
+ && (this.key == null ? o.key == null : this.key.equals(o.key))
+ && this.value == o.value;
+ }
+
+}
diff --git a/kamon-core/src/main/java/kamon/context/generated/binary/context/StringTag.java b/kamon-core/src/main/java/kamon/context/generated/binary/context/StringTag.java
new file mode 100644
index 00000000..366744b4
--- /dev/null
+++ b/kamon-core/src/main/java/kamon/context/generated/binary/context/StringTag.java
@@ -0,0 +1,466 @@
+package kamon.context.generated.binary.context;
+
+
+// Code generated by colf(1); DO NOT EDIT.
+
+
+import static java.lang.String.format;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.util.InputMismatchException;
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
+
+
+/**
+ * Data bean with built-in serialization support.
+
+ * @author generated by colf(1)
+ * @see <a href="https://github.com/pascaldekloe/colfer">Colfer's home</a>
+ */
+@javax.annotation.Generated(value="colf(1)", comments="Colfer from schema file Context.colf")
+public class StringTag implements Serializable {
+
+ /** The upper limit for serial byte sizes. */
+ public static int colferSizeMax = 16 * 1024 * 1024;
+
+
+
+
+ public String key;
+
+ public String value;
+
+
+ /** Default constructor */
+ public StringTag() {
+ init();
+ }
+
+
+ /** Colfer zero values. */
+ private void init() {
+ key = "";
+ value = "";
+ }
+
+ /**
+ * {@link #reset(InputStream) Reusable} deserialization of Colfer streams.
+ */
+ public static class Unmarshaller {
+
+ /** The data source. */
+ protected InputStream in;
+
+ /** The read buffer. */
+ public byte[] buf;
+
+ /** The {@link #buf buffer}'s data start index, inclusive. */
+ protected int offset;
+
+ /** The {@link #buf buffer}'s data end index, exclusive. */
+ protected int i;
+
+
+ /**
+ * @param in the data source or {@code null}.
+ * @param buf the initial buffer or {@code null}.
+ */
+ public Unmarshaller(InputStream in, byte[] buf) {
+ // TODO: better size estimation
+ if (buf == null || buf.length == 0)
+ buf = new byte[Math.min(StringTag.colferSizeMax, 2048)];
+ this.buf = buf;
+ reset(in);
+ }
+
+ /**
+ * Reuses the marshaller.
+ * @param in the data source or {@code null}.
+ * @throws IllegalStateException on pending data.
+ */
+ public void reset(InputStream in) {
+ if (this.i != this.offset) throw new IllegalStateException("colfer: pending data");
+ this.in = in;
+ this.offset = 0;
+ this.i = 0;
+ }
+
+ /**
+ * Deserializes the following object.
+ * @return the result or {@code null} when EOF.
+ * @throws IOException from the input stream.
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public StringTag next() throws IOException {
+ if (in == null) return null;
+
+ while (true) {
+ if (this.i > this.offset) {
+ try {
+ StringTag o = new StringTag();
+ this.offset = o.unmarshal(this.buf, this.offset, this.i);
+ return o;
+ } catch (BufferUnderflowException e) {
+ }
+ }
+ // not enough data
+
+ if (this.i <= this.offset) {
+ this.offset = 0;
+ this.i = 0;
+ } else if (i == buf.length) {
+ byte[] src = this.buf;
+ // TODO: better size estimation
+ if (offset == 0) this.buf = new byte[Math.min(StringTag.colferSizeMax, this.buf.length * 4)];
+ System.arraycopy(src, this.offset, this.buf, 0, this.i - this.offset);
+ this.i -= this.offset;
+ this.offset = 0;
+ }
+ assert this.i < this.buf.length;
+
+ int n = in.read(buf, i, buf.length - i);
+ if (n < 0) {
+ if (this.i > this.offset)
+ throw new InputMismatchException("colfer: pending data with EOF");
+ return null;
+ }
+ assert n > 0;
+ i += n;
+ }
+ }
+
+ }
+
+
+ /**
+ * Serializes the object.
+ * @param out the data destination.
+ * @param buf the initial buffer or {@code null}.
+ * @return the final buffer. When the serial fits into {@code buf} then the return is {@code buf}.
+ * Otherwise the return is a new buffer, large enough to hold the whole serial.
+ * @throws IOException from {@code out}.
+ * @throws IllegalStateException on an upper limit breach defined by {@link #colferSizeMax}.
+ */
+ public byte[] marshal(OutputStream out, byte[] buf) throws IOException {
+ // TODO: better size estimation
+ if (buf == null || buf.length == 0)
+ buf = new byte[Math.min(StringTag.colferSizeMax, 2048)];
+
+ while (true) {
+ int i;
+ try {
+ i = marshal(buf, 0);
+ } catch (BufferOverflowException e) {
+ buf = new byte[Math.min(StringTag.colferSizeMax, buf.length * 4)];
+ continue;
+ }
+
+ out.write(buf, 0, i);
+ return buf;
+ }
+ }
+
+ /**
+ * Serializes the object.
+ * @param buf the data destination.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferOverflowException when {@code buf} is too small.
+ * @throws IllegalStateException on an upper limit breach defined by {@link #colferSizeMax}.
+ */
+ public int marshal(byte[] buf, int offset) {
+ int i = offset;
+
+ try {
+ if (! this.key.isEmpty()) {
+ buf[i++] = (byte) 0;
+ int start = ++i;
+
+ String s = this.key;
+ for (int sIndex = 0, sLength = s.length(); sIndex < sLength; sIndex++) {
+ char c = s.charAt(sIndex);
+ if (c < '\u0080') {
+ buf[i++] = (byte) c;
+ } else if (c < '\u0800') {
+ buf[i++] = (byte) (192 | c >>> 6);
+ buf[i++] = (byte) (128 | c & 63);
+ } else if (c < '\ud800' || c > '\udfff') {
+ buf[i++] = (byte) (224 | c >>> 12);
+ buf[i++] = (byte) (128 | c >>> 6 & 63);
+ buf[i++] = (byte) (128 | c & 63);
+ } else {
+ int cp = 0;
+ if (++sIndex < sLength) cp = Character.toCodePoint(c, s.charAt(sIndex));
+ if ((cp >= 1 << 16) && (cp < 1 << 21)) {
+ buf[i++] = (byte) (240 | cp >>> 18);
+ buf[i++] = (byte) (128 | cp >>> 12 & 63);
+ buf[i++] = (byte) (128 | cp >>> 6 & 63);
+ buf[i++] = (byte) (128 | cp & 63);
+ } else
+ buf[i++] = (byte) '?';
+ }
+ }
+ int size = i - start;
+ if (size > StringTag.colferSizeMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.StringTag.key size %d exceeds %d UTF-8 bytes", size, StringTag.colferSizeMax));
+
+ int ii = start - 1;
+ if (size > 0x7f) {
+ i++;
+ for (int x = size; x >= 1 << 14; x >>>= 7) i++;
+ System.arraycopy(buf, start, buf, i - size, size);
+
+ do {
+ buf[ii++] = (byte) (size | 0x80);
+ size >>>= 7;
+ } while (size > 0x7f);
+ }
+ buf[ii] = (byte) size;
+ }
+
+ if (! this.value.isEmpty()) {
+ buf[i++] = (byte) 1;
+ int start = ++i;
+
+ String s = this.value;
+ for (int sIndex = 0, sLength = s.length(); sIndex < sLength; sIndex++) {
+ char c = s.charAt(sIndex);
+ if (c < '\u0080') {
+ buf[i++] = (byte) c;
+ } else if (c < '\u0800') {
+ buf[i++] = (byte) (192 | c >>> 6);
+ buf[i++] = (byte) (128 | c & 63);
+ } else if (c < '\ud800' || c > '\udfff') {
+ buf[i++] = (byte) (224 | c >>> 12);
+ buf[i++] = (byte) (128 | c >>> 6 & 63);
+ buf[i++] = (byte) (128 | c & 63);
+ } else {
+ int cp = 0;
+ if (++sIndex < sLength) cp = Character.toCodePoint(c, s.charAt(sIndex));
+ if ((cp >= 1 << 16) && (cp < 1 << 21)) {
+ buf[i++] = (byte) (240 | cp >>> 18);
+ buf[i++] = (byte) (128 | cp >>> 12 & 63);
+ buf[i++] = (byte) (128 | cp >>> 6 & 63);
+ buf[i++] = (byte) (128 | cp & 63);
+ } else
+ buf[i++] = (byte) '?';
+ }
+ }
+ int size = i - start;
+ if (size > StringTag.colferSizeMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.StringTag.value size %d exceeds %d UTF-8 bytes", size, StringTag.colferSizeMax));
+
+ int ii = start - 1;
+ if (size > 0x7f) {
+ i++;
+ for (int x = size; x >= 1 << 14; x >>>= 7) i++;
+ System.arraycopy(buf, start, buf, i - size, size);
+
+ do {
+ buf[ii++] = (byte) (size | 0x80);
+ size >>>= 7;
+ } while (size > 0x7f);
+ }
+ buf[ii] = (byte) size;
+ }
+
+ buf[i++] = (byte) 0x7f;
+ return i;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ if (i - offset > StringTag.colferSizeMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.StringTag exceeds %d bytes", StringTag.colferSizeMax));
+ if (i > buf.length) throw new BufferOverflowException();
+ throw e;
+ }
+ }
+
+ /**
+ * Deserializes the object.
+ * @param buf the data source.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public int unmarshal(byte[] buf, int offset) {
+ return unmarshal(buf, offset, buf.length);
+ }
+
+ /**
+ * Deserializes the object.
+ * @param buf the data source.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @param end the index limit for {@code buf}, exclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
+ * @throws SecurityException on an upper limit breach defined by {@link #colferSizeMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public int unmarshal(byte[] buf, int offset, int end) {
+ if (end > buf.length) end = buf.length;
+ int i = offset;
+
+ try {
+ byte header = buf[i++];
+
+ if (header == (byte) 0) {
+ int size = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ size |= (b & 0x7f) << shift;
+ if (shift == 28 || b >= 0) break;
+ }
+ if (size < 0 || size > StringTag.colferSizeMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.StringTag.key size %d exceeds %d UTF-8 bytes", size, StringTag.colferSizeMax));
+
+ int start = i;
+ i += size;
+ this.key = new String(buf, start, size, StandardCharsets.UTF_8);
+ header = buf[i++];
+ }
+
+ if (header == (byte) 1) {
+ int size = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ size |= (b & 0x7f) << shift;
+ if (shift == 28 || b >= 0) break;
+ }
+ if (size < 0 || size > StringTag.colferSizeMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.StringTag.value size %d exceeds %d UTF-8 bytes", size, StringTag.colferSizeMax));
+
+ int start = i;
+ i += size;
+ this.value = new String(buf, start, size, StandardCharsets.UTF_8);
+ header = buf[i++];
+ }
+
+ if (header != (byte) 0x7f)
+ throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
+ } finally {
+ if (i > end && end - offset < StringTag.colferSizeMax) throw new BufferUnderflowException();
+ if (i < 0 || i - offset > StringTag.colferSizeMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.StringTag exceeds %d bytes", StringTag.colferSizeMax));
+ if (i > end) throw new BufferUnderflowException();
+ }
+
+ return i;
+ }
+
+ // {@link Serializable} version number.
+ private static final long serialVersionUID = 2L;
+
+ // {@link Serializable} Colfer extension.
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ // TODO: better size estimation
+ byte[] buf = new byte[1024];
+ int n;
+ while (true) try {
+ n = marshal(buf, 0);
+ break;
+ } catch (BufferUnderflowException e) {
+ buf = new byte[4 * buf.length];
+ }
+
+ out.writeInt(n);
+ out.write(buf, 0, n);
+ }
+
+ // {@link Serializable} Colfer extension.
+ private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
+ init();
+
+ int n = in.readInt();
+ byte[] buf = new byte[n];
+ in.readFully(buf);
+ unmarshal(buf, 0);
+ }
+
+ // {@link Serializable} Colfer extension.
+ private void readObjectNoData() throws ObjectStreamException {
+ init();
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.StringTag.key.
+ * @return the value.
+ */
+ public String getKey() {
+ return this.key;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.StringTag.key.
+ * @param value the replacement.
+ */
+ public void setKey(String value) {
+ this.key = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.StringTag.key.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public StringTag withKey(String value) {
+ this.key = value;
+ return this;
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.StringTag.value.
+ * @return the value.
+ */
+ public String getValue() {
+ return this.value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.StringTag.value.
+ * @param value the replacement.
+ */
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.StringTag.value.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public StringTag withValue(String value) {
+ this.value = value;
+ return this;
+ }
+
+ @Override
+ public final int hashCode() {
+ int h = 1;
+ if (this.key != null) h = 31 * h + this.key.hashCode();
+ if (this.value != null) h = 31 * h + this.value.hashCode();
+ return h;
+ }
+
+ @Override
+ public final boolean equals(Object o) {
+ return o instanceof StringTag && equals((StringTag) o);
+ }
+
+ public final boolean equals(StringTag o) {
+ if (o == null) return false;
+ if (o == this) return true;
+ return o.getClass() == StringTag.class
+ && (this.key == null ? o.key == null : this.key.equals(o.key))
+ && (this.value == null ? o.value == null : this.value.equals(o.value));
+ }
+
+}
diff --git a/kamon-core/src/main/java/kamon/context/generated/binary/context/Tags.java b/kamon-core/src/main/java/kamon/context/generated/binary/context/Tags.java
new file mode 100644
index 00000000..ce4d66db
--- /dev/null
+++ b/kamon-core/src/main/java/kamon/context/generated/binary/context/Tags.java
@@ -0,0 +1,513 @@
+package kamon.context.generated.binary.context;
+
+
+// Code generated by colf(1); DO NOT EDIT.
+
+
+import static java.lang.String.format;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.util.InputMismatchException;
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
+
+
+/**
+ * Data bean with built-in serialization support.
+
+ * @author generated by colf(1)
+ * @see <a href="https://github.com/pascaldekloe/colfer">Colfer's home</a>
+ */
+@javax.annotation.Generated(value="colf(1)", comments="Colfer from schema file Context.colf")
+public class Tags implements Serializable {
+
+ /** The upper limit for serial byte sizes. */
+ public static int colferSizeMax = 16 * 1024 * 1024;
+
+ /** The upper limit for the number of elements in a list. */
+ public static int colferListMax = 64 * 1024;
+
+
+
+
+ public StringTag[] strings;
+
+ public LongTag[] longs;
+
+ public BooleanTag[] booleans;
+
+
+ /** Default constructor */
+ public Tags() {
+ init();
+ }
+
+ private static final StringTag[] _zeroStrings = new StringTag[0];
+ private static final LongTag[] _zeroLongs = new LongTag[0];
+ private static final BooleanTag[] _zeroBooleans = new BooleanTag[0];
+
+ /** Colfer zero values. */
+ private void init() {
+ strings = _zeroStrings;
+ longs = _zeroLongs;
+ booleans = _zeroBooleans;
+ }
+
+ /**
+ * {@link #reset(InputStream) Reusable} deserialization of Colfer streams.
+ */
+ public static class Unmarshaller {
+
+ /** The data source. */
+ protected InputStream in;
+
+ /** The read buffer. */
+ public byte[] buf;
+
+ /** The {@link #buf buffer}'s data start index, inclusive. */
+ protected int offset;
+
+ /** The {@link #buf buffer}'s data end index, exclusive. */
+ protected int i;
+
+
+ /**
+ * @param in the data source or {@code null}.
+ * @param buf the initial buffer or {@code null}.
+ */
+ public Unmarshaller(InputStream in, byte[] buf) {
+ // TODO: better size estimation
+ if (buf == null || buf.length == 0)
+ buf = new byte[Math.min(Tags.colferSizeMax, 2048)];
+ this.buf = buf;
+ reset(in);
+ }
+
+ /**
+ * Reuses the marshaller.
+ * @param in the data source or {@code null}.
+ * @throws IllegalStateException on pending data.
+ */
+ public void reset(InputStream in) {
+ if (this.i != this.offset) throw new IllegalStateException("colfer: pending data");
+ this.in = in;
+ this.offset = 0;
+ this.i = 0;
+ }
+
+ /**
+ * Deserializes the following object.
+ * @return the result or {@code null} when EOF.
+ * @throws IOException from the input stream.
+ * @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public Tags next() throws IOException {
+ if (in == null) return null;
+
+ while (true) {
+ if (this.i > this.offset) {
+ try {
+ Tags o = new Tags();
+ this.offset = o.unmarshal(this.buf, this.offset, this.i);
+ return o;
+ } catch (BufferUnderflowException e) {
+ }
+ }
+ // not enough data
+
+ if (this.i <= this.offset) {
+ this.offset = 0;
+ this.i = 0;
+ } else if (i == buf.length) {
+ byte[] src = this.buf;
+ // TODO: better size estimation
+ if (offset == 0) this.buf = new byte[Math.min(Tags.colferSizeMax, this.buf.length * 4)];
+ System.arraycopy(src, this.offset, this.buf, 0, this.i - this.offset);
+ this.i -= this.offset;
+ this.offset = 0;
+ }
+ assert this.i < this.buf.length;
+
+ int n = in.read(buf, i, buf.length - i);
+ if (n < 0) {
+ if (this.i > this.offset)
+ throw new InputMismatchException("colfer: pending data with EOF");
+ return null;
+ }
+ assert n > 0;
+ i += n;
+ }
+ }
+
+ }
+
+
+ /**
+ * Serializes the object.
+ * All {@code null} elements in {@link #strings} will be replaced with a {@code new} value.
+ * All {@code null} elements in {@link #longs} will be replaced with a {@code new} value.
+ * All {@code null} elements in {@link #booleans} will be replaced with a {@code new} value.
+ * @param out the data destination.
+ * @param buf the initial buffer or {@code null}.
+ * @return the final buffer. When the serial fits into {@code buf} then the return is {@code buf}.
+ * Otherwise the return is a new buffer, large enough to hold the whole serial.
+ * @throws IOException from {@code out}.
+ * @throws IllegalStateException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
+ */
+ public byte[] marshal(OutputStream out, byte[] buf) throws IOException {
+ // TODO: better size estimation
+ if (buf == null || buf.length == 0)
+ buf = new byte[Math.min(Tags.colferSizeMax, 2048)];
+
+ while (true) {
+ int i;
+ try {
+ i = marshal(buf, 0);
+ } catch (BufferOverflowException e) {
+ buf = new byte[Math.min(Tags.colferSizeMax, buf.length * 4)];
+ continue;
+ }
+
+ out.write(buf, 0, i);
+ return buf;
+ }
+ }
+
+ /**
+ * Serializes the object.
+ * All {@code null} elements in {@link #strings} will be replaced with a {@code new} value.
+ * All {@code null} elements in {@link #longs} will be replaced with a {@code new} value.
+ * All {@code null} elements in {@link #booleans} will be replaced with a {@code new} value.
+ * @param buf the data destination.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferOverflowException when {@code buf} is too small.
+ * @throws IllegalStateException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
+ */
+ public int marshal(byte[] buf, int offset) {
+ int i = offset;
+
+ try {
+ if (this.strings.length != 0) {
+ buf[i++] = (byte) 0;
+ StringTag[] a = this.strings;
+
+ int x = a.length;
+ if (x > Tags.colferListMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Tags.strings length %d exceeds %d elements", x, Tags.colferListMax));
+ while (x > 0x7f) {
+ buf[i++] = (byte) (x | 0x80);
+ x >>>= 7;
+ }
+ buf[i++] = (byte) x;
+
+ for (int ai = 0; ai < a.length; ai++) {
+ StringTag o = a[ai];
+ if (o == null) {
+ o = new StringTag();
+ a[ai] = o;
+ }
+ i = o.marshal(buf, i);
+ }
+ }
+
+ if (this.longs.length != 0) {
+ buf[i++] = (byte) 1;
+ LongTag[] a = this.longs;
+
+ int x = a.length;
+ if (x > Tags.colferListMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Tags.longs length %d exceeds %d elements", x, Tags.colferListMax));
+ while (x > 0x7f) {
+ buf[i++] = (byte) (x | 0x80);
+ x >>>= 7;
+ }
+ buf[i++] = (byte) x;
+
+ for (int ai = 0; ai < a.length; ai++) {
+ LongTag o = a[ai];
+ if (o == null) {
+ o = new LongTag();
+ a[ai] = o;
+ }
+ i = o.marshal(buf, i);
+ }
+ }
+
+ if (this.booleans.length != 0) {
+ buf[i++] = (byte) 2;
+ BooleanTag[] a = this.booleans;
+
+ int x = a.length;
+ if (x > Tags.colferListMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Tags.booleans length %d exceeds %d elements", x, Tags.colferListMax));
+ while (x > 0x7f) {
+ buf[i++] = (byte) (x | 0x80);
+ x >>>= 7;
+ }
+ buf[i++] = (byte) x;
+
+ for (int ai = 0; ai < a.length; ai++) {
+ BooleanTag o = a[ai];
+ if (o == null) {
+ o = new BooleanTag();
+ a[ai] = o;
+ }
+ i = o.marshal(buf, i);
+ }
+ }
+
+ buf[i++] = (byte) 0x7f;
+ return i;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ if (i - offset > Tags.colferSizeMax)
+ throw new IllegalStateException(format("colfer: kamon/context/generated/binary/context.Tags exceeds %d bytes", Tags.colferSizeMax));
+ if (i > buf.length) throw new BufferOverflowException();
+ throw e;
+ }
+ }
+
+ /**
+ * Deserializes the object.
+ * @param buf the data source.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
+ * @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public int unmarshal(byte[] buf, int offset) {
+ return unmarshal(buf, offset, buf.length);
+ }
+
+ /**
+ * Deserializes the object.
+ * @param buf the data source.
+ * @param offset the initial index for {@code buf}, inclusive.
+ * @param end the index limit for {@code buf}, exclusive.
+ * @return the final index for {@code buf}, exclusive.
+ * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
+ * @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
+ * @throws InputMismatchException when the data does not match this object's schema.
+ */
+ public int unmarshal(byte[] buf, int offset, int end) {
+ if (end > buf.length) end = buf.length;
+ int i = offset;
+
+ try {
+ byte header = buf[i++];
+
+ if (header == (byte) 0) {
+ int length = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ length |= (b & 0x7f) << shift;
+ if (shift == 28 || b >= 0) break;
+ }
+ if (length < 0 || length > Tags.colferListMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Tags.strings length %d exceeds %d elements", length, Tags.colferListMax));
+
+ StringTag[] a = new StringTag[length];
+ for (int ai = 0; ai < length; ai++) {
+ StringTag o = new StringTag();
+ i = o.unmarshal(buf, i, end);
+ a[ai] = o;
+ }
+ this.strings = a;
+ header = buf[i++];
+ }
+
+ if (header == (byte) 1) {
+ int length = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ length |= (b & 0x7f) << shift;
+ if (shift == 28 || b >= 0) break;
+ }
+ if (length < 0 || length > Tags.colferListMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Tags.longs length %d exceeds %d elements", length, Tags.colferListMax));
+
+ LongTag[] a = new LongTag[length];
+ for (int ai = 0; ai < length; ai++) {
+ LongTag o = new LongTag();
+ i = o.unmarshal(buf, i, end);
+ a[ai] = o;
+ }
+ this.longs = a;
+ header = buf[i++];
+ }
+
+ if (header == (byte) 2) {
+ int length = 0;
+ for (int shift = 0; true; shift += 7) {
+ byte b = buf[i++];
+ length |= (b & 0x7f) << shift;
+ if (shift == 28 || b >= 0) break;
+ }
+ if (length < 0 || length > Tags.colferListMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Tags.booleans length %d exceeds %d elements", length, Tags.colferListMax));
+
+ BooleanTag[] a = new BooleanTag[length];
+ for (int ai = 0; ai < length; ai++) {
+ BooleanTag o = new BooleanTag();
+ i = o.unmarshal(buf, i, end);
+ a[ai] = o;
+ }
+ this.booleans = a;
+ header = buf[i++];
+ }
+
+ if (header != (byte) 0x7f)
+ throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
+ } finally {
+ if (i > end && end - offset < Tags.colferSizeMax) throw new BufferUnderflowException();
+ if (i < 0 || i - offset > Tags.colferSizeMax)
+ throw new SecurityException(format("colfer: kamon/context/generated/binary/context.Tags exceeds %d bytes", Tags.colferSizeMax));
+ if (i > end) throw new BufferUnderflowException();
+ }
+
+ return i;
+ }
+
+ // {@link Serializable} version number.
+ private static final long serialVersionUID = 3L;
+
+ // {@link Serializable} Colfer extension.
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ // TODO: better size estimation
+ byte[] buf = new byte[1024];
+ int n;
+ while (true) try {
+ n = marshal(buf, 0);
+ break;
+ } catch (BufferUnderflowException e) {
+ buf = new byte[4 * buf.length];
+ }
+
+ out.writeInt(n);
+ out.write(buf, 0, n);
+ }
+
+ // {@link Serializable} Colfer extension.
+ private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
+ init();
+
+ int n = in.readInt();
+ byte[] buf = new byte[n];
+ in.readFully(buf);
+ unmarshal(buf, 0);
+ }
+
+ // {@link Serializable} Colfer extension.
+ private void readObjectNoData() throws ObjectStreamException {
+ init();
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.Tags.strings.
+ * @return the value.
+ */
+ public StringTag[] getStrings() {
+ return this.strings;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.Tags.strings.
+ * @param value the replacement.
+ */
+ public void setStrings(StringTag[] value) {
+ this.strings = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.Tags.strings.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public Tags withStrings(StringTag[] value) {
+ this.strings = value;
+ return this;
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.Tags.longs.
+ * @return the value.
+ */
+ public LongTag[] getLongs() {
+ return this.longs;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.Tags.longs.
+ * @param value the replacement.
+ */
+ public void setLongs(LongTag[] value) {
+ this.longs = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.Tags.longs.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public Tags withLongs(LongTag[] value) {
+ this.longs = value;
+ return this;
+ }
+
+ /**
+ * Gets kamon/context/generated/binary/context.Tags.booleans.
+ * @return the value.
+ */
+ public BooleanTag[] getBooleans() {
+ return this.booleans;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.Tags.booleans.
+ * @param value the replacement.
+ */
+ public void setBooleans(BooleanTag[] value) {
+ this.booleans = value;
+ }
+
+ /**
+ * Sets kamon/context/generated/binary/context.Tags.booleans.
+ * @param value the replacement.
+ * @return {link this}.
+ */
+ public Tags withBooleans(BooleanTag[] value) {
+ this.booleans = value;
+ return this;
+ }
+
+ @Override
+ public final int hashCode() {
+ int h = 1;
+ for (StringTag o : this.strings) h = 31 * h + (o == null ? 0 : o.hashCode());
+ for (LongTag o : this.longs) h = 31 * h + (o == null ? 0 : o.hashCode());
+ for (BooleanTag o : this.booleans) h = 31 * h + (o == null ? 0 : o.hashCode());
+ return h;
+ }
+
+ @Override
+ public final boolean equals(Object o) {
+ return o instanceof Tags && equals((Tags) o);
+ }
+
+ public final boolean equals(Tags o) {
+ if (o == null) return false;
+ if (o == this) return true;
+ return o.getClass() == Tags.class
+ && java.util.Arrays.equals(this.strings, o.strings)
+ && java.util.Arrays.equals(this.longs, o.longs)
+ && java.util.Arrays.equals(this.booleans, o.booleans);
+ }
+
+}
diff --git a/kamon-core/src/main/scala/kamon/context/BinaryPropagation.scala b/kamon-core/src/main/scala/kamon/context/BinaryPropagation.scala
index 75e65c44..296003d5 100644
--- a/kamon-core/src/main/scala/kamon/context/BinaryPropagation.scala
+++ b/kamon-core/src/main/scala/kamon/context/BinaryPropagation.scala
@@ -20,7 +20,9 @@ import java.io.{ByteArrayInputStream, ByteArrayOutputStream, InputStream, Output
import com.typesafe.config.Config
import kamon.context.BinaryPropagation.{ByteStreamReader, ByteStreamWriter}
-import kamon.context.generated.binary.context.{Context => ColferContext, Entry => ColferEntry}
+import kamon.context.generated.binary.context.{Context => ColferContext, Entry => ColferEntry, Tags => ColferTags}
+import kamon.context.generated.binary.context.{StringTag => ColferStringTag, LongTag => ColferLongTag, BooleanTag => ColferBooleanTag}
+import kamon.tag.{Tag, TagSet}
import org.slf4j.LoggerFactory
import scala.reflect.ClassTag
@@ -152,7 +154,7 @@ object BinaryPropagation {
* configured entry readers and writers.
*/
class Default(settings: Settings) extends Propagation[ByteStreamReader, ByteStreamWriter] {
- private val _log = LoggerFactory.getLogger(classOf[BinaryPropagation.Default])
+ private val _logger = LoggerFactory.getLogger(classOf[BinaryPropagation.Default])
private val _streamPool = new ThreadLocal[Default.ReusableByteStreamWriter] {
override def initialValue(): Default.ReusableByteStreamWriter = new Default.ReusableByteStreamWriter(128)
}
@@ -170,39 +172,29 @@ object BinaryPropagation {
}
contextData.failed.foreach {
- case NonFatal(t) => _log.warn("Failed to read Context from ByteStreamReader", t)
+ case NonFatal(t) => _logger.warn("Failed to read Context from ByteStreamReader", t)
}
contextData.map { colferContext =>
// Context tags
- var tagSectionsCount = colferContext.tags.length
- if (tagSectionsCount > 0 && tagSectionsCount % 2 != 0) {
- _log.warn("Malformed Context tags found, tags consistency might be compromised")
- tagSectionsCount -= 1
+ val tagsBuilder = Map.newBuilder[String, Any]
+ if(colferContext.tags != null) {
+ colferContext.tags.strings.foreach(t => tagsBuilder += (t.key -> t.value))
+ colferContext.tags.longs.foreach(t => tagsBuilder += (t.key -> t.value))
+ colferContext.tags.booleans.foreach(t => tagsBuilder += (t.key -> t.value))
}
-
- val tags = if (tagSectionsCount > 0) {
- val tagsBuilder = Map.newBuilder[String, String]
- var tagIndex = 0
- while (tagIndex < tagSectionsCount) {
- tagsBuilder += (colferContext.tags(tagIndex) -> colferContext.tags(tagIndex + 1))
- tagIndex += 2
- }
- tagsBuilder.result()
-
- } else Map.empty[String, String]
-
+ val tags = TagSet.from(tagsBuilder.result())
// Only reads the entries for which there is a registered reader
colferContext.entries.foldLeft(Context.of(tags)) {
case (context, entryData) =>
- settings.incomingEntries.get(entryData.name).map { entryReader =>
+ settings.incomingEntries.get(entryData.key).map { entryReader =>
var contextWithEntry = context
try {
- contextWithEntry = entryReader.read(ByteStreamReader.of(entryData.content), context)
+ contextWithEntry = entryReader.read(ByteStreamReader.of(entryData.value), context)
} catch {
- case NonFatal(t) => _log.warn("Failed to read entry [{}]", entryData.name.asInstanceOf[Any], t)
+ case NonFatal(t) => _logger.warn("Failed to read entry [{}]", entryData.key.asInstanceOf[Any], t)
}
contextWithEntry
@@ -218,17 +210,36 @@ object BinaryPropagation {
val output = _streamPool.get()
val contextOutgoingBuffer = _contextBufferPool.get()
- if (context.tags.nonEmpty) {
- val tags = Array.ofDim[String](context.tags.size * 2)
- var tagIndex = 0
- context.tags.foreach {
- case (key, value) =>
- tags.update(tagIndex, key)
- tags.update(tagIndex + 1, value)
- tagIndex += 2
+ if(context.tags.nonEmpty()) {
+ val tagsData = new ColferTags()
+ val strings = Array.newBuilder[ColferStringTag]
+ val longs = Array.newBuilder[ColferLongTag]
+ val booleans = Array.newBuilder[ColferBooleanTag]
+
+ context.tags.iterator().foreach {
+ case t: Tag.String =>
+ val st = new ColferStringTag()
+ st.setKey(t.key)
+ st.setValue(t.value)
+ strings += st
+
+ case t: Tag.Long =>
+ val lt = new ColferLongTag()
+ lt.setKey(t.key)
+ lt.setValue(t.value)
+ longs += lt
+
+ case t: Tag.Boolean =>
+ val bt = new ColferBooleanTag()
+ bt.setKey(t.key)
+ bt.setValue(t.value)
+ booleans += bt
}
- contextData.tags = tags
+ tagsData.setStrings(strings.result())
+ tagsData.setLongs(longs.result())
+ tagsData.setBooleans(booleans.result())
+ contextData.setTags(tagsData)
}
if (context.entries.nonEmpty) {
@@ -239,10 +250,10 @@ object BinaryPropagation {
output.reset()
entryWriter.write(context, output)
- colferEntry.name = entryName
- colferEntry.content = output.toByteArray()
+ colferEntry.key = entryName
+ colferEntry.value = output.toByteArray()
} catch {
- case NonFatal(t) => _log.warn("Failed to write entry [{}]", entryName.asInstanceOf[Any], t)
+ case NonFatal(t) => _logger.warn("Failed to write entry [{}]", entryName.asInstanceOf[Any], t)
}
colferEntry
@@ -255,7 +266,7 @@ object BinaryPropagation {
val contextSize = contextData.marshal(contextOutgoingBuffer, 0)
writer.write(contextOutgoingBuffer, 0, contextSize)
} catch {
- case NonFatal(t) => _log.warn("Failed to write Context to ByteStreamWriter", t)
+ case NonFatal(t) => _logger.warn("Failed to write Context to ByteStreamWriter", t)
}
}
}
diff --git a/kamon-core/src/main/scala/kamon/context/Context.scala b/kamon-core/src/main/scala/kamon/context/Context.scala
index 2a7a382e..054a7897 100644
--- a/kamon-core/src/main/scala/kamon/context/Context.scala
+++ b/kamon-core/src/main/scala/kamon/context/Context.scala
@@ -16,32 +16,92 @@
package kamon
package context
-import java.util.{Map => JavaMap}
-import scala.collection.JavaConverters._
-
-class Context private (val entries: Map[String, Any], val tags: Map[String, String]) {
+import kamon.tag.TagSet
+
+
+/**
+ * An immutable set of information that is tied to the processing of single operation in a service. A Context instance
+ * can contain tags and entries.
+ *
+ * Context tags are built on top of the TagSet abstraction that ships with Kamon and since Kamon knows exactly what
+ * types of values can be included in a TagSet it can automatically serialize and deserialize them when going over
+ * HTTP and/or Binary transports.
+ *
+ * Context entries can contain any arbitrary type specified by the user, but require additional configuration and
+ * implementation of entry readers and writers if you need them to go over HTTP and/or Binary transports.
+ *
+ * Context instances are meant to be constructed by using the builder functions on the Context companion object.
+ *
+ */
+class Context private (val entries: Map[String, Any], val tags: TagSet) {
+ /**
+ * Gets an entry from this Context. If the entry is present it's current value is returned, otherwise the empty value
+ * from the provided key will be returned.
+ */
def get[T](key: Context.Key[T]): T =
entries.getOrElse(key.name, key.emptyValue).asInstanceOf[T]
- def getTag(tagKey: String): Option[String] =
- tags.get(tagKey)
+ /**
+ * Executes a lookup on the context tags. The actual return type depends on the provided lookup instance. Take a look
+ * at the built-in lookups available on the Lookups companion object.
+ */
+ def getTag[T](lookup: TagSet.Lookup[T]): T =
+ tags.get(lookup)
+
+
+ /**
+ * Creates a new Context instance that includes the provided key and value. If the provided key was already
+ * associated with another value then the previous value will be discarded and overwritten with the provided one.
+ */
def withKey[T](key: Context.Key[T], value: T): Context =
new Context(entries.updated(key.name, value), tags)
- def withTag(tagKey: String, tagValue: String): Context =
- new Context(entries, tags.updated(tagKey, tagValue))
- def withTags(tags: Map[String, String]): Context =
- new Context(entries, this.tags ++ tags)
+ /**
+ * Creates a new Context instance that includes the provided tag key and value. If the provided tag key was already
+ * associated with another value then the previous tag value will be discarded and overwritten with the provided one.
+ */
+ def withTag(key: String, value: String): Context =
+ new Context(entries, tags.withTag(key, value))
+
+
+ /**
+ * Creates a new Context instance that includes the provided tag key and value. If the provided tag key was already
+ * associated with another value then the previous tag value will be discarded and overwritten with the provided one.
+ */
+ def withTag(key: String, value: Long): Context =
+ new Context(entries, tags.withTag(key, value))
+
+
+ /**
+ * Creates a new Context instance that includes the provided tag key and value. If the provided tag key was already
+ * associated with another value then the previous tag value will be discarded and overwritten with the provided one.
+ */
+ def withTag(key: String, value: Boolean): Context =
+ new Context(entries, tags.withTag(key, value))
+
+
+ /**
+ * Creates a new Context instance that includes the provided tags. If any of the tags in this instance are associated
+ * to a key present on the provided tags then the previous values will be discarded and overwritten with the provided
+ * ones.
+ */
+ def withTags(tags: TagSet): Context =
+ new Context(entries, this.tags.and(tags))
- def withTags(tags: JavaMap[String, String]): Context =
- new Context(entries, this.tags ++ tags.asScala.toMap)
+ /**
+ * Returns whether this Context does not have any tags and does not have any entries.
+ */
def isEmpty(): Boolean =
entries.isEmpty && tags.isEmpty
+
+ /**
+ * Returns whether this Context has any information, either as tags or entries.
+ */
def nonEmpty(): Boolean =
!isEmpty()
@@ -49,32 +109,48 @@ class Context private (val entries: Map[String, Any], val tags: Map[String, Stri
object Context {
- val Empty = new Context(Map.empty, Map.empty)
+ val Empty = new Context(Map.empty, TagSet.Empty)
- def of(tags: JavaMap[String, String]): Context =
- new Context(Map.empty, tags.asScala.toMap)
-
- def of(tags: Map[String, String]): Context =
+ /**
+ * Creates a new Context instance with the provided tags and no entries.
+ */
+ def of(tags: TagSet): Context =
new Context(Map.empty, tags)
+
+ /**
+ * Creates a new Context instance with the provided key and no tags.
+ */
def of[T](key: Context.Key[T], value: T): Context =
- new Context(Map(key.name -> value), Map.empty)
+ new Context(Map(key.name -> value), TagSet.Empty)
- def of[T](key: Context.Key[T], value: T, tags: JavaMap[String, String]): Context =
- new Context(Map(key.name -> value), tags.asScala.toMap)
- def of[T](key: Context.Key[T], value: T, tags: Map[String, String]): Context =
+ /**
+ * Creates a new Context instance with a single entry and the provided tags.
+ */
+ def of[T](key: Context.Key[T], value: T, tags: TagSet): Context =
new Context(Map(key.name -> value), tags)
+
+ /**
+ * Creates a new Context instance with two entries and no tags.
+ */
def of[T, U](keyOne: Context.Key[T], valueOne: T, keyTwo: Context.Key[U], valueTwo: U): Context =
- new Context(Map(keyOne.name -> valueOne, keyTwo.name -> valueTwo), Map.empty)
+ new Context(Map(keyOne.name -> valueOne, keyTwo.name -> valueTwo), TagSet.Empty)
- def of[T, U](keyOne: Context.Key[T], valueOne: T, keyTwo: Context.Key[U], valueTwo: U, tags: JavaMap[String, String]): Context =
- new Context(Map(keyOne.name -> valueOne, keyTwo.name -> valueTwo), tags.asScala.toMap)
- def of[T, U](keyOne: Context.Key[T], valueOne: T, keyTwo: Context.Key[U], valueTwo: U, tags: Map[String, String]): Context =
+ /**
+ * Creates a new Context instance with two entries and the provided tags.
+ */
+ def of[T, U](keyOne: Context.Key[T], valueOne: T, keyTwo: Context.Key[U], valueTwo: U, tags: TagSet): Context =
new Context(Map(keyOne.name -> valueOne, keyTwo.name -> valueTwo), tags)
+
+ /**
+ * Creates a new Context.Key instance that can be used to insert and retrieve values from the context entries.
+ * Context keys must have a unique name since they will be looked up in transports by their name and the context
+ * entries are internally stored using their key name as index.
+ */
def key[T](name: String, emptyValue: T): Context.Key[T] =
new Context.Key(name, emptyValue)
diff --git a/kamon-core/src/main/scala/kamon/context/HttpPropagation.scala b/kamon-core/src/main/scala/kamon/context/HttpPropagation.scala
index 6a15e2a6..fbee75cc 100644
--- a/kamon-core/src/main/scala/kamon/context/HttpPropagation.scala
+++ b/kamon-core/src/main/scala/kamon/context/HttpPropagation.scala
@@ -17,6 +17,7 @@ package kamon
package context
import com.typesafe.config.Config
+import kamon.tag.{Tag, TagSet}
import org.slf4j.LoggerFactory
import scala.reflect.ClassTag
@@ -91,7 +92,7 @@ object HttpPropagation {
* 3. Read all context entries using the incoming entries configuration.
*/
override def read(reader: HeaderReader): Context = {
- val tags = Map.newBuilder[String, String]
+ val tags = Map.newBuilder[String, Any]
// Tags encoded together in the context tags header.
try {
@@ -99,7 +100,7 @@ object HttpPropagation {
contextTagsHeader.split(";").foreach(tagData => {
val tagPair = tagData.split("=")
if (tagPair.length == 2) {
- tags += (tagPair(0) -> tagPair(1))
+ tags += (tagPair(0) -> parseTagValue(tagPair(1)))
}
})
}
@@ -118,7 +119,7 @@ object HttpPropagation {
}
// Incoming Entries
- settings.incomingEntries.foldLeft(Context.of(tags.result())) {
+ settings.incomingEntries.foldLeft(Context.of(TagSet.from(tags.result()))) {
case (context, (entryName, entryDecoder)) =>
var result = context
try {
@@ -145,10 +146,12 @@ object HttpPropagation {
}
// Write tags with specific mappings or append them to the context tags header.
- context.tags.foreach {
- case (tagKey, tagValue) => settings.tagsMappings.get(tagKey) match {
- case Some(mappedHeader) => writer.write(mappedHeader, tagValue)
- case None => appendTag(tagKey, tagValue)
+ context.tags.iterator().foreach { tag =>
+ val tagKey = tag.key
+
+ settings.tagsMappings.get(tagKey) match {
+ case Some(mappedHeader) => writer.write(mappedHeader, tagValueWithPrefix(tag))
+ case None => appendTag(tagKey, Tag.unwrapValue(tag).toString)
}
}
@@ -167,6 +170,54 @@ object HttpPropagation {
}
}
}
+
+
+ private val _longTypePrefix = "l:"
+ private val _booleanTypePrefix = "b:"
+ private val _booleanTrue = "true"
+ private val _booleanFalse = "false"
+
+ /**
+ * Tries to infer and parse a value into one of the supported tag types: String, Long or Boolean by looking for the
+ * type indicator prefix on the tag value. If the inference fails it will default to treat the value as a String.
+ */
+ private def parseTagValue(value: String): Any = {
+ if (value.isEmpty || value.length < 2) // Empty and short values definitely do not have type indicators.
+ value
+ else {
+ if(value.startsWith(_longTypePrefix)) {
+ // Try to parse the content as a Long value.
+ val remaining = value.substring(2)
+ try {
+ java.lang.Long.parseLong(remaining)
+ } catch {
+ case _: Throwable => remaining
+ }
+
+ } else if(value.startsWith(_booleanTypePrefix)) {
+
+ // Try to parse the content as a Boolean value.
+ val remaining = value.substring(2)
+ if(remaining.equals(_booleanTrue))
+ true
+ else if(remaining.equals(_booleanFalse))
+ false
+ else
+ remaining
+
+ } else value
+ }
+ }
+
+ /**
+ * Returns the actual value to be written in the HTTP transport, with a type prefix if applicable.
+ */
+ private def tagValueWithPrefix(tag: Tag): String = tag match {
+ case t: Tag.String => t.value
+ case t: Tag.Boolean => _booleanTypePrefix + t.value.toString
+ case t: Tag.Long => _longTypePrefix + t.value.toString
+ }
+
}
case class Settings(
diff --git a/kamon-core/src/main/scala/kamon/instrumentation/HttpServer.scala b/kamon-core/src/main/scala/kamon/instrumentation/HttpServer.scala
index 659da8aa..68975711 100644
--- a/kamon-core/src/main/scala/kamon/instrumentation/HttpServer.scala
+++ b/kamon-core/src/main/scala/kamon/instrumentation/HttpServer.scala
@@ -6,7 +6,9 @@ import java.time.Duration
import com.typesafe.config.Config
import kamon.context.Context
import kamon.instrumentation.HttpServer.Settings.TagMode
-import kamon.metric.MeasurementUnit.{time, information}
+import kamon.metric.MeasurementUnit.{information, time}
+import kamon.tag.TagSet
+import kamon.tag.Lookups.{any, option}
import kamon.trace.{IdentityProvider, Span}
import kamon.util.GlobPathFilter
import org.slf4j.LoggerFactory
@@ -348,7 +350,7 @@ object HttpServer {
span.disableMetrics()
- for { traceIdTag <- settings.traceIDTag; customTraceID <- context.getTag(traceIdTag) } {
+ for {traceIdTag <- settings.traceIDTagLookup; customTraceID <- context.getTag(traceIdTag) } {
val identifier = Kamon.identityProvider.traceIdGenerator().from(customTraceID)
if(identifier != IdentityProvider.NoIdentifier)
span.withTraceID(identifier)
@@ -361,9 +363,12 @@ object HttpServer {
}
addRequestTag("http.url", request.url, settings.urlTagMode)
- addRequestTag("http.method", request.method, settings.urlTagMode)
+ addRequestTag("http.method", request.method, settings.methodTagMode)
settings.contextTags.foreach {
- case (tagName, mode) => context.getTag(tagName).foreach(tagValue => addRequestTag(tagName, tagValue, mode))
+ case (tagName, mode) =>
+ val tagValue = context.getTag(any(tagName))
+ if(tagValue != null)
+ addRequestTag(tagName, tagValue.toString, mode)
}
span.start()
@@ -385,7 +390,7 @@ object HttpServer {
propagationChannel: String,
enableServerMetrics: Boolean,
enableTracing: Boolean,
- traceIDTag: Option[String],
+ traceIDTagLookup: Option[TagSet.Lookup[Option[String]]],
enableSpanMetrics: Boolean,
urlTagMode: TagMode,
methodTagMode: TagMode,
@@ -424,7 +429,10 @@ object HttpServer {
// Tracing settings
val enableTracing = config.getBoolean("tracing.enabled")
- val traceIdTag = Option(config.getString("tracing.preferred-trace-id-tag")).filterNot(_ == "none")
+ val traceIdTagLookup = Option(config.getString("tracing.preferred-trace-id-tag"))
+ .filterNot(_ == "none")
+ .map(option)
+
val enableSpanMetrics = config.getBoolean("tracing.span-metrics")
val urlTagMode = TagMode.from(config.getString("tracing.tags.url"))
val methodTagMode = TagMode.from(config.getString("tracing.tags.method"))
@@ -441,12 +449,12 @@ object HttpServer {
case (pattern, operationName) => (new GlobPathFilter(pattern), operationName)
}
- Settings(
+ Settings (
enablePropagation,
propagationChannel,
enableServerMetrics,
enableTracing,
- traceIdTag,
+ traceIdTagLookup,
enableSpanMetrics,
urlTagMode,
methodTagMode,
diff --git a/kamon-core/src/main/scala/kamon/metric/Accumulator.scala b/kamon-core/src/main/scala/kamon/metric/Accumulator.scala
index bf412980..945feeeb 100644
--- a/kamon-core/src/main/scala/kamon/metric/Accumulator.scala
+++ b/kamon-core/src/main/scala/kamon/metric/Accumulator.scala
@@ -17,7 +17,7 @@ package kamon.metric
import java.time.{Duration, Instant}
-import kamon.{Kamon, Tags}
+import kamon.{Kamon, STags}
import kamon.metric.PeriodSnapshotAccumulator.{MetricDistributionKey, MetricValueKey}
import kamon.util.Clock
@@ -169,6 +169,6 @@ class PeriodSnapshotAccumulator(duration: Duration, margin: Duration) {
}
object PeriodSnapshotAccumulator {
- case class MetricValueKey(name: String, tags: Tags, unit: MeasurementUnit)
- case class MetricDistributionKey(name: String, tags: Tags, unit: MeasurementUnit, dynamicRange: DynamicRange)
+ case class MetricValueKey(name: String, tags: STags, unit: MeasurementUnit)
+ case class MetricDistributionKey(name: String, tags: STags, unit: MeasurementUnit, dynamicRange: DynamicRange)
}
diff --git a/kamon-core/src/main/scala/kamon/metric/Metric.scala b/kamon-core/src/main/scala/kamon/metric/Metric.scala
index f5ce7b45..69ef88bc 100644
--- a/kamon-core/src/main/scala/kamon/metric/Metric.scala
+++ b/kamon-core/src/main/scala/kamon/metric/Metric.scala
@@ -34,12 +34,12 @@ trait Metric[T] {
def unit: MeasurementUnit
def refine(tags: JTags): T
- def refine(tags: Tags): T
+ def refine(tags: STags): T
def refine(tags: (String, String)*): T
def refine(tag: String, value: String): T
def remove(tags: JTags): Boolean
- def remove(tags: Tags): Boolean
+ def remove(tags: STags): Boolean
def remove(tags: (String, String)*): Boolean
def remove(tag: String, value: String): Boolean
}
@@ -52,7 +52,7 @@ trait CounterMetric extends Metric[Counter] with Counter
private[kamon] abstract sealed class BaseMetric[T, S](val instrumentType: InstrumentType) extends Metric[T] {
- private[kamon] val instruments = TrieMap.empty[Tags, T]
+ private[kamon] val instruments = TrieMap.empty[STags, T]
protected lazy val baseInstrument: T = instruments.atomicGetOrElseUpdate(Map.empty, createInstrument(Map.empty))
override def refine(tags: JTags):T =
@@ -72,7 +72,7 @@ private[kamon] abstract sealed class BaseMetric[T, S](val instrumentType: Instru
override def remove(tags: JTags):Boolean =
remove(tags.asScala.toMap)
- override def remove(tags: Tags): Boolean =
+ override def remove(tags: STags): Boolean =
if(tags.nonEmpty) instruments.remove(tags).nonEmpty else false
override def remove(tags: (String, String)*): Boolean =
@@ -88,7 +88,7 @@ private[kamon] abstract sealed class BaseMetric[T, S](val instrumentType: Instru
private[kamon] def incarnations(): Seq[Map[String, String]] =
instruments.keys.toSeq
- protected def createInstrument(tags: Tags): T
+ protected def createInstrument(tags: STags): T
protected def createSnapshot(instrument: T): S
}
@@ -106,7 +106,7 @@ private[kamon] final class HistogramMetricImpl(val name: String, val unit: Measu
override def record(value: Long, times: Long): Unit =
baseInstrument.record(value, times)
- override protected def createInstrument(tags: Tags): Histogram =
+ override protected def createInstrument(tags: STags): Histogram =
factory.get().buildHistogram(customDynamicRange)(name, tags, unit)
override protected def createSnapshot(instrument: Histogram): MetricDistribution =
@@ -118,7 +118,7 @@ private[kamon] final class RangeSamplerMetricImpl(val name: String, val unit: Me
extends BaseMetric[RangeSampler, MetricDistribution](RangeSampler) with RangeSamplerMetric {
private val logger = LoggerFactory.getLogger(classOf[RangeSamplerMetric])
- private val scheduledSamplers = TrieMap.empty[Tags, ScheduledFuture[_]]
+ private val scheduledSamplers = TrieMap.empty[STags, ScheduledFuture[_]]
def dynamicRange: DynamicRange =
baseInstrument.dynamicRange
@@ -141,7 +141,7 @@ private[kamon] final class RangeSamplerMetricImpl(val name: String, val unit: Me
override def sample(): Unit =
baseInstrument.sample()
- override protected def createInstrument(tags: Tags): RangeSampler = {
+ override protected def createInstrument(tags: STags): RangeSampler = {
val rangeSampler = factory.get().buildRangeSampler(customDynamicRange, customSampleInterval)(name, tags, unit)
val sampleInterval = rangeSampler.sampleInterval.toMillis
val scheduledFuture = scheduler.scheduleAtFixedRate(scheduledSampler(rangeSampler), sampleInterval, sampleInterval, TimeUnit.MILLISECONDS)
@@ -153,7 +153,7 @@ private[kamon] final class RangeSamplerMetricImpl(val name: String, val unit: Me
override def remove(tags: JTags): Boolean =
removeAndStopSampler(tags.asScala.toMap)
- override def remove(tags: Tags): Boolean =
+ override def remove(tags: STags): Boolean =
removeAndStopSampler(tags)
override def remove(tags: (String, String)*): Boolean =
@@ -162,7 +162,7 @@ private[kamon] final class RangeSamplerMetricImpl(val name: String, val unit: Me
override def remove(tag: String, value: String): Boolean =
removeAndStopSampler(Map(tag -> value))
- private def removeAndStopSampler(tags: Tags): Boolean = {
+ private def removeAndStopSampler(tags: STags): Boolean = {
val removed = super.remove(tags)
if(removed)
scheduledSamplers.remove(tags).foreach(sf => {
@@ -190,7 +190,7 @@ private[kamon] final class CounterMetricImpl(val name: String, val unit: Measure
override def increment(times: Long): Unit =
baseInstrument.increment(times)
- override protected def createInstrument(tags: Tags): Counter =
+ override protected def createInstrument(tags: STags): Counter =
factory.get().buildCounter(name, tags, unit)
override protected def createSnapshot(instrument: Counter): MetricValue =
@@ -215,7 +215,7 @@ private[kamon] final class GaugeMetricImpl(val name: String, val unit: Measureme
override def set(value: Long): Unit =
baseInstrument.set(value)
- override protected def createInstrument(tags: Tags): Gauge =
+ override protected def createInstrument(tags: STags): Gauge =
factory.get().buildGauge(name, tags, unit)
override protected def createSnapshot(instrument: Gauge): MetricValue =
diff --git a/kamon-core/src/main/scala/kamon/metric/PeriodSnapshot.scala b/kamon-core/src/main/scala/kamon/metric/PeriodSnapshot.scala
index 50a5f778..09a0e029 100644
--- a/kamon-core/src/main/scala/kamon/metric/PeriodSnapshot.scala
+++ b/kamon-core/src/main/scala/kamon/metric/PeriodSnapshot.scala
@@ -39,13 +39,13 @@ case class MetricsSnapshot(
* Snapshot for instruments that internally track a single value. Meant to be used for counters and gauges.
*
*/
-case class MetricValue(name: String, tags: Tags, unit: MeasurementUnit, value: Long)
+case class MetricValue(name: String, tags: STags, unit: MeasurementUnit, value: Long)
/**
* Snapshot for instruments that internally the distribution of values in a defined dynamic range. Meant to be used
* with histograms and min max counters.
*/
-case class MetricDistribution(name: String, tags: Tags, unit: MeasurementUnit, dynamicRange: DynamicRange, distribution: Distribution)
+case class MetricDistribution(name: String, tags: STags, unit: MeasurementUnit, dynamicRange: DynamicRange, distribution: Distribution)
trait Distribution {
diff --git a/kamon-core/src/main/scala/kamon/metric/Timer.scala b/kamon-core/src/main/scala/kamon/metric/Timer.scala
index 74d203a9..749ac876 100644
--- a/kamon-core/src/main/scala/kamon/metric/Timer.scala
+++ b/kamon-core/src/main/scala/kamon/metric/Timer.scala
@@ -15,7 +15,7 @@
package kamon.metric
-import kamon.{JTags, Tags}
+import kamon.{JTags, STags}
trait Timer extends Histogram {
def start(): StartedTimer
@@ -82,7 +82,7 @@ private[kamon] final class TimerMetricImpl(val underlyingHistogram: HistogramMet
override def refine(tags: JTags): Timer =
refine(tags.asScala.toMap)
- override def refine(tags: Tags): Timer =
+ override def refine(tags: STags): Timer =
new TimerImpl(underlyingHistogram.refine(tags))
override def refine(tags: (String, String)*): Timer =
@@ -94,7 +94,7 @@ private[kamon] final class TimerMetricImpl(val underlyingHistogram: HistogramMet
override def remove(tags: JTags): Boolean =
remove(tags.asScala.toMap)
- override def remove(tags: Tags): Boolean =
+ override def remove(tags: STags): Boolean =
underlyingHistogram.remove(tags)
override def remove(tags: (String, String)*): Boolean =
diff --git a/kamon-core/src/main/scala/kamon/package.scala b/kamon-core/src/main/scala/kamon/package.scala
index d694206c..3da676cd 100644
--- a/kamon-core/src/main/scala/kamon/package.scala
+++ b/kamon-core/src/main/scala/kamon/package.scala
@@ -23,7 +23,7 @@ import scala.collection.concurrent.TrieMap
package object kamon {
- type Tags = Map[String, String]
+ type STags = Map[String, String]
type JTags = java.util.Map[String, String]
diff --git a/kamon-core/src/main/scala/kamon/tag/Lookups.scala b/kamon-core/src/main/scala/kamon/tag/Lookups.scala
index 44ffb6f4..beb1996a 100644
--- a/kamon-core/src/main/scala/kamon/tag/Lookups.scala
+++ b/kamon-core/src/main/scala/kamon/tag/Lookups.scala
@@ -3,18 +3,27 @@ package kamon.tag
import java.util.Optional
import java.lang.{Boolean => JBoolean, Long => JLong, String => JString}
-import kamon.tag.Tags.Lookup
+import kamon.tag.TagSet.Lookup
import scala.reflect.ClassTag
object Lookups {
/**
+ * Finds a value associated to the provided key and returns it. If the key is not present then a null is returned.
+ */
+ def any(key: JString) = new Lookup[Any] {
+ override def execute(storage: Map[JString, Any]): Any =
+ findAndTransform(key, storage, _any, null)
+ }
+
+
+ /**
* Finds a String value associated to the provided key and returns it. If the key is not present or the value
* associated with they is not a String then a null is returned.
*/
def plain(key: JString) = new Lookup[JString] {
- override def run(storage: Map[JString, Any]): JString =
+ override def execute(storage: Map[JString, Any]): JString =
findAndTransform(key, storage, _plainString, null)
}
@@ -24,7 +33,7 @@ object Lookups {
* not present or the value associated with they is not a String then a None is returned.
*/
def option(key: JString) = new Lookup[Option[JString]] {
- override def run(storage: Map[JString, Any]): Option[JString] =
+ override def execute(storage: Map[JString, Any]): Option[JString] =
findAndTransform(key, storage, _stringOption, None)
}
@@ -34,7 +43,7 @@ object Lookups {
* is not present or the value associated with they is not a String then Optional.empty() is returned.
*/
def optional(key: JString) = new Lookup[Optional[String]] {
- override def run(storage: Map[String, Any]): Optional[String] =
+ override def execute(storage: Map[String, Any]): Optional[String] =
findAndTransform(key, storage, _stringOptional, Optional.empty())
}
@@ -47,7 +56,7 @@ object Lookups {
* This lookup type is guaranteed to return a non-null String representation of value.
*/
def coerce(key: String) = new Lookup[String] {
- override def run(storage: Map[String, Any]): String = {
+ override def execute(storage: Map[String, Any]): String = {
val value = storage(key)
if(value == null)
"unknown"
@@ -62,7 +71,7 @@ object Lookups {
* associated with they is not a Boolean then a null is returned.
*/
def plainBoolean(key: String) = new Lookup[JBoolean] {
- override def run(storage: Map[String, Any]): JBoolean =
+ override def execute(storage: Map[String, Any]): JBoolean =
findAndTransform(key, storage, _plainBoolean, null)
}
@@ -72,7 +81,7 @@ object Lookups {
* is not present or the value associated with they is not a Boolean then a None is returned.
*/
def booleanOption(key: String) = new Lookup[Option[JBoolean]] {
- override def run(storage: Map[String, Any]): Option[JBoolean] =
+ override def execute(storage: Map[String, Any]): Option[JBoolean] =
findAndTransform(key, storage, _booleanOption, None)
}
@@ -82,7 +91,7 @@ object Lookups {
* is not present or the value associated with they is not a Boolean then Optional.empty() is returned.
*/
def booleanOptional(key: String) = new Lookup[Optional[JBoolean]] {
- override def run(storage: Map[String, Any]): Optional[JBoolean] =
+ override def execute(storage: Map[String, Any]): Optional[JBoolean] =
findAndTransform(key, storage, _booleanOptional, Optional.empty())
}
@@ -92,7 +101,7 @@ object Lookups {
* associated with they is not a Long then a null is returned.
*/
def plainLong(key: String) = new Lookup[JLong] {
- override def run(storage: Map[String, Any]): JLong =
+ override def execute(storage: Map[String, Any]): JLong =
findAndTransform(key, storage, _plainLong, null)
}
@@ -102,7 +111,7 @@ object Lookups {
* not present or the value associated with they is not a Long then a None is returned.
*/
def longOption(key: String) = new Lookup[Option[JLong]] {
- override def run(storage: Map[String, Any]): Option[JLong] =
+ override def execute(storage: Map[String, Any]): Option[JLong] =
findAndTransform(key, storage, _longOption, None)
}
@@ -112,7 +121,7 @@ object Lookups {
* is not present or the value associated with they is not a Long then Optional.empty() is returned.
*/
def longOptional(key: String) = new Lookup[Optional[JLong]] {
- override def run(storage: Map[String, Any]): Optional[JLong] =
+ override def execute(storage: Map[String, Any]): Optional[JLong] =
findAndTransform(key, storage, _longOptional, Optional.empty())
}
@@ -134,6 +143,7 @@ object Lookups {
transform(value.asInstanceOf[R])
}
+ private val _any = (a: Any) => a
private val _plainString = (a: JString) => a
private val _stringOption = (a: JString) => Option(a)
private val _stringOptional = (a: JString) => Optional.of(a)
diff --git a/kamon-core/src/main/scala/kamon/tag/Tag.scala b/kamon-core/src/main/scala/kamon/tag/Tag.scala
new file mode 100644
index 00000000..69a5d7e7
--- /dev/null
+++ b/kamon-core/src/main/scala/kamon/tag/Tag.scala
@@ -0,0 +1,50 @@
+package kamon.tag
+
+import java.lang.{Boolean => JBoolean, Long => JLong, String => JString}
+
+/**
+ * Marker trait for allowed Tag implementations. Users are not meant to create implementations of this trait outside
+ * of Kamon. Furthermore, users of TagSet might never need to interact with these classes but rather perform lookups
+ * using the lookup DSL.
+ */
+sealed trait Tag {
+ def key: JString
+}
+
+object Tag {
+
+ /**
+ * Represents a String key pointing to a String value.
+ */
+ trait String extends Tag {
+ def value: JString
+ }
+
+
+ /**
+ * Represents a String key pointing to a Boolean value.
+ */
+ trait Boolean extends Tag {
+ def value: JBoolean
+ }
+
+
+ /**
+ * Represents a String key pointing to a Long value.
+ */
+ trait Long extends Tag {
+ def value: JLong
+ }
+
+
+ /**
+ * Returns the value held inside of a Tag instance. This utility function is specially useful when iterating over
+ * tags but not caring about the concrete tag type.
+ */
+ def unwrapValue(tag: Tag): Any = tag match {
+ case t: Tag.String => t.value
+ case t: Tag.Boolean => t.value
+ case t: Tag.Long => t.value
+ }
+}
+
diff --git a/kamon-core/src/main/scala/kamon/tag/Tags.scala b/kamon-core/src/main/scala/kamon/tag/TagSet.scala
index b7813da6..1090ca5a 100644
--- a/kamon-core/src/main/scala/kamon/tag/Tags.scala
+++ b/kamon-core/src/main/scala/kamon/tag/TagSet.scala
@@ -1,47 +1,12 @@
package kamon.tag
-import kamon.tag.Tags.Lookup
+import kamon.tag.TagSet.Lookup
import scala.collection.JavaConverters.asScalaIteratorConverter
import java.lang.{Boolean => JBoolean, Long => JLong, String => JString}
import org.slf4j.LoggerFactory
-
-/**
- * Marker trait for allowed Tag implementations. Users are not meant to create implementations of this trait outside
- * of Kamon.
- */
-sealed trait Tag
-
-object Tag {
-
- /**
- * Represents a String key pointing to a String value.
- */
- trait String extends Tag {
- def key: JString
- def value: JString
- }
-
- /**
- * Represents a String key pointing to a Boolean value.
- */
- trait Boolean extends Tag {
- def key: JString
- def value: JBoolean
- }
-
- /**
- * Represents a String key pointing to a Long value.
- */
- trait Long extends Tag {
- def key: JString
- def value: JLong
- }
-}
-
-
/**
* A immutable collection of key/value pairs with specialized support for storing String keys pointing to String, Long
* and/or Boolean values.
@@ -51,8 +16,8 @@ object Tag {
* lookup pairs without prescribing a mechanism for handling missing values. I.e. users of this class can decide
* whether to receive a null, java.util.Optional, scala.Option or any other value when looking up a pair.
*
- * Tags can only be created from the builder functions on the Tags companion object. There are two different options
- * to read the contained pairs from a Tags instance:
+ * TagSet instances can only be created from the builder functions on the TagSet companion object. There are two
+ * different options to read the contained pairs from a Tags instance:
*
* 1. Using the lookup DSL. You can use the Lookup DSL when you know exactly that you are trying to get out of the
* tags instance. The lookup DSL is biased towards String keys since they are by far the most common case. For
@@ -69,80 +34,96 @@ object Tag {
* cumbersome operation is rarely necessary on user-facing code.
*
*/
-class Tags private(private val _tags: Map[String, Any]) {
- import Tags.withPair
+class TagSet private(private val _tags: Map[String, Any]) {
+ import TagSet.withPair
/**
- * Creates a new Tags instance that includes the provided key/value pair. If the provided key was already associated
+ * Creates a new TagSet instance that includes the provided key/value pair. If the provided key was already associated
* with another value then the previous value will be discarded and overwritten with the provided one.
*/
- def withTag(key: String, value: JString): Tags =
+ def withTag(key: String, value: JString): TagSet =
withPair(this, key, value)
/**
- * Creates a new Tags instance that includes the provided key/value pair. If the provided key was already associated
+ * Creates a new TagSet instance that includes the provided key/value pair. If the provided key was already associated
* with another value then the previous value will be discarded and overwritten with the provided one.
*/
- def withTag(key: String, value: JBoolean): Tags =
+ def withTag(key: String, value: JBoolean): TagSet =
withPair(this, key, value)
/**
- * Creates a new Tags instance that includes the provided key/value pair. If the provided key was already associated
+ * Creates a new TagSet instance that includes the provided key/value pair. If the provided key was already associated
* with another value then the previous value will be discarded and overwritten with the provided one.
*/
- def withTag(key: String, value: JLong): Tags =
+ def withTag(key: String, value: JLong): TagSet =
withPair(this, key, value)
/**
- * Creates a new Tags instance that includes all the tags from the provided Tags instance. If any of the tags in this
+ * Creates a new TagSet instance that includes all the tags from the provided Tags instance. If any of the tags in this
* instance are associated to a key present on the provided instance then the previous value will be discarded and
* overwritten with the provided one.
*/
- def withTags(other: Tags): Tags =
- new Tags(_tags ++ other._tags)
+ def withTags(other: TagSet): TagSet =
+ new TagSet(_tags ++ other._tags)
/**
- * Creates a new Tags instance that includes the provided key/value pair. If the provided key was already associated
+ * Creates a new TagSet instance that includes the provided key/value pair. If the provided key was already associated
* with another value then the previous value will be discarded and overwritten with the provided one.
*/
- def and(key: String, value: JString): Tags =
+ def and(key: String, value: JString): TagSet =
withPair(this, key, value)
/**
- * Creates a new Tags instance that includes the provided key/value pair. If the provided key was already associated
+ * Creates a new TagSet instance that includes the provided key/value pair. If the provided key was already associated
* with another value then the previous value will be discarded and overwritten with the provided one.
*/
- def and(key: String, value: JBoolean): Tags =
+ def and(key: String, value: JBoolean): TagSet =
withPair(this, key, value)
/**
- * Creates a new Tags instance that includes the provided key/value pair. If the provided key was already associated
+ * Creates a new TagSet instance that includes the provided key/value pair. If the provided key was already associated
* with another value then the previous value will be discarded and overwritten with the provided one.
*/
- def and(key: String, value: JLong): Tags =
+ def and(key: String, value: JLong): TagSet =
withPair(this, key, value)
/**
- * Creates a new Tags instance that includes all the tags from the provided Tags instance. If any of the tags in this
+ * Creates a new TagSet instance that includes all the tags from the provided Tags instance. If any of the tags in this
* instance are associated to a key present on the provided instance then the previous value will be discarded and
* overwritten with the provided one.
*/
- def and(other: Tags): Tags =
- new Tags(_tags ++ other._tags)
+ def and(other: TagSet): TagSet =
+ new TagSet(_tags ++ other._tags)
+
/**
- * Executes a tag lookup on the instance. The return type of this function will depend on the provided Lookup
- * instance. Take a look at the built-in lookups on the Tags.Lookup companion object for more information.
+ * Returns whether this TagSet instance does not contain any tags.
+ */
+ def isEmpty(): Boolean =
+ _tags.isEmpty
+
+
+ /**
+ * Returns whether this TagSet instance contains any tags.
+ */
+ def nonEmpty(): Boolean =
+ _tags.nonEmpty
+
+
+ /**
+ * Executes a tag lookup. The return type of this function will depend on the provided Lookup. Take a look at the
+ * built-in lookups on the [[Lookups]] companion object for more information.
*/
def get[T](lookup: Lookup[T]): T =
- lookup.run(_tags)
+ lookup.execute(_tags)
+
/**
* Returns a immutable sequence of tags created from the contained tags internal representation. Calling this method
@@ -160,6 +141,7 @@ class Tags private(private val _tags: Map[String, Any]) {
}
}
+
/**
* Returns an iterator of tags. The underlying iterator reuses the Tag instances to avoid unnecessary intermediate
* allocations and thus, it is not safe to share across threads. The most common case for tags iterators is on
@@ -205,7 +187,7 @@ class Tags private(private val _tags: Map[String, Any]) {
override def equals(other: Any): Boolean =
- other != null && other.isInstanceOf[Tags] && other.asInstanceOf[Tags]._tags == this._tags
+ other != null && other.isInstanceOf[TagSet] && other.asInstanceOf[TagSet]._tags == this._tags
override def toString: JString = {
@@ -228,15 +210,15 @@ class Tags private(private val _tags: Map[String, Any]) {
}
private object immutable {
- class String(val key: JString, val value: JString) extends Tag.String
- class Boolean(val key: JString, val value: JBoolean) extends Tag.Boolean
- class Long(val key: JString, val value: JLong) extends Tag.Long
+ case class String(key: JString, value: JString) extends Tag.String
+ case class Boolean(key: JString, value: JBoolean) extends Tag.Boolean
+ case class Long(key: JString, value: JLong) extends Tag.Long
}
private object mutable {
- class String(var key: JString, var value: JString) extends Tag.String with Updateable[JString]
- class Boolean(var key: JString, var value: JBoolean) extends Tag.Boolean with Updateable[JBoolean]
- class Long(var key: JString, var value: JLong) extends Tag.Long with Updateable[JLong]
+ case class String(var key: JString, var value: JString) extends Tag.String with Updateable[JString]
+ case class Boolean(var key: JString, var value: JBoolean) extends Tag.Boolean with Updateable[JBoolean]
+ case class Long(var key: JString, var value: JLong) extends Tag.Long with Updateable[JLong]
trait Updateable[T] {
var key: JString
@@ -251,63 +233,63 @@ class Tags private(private val _tags: Map[String, Any]) {
}
}
-object Tags {
+object TagSet {
/**
* A valid instance of tags that doesn't contain any pairs.
*/
- val Empty = new Tags(Map.empty.withDefaultValue(null))
+ val Empty = new TagSet(Map.empty.withDefaultValue(null))
/**
- * Construct a new Tags instance with a single key/value pair.
+ * Construct a new TagSet instance with a single key/value pair.
*/
- def from(key: String, value: JString): Tags =
+ def from(key: String, value: JString): TagSet =
withPair(Empty, key, value)
/**
- * Construct a new Tags instance with a single key/value pair.
+ * Construct a new TagSet instance with a single key/value pair.
*/
- def from(key: String, value: JBoolean): Tags =
+ def from(key: String, value: JBoolean): TagSet =
withPair(Empty, key, value)
/**
- * Construct a new Tags instance with a single key/value pair.
+ * Construct a new TagSet instance with a single key/value pair.
*/
- def from(key: String, value: JLong): Tags =
+ def from(key: String, value: JLong): TagSet =
withPair(Empty, key, value)
/**
- * Constructs a new Tags instance from a Map. The returned Tags will only contain the entries that have String, Long
- * or Boolean values from the supplied map, any other entry in the map will be ignored.
+ * Constructs a new TagSet instance from a Map. The returned TagSet will only contain the entries that have String,
+ * Long or Boolean values from the supplied map, any other entry in the map will be ignored.
*/
- def from(map: Map[String, Any]): Tags =
- new Tags(map.filter { case (k, v) => isValidPair(k, v) } withDefaultValue(null))
+ def from(map: Map[String, Any]): TagSet =
+ new TagSet(map.filter { case (k, v) => isValidPair(k, v) } withDefaultValue(null))
/**
- * Constructs a new Tags instance from a Map. The returned Tags will only contain the entries that have String, Long
- * or Boolean values from the supplied map, any other entry in the map will be ignored.
+ * Constructs a new TagSet instance from a Map. The returned TagSet will only contain the entries that have String,
+ * Long or Boolean values from the supplied map, any other entry in the map will be ignored.
*/
- def from(map: java.util.Map[String, Any]): Tags = {
+ def from(map: java.util.Map[String, Any]): TagSet = {
val allowedTags = Map.newBuilder[String, Any]
map.entrySet()
.iterator()
.asScala
.foreach(e => if(isValidPair(e.getKey, e.getValue)) allowedTags += (e.getKey -> e.getValue))
- new Tags(allowedTags.result().withDefaultValue(null))
+ new TagSet(allowedTags.result().withDefaultValue(null))
}
- private val _logger = LoggerFactory.getLogger(classOf[Tags])
+ private val _logger = LoggerFactory.getLogger(classOf[TagSet])
- private def withPair(parent: Tags, key: String, value: Any): Tags =
+ private def withPair(parent: TagSet, key: String, value: Any): TagSet =
if(isValidPair(key, value))
- new Tags(parent._tags.updated(key, value))
+ new TagSet(parent._tags.updated(key, value))
else
parent
@@ -333,7 +315,7 @@ object Tags {
/**
- * Describes a strategy to lookup values from a Tags instance. Implementations of this interface will be provided
+ * Describes a strategy to lookup values from a TagSet instance. Implementations of this interface will be provided
* with the actual data structure containing the tags and must perform any necessary runtime type checks to ensure
* that the returned value is in assignable to the expected type T.
*
@@ -341,6 +323,6 @@ object Tags {
* definitions when looking up keys from a Tags instance.
*/
trait Lookup[T] {
- def run(storage: Map[String, Any]): T
+ def execute(storage: Map[String, Any]): T
}
} \ No newline at end of file