这是indexloc提供的服务,不要输入任何密码
Skip to content

Add setLoggerConfigurator support to LoggerProvider #7332

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

Merged
merged 6 commits into from
May 9, 2025

Conversation

fandreuz
Copy link
Contributor

@fandreuz fandreuz commented May 7, 2025

Fixes (partially) #7051

@fandreuz fandreuz requested a review from a team as a code owner May 7, 2025 23:25
Copy link

codecov bot commented May 7, 2025

Codecov Report

Attention: Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.

Project coverage is 89.85%. Comparing base (eec2120) to head (bd301f1).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
...metry/sdk/logs/internal/SdkLoggerProviderUtil.java 75.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #7332      +/-   ##
============================================
- Coverage     89.85%   89.85%   -0.01%     
- Complexity     6890     6895       +5     
============================================
  Files           786      786              
  Lines         20772    20789      +17     
  Branches       2025     2025              
============================================
+ Hits          18665    18680      +15     
- Misses         1464     1466       +2     
  Partials        643      643              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@jack-berg jack-berg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment about retaining ExtendedSdkLogger#isEnabled().

Thanks for the contribution.. now to tackle the much more difficult metrics implementation 😅

}

@Override
public boolean isEnabled() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this around.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What for? isEnabled is inherited from SdkLogger, am I missing something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a minor thing: ExtendedSdkLogger implemented ExtendedLogger, which is where the isEnabled() API is defined.

If we only define it in SdkLogger, we can't keep the @Override annotation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, fixed in 4d3871c

@fandreuz
Copy link
Contributor Author

fandreuz commented May 8, 2025

Thanks for the contribution.. now to tackle the much more difficult metrics implementation 😅

@jack-berg can you clarify why that's more difficult than something like this?

diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java
index c0f147607..71a907f7f 100644
--- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java
+++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java
@@ -88,7 +88,8 @@ final class SdkMeter implements Meter {
   private final MeterProviderSharedState meterProviderSharedState;
   private final InstrumentationScopeInfo instrumentationScopeInfo;
   private final Map<RegisteredReader, MetricStorageRegistry> readerStorageRegistries;
-  private final boolean meterEnabled;
+
+  private boolean meterEnabled;
 
   SdkMeter(
       MeterProviderSharedState meterProviderSharedState,
@@ -103,6 +104,10 @@ final class SdkMeter implements Meter {
     this.meterEnabled = meterConfig.isEnabled();
   }
 
+  void updateMeterConfig(MeterConfig meterConfig) {
+    meterEnabled = meterConfig.isEnabled();
+  }
+
   // Visible for testing
   InstrumentationScopeInfo getInstrumentationScopeInfo() {
     return instrumentationScopeInfo;
diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProvider.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProvider.java
index 30ae0b1da..a143aa0dd 100644
--- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProvider.java
+++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProvider.java
@@ -52,9 +52,19 @@ public final class SdkMeterProvider implements MeterProvider, Closeable {
   private final List<MetricProducer> metricProducers;
   private final MeterProviderSharedState sharedState;
   private final ComponentRegistry<SdkMeter> registry;
-  private final ScopeConfigurator<MeterConfig> meterConfigurator;
   private final AtomicBoolean isClosed = new AtomicBoolean(false);
 
+  private ScopeConfigurator<MeterConfig> meterConfigurator;
+
+  void setMeterConfigurator(ScopeConfigurator<MeterConfig> meterConfigurator) {
+    this.meterConfigurator = meterConfigurator;
+    this.registry
+        .getComponents()
+        .forEach(
+            sdkMeter ->
+                sdkMeter.updateMeterConfig(getMeterConfig(sdkMeter.getInstrumentationScopeInfo())));
+  }
+
   /** Returns a new {@link SdkMeterProviderBuilder} for {@link SdkMeterProvider}. */
   public static SdkMeterProviderBuilder builder() {
     return new SdkMeterProviderBuilder();
diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/SdkMeterProviderUtil.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/SdkMeterProviderUtil.java
index 9fc690366..c324900ab 100644
--- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/SdkMeterProviderUtil.java
+++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/SdkMeterProviderUtil.java
@@ -49,6 +49,19 @@ public final class SdkMeterProviderUtil {
     return sdkMeterProviderBuilder;
   }
 
+  /** Reflectively set the {@link ScopeConfigurator} to the {@link SdkMeterProvider}. */
+  public static void setMeterConfigurator(
+      SdkMeterProvider sdkMeterProvider, ScopeConfigurator<MeterConfig> scopeConfigurator) {
+    try {
+      Method method =
+          SdkMeterProvider.class.getDeclaredMethod("setMeterConfigurator", ScopeConfigurator.class);
+      method.setAccessible(true);
+      method.invoke(sdkMeterProvider, scopeConfigurator);
+    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+      throw new IllegalStateException("Error calling setMeterConfigurator on SdkMeterProvider", e);
+    }
+  }
+
   /** Reflectively set the {@link ScopeConfigurator} to the {@link SdkMeterProviderBuilder}. */
   public static SdkMeterProviderBuilder setMeterConfigurator(
       SdkMeterProviderBuilder sdkMeterProviderBuilder,

I've seen the remark in #7051, but I'm missing why the implementation above would not be enough 😅

@jack-berg
Copy link
Member

@jack-berg can you clarify why that's more difficult than something like this?

Logs and spans are pretty much stateless: tracer and logger record spans and logs, and those flow through to processors and exporters. So its straight forward to change these pipelines to enable / disable tracers and loggers. Spans and logs start and stop flowing and all is good.

Metrics are a whole different beast because they are stateful. Consider the case where you have an instrument from a meter which is initially disabled, and later enabled. The implementation needs to initially drop measurements, and then when the meter is enabled, it needs to initialize the metric storage and start recording / aggregating measurements.

@jack-berg jack-berg merged commit 983133f into open-telemetry:main May 9, 2025
28 checks passed
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

Successfully merging this pull request may close these issues.

2 participants