aboutsummaryrefslogtreecommitdiff
path: root/kamon-newrelic/src/main/scala/kamon/newrelic/JsonProtocol.scala
blob: 26e8839ee1b29fc755a69b62cedf0c717ecbbde0 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* ===================================================
 * Copyright © 2013 the kamon project <http://kamon.io/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */
package kamon.newrelic

import spray.json._
import kamon.newrelic.Agent._

object JsonProtocol extends DefaultJsonProtocol {

  implicit object ConnectJsonWriter extends RootJsonWriter[Settings] {
    def write(obj: Settings): JsValue =
      JsArray(
        JsObject(
          "agent_version" -> JsString("3.1.0"),
          "app_name" -> JsArray(JsString(obj.appName)),
          "host" -> JsString(obj.host),
          "identifier" -> JsString(s"java:${obj.appName}"),
          "language" -> JsString("java"),
          "pid" -> JsNumber(obj.pid)))
  }

  implicit def seqWriter[T: JsonFormat] = new JsonFormat[Seq[T]] {
    def read(value: JsValue): Seq[T] = value match {
      case JsArray(elements)  elements.map(_.convertTo[T])(collection.breakOut)
      case x                  deserializationError("Expected Seq as JsArray, but got " + x)
    }

    def write(seq: Seq[T]) = JsArray(seq.map(_.toJson).toVector)
  }

  implicit object MetricDetailWriter extends JsonFormat[Metric] {
    def read(json: JsValue): (MetricID, MetricData) = json match {
      case JsArray(elements) 
        val metricID = elements(0) match {
          case JsObject(fields)  MetricID(fields("name").convertTo[String], fields.get("scope").map(_.convertTo[String]))
          case x                 deserializationError("Expected MetricID as JsObject, but got " + x)
        }

        val metricData = elements(1) match {
          case JsArray(dataElements) 
            MetricData(
              dataElements(0).convertTo[Long],
              dataElements(1).convertTo[Double],
              dataElements(2).convertTo[Double],
              dataElements(3).convertTo[Double],
              dataElements(4).convertTo[Double],
              dataElements(5).convertTo[Double])
          case x  deserializationError("Expected MetricData as JsArray, but got " + x)
        }

        (metricID, metricData)

      case x  deserializationError("Expected Metric as JsArray, but got " + x)
    }

    def write(obj: Metric): JsValue = {
      val (metricID, metricData) = obj
      val nameAndScope = metricID.scope.foldLeft(Map("name" -> JsString(metricID.name)))((m, scope)  m + ("scope" -> JsString(scope)))

      JsArray(
        JsObject(nameAndScope),
        JsArray(
          JsNumber(metricData.callCount),
          JsNumber(metricData.total),
          JsNumber(metricData.totalExclusive),
          JsNumber(metricData.min),
          JsNumber(metricData.max),
          JsNumber(metricData.sumOfSquares)))
    }
  }

  implicit object MetricBatchWriter extends RootJsonFormat[MetricBatch] {

    def read(json: JsValue): MetricBatch = json match {
      case JsArray(elements) 
        val runID = elements(0).convertTo[Long]
        val timeSliceFrom = elements(1).convertTo[Long]
        val timeSliceTo = elements(2).convertTo[Long]
        val metrics = elements(3).convertTo[Seq[Metric]]

        MetricBatch(runID, TimeSliceMetrics(timeSliceFrom, timeSliceTo, metrics.toMap))

      case x  deserializationError("Expected Array as JsArray, but got " + x)
    }

    def write(obj: MetricBatch): JsValue =
      JsArray(
        JsNumber(obj.runID),
        JsNumber(obj.timeSliceMetrics.from),
        JsNumber(obj.timeSliceMetrics.to),
        obj.timeSliceMetrics.metrics.toSeq.toJson)
  }
}