aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/tag
diff options
context:
space:
mode:
Diffstat (limited to 'kamon-core/src/main/scala/kamon/tag')
-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
3 files changed, 142 insertions, 100 deletions
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