From 9fa2b5ae833bef734dcd219771d901c0310cfb3b Mon Sep 17 00:00:00 2001 From: Paul Draper Date: Sun, 5 Mar 2017 23:25:58 -0700 Subject: [PATCH] ThreadContextSpan --- .travis.yml | 28 ++++++++++++++ build.sbt | 26 +++++++++++++ project/build.properties | 1 + project/build.sbt | 3 ++ thread-context/build.sbt | 7 ++++ .../threadcontext/ThreadContextSpan.java | 37 +++++++++++++++++++ 6 files changed, 102 insertions(+) create mode 100644 .travis.yml create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 project/build.sbt create mode 100644 thread-context/build.sbt create mode 100644 thread-context/src/main/java/io/opentracing/threadcontext/ThreadContextSpan.java diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ed0c9aa --- /dev/null +++ b/.travis.yml @@ -0,0 +1,28 @@ +after_success: | + if [ "$TRAVIS_SECURE_ENV_VARS" == true ]; then + echo "$PGP_SECRET" | base64 --decode | gpg --import + if [ -z "$TRAVIS_TAG" ]; then + sbt publishSigned + else + sbt publishSigned sonatypeRelease + fi + fi +cache: + directories: + - $HOME/.ivy2 + - $HOME/.sbt +deploy: + api_key: $GITHUB_AUTH + file: '*/target/**/*.jar' + file_glob: true + on: + tags: true + provider: releases + skip_cleanup: true +git: + depth: 1 +jdk: oraclejdk8 +language: scala +script: + - '[ "$TRAVIS_PULL_REQUEST" != false ] || export SBT_OPTS=-Dbuild.version=${TRAVIS_TAG:-$TRAVIS_BRANCH-SNAPSHOT}' + - sbt package diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..3ef84aa --- /dev/null +++ b/build.sbt @@ -0,0 +1,26 @@ +lazy val `thread-context` = project + +inScope(Global)(Seq( + autoScalaLibrary := false, + credentials += Credentials( + "Sonatype Nexus Repository Manager", + "oss.sonatype.org", + sys.env.getOrElse("SONATYPE_USERNAME", ""), + sys.env.getOrElse("SONATYPE_PASSWORD", "") + ), + crossPaths := false, + developers += Developer("pauldraper", "Paul Draper", "paulddraper@gmail.com", url("https://github.com/pauldraper")), + homepage := Some(url("https://git.lucidchart.com/lucidsoftware/opentracing-thread-context")), + licenses += "Apache 2.0 License" -> url("https://www.apache.org/licenses/LICENSE-2.0"), + organization := "com.lucidchart", + organizationHomepage := Some(url("https://github.com/lucidsoftware")), + organizationName := "Lucid Software", + PgpKeys.pgpPassphrase := Some(Array.emptyCharArray), + resolvers += Resolver.typesafeRepo("releases"), + scmInfo := Some(ScmInfo( + url("https://github.com/lucidsoftware/opentracing-thread-context"), + "scm:git:git@github.com:lucidsoftware/opentracing-thread-context" + )), + startYear := Some(2017), + version := sys.props.getOrElse("build.version", "0-SNAPSHOT") +)) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..27e88aa --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=0.13.13 diff --git a/project/build.sbt b/project/build.sbt new file mode 100644 index 0000000..8e1620a --- /dev/null +++ b/project/build.sbt @@ -0,0 +1,3 @@ +addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") + +addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1") diff --git a/thread-context/build.sbt b/thread-context/build.sbt new file mode 100644 index 0000000..e30e5bb --- /dev/null +++ b/thread-context/build.sbt @@ -0,0 +1,7 @@ +libraryDependencies ++= Seq( + "com.lucidchart" % "thread-context" % "0.4", + "io.opentracing" % "opentracing-api" % "0.20.7", + "io.opentracing" % "opentracing-noop" % "0.20.7" +) + +name := s"opentracing-${name.value}" diff --git a/thread-context/src/main/java/io/opentracing/threadcontext/ThreadContextSpan.java b/thread-context/src/main/java/io/opentracing/threadcontext/ThreadContextSpan.java new file mode 100644 index 0000000..91326e1 --- /dev/null +++ b/thread-context/src/main/java/io/opentracing/threadcontext/ThreadContextSpan.java @@ -0,0 +1,37 @@ +package io.opentracing.threadcontext; + +import com.github.threadcontext.ThreadContext; +import com.github.threadcontext.ThreadLocalSaver; +import io.opentracing.NoopSpan; +import io.opentracing.Span; +import java.util.function.Supplier; + +public final class ThreadContextSpan { + + private static final ThreadLocal span = new ThreadLocal() { + protected Span initialValue() { + return defaultSpan.get(); + } + }; + static { + ThreadContext.savers.add(new ThreadLocalSaver<>(span)); + } + + public static Supplier defaultSpan = () -> NoopSpan.INSTANCE; + + private ThreadContextSpan() { + } + + public static Span get() { + return span.get(); + } + + public static void set(Span span) { + ThreadContextSpan.span.set(span); + } + + public static void clear() { + span.remove(); + } + +}