diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2015-04-17 22:41:03 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2015-04-17 22:41:03 +0200 |
commit | e3e0055955ba82a3ba0f2242510397b8cc016c90 (patch) | |
tree | ada426b03860b9a07916c37f7285c6dd0d5f2527 /kamon-core/src/main | |
parent | 14fbca502599ece15d46b726ba066d3f4332f650 (diff) | |
download | Kamon-e3e0055955ba82a3ba0f2242510397b8cc016c90.tar.gz Kamon-e3e0055955ba82a3ba0f2242510397b8cc016c90.tar.bz2 Kamon-e3e0055955ba82a3ba0f2242510397b8cc016c90.zip |
+ core: add a couple helper methods for creating segments, related to #126.
Diffstat (limited to 'kamon-core/src/main')
-rw-r--r-- | kamon-core/src/main/scala/kamon/trace/TraceContext.scala | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/kamon-core/src/main/scala/kamon/trace/TraceContext.scala b/kamon-core/src/main/scala/kamon/trace/TraceContext.scala index 25f3a0a3..85b7396f 100644 --- a/kamon-core/src/main/scala/kamon/trace/TraceContext.scala +++ b/kamon-core/src/main/scala/kamon/trace/TraceContext.scala @@ -18,7 +18,9 @@ package kamon.trace import java.io.ObjectStreamException import kamon.trace.TraceContextAware.DefaultTraceContextAware -import kamon.util.{ Function, RelativeNanoTimestamp } +import kamon.util.{ SameThreadExecutionContext, Supplier, Function, RelativeNanoTimestamp } + +import scala.concurrent.Future trait TraceContext { def name: String @@ -45,6 +47,23 @@ trait TraceContext { if (nonEmpty) Some(f(this)) else None + + def withNewSegment[T](segmentName: String, category: String, library: String)(code: ⇒ T): T = { + val segment = startSegment(segmentName, category, library) + try code finally segment.finish() + } + + // Java variant. + def withNewSegment[T](segmentName: String, category: String, library: String, code: Supplier[T]): T = + withNewSegment(segmentName, category, library)(code.get) + + def withNewAsyncSegment[T](segmentName: String, category: String, library: String)(code: ⇒ Future[T]): Future[T] = { + val segment = startSegment(segmentName, category, library) + val result = code + code.onComplete(_ ⇒ segment.finish())(SameThreadExecutionContext) + result + } + } trait Segment { |