Skip to content

Commit

Permalink
Merge pull request #667 from Dwolla/TraceableValue
Browse files Browse the repository at this point in the history
add TraceableValue typeclass and base implementations
  • Loading branch information
mpilquist authored Nov 29, 2022
2 parents 9d74b8f + eb25672 commit 2d5aac3
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions modules/core/shared/src/main/scala/TraceValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,47 @@ object TraceValue {
case class BooleanValue(value: Boolean) extends TraceValue
case class NumberValue(value: Number) extends TraceValue

implicit def stringToTraceValue(value: String): TraceValue = StringValue(value)
implicit def boolToTraceValue(value: Boolean): TraceValue = BooleanValue(value)
implicit def intToTraceValue(value: Int): TraceValue = NumberValue(value)
implicit def longToTraceValue(value: Long): TraceValue = NumberValue(value)
implicit def floatToTraceValue(value: Float): TraceValue = NumberValue(value)
implicit def doubleToTraceValue(value: Double): TraceValue = NumberValue(value)
implicit def viaTraceableValue[A: TraceableValue](a: A): TraceValue =
TraceableValue[A].toTraceValue(a)

@deprecated("use toTraceValue", "0.3.0")
def stringToTraceValue(value: String): TraceValue = StringValue(value)
@deprecated("use toTraceValue", "0.3.0")
def boolToTraceValue(value: Boolean): TraceValue = BooleanValue(value)
@deprecated("use toTraceValue", "0.3.0")
def intToTraceValue(value: Int): TraceValue = NumberValue(value)
@deprecated("use toTraceValue", "0.3.0")
def longToTraceValue(value: Long): TraceValue = NumberValue(value)
@deprecated("use toTraceValue", "0.3.0")
def floatToTraceValue(value: Float): TraceValue = NumberValue(value)
@deprecated("use toTraceValue", "0.3.0")
def doubleToTraceValue(value: Double): TraceValue = NumberValue(value)
}

/** A lawless typeclass responsible for converting a value of the type
* parameter `A` to Natchez's `TraceValue`.
*
* You may want to use this to customize the formatting of a value
* before attaching it to a span, or to support adding tracing as a
* cross-cutting concern using aspect-oriented programming from a
* library such as cats-tagless.
*
* @tparam A The type to be converted to `TraceValue`
*/
trait TraceableValue[A] { outer =>
def toTraceValue(a: A): TraceValue

final def contramap[B](f: B => A): TraceableValue[B] =
(b: B) => outer.toTraceValue(f(b))
}

object TraceableValue {
def apply[A: TraceableValue]: TraceableValue[A] = implicitly

implicit val stringToTraceValue: TraceableValue[String] = TraceValue.StringValue(_)
implicit val booleanToTraceValue: TraceableValue[Boolean] = TraceValue.BooleanValue(_)
implicit val intToTraceValue: TraceableValue[Int] = TraceValue.NumberValue(_)
implicit val longToTraceValue: TraceableValue[Long] = TraceValue.NumberValue(_)
implicit val doubleToTraceValue: TraceableValue[Double] = TraceValue.NumberValue(_)
implicit val floatToTraceValue: TraceableValue[Float] = TraceValue.NumberValue(_)
}

0 comments on commit 2d5aac3

Please sign in to comment.