aboutsummaryrefslogtreecommitdiff
path: root/kamon-newrelic/src/main/scala/kamon/newrelic/NewRelicJsonPrinter.scala
blob: 713a55864f88d927b1362f121e45932329d81a9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package kamon.newrelic

import java.lang.{ StringBuilder  JStringBuilder }
import spray.json._
import scala.annotation.tailrec

// We need a special treatment of / that needs to be escaped as \/ for New Relic to work properly with all metrics.
// Without this custom json printer the scoped metrics are not displayed in the site.

// format: OFF
trait NewRelicJsonPrinter extends CompactPrinter {

  override def printString(s: String, sb: JStringBuilder) {
    import NewRelicJsonPrinter._
    @tailrec def firstToBeEncoded(ix: Int = 0): Int =
      if (ix == s.length) -1 else if (requiresEncoding(s.charAt(ix))) ix else firstToBeEncoded(ix + 1)

    sb.append('"')
    firstToBeEncoded() match {
      case -1  sb.append(s)
      case first 
        sb.append(s, 0, first)
        @tailrec def append(ix: Int): Unit =
          if (ix < s.length) {
            s.charAt(ix) match {
              case c if !requiresEncoding(c) => sb.append(c)
              case '"' => sb.append("\\\"")
              case '\\' => sb.append("\\\\")
              case '/'  => sb.append("\\/")
              case '\b' => sb.append("\\b")
              case '\f' => sb.append("\\f")
              case '\n' => sb.append("\\n")
              case '\r' => sb.append("\\r")
              case '\t' => sb.append("\\t")
              case x if x <= 0xF => sb.append("\\u000").append(Integer.toHexString(x))
              case x if x <= 0xFF => sb.append("\\u00").append(Integer.toHexString(x))
              case x if x <= 0xFFF => sb.append("\\u0").append(Integer.toHexString(x))
              case x => sb.append("\\u").append(Integer.toHexString(x))
            }
            append(ix + 1)
          }
        append(first)
    }
    sb.append('"')
  }
}

object NewRelicJsonPrinter extends NewRelicJsonPrinter {

  def requiresEncoding(c: Char): Boolean =
  // from RFC 4627
  // unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
    c match {
      case '"'   true
      case '\\'  true
      case '/'   true
      case c     c < 0x20
    }
}