-
Notifications
You must be signed in to change notification settings - Fork 903
pass meter provider to avoid using the global otel instance #7475
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import io.opentelemetry.api.incubator.config.DeclarativeConfigException; | ||
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; | ||
import io.opentelemetry.api.metrics.MeterProvider; | ||
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; | ||
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; | ||
import java.io.Closeable; | ||
|
@@ -15,11 +16,13 @@ | |
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
|
||
/** Declarative configuration context and state carrier. */ | ||
class DeclarativeConfigContext { | ||
|
||
private final SpiHelper spiHelper; | ||
@Nullable private MeterProvider meterProvider; | ||
private final List<Closeable> closeables = new ArrayList<>(); | ||
|
||
DeclarativeConfigContext(SpiHelper spiHelper) { | ||
|
@@ -39,6 +42,15 @@ List<Closeable> getCloseables() { | |
return Collections.unmodifiableList(closeables); | ||
} | ||
|
||
@Nullable | ||
public MeterProvider getMeterProvider() { | ||
return meterProvider; | ||
} | ||
|
||
public void setMeterProvider(MeterProvider meterProvider) { | ||
this.meterProvider = meterProvider; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any chance this needs to be cross-thread safe? In which case, putting a memory barrier around the assignment would be necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, this doesn't need to be thread safe - it belongs to the set up logic that is done in a single thread at the start |
||
} | ||
|
||
/** | ||
* Find a registered {@link ComponentProvider} with {@link ComponentProvider#getType()} matching | ||
* {@code type}, {@link ComponentProvider#getName()} matching {@code name}, and call {@link | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.Objects; | ||
import java.util.regex.Pattern; | ||
|
@@ -53,6 +54,16 @@ public OpenTelemetrySdk create( | |
resource = ResourceFactory.getInstance().create(model.getResource(), context); | ||
} | ||
|
||
if (model.getMeterProvider() != null) { | ||
SdkMeterProvider meterProvider = | ||
MeterProviderFactory.getInstance() | ||
.create(model.getMeterProvider(), context) | ||
.setResource(resource) | ||
.build(); | ||
context.setMeterProvider(meterProvider); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be less error prone if context.setMeterProvider call addCloseable internally. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would work if we change the parameter type to |
||
builder.setMeterProvider(context.addCloseable(meterProvider)); | ||
} | ||
|
||
if (model.getLoggerProvider() != null) { | ||
builder.setLoggerProvider( | ||
context.addCloseable( | ||
|
@@ -77,15 +88,6 @@ public OpenTelemetrySdk create( | |
.build())); | ||
} | ||
|
||
if (model.getMeterProvider() != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was there any reason to move this code block upper? If not then it leaving it in the same place would make changes analysis a bit simpler. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the meter provider is taken out of the context for logger and tracer |
||
builder.setMeterProvider( | ||
context.addCloseable( | ||
MeterProviderFactory.getInstance() | ||
.create(model.getMeterProvider(), context) | ||
.setResource(resource) | ||
.build())); | ||
} | ||
|
||
return context.addCloseable(builder.build()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,6 +5,7 @@ | |||
|
||||
package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||||
|
||||
import io.opentelemetry.api.metrics.MeterProvider; | ||||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.BatchSpanProcessorModel; | ||||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SimpleSpanProcessorModel; | ||||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SpanExporterModel; | ||||
|
@@ -47,6 +48,11 @@ public SpanProcessor create(SpanProcessorModel model, DeclarativeConfigContext c | |||
if (batchModel.getScheduleDelay() != null) { | ||||
builder.setScheduleDelay(Duration.ofMillis(batchModel.getScheduleDelay())); | ||||
} | ||||
MeterProvider meterProvider = context.getMeterProvider(); | ||||
if (meterProvider != null) { | ||||
builder.setMeterProvider(meterProvider); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This partially solves the problem, but we still won't get the internal exporter telemetry. The way we solve this in autoconfigure today is with the AutoConfigureListener. SDK components which are loaded via SPI and which report internal telemetry implement I think what you've done here does work for SDK components which report internal telemetry and which are NOT loaded via SPI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That SPI would implement Line 17 in eec2120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the SPI guaranteed to be run on the same thread that set the value onto the context? If not, then we will need a memory barrier on the setting of the value into the context, I think. |
||||
} | ||||
|
||||
return context.addCloseable(builder.build()); | ||||
} | ||||
|
||||
|
Uh oh!
There was an error while loading. Please reload this page.