Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: abstracting google tracing provider #7

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Loading