Skip to content

Commit

Permalink
Move TraceSummary to zipkin-common
Browse files Browse the repository at this point in the history
Author: @franklinhu
Fixes #62
URL: #62
  • Loading branch information
Franklin Hu committed Jul 6, 2012
1 parent 8528eac commit 1807e47
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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])
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(_)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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())()
}

Expand All @@ -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())()
Expand Down

0 comments on commit 1807e47

Please sign in to comment.