Skip to content

Commit

Permalink
feat: abstracting google tracing provider
Browse files Browse the repository at this point in the history
  • Loading branch information
MoeQuadrat committed Oct 11, 2023
1 parent 6df5425 commit 6d73591
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 74 deletions.
6 changes: 1 addition & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sbt._
//settings

name := """scala-utils"""
val releaseVersion = "2.0.8"
val releaseVersion = "2.0.9"

val token = sys.env.getOrElse("GITHUB_TOKEN", "")

Expand Down Expand Up @@ -76,8 +76,6 @@ val opentelemetryApi = "io.opentelemetry" % "opentele
val opentelemetryBom = "io.opentelemetry" % "opentelemetry-bom" % "1.18.0"
val opentelemetrySdk = "io.opentelemetry" % "opentelemetry-sdk" % "1.18.0"
val opentelemetryInMemoryExporter = "io.opentelemetry" % "opentelemetry-exporter-logging" % "1.19.0"
val opentelemetryGCloudTraces = "com.google.cloud.opentelemetry" % "exporter-trace" % "0.23.0"
val opentelemetryGCloudMetrics = "com.google.cloud.opentelemetry" % "exporter-metrics" % "0.23.0"

val cats = "org.typelevel" %% "cats-core" % "2.7.0"

Expand Down Expand Up @@ -126,8 +124,6 @@ lazy val play = (project in file("util-play"))
opentelemetryApi,
opentelemetryBom,
opentelemetrySdk,
opentelemetryGCloudTraces,
opentelemetryGCloudMetrics,
opentelemetryInMemoryExporter
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package de.innfactory.play.tracing

import com.google.auth.oauth2.GoogleCredentials
import com.google.cloud.opentelemetry.metric.{ GoogleCloudMetricExporter, MetricConfiguration }
import com.google.cloud.opentelemetry.trace.{ TraceConfiguration, TraceExporter }
import com.google.devtools.cloudtrace.v2.{ AttributeValue, TruncatableString }
import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.metrics.Meter
import io.opentelemetry.api.trace.Tracer
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator
import io.opentelemetry.context.propagation.ContextPropagators
import io.opentelemetry.exporter.logging.{ LoggingMetricExporter, LoggingSpanExporter }
import io.opentelemetry.sdk.OpenTelemetrySdk
import io.opentelemetry.sdk.metrics.SdkMeterProvider
import io.opentelemetry.sdk.metrics.`export`.{ MetricExporter, PeriodicMetricReader }
import io.opentelemetry.sdk.metrics.`export`.MetricExporter
import io.opentelemetry.sdk.trace.SdkTracerProvider
import io.opentelemetry.sdk.trace.`export`.{ BatchSpanProcessor, SpanExporter }
import io.opentelemetry.sdk.trace.`export`.SpanExporter
import play.api.inject.ApplicationLifecycle

import java.io.InputStream
import scala.concurrent.{ ExecutionContext, Future, Promise }
import scala.jdk.CollectionConverters.MapHasAsJava
import scala.concurrent.{ExecutionContext, Future, Promise}

object OpentelemetryProvider {
private var tracer: Option[Tracer] = None
Expand Down Expand Up @@ -60,19 +53,17 @@ object OpentelemetryProvider {

def configure(
instrumenationScopeName: String,
projectId: String,
credentialsFile: Option[String]
spanExporter: SpanExporter,
sdkTracerProvider: SdkTracerProvider,
metricExporter: MetricExporter,
sdkMeterProvider: SdkMeterProvider,
contextPropagators: ContextPropagators = ContextPropagators.create(W3CTraceContextPropagator.getInstance),
)(implicit lifeCycle: ApplicationLifecycle, ec: ExecutionContext): Unit = {

val traceExporter = createTraceExporter(projectId, credentialsFile)
val meterExporter = createMeterExporter(projectId, credentialsFile)
val sdkTracerProvider = createSdkTracerProvider(traceExporter)
val sdkMeterProvider = createSdkMeterProvider(meterExporter)

val openTelemetry: OpenTelemetry = OpenTelemetrySdk.builder
.setTracerProvider(sdkTracerProvider)
.setMeterProvider(sdkMeterProvider)
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance))
.setPropagators(contextPropagators)
.buildAndRegisterGlobal

tracer = Some(openTelemetry.getTracer(instrumenationScopeName))
Expand All @@ -81,8 +72,8 @@ object OpentelemetryProvider {
lifeCycle.addStopHook { () =>
Future.sequence(
Seq(
createStopHookTracer(sdkTracerProvider, traceExporter),
createStopHookMeter(sdkMeterProvider, meterExporter)
createStopHookTracer(sdkTracerProvider, spanExporter),
createStopHookMeter(sdkMeterProvider, metricExporter)
)
)
}
Expand Down Expand Up @@ -111,53 +102,4 @@ object OpentelemetryProvider {
}
promise.future
}

private def createGcpCredentials(credentialsFile: Option[String]) = credentialsFile.map { file =>
val serviceAccount: InputStream = getClass.getClassLoader.getResourceAsStream(file)
GoogleCredentials.fromStream(serviceAccount)
}

private def createTraceExporter(projectId: String, credentialsFile: Option[String]): SpanExporter =
createGcpCredentials(credentialsFile) match {
case Some(credentials) =>
val traceConfig = TraceConfiguration
.builder()
.setProjectId(projectId)
.setCredentials(credentials)
.setFixedAttributes(
Map(
(
"/component",
AttributeValue
.newBuilder()
.setStringValue(TruncatableString.newBuilder().setValue("PlayServer").build())
.build()
)
).asJava
)
TraceExporter.createWithConfiguration(traceConfig.build())
case None => LoggingSpanExporter.create()
}

private def createMeterExporter(projectId: String, credentialsFile: Option[String]): MetricExporter =
createGcpCredentials(credentialsFile) match {
case Some(credentials) =>
val meterConfig = MetricConfiguration
.builder()
.setProjectId(projectId)
.setCredentials(credentials)
GoogleCloudMetricExporter.createWithConfiguration(meterConfig.build())
case None => LoggingMetricExporter.create()
}

private def createSdkTracerProvider(spanExporter: SpanExporter) =
SdkTracerProvider.builder
.addSpanProcessor(BatchSpanProcessor.builder(spanExporter).build())
.build()

private def createSdkMeterProvider(metricExporter: MetricExporter) =
SdkMeterProvider
.builder()
.registerMetricReader(PeriodicMetricReader.builder(metricExporter).build())
.build();
}

0 comments on commit 6d73591

Please sign in to comment.