Repository: open-telemetry/opentelemetry-java
File: api/all/src/main/java/io/opentelemetry/api/GlobalOpenTelemetry.java (Java)

Code before:
    try {
      openTelemetrySdkAutoConfiguration =
          Class.forName("io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk");
    } catch (ClassNotFoundException e) {
      return null;
    }

    // If autoconfigure module is present but global autoconfigure disabled log a warning and return
    boolean globalAutoconfigureEnabled =
        Boolean.parseBoolean(ConfigUtil.getString(GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY, "false"));
    if (!globalAutoconfigureEnabled) {
      logger.log(
          Level.INFO,
          "AutoConfiguredOpenTelemetrySdk found on classpath but automatic configuration is disabled."
              + " To enable, run your JVM with -D"
              + GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY
              + "=true");
      return null;
    }

    try {
      Method initialize = openTelemetrySdkAutoConfiguration.getMethod("initialize");
      Object autoConfiguredSdk = initialize.invoke(null);
      Method getOpenTelemetrySdk =
          openTelemetrySdkAutoConfiguration.getMethod("getOpenTelemetrySdk");
      return IncubatingUtil.obfuscatedOpenTelemetry(
          (OpenTelemetry) getOpenTelemetrySdk.invoke(autoConfiguredSdk));
    } catch (NoSuchMethodException | IllegalAccessException e) {
      throw new IllegalStateException(
          "AutoConfiguredOpenTelemetrySdk detected on classpath "
              + "but could not invoke initialize method. This is a bug in OpenTelemetry.",
          e);
    } catch (InvocationTargetException t) {
      logger.log(
          Level.SEVERE,
          "Error automatically configuring OpenTelemetry SDK. OpenTelemetry will not be enabled.",
          t.getTargetException());
      return null;
    }
  }
}


Review comment (suggestion):
#nit: I think we should consider moving the logic from `IncubatingUtil.obfuscatedOpenTelemetry` into `GlobalOpenTelemetry`. Its a bit jarring to see code that looks like it will be using incubating code even when the incubator is not present. Also allows the standard ObfuscatedOpenTelemetry class to stay put here. It looks like its incubating when it lives as an inner class of `IncubatingUtil`. 

Code after:
  private static OpenTelemetry maybeAutoConfigureAndSetGlobal() {
    Class<?> openTelemetrySdkAutoConfiguration;
    try {
      openTelemetrySdkAutoConfiguration =
          Class.forName("io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk");
    } catch (ClassNotFoundException e) {
      return null;
    }

    // If autoconfigure module is present but global autoconfigure disabled log a warning and return
    boolean globalAutoconfigureEnabled =
        Boolean.parseBoolean(ConfigUtil.getString(GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY, "false"));
    if (!globalAutoconfigureEnabled) {
      logger.log(
          Level.INFO,
          "AutoConfiguredOpenTelemetrySdk found on classpath but automatic configuration is disabled."
              + " To enable, run your JVM with -D"
              + GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY
              + "=true");
      return null;
    }

    try {
      Method initialize = openTelemetrySdkAutoConfiguration.getMethod("initialize");
      Object autoConfiguredSdk = initialize.invoke(null);
      Method getOpenTelemetrySdk =
          openTelemetrySdkAutoConfiguration.getMethod("getOpenTelemetrySdk");
      return obfuscatedOpenTelemetry((OpenTelemetry) getOpenTelemetrySdk.invoke(autoConfiguredSdk));
    } catch (NoSuchMethodException | IllegalAccessException e) {
      throw new IllegalStateException(
          "AutoConfiguredOpenTelemetrySdk detected on classpath "
              + "but could not invoke initialize method. This is a bug in OpenTelemetry.",
          e);
    } catch (InvocationTargetException t) {
      logger.log(
          Level.SEVERE,
          "Error automatically configuring OpenTelemetry SDK. OpenTelemetry will not be enabled.",
          t.getTargetException());
      return null;
    }
  }

  private static OpenTelemetry obfuscatedOpenTelemetry(OpenTelemetry openTelemetry) {
    OpenTelemetry incubating = IncubatingUtil.obfuscatedOpenTelemetryIfIncubating(openTelemetry);
    if (incubating != null) {
      return incubating;
    }
    return new ObfuscatedOpenTelemetry(openTelemetry);
  }

  /**