diff --git a/zipkin-common/src/main/scala/com/twitter/zipkin/adapter/Adapter.scala b/zipkin-common/src/main/scala/com/twitter/zipkin/adapter/Adapter.scala index b78c910e861..8373daacc5e 100644 --- a/zipkin-common/src/main/scala/com/twitter/zipkin/adapter/Adapter.scala +++ b/zipkin-common/src/main/scala/com/twitter/zipkin/adapter/Adapter.scala @@ -32,6 +32,7 @@ trait Adapter { type binaryAnnotationType /* corresponds to com.twitter.zipkin.common.BinaryAnnotation */ type endpointType /* corresponds to com.twitter.zipkin.common.Endpoint */ type spanType /* corresponds to com.twitter.zipkin.common.Span */ + type traceSummaryType /* corresponds to com.twitter.zipkin.common.TraceSummary */ def apply(a: annotationType): Annotation def apply(a: Annotation): annotationType @@ -47,4 +48,7 @@ trait Adapter { def apply(s: spanType): Span def apply(s: Span): spanType + + def apply(t: traceSummaryType): TraceSummary + def apply(t: TraceSummary): traceSummaryType } diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/common/TraceSummary.scala b/zipkin-common/src/main/scala/com/twitter/zipkin/common/TraceSummary.scala similarity index 62% rename from zipkin-server/src/main/scala/com/twitter/zipkin/common/TraceSummary.scala rename to zipkin-common/src/main/scala/com/twitter/zipkin/common/TraceSummary.scala index 099906b2de8..2a1fb2e916c 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/common/TraceSummary.scala +++ b/zipkin-common/src/main/scala/com/twitter/zipkin/common/TraceSummary.scala @@ -1,6 +1,6 @@ /* * Copyright 2012 Twitter Inc. - * + * * 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 @@ -16,24 +16,7 @@ */ package com.twitter.zipkin.common -import com.twitter.zipkin.gen import scala.collection.Map -import com.twitter.zipkin.adapter.ThriftAdapter - -/** - * Represents the summary of a trace. - * - * Instead of containing the full spans it has summed them up into - * duration, endpoints involved and services involved. - */ -object TraceSummary { - def fromThrift(summary: gen.TraceSummary): TraceSummary = { - new TraceSummary(summary.traceId, summary.startTimestamp, summary.endTimestamp, - summary.durationMicro, summary.serviceCounts, - summary.endpoints.map(ThriftAdapter(_)).toList) - } - -} /** * @param traceId id of this trace @@ -45,10 +28,4 @@ object TraceSummary { * @param endpoints endpoints involved in the traced operation */ case class TraceSummary(traceId: Long, startTimestamp: Long, endTimestamp: Long, - durationMicro: Int, serviceCounts: Map[String, Int], endpoints: List[Endpoint]) { - - def toThrift: gen.TraceSummary = { - gen.TraceSummary(traceId, startTimestamp, endTimestamp, - durationMicro, serviceCounts, endpoints.map(ThriftAdapter(_))) - } -} + durationMicro: Int, serviceCounts: Map[String, Int], endpoints: List[Endpoint]) diff --git a/zipkin-scrooge/src/main/scala/com/twitter/zipkin/adapter/ThriftAdapter.scala b/zipkin-scrooge/src/main/scala/com/twitter/zipkin/adapter/ThriftAdapter.scala index 9a5d4513ded..948e57438b4 100644 --- a/zipkin-scrooge/src/main/scala/com/twitter/zipkin/adapter/ThriftAdapter.scala +++ b/zipkin-scrooge/src/main/scala/com/twitter/zipkin/adapter/ThriftAdapter.scala @@ -25,6 +25,7 @@ object ThriftAdapter extends Adapter { type binaryAnnotationType = gen.BinaryAnnotation type endpointType = gen.Endpoint type spanType = gen.Span + type traceSummaryType = gen.TraceSummary /* Annotation from Thrift */ def apply(a: annotationType): Annotation = { @@ -113,4 +114,17 @@ object ThriftAdapter extends Adapter { gen.Span(s.traceId, s.name, s.id, s.parentId, s.annotations.map { this(_) }, s.binaryAnnotations.map { this(_) }) } + + /* TraceSummary from Thrift */ + def apply(t: traceSummaryType): TraceSummary = { + new TraceSummary(t.traceId, t.startTimestamp, t.endTimestamp, + t.durationMicro, t.serviceCounts, + t.endpoints.map(ThriftAdapter(_)).toList) + } + + /* TraceSummary to Thrift */ + def apply(t: TraceSummary): traceSummaryType = { + gen.TraceSummary(t.traceId, t.startTimestamp, t.endTimestamp, + t.durationMicro, t.serviceCounts, t.endpoints.map(ThriftAdapter(_))) + } } diff --git a/zipkin-scrooge/src/test/scala/com/twitter/zipkin/adapter/ThriftAdapterSpec.scala b/zipkin-scrooge/src/test/scala/com/twitter/zipkin/adapter/ThriftAdapterSpec.scala index 109fd256d28..22e9535d295 100644 --- a/zipkin-scrooge/src/test/scala/com/twitter/zipkin/adapter/ThriftAdapterSpec.scala +++ b/zipkin-scrooge/src/test/scala/com/twitter/zipkin/adapter/ThriftAdapterSpec.scala @@ -97,6 +97,16 @@ class ThriftAdapterSpec extends Specification with JMocker with ClassMocker { ThriftAdapter(noBinaryAnnotationsSpan) mustEqual Span(0, "name", 0, None, List(), Seq()) } } + + "convert TraceSummary" in { + "to thrift and back" in { + val expectedTraceSummary = TraceSummary(123, 10000, 10300, 300, Map("service1" -> 1), + List(Endpoint(123, 123, "service1"))) + val thriftTraceSummary = ThriftAdapter(expectedTraceSummary) + val actualTraceSummary = ThriftAdapter(thriftTraceSummary) + expectedTraceSummary mustEqual actualTraceSummary + } + } } } diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/common/Trace.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/common/Trace.scala index 40876be2928..504b15558c0 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/common/Trace.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/common/Trace.scala @@ -156,7 +156,7 @@ case class Trace(spans: Seq[Span]) { } def toTraceCombo: gen.TraceCombo = { - gen.TraceCombo(toThrift, toTraceSummary.map(_.toThrift), toTimeline, toSpanDepths) + gen.TraceCombo(toThrift, toTraceSummary.map(ThriftAdapter(_)), toTimeline, toSpanDepths) } /** diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/query/QueryService.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/query/QueryService.scala index 8ef2bca8211..0f4ffd161f6 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/query/QueryService.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/query/QueryService.scala @@ -28,6 +28,7 @@ import com.twitter.zipkin.storage.{Aggregates, TraceIdDuration, Index, Storage} import java.nio.ByteBuffer import org.apache.thrift.TException import scala.collection.Set +import com.twitter.zipkin.adapter.ThriftAdapter /** * Able to respond to users queries regarding the traces. Usually does so @@ -223,7 +224,7 @@ class QueryService(storage: Storage, index: Index, aggregates: Aggregates, adjus Stats.timeFutureMillis("query.getTraceSummariesByIds") { storage.getTracesByIds(traceIds.toList).map { id => - id.flatMap(adjusters.foldLeft(_)((trace, adjuster) => adjuster.adjust(trace)).toTraceSummary.map(_.toThrift)) + id.flatMap(adjusters.foldLeft(_)((trace, adjuster) => adjuster.adjust(trace)).toTraceSummary.map(ThriftAdapter(_))) } rescue { case e: Exception => log.error(e, "getTraceSummariesByIds query failed") diff --git a/zipkin-server/src/test/scala/com/twitter/zipkin/common/TraceSummarySpec.scala b/zipkin-server/src/test/scala/com/twitter/zipkin/common/TraceSummarySpec.scala deleted file mode 100644 index 08f0b22c750..00000000000 --- a/zipkin-server/src/test/scala/com/twitter/zipkin/common/TraceSummarySpec.scala +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2012 Twitter Inc. - * - * 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 com.twitter.zipkin.common - -import org.specs.Specification - -class TraceSummarySpec extends Specification { - "TraceSummary" should { - "convert to thrift and back" in { - val expectedTraceSummary = TraceSummary(123, 10000, 10300, 300, Map("service1" -> 1), - List(Endpoint(123, 123, "service1"))) - val thriftTraceSummary = expectedTraceSummary.toThrift - val actualTraceSummary = TraceSummary.fromThrift(thriftTraceSummary) - expectedTraceSummary mustEqual actualTraceSummary - } - } -} diff --git a/zipkin-server/src/test/scala/com/twitter/zipkin/query/QueryServiceSpec.scala b/zipkin-server/src/test/scala/com/twitter/zipkin/query/QueryServiceSpec.scala index a5204a071db..f06850c9dcc 100644 --- a/zipkin-server/src/test/scala/com/twitter/zipkin/query/QueryServiceSpec.scala +++ b/zipkin-server/src/test/scala/com/twitter/zipkin/query/QueryServiceSpec.scala @@ -164,7 +164,7 @@ class QueryServiceSpec extends Specification with JMocker with ClassMocker { one(storage).getTracesByIds(List(traceId)) willReturn Future(List(trace1)) } - val ts = List(TraceSummary(1, 100, 150, 50, Map("service1" -> 1), List(ep1)).toThrift) + val ts = List(ThriftAdapter(TraceSummary(1, 100, 150, 50, Map("service1" -> 1), List(ep1)))) ts mustEqual qs.getTraceSummariesByIds(List(traceId), List())() } @@ -180,7 +180,7 @@ class QueryServiceSpec extends Specification with JMocker with ClassMocker { one(storage).getTracesByIds(List(traceId)) willReturn Future(List(trace1)) } val trace = trace1.toThrift - val summary = TraceSummary(1, 100, 150, 50, Map("service1" -> 1), List(ep1)).toThrift + val summary = ThriftAdapter(TraceSummary(1, 100, 150, 50, Map("service1" -> 1), List(ep1))) val timeline = trace1.toTimeline val combo = gen.TraceCombo(trace, Some(summary), timeline, Some(Map(666L -> 1))) Seq(combo) mustEqual qs.getTraceCombosByIds(List(traceId), List())()