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

Confused trying to follow basic examples for tracing #3717

Closed
vasigorc opened this issue Oct 8, 2021 · 3 comments
Closed

Confused trying to follow basic examples for tracing #3717

vasigorc opened this issue Oct 8, 2021 · 3 comments

Comments

@vasigorc
Copy link

vasigorc commented Oct 8, 2021

Hello 👋
I have a Scala application built with sbt, and which relies on Akka framework that I am trying to add tracing to.

Following the Docs for Opentelemetry Java and otlp based example from this repository, I've added the following dependencies:

val compileDependencies: Seq[ModuleID] = Seq(
...
  "io.opentelemetry" % "opentelemetry-exporter-otlp" % otelVersion,
   "io.opentelemetry" % "opentelemetry-sdk" % otelVersion,
   "io.opentelemetry" % "opentelemetry-sdk-extension-autoconfigure" % otelInstrumentationVersion,
   "io.grpc" % "grpc-netty-shadded" % grpcVersion
 )

// and a runtime dependency
val runtimeDependencies: Seq[ModuleID] = Seq(
    "io.opentelemetry.instrumentation" % "opentelemetry-instrumentation-api" % otelInstrumentationVersion % "runtime"
  )

I am further adding the set-up:

trait OpenTelemetryProvider {

  private val sdkTracerProvider: SdkTracerProvider = SdkTracerProvider
    .builder()
    .addSpanProcessor(
      BatchSpanProcessor
        .builder(OtlpGrpcSpanExporter.builder().build())
        .build()
    )
    .build()

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

  Runtime.getRuntime.addShutdownHook(new Thread(() => sdkTracerProvider.close()))
}

This is the error when I try to run a test:

Caused by: java.lang.IllegalStateException: GlobalOpenTelemetry.set has already been called. GlobalOpenTelemetry.set must be called only once before any calls to GlobalOpenTelemetry.get. If you are using the OpenTelemetrySdk, use OpenTelemetrySdkBuilder.buildAndRegisterGlobal instead. Previous invocation set to cause of this exception.
	at io.opentelemetry.api.GlobalOpenTelemetry.set(GlobalOpenTelemetry.java:84)
	at io.opentelemetry.sdk.OpenTelemetrySdkBuilder.buildAndRegisterGlobal(OpenTelemetrySdkBuilder.java:63)
	at com.myproject.observability.OpenTelemetryProvider.$init$(OpenTelemetryProvider.scala:27)

These are the lines that throw this exception:

  public static void set(OpenTelemetry openTelemetry) {
    synchronized (mutex) {
      if (globalOpenTelemetry != null) {
        throw new IllegalStateException(

I am wondering what has gone wrong that by following basic examples from the Docs I ran in a state unexpected by the library. Please note that the docs don't list all dependencies required for using SDK. IMO either this is not obvious, or I am missing something obvious.

@jkwatson
Copy link
Contributor

jkwatson commented Oct 8, 2021

It sounds like you're running your initialization code more than once (hence the exception that you see). I don't know anything about akka or how your configuration code might be being called, but you do need to make sure that it is only called once if you want to register the global instance. If you don't care about the global instance, you can certainly get rid of this error by not registering the global. But, even in that case, if you're creating more than once instance of the SDK, things are probably getting confused somewhere.

@vasigorc
Copy link
Author

vasigorc commented Oct 8, 2021

Thanks @jkwatson I actually removed all sdk packages and I've only added opentelemetry-api. With GlobalOpenTelemetry available there it works for me. I hope I won't run into issues later. Closing this ticket.

@vasigorc vasigorc closed this as completed Oct 8, 2021
@jkwatson
Copy link
Contributor

jkwatson commented Oct 8, 2021

Just curious...are you running with the -javaagent in place? If so, then 👍🏽 . If not, then I'm wondering where the SDK is getting configured. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants