From 2775ea09e92a84d99ec84662f76f31f75d037fd1 Mon Sep 17 00:00:00 2001 From: Johan Oskarsson Date: Tue, 17 Jul 2012 14:49:45 -0700 Subject: [PATCH 1/3] Allow the Spans with a debug flag set to pass through the sampler --- .../com/twitter/zipkin/common/Span.scala | 6 +-- .../ScribeCollectorServiceSpec.scala | 2 +- .../zipkin/adapter/ThriftAdapter.scala | 4 +- .../processor/SamplerProcessorFilter.scala | 2 +- .../sampler/EverythingGlobalSampler.scala | 4 +- .../collector/sampler/GlobalSampler.scala | 3 +- .../sampler/ZooKeeperGlobalSampler.scala | 15 +++--- .../sampler/ZooKeeperGlobalSamplerSpec.scala | 47 ++++++++++++++----- .../src/test/resources/TestCollector.scala | 6 +-- .../src/main/thrift/zipkinCore.thrift | 2 + 10 files changed, 60 insertions(+), 31 deletions(-) diff --git a/zipkin-common/src/main/scala/com/twitter/zipkin/common/Span.scala b/zipkin-common/src/main/scala/com/twitter/zipkin/common/Span.scala index 15a4d6e8bf4..9025824d6e5 100644 --- a/zipkin-common/src/main/scala/com/twitter/zipkin/common/Span.scala +++ b/zipkin-common/src/main/scala/com/twitter/zipkin/common/Span.scala @@ -30,7 +30,7 @@ import com.twitter.zipkin.Constants object Span { def apply(span: Span): Span = Span(span.traceId, span.name, span.id, - span.parentId, span.annotations, span.binaryAnnotations) + span.parentId, span.annotations, span.binaryAnnotations, span.debug) } /** @@ -44,7 +44,7 @@ object Span { * serialized objects */ case class Span(traceId: Long, name: String, id: Long, parentId: Option[Long], - annotations: List[Annotation], binaryAnnotations: Seq[BinaryAnnotation]) { + annotations: List[Annotation], binaryAnnotations: Seq[BinaryAnnotation], debug: Boolean = false) { /** * Order annotations by timestamp. */ @@ -96,7 +96,7 @@ case class Span(traceId: Long, name: String, id: Long, parentId: Option[Long], new Span(traceId, selectedName, id, parentId, annotations ++ mergeFrom.annotations, - binaryAnnotations ++ mergeFrom.binaryAnnotations) + binaryAnnotations ++ mergeFrom.binaryAnnotations, debug | mergeFrom.debug) } /** diff --git a/zipkin-scribe/src/test/scala/com/twitter/zipkin/collector/ScribeCollectorServiceSpec.scala b/zipkin-scribe/src/test/scala/com/twitter/zipkin/collector/ScribeCollectorServiceSpec.scala index ef54517cf42..682d803be82 100644 --- a/zipkin-scribe/src/test/scala/com/twitter/zipkin/collector/ScribeCollectorServiceSpec.scala +++ b/zipkin-scribe/src/test/scala/com/twitter/zipkin/collector/ScribeCollectorServiceSpec.scala @@ -38,7 +38,7 @@ class ScribeCollectorServiceSpec extends Specification with JMocker with ClassMo val wrongCatList = List(gen.LogEntry("wrongcat", serializer.toString(ThriftAdapter(validSpan)))) - val base64 = "CgABAAAAAAAAAHsLAAMAAAADYm9vCgAEAAAAAAAAAcgPAAYMAAAAAQoAAQAAAAAAAAABCwACAAAAA2JhaAAPAAgMAAAAAAA=" + val base64 = "CgABAAAAAAAAAHsLAAMAAAADYm9vCgAEAAAAAAAAAcgPAAYMAAAAAQoAAQAAAAAAAAABCwACAAAAA2JhaAAPAAgMAAAAAAIACQAA" val queue = mock[WriteQueue[Seq[String]]] val zkSampleRateConfig = mock[AdjustableRateConfig] 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 948e57438b4..560bec36315 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 @@ -106,13 +106,13 @@ object ThriftAdapter extends Adapter { case b => b.map { this(_) } } - new Span(s.traceId, s.name, s.id, s.parentId, annotations, binaryAnnotations) + new Span(s.traceId, s.name, s.id, s.parentId, annotations, binaryAnnotations, s.debug) } /* Span to Thrift */ def apply(s: Span): spanType = { gen.Span(s.traceId, s.name, s.id, s.parentId, s.annotations.map { this(_) }, - s.binaryAnnotations.map { this(_) }) + s.binaryAnnotations.map { this(_) }, s.debug) } /* TraceSummary from Thrift */ diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala index 0d763685e45..102926f60b2 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala @@ -29,7 +29,7 @@ class SamplerProcessorFilter(sampler: GlobalSampler) extends ProcessorFilter[Seq spans.flatMap { span => span.serviceNames.foreach { name => Stats.incr("received_" + name) } - if (sampler(span.traceId)) { + if (sampler(span)) { Some(span) } else { None diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/EverythingGlobalSampler.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/EverythingGlobalSampler.scala index aed8b0a60a1..13e302443cd 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/EverythingGlobalSampler.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/EverythingGlobalSampler.scala @@ -16,9 +16,11 @@ */ package com.twitter.zipkin.collector.sampler +import com.twitter.zipkin.common.Span + /** * Let through everything. */ object EverythingGlobalSampler extends GlobalSampler { - override def apply(traceId: Long): Boolean = true + override def apply(span: Span): Boolean = true } diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/GlobalSampler.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/GlobalSampler.scala index 501ab2982de..dcd1be1a15d 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/GlobalSampler.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/GlobalSampler.scala @@ -17,6 +17,7 @@ package com.twitter.zipkin.collector.sampler import com.twitter.ostrich.stats.Stats +import com.twitter.zipkin.common.Span /** * Even after the traces have been created and transported to the @@ -36,6 +37,6 @@ trait GlobalSampler { * True: drop trace on the floor * False: process trace */ - def apply(traceId: Long) : Boolean = false + def apply(span: Span): Boolean = false } diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSampler.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSampler.scala index afd3e49cad9..cbe8bebe2c8 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSampler.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSampler.scala @@ -17,6 +17,7 @@ package com.twitter.zipkin.collector.sampler import com.twitter.zipkin.config.sampler.AdjustableRateConfig +import com.twitter.zipkin.common.Span /** * Get the rate of sample from ZooKeeper so that @@ -28,8 +29,8 @@ class ZooKeeperGlobalSampler(sampleRateConfig: AdjustableRateConfig) extends Glo * True: process trace * False: drop trace on the floor */ - override def apply(traceId: Long) : Boolean = { - if (sample(traceId, sampleRateConfig.get)) { + override def apply(span: Span): Boolean = { + if (sample(span, sampleRateConfig.get)) { SAMPLER_PASSED.incr true } else { @@ -47,14 +48,16 @@ class ZooKeeperGlobalSampler(sampleRateConfig: AdjustableRateConfig) extends Glo * * In addition, math.abs(Long.MinValue) = Long.MinValue due to overflow, * so we treat Long.MinValue as Long.MaxValue + * + * If the span is marked as debug we let it through, no questions asked. */ - def sample(traceId: Long, sampleRate: Double) : Boolean = { - if (sampleRate == 1) { + def sample(span: Span, sampleRate: Double): Boolean = { + if (span.debug || sampleRate == 1) { true } else { val t = - if (traceId == Long.MinValue) Long.MaxValue - else math.abs(traceId) + if (span.traceId == Long.MinValue) Long.MaxValue + else math.abs(span.traceId) t < Long.MaxValue * sampleRate } } diff --git a/zipkin-server/src/test/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSamplerSpec.scala b/zipkin-server/src/test/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSamplerSpec.scala index 28bac05a1a3..d795acac366 100644 --- a/zipkin-server/src/test/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSamplerSpec.scala +++ b/zipkin-server/src/test/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSamplerSpec.scala @@ -18,10 +18,18 @@ package com.twitter.zipkin.collector.sampler */ import com.twitter.zipkin.config.sampler.AdjustableRateConfig +import com.twitter.zipkin.common.Span import org.specs.mock.{ClassMocker, JMocker} import org.specs.Specification class ZooKeeperGlobalSamplerSpec extends Specification with JMocker with ClassMocker { + + def span(traceId: Long): Span = span(traceId, false) + + def span(traceId: Long, debug: Boolean): Span = { + Span(traceId, "bah", 0L, None, List(), Seq(), debug) + } + "Sample" should { "keep 10% of traces" in { @@ -32,11 +40,26 @@ class ZooKeeperGlobalSamplerSpec extends Specification with JMocker with ClassMo } val sampler = new ZooKeeperGlobalSampler(zkConfig) - sampler(Long.MinValue) mustEqual false - sampler(-1) mustEqual true - sampler(0) mustEqual true - sampler(1) mustEqual true - sampler(Long.MaxValue) mustEqual false + sampler(span(Long.MinValue)) mustEqual false + sampler(span(-1)) mustEqual true + sampler(span(0)) mustEqual true + sampler(span(1)) mustEqual true + sampler(span(Long.MaxValue)) mustEqual false + } + + "let pass if debug flag is set" in { + val sampleRate = 0 + val zkConfig = mock[AdjustableRateConfig] + expect { + allowing(zkConfig).get willReturn sampleRate + } + val sampler = new ZooKeeperGlobalSampler(zkConfig) + + sampler(span(Long.MinValue, true)) mustEqual true + sampler(span(-1, true)) mustEqual true + sampler(span(0, true)) mustEqual true + sampler(span(1, true)) mustEqual true + sampler(span(Long.MaxValue, true)) mustEqual true } "drop all traces" in { @@ -46,12 +69,12 @@ class ZooKeeperGlobalSamplerSpec extends Specification with JMocker with ClassMo } val sampler = new ZooKeeperGlobalSampler(zkConfig) - sampler(Long.MinValue) mustEqual false - sampler(Long.MinValue + 1) + sampler(span(Long.MinValue)) mustEqual false + sampler(span(Long.MinValue + 1)) -5000 to 5000 foreach { i => - sampler(i) mustEqual false + sampler(span(i)) mustEqual false } - sampler(Long.MaxValue) mustEqual false + sampler(span(Long.MaxValue)) mustEqual false } "keep all traces" in { @@ -61,11 +84,11 @@ class ZooKeeperGlobalSamplerSpec extends Specification with JMocker with ClassMo } val sampler = new ZooKeeperGlobalSampler(zkConfig) - sampler(Long.MinValue) mustEqual true + sampler(span(Long.MinValue)) mustEqual true -5000 to 5000 foreach { i => - sampler(i) mustEqual true + sampler(span(i)) mustEqual true } - sampler(Long.MinValue) mustEqual true + sampler(span(Long.MinValue)) mustEqual true } } diff --git a/zipkin-test/src/test/resources/TestCollector.scala b/zipkin-test/src/test/resources/TestCollector.scala index 29c8759a58f..ddc9fb8f57d 100644 --- a/zipkin-test/src/test/resources/TestCollector.scala +++ b/zipkin-test/src/test/resources/TestCollector.scala @@ -16,7 +16,7 @@ import com.twitter.zipkin.config._ import com.twitter.zipkin.config.sampler.NullAdaptiveSamplerConfig import com.twitter.zipkin.config.zookeeper.ZooKeeperConfig -import com.twitter.zipkin.collector.sampler.GlobalSampler +import com.twitter.zipkin.collector.sampler.{GlobalSampler, EverythingGlobalSampler} import com.twitter.conversions.time._ import com.twitter.logging.LoggerFactory import com.twitter.logging.config._ @@ -64,9 +64,7 @@ new ScribeZipkinCollectorConfig { override def adaptiveSamplerConfig = new NullAdaptiveSamplerConfig {} // sample it all - override def globalSampler: GlobalSampler = new GlobalSampler() { - override def apply(traceId: Long) : Boolean = true - } + override def globalSampler: GlobalSampler = EverythingGlobalSampler def zkConfig = new ZooKeeperConfig { servers = List("localhost:2181") diff --git a/zipkin-thrift/src/main/thrift/zipkinCore.thrift b/zipkin-thrift/src/main/thrift/zipkinCore.thrift index 0787ca8e704..3cdf99d1f82 100755 --- a/zipkin-thrift/src/main/thrift/zipkinCore.thrift +++ b/zipkin-thrift/src/main/thrift/zipkinCore.thrift @@ -52,4 +52,6 @@ struct Span { 5: optional i64 parent_id, // parent span id 6: list annotations, // list of all annotations/events that occured 8: list binary_annotations // any binary annotations + 9: optional bool debug = 0 // if true, we DEMAND that this span passes all samplers } + From 6e19008542b043c1dc9ac47aeaa8c97a2b3896b2 Mon Sep 17 00:00:00 2001 From: Johan Oskarsson Date: Mon, 30 Jul 2012 13:02:59 -0700 Subject: [PATCH 2/3] If the debug flag is set on a span we ensure it is stored. This allows developers to force tracing on a request --- .../com/twitter/zipkin/common/Span.scala | 3 +- .../com/twitter/zipkin/common/SpanSpec.scala | 6 +-- .../processor/SamplerProcessorFilter.scala | 6 ++- .../sampler/EverythingGlobalSampler.scala | 26 ---------- .../collector/sampler/GlobalSampler.scala | 17 ++++++- .../sampler/ZooKeeperGlobalSampler.scala | 15 +++--- .../SamplerProcessorFilterSpec.scala | 50 +++++++++++++++++++ .../sampler/ZooKeeperGlobalSamplerSpec.scala | 47 +++++------------ 8 files changed, 93 insertions(+), 77 deletions(-) delete mode 100644 zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/EverythingGlobalSampler.scala create mode 100644 zipkin-server/src/test/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilterSpec.scala diff --git a/zipkin-common/src/main/scala/com/twitter/zipkin/common/Span.scala b/zipkin-common/src/main/scala/com/twitter/zipkin/common/Span.scala index 9025824d6e5..414ec3d201a 100644 --- a/zipkin-common/src/main/scala/com/twitter/zipkin/common/Span.scala +++ b/zipkin-common/src/main/scala/com/twitter/zipkin/common/Span.scala @@ -21,7 +21,7 @@ import com.twitter.zipkin.Constants /** * A span represents one RPC request. A trace is made up of many spans. * - * A span can contain multiple annotations, some are always incuded such as + * A span can contain multiple annotations, some are always included such as * Client send -> Server received -> Server send -> Client receive. * * Some are created by users, describing application specific information, @@ -42,6 +42,7 @@ object Span { * some fixed ones from the tracing framework * @param binaryAnnotations binary annotations, can contain more detailed information such as * serialized objects + * @param debug if this is set we will make sure this span is stored, no matter what the samplers want */ case class Span(traceId: Long, name: String, id: Long, parentId: Option[Long], annotations: List[Annotation], binaryAnnotations: Seq[BinaryAnnotation], debug: Boolean = false) { diff --git a/zipkin-common/src/test/scala/com/twitter/zipkin/common/SpanSpec.scala b/zipkin-common/src/test/scala/com/twitter/zipkin/common/SpanSpec.scala index 38209ccaa26..1b68f68754a 100644 --- a/zipkin-common/src/test/scala/com/twitter/zipkin/common/SpanSpec.scala +++ b/zipkin-common/src/test/scala/com/twitter/zipkin/common/SpanSpec.scala @@ -52,9 +52,9 @@ class SpanSpec extends Specification { val ann1 = Annotation(1, "value1", Some(Endpoint(1, 2, "service"))) val ann2 = Annotation(2, "value2", Some(Endpoint(3, 4, "service"))) - val span1 = Span(12345, "", 666, None, List(ann1), Nil) - val span2 = Span(12345, "methodcall", 666, None, List(ann2), Nil) - val expectedSpan = Span(12345, "methodcall", 666, None, List(ann1, ann2), Nil) + val span1 = Span(12345, "", 666, None, List(ann1), Nil, true) + val span2 = Span(12345, "methodcall", 666, None, List(ann2), Nil, false) + val expectedSpan = Span(12345, "methodcall", 666, None, List(ann1, ann2), Nil, true) val actualSpan = span1.mergeSpan(span2) actualSpan mustEqual expectedSpan } diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala index 102926f60b2..ba972bbf35d 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala @@ -29,7 +29,11 @@ class SamplerProcessorFilter(sampler: GlobalSampler) extends ProcessorFilter[Seq spans.flatMap { span => span.serviceNames.foreach { name => Stats.incr("received_" + name) } - if (sampler(span)) { + /** + * If the span was created with debug mode on we guarantee that it will be + * stored no matter what our sampler tells us + */ + if (span.debug || sampler(span.traceId)) { Some(span) } else { None diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/EverythingGlobalSampler.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/EverythingGlobalSampler.scala deleted file mode 100644 index 13e302443cd..00000000000 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/EverythingGlobalSampler.scala +++ /dev/null @@ -1,26 +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.collector.sampler - -import com.twitter.zipkin.common.Span - -/** - * Let through everything. - */ -object EverythingGlobalSampler extends GlobalSampler { - override def apply(span: Span): Boolean = true -} diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/GlobalSampler.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/GlobalSampler.scala index dcd1be1a15d..2af0ed9b094 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/GlobalSampler.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/GlobalSampler.scala @@ -17,7 +17,6 @@ package com.twitter.zipkin.collector.sampler import com.twitter.ostrich.stats.Stats -import com.twitter.zipkin.common.Span /** * Even after the traces have been created and transported to the @@ -37,6 +36,20 @@ trait GlobalSampler { * True: drop trace on the floor * False: process trace */ - def apply(span: Span): Boolean = false + def apply(traceId: Long): Boolean = false } + +/** + * None shall pass! Drop all the trace data. + */ +object NullGlobalSampler extends GlobalSampler { + override def apply(traceId: Long) = false +} + +/** + * Let everything through. + */ +object EverythingGlobalSampler extends GlobalSampler { + override def apply(traceId: Long) = true +} diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSampler.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSampler.scala index cbe8bebe2c8..afd3e49cad9 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSampler.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSampler.scala @@ -17,7 +17,6 @@ package com.twitter.zipkin.collector.sampler import com.twitter.zipkin.config.sampler.AdjustableRateConfig -import com.twitter.zipkin.common.Span /** * Get the rate of sample from ZooKeeper so that @@ -29,8 +28,8 @@ class ZooKeeperGlobalSampler(sampleRateConfig: AdjustableRateConfig) extends Glo * True: process trace * False: drop trace on the floor */ - override def apply(span: Span): Boolean = { - if (sample(span, sampleRateConfig.get)) { + override def apply(traceId: Long) : Boolean = { + if (sample(traceId, sampleRateConfig.get)) { SAMPLER_PASSED.incr true } else { @@ -48,16 +47,14 @@ class ZooKeeperGlobalSampler(sampleRateConfig: AdjustableRateConfig) extends Glo * * In addition, math.abs(Long.MinValue) = Long.MinValue due to overflow, * so we treat Long.MinValue as Long.MaxValue - * - * If the span is marked as debug we let it through, no questions asked. */ - def sample(span: Span, sampleRate: Double): Boolean = { - if (span.debug || sampleRate == 1) { + def sample(traceId: Long, sampleRate: Double) : Boolean = { + if (sampleRate == 1) { true } else { val t = - if (span.traceId == Long.MinValue) Long.MaxValue - else math.abs(span.traceId) + if (traceId == Long.MinValue) Long.MaxValue + else math.abs(traceId) t < Long.MaxValue * sampleRate } } diff --git a/zipkin-server/src/test/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilterSpec.scala b/zipkin-server/src/test/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilterSpec.scala new file mode 100644 index 00000000000..92b4fa383ca --- /dev/null +++ b/zipkin-server/src/test/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilterSpec.scala @@ -0,0 +1,50 @@ +package com.twitter.zipkin.collector.processor + +/* + * 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. + * + */ + +import org.specs.Specification +import com.twitter.zipkin.gen +import com.twitter.zipkin.common.{Span, Endpoint, Annotation} +import com.twitter.ostrich.stats.{Histogram, Distribution, Stats} +import com.twitter.zipkin.collector.sampler.{EverythingGlobalSampler, NullGlobalSampler} + +class SamplerProcessorFilterSpec extends Specification { + + "SamplerProcessorFilter" should { + "let the span pass if debug flag is set" in { + val span = Span(12345, "methodcall", 666, None, List(), Nil, true) + val spans = Seq(span) + val samplerProcessor = new SamplerProcessorFilter(NullGlobalSampler) + samplerProcessor(spans) mustEqual spans + } + + "let the span pass if debug flag false and sampler says yes" in { + val span = Span(12345, "methodcall", 666, None, List(), Nil, false) + val spans = Seq(span) + val samplerProcessor = new SamplerProcessorFilter(EverythingGlobalSampler) + samplerProcessor(spans) mustEqual spans + } + + "don't let the span pass if debug flag false and sampler says no" in { + val span = Span(12345, "methodcall", 666, None, List(), Nil, false) + val spans = Seq(span) + val samplerProcessor = new SamplerProcessorFilter(NullGlobalSampler) + samplerProcessor(spans) mustEqual Seq() + } + } +} diff --git a/zipkin-server/src/test/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSamplerSpec.scala b/zipkin-server/src/test/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSamplerSpec.scala index d795acac366..28bac05a1a3 100644 --- a/zipkin-server/src/test/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSamplerSpec.scala +++ b/zipkin-server/src/test/scala/com/twitter/zipkin/collector/sampler/ZooKeeperGlobalSamplerSpec.scala @@ -18,18 +18,10 @@ package com.twitter.zipkin.collector.sampler */ import com.twitter.zipkin.config.sampler.AdjustableRateConfig -import com.twitter.zipkin.common.Span import org.specs.mock.{ClassMocker, JMocker} import org.specs.Specification class ZooKeeperGlobalSamplerSpec extends Specification with JMocker with ClassMocker { - - def span(traceId: Long): Span = span(traceId, false) - - def span(traceId: Long, debug: Boolean): Span = { - Span(traceId, "bah", 0L, None, List(), Seq(), debug) - } - "Sample" should { "keep 10% of traces" in { @@ -40,26 +32,11 @@ class ZooKeeperGlobalSamplerSpec extends Specification with JMocker with ClassMo } val sampler = new ZooKeeperGlobalSampler(zkConfig) - sampler(span(Long.MinValue)) mustEqual false - sampler(span(-1)) mustEqual true - sampler(span(0)) mustEqual true - sampler(span(1)) mustEqual true - sampler(span(Long.MaxValue)) mustEqual false - } - - "let pass if debug flag is set" in { - val sampleRate = 0 - val zkConfig = mock[AdjustableRateConfig] - expect { - allowing(zkConfig).get willReturn sampleRate - } - val sampler = new ZooKeeperGlobalSampler(zkConfig) - - sampler(span(Long.MinValue, true)) mustEqual true - sampler(span(-1, true)) mustEqual true - sampler(span(0, true)) mustEqual true - sampler(span(1, true)) mustEqual true - sampler(span(Long.MaxValue, true)) mustEqual true + sampler(Long.MinValue) mustEqual false + sampler(-1) mustEqual true + sampler(0) mustEqual true + sampler(1) mustEqual true + sampler(Long.MaxValue) mustEqual false } "drop all traces" in { @@ -69,12 +46,12 @@ class ZooKeeperGlobalSamplerSpec extends Specification with JMocker with ClassMo } val sampler = new ZooKeeperGlobalSampler(zkConfig) - sampler(span(Long.MinValue)) mustEqual false - sampler(span(Long.MinValue + 1)) + sampler(Long.MinValue) mustEqual false + sampler(Long.MinValue + 1) -5000 to 5000 foreach { i => - sampler(span(i)) mustEqual false + sampler(i) mustEqual false } - sampler(span(Long.MaxValue)) mustEqual false + sampler(Long.MaxValue) mustEqual false } "keep all traces" in { @@ -84,11 +61,11 @@ class ZooKeeperGlobalSamplerSpec extends Specification with JMocker with ClassMo } val sampler = new ZooKeeperGlobalSampler(zkConfig) - sampler(span(Long.MinValue)) mustEqual true + sampler(Long.MinValue) mustEqual true -5000 to 5000 foreach { i => - sampler(span(i)) mustEqual true + sampler(i) mustEqual true } - sampler(span(Long.MinValue)) mustEqual true + sampler(Long.MinValue) mustEqual true } } From 628e5cbd1f255f0e53faa935963b247e045d8afb Mon Sep 17 00:00:00 2001 From: Johan Oskarsson Date: Mon, 30 Jul 2012 13:32:30 -0700 Subject: [PATCH 3/3] Added metric for when we receive debug flag --- .../zipkin/collector/processor/SamplerProcessorFilter.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala index ba972bbf35d..19e26371343 100644 --- a/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala +++ b/zipkin-server/src/main/scala/com/twitter/zipkin/collector/processor/SamplerProcessorFilter.scala @@ -33,7 +33,10 @@ class SamplerProcessorFilter(sampler: GlobalSampler) extends ProcessorFilter[Seq * If the span was created with debug mode on we guarantee that it will be * stored no matter what our sampler tells us */ - if (span.debug || sampler(span.traceId)) { + if (span.debug) { + Stats.incr("debugflag") + Some(span) + } else if (sampler(span.traceId)) { Some(span) } else { None