aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/trace/GoogleStackdriverTrace.scala
diff options
context:
space:
mode:
authorJohn St. John <jstjohn@users.noreply.github.com>2017-09-21 16:28:23 -0700
committerGitHub <noreply@github.com>2017-09-21 16:28:23 -0700
commit9b0bed19489dbbd59c52a77e9c0c080a880ca262 (patch)
tree74934c57133ada3cd84a5c8a702cdf2683e8881e /src/main/scala/xyz/driver/core/trace/GoogleStackdriverTrace.scala
parent037f522caf47d70d674daba62b540a451542c64d (diff)
downloaddriver-core-9b0bed19489dbbd59c52a77e9c0c080a880ca262.tar.gz
driver-core-9b0bed19489dbbd59c52a77e9c0c080a880ca262.tar.bz2
driver-core-9b0bed19489dbbd59c52a77e9c0c080a880ca262.zip
Jstjohn/google stackdriver trace (#64)v1.0.1
Add app-level tracing to driver-core (https://www.pivotaltracker.com/story/show/151100422)
Diffstat (limited to 'src/main/scala/xyz/driver/core/trace/GoogleStackdriverTrace.scala')
-rw-r--r--src/main/scala/xyz/driver/core/trace/GoogleStackdriverTrace.scala47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/trace/GoogleStackdriverTrace.scala b/src/main/scala/xyz/driver/core/trace/GoogleStackdriverTrace.scala
new file mode 100644
index 0000000..1ff8d10
--- /dev/null
+++ b/src/main/scala/xyz/driver/core/trace/GoogleStackdriverTrace.scala
@@ -0,0 +1,47 @@
+package xyz.driver.core.trace
+
+import java.io.FileInputStream
+import java.nio.file.{Files, Paths}
+import java.util
+
+import akka.http.scaladsl.model.HttpRequest
+import com.google.auth.oauth2.GoogleCredentials
+import com.google.cloud.trace.grpc.v1.GrpcTraceConsumer
+import com.google.cloud.trace.v1.consumer.TraceConsumer
+import com.typesafe.scalalogging.Logger
+
+final class GoogleStackdriverTrace(projectId: String,
+ clientSecretsFile: String,
+ appName: String,
+ appEnvironment: String,
+ log: Logger)
+ extends GoogleServiceTracer {
+
+ // initialize our various tracking storage systems
+ private val clientSecretsInputStreamOpt: Option[FileInputStream] = if (Files.exists(Paths.get(clientSecretsFile))) {
+ Some(new FileInputStream(clientSecretsFile))
+ } else {
+ None
+ }
+ // if the google credentials are invalid, just log the traces
+ private val traceConsumer: TraceConsumer = clientSecretsInputStreamOpt.fold[TraceConsumer] {
+ log.warn(s"Google credentials not found in path: $clientSecretsFile")
+ new LoggingTraceConsumer(log)
+ } { clientSecretsInputStream =>
+ GrpcTraceConsumer
+ .create(
+ "cloudtrace.googleapis.com",
+ GoogleCredentials
+ .fromStream(clientSecretsInputStream)
+ .createScoped(util.Arrays.asList("https://www.googleapis.com/auth/trace.append"))
+ )
+ }
+
+ private val googleServiceTracer =
+ new GoogleStackdriverTraceWithConsumer(projectId, appName, appEnvironment, traceConsumer)
+
+ override def startSpan(httpRequest: HttpRequest): GoogleStackdriverTraceSpan =
+ googleServiceTracer.startSpan(httpRequest)
+
+ override def endSpan(span: GoogleStackdriverTraceSpan): Unit = googleServiceTracer.endSpan(span)
+}