GoogleCloudPlatform / opentelemetry-operations-java

Apache License 2.0
73 stars 38 forks source link

Lazy load project ID in TraceConfiguration #195

Closed psx95 closed 1 year ago

psx95 commented 1 year ago

Fixes #141

Lazy loading Project ID in trace configuration prevents crashes when using the exporter-trace with opencensus-shim.

Test Setup

Initialization code

private static void initializeOTel() {
        try {
            traceExporter = TraceExporter.createWithConfiguration(TraceConfiguration.builder()
                    .setProjectId(PROJECT_ID).build()); //
        } catch (IOException e) {
            e.printStackTrace();
        }

        sdkTracerProvider = SdkTracerProvider.builder()
                .setResource(resource)
                .addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build())
                .build();

        openTelemetry = OpenTelemetrySdk.builder()
                .setTracerProvider(sdkTracerProvider)
                .buildAndRegisterGlobal();

        System.out.println("OpenTelemetry initialized Successfully !!!");
}
punya commented 1 year ago

I don't understand how this fixes the problem. If you remove the .setProjectId(PROJECT_ID) from your sample code, it seems like we'll still have the following sequence of events

  1. Build the configuration -> call metadata server -> touch OC -> implicitly set the OTel global
  2. Try to explicitly set the global, which will fail
psx95 commented 1 year ago

Yes, that's a good point, let me retrace my steps and see if I missed something. Thanks.

psx95 commented 1 year ago

@punya Yup, you are right when project ID is not explicitly set, it leads to the same issue. Closing this PR since #193 contains this change and additional changes required to make it work for the case when Project ID is not set.