2222import io .micrometer .core .instrument .internal .DefaultLongTaskTimer ;
2323import io .micrometer .core .instrument .internal .DefaultMeter ;
2424import io .micrometer .core .instrument .util .NamedThreadFactory ;
25- import io .sentry .Sentry ;
25+ import io .sentry .IScopes ;
26+ import io .sentry .ScopesAdapter ;
2627import io .sentry .SentryAttributes ;
2728import io .sentry .SentryIntegrationPackageStorage ;
2829import io .sentry .SentryLevel ;
2930import io .sentry .metrics .MetricsUnit ;
3031import io .sentry .util .ExceptionUtils ;
32+ import io .sentry .util .Objects ;
3133import java .util .HashMap ;
3234import java .util .Map ;
3335import java .util .concurrent .Executors ;
@@ -44,6 +46,7 @@ public final class SentryMeterRegistry extends MeterRegistry {
4446 private static final @ NotNull String INTEGRATION_NAME = "Micrometer" ;
4547 private static final long DEFAULT_POLL_INTERVAL_MILLIS = 60_000 ;
4648
49+ private final @ NotNull IScopes scopes ;
4750 private final @ Nullable ScheduledExecutorService scheduler ;
4851 private final @ Nullable ScheduledFuture <?> pollingTask ;
4952
@@ -63,14 +66,25 @@ public SentryMeterRegistry() {
6366 * <p>A zero interval disables passive meter polling. Negative intervals are not supported.
6467 */
6568 public SentryMeterRegistry (final long pollIntervalMillis ) {
66- this (pollIntervalMillis , Clock .SYSTEM , createScheduler (pollIntervalMillis ));
69+ this (ScopesAdapter .getInstance (), pollIntervalMillis );
70+ }
71+
72+ /**
73+ * Creates a registry with the given scopes and passive meter polling interval in milliseconds.
74+ *
75+ * <p>A zero interval disables passive meter polling. Negative intervals are not supported.
76+ */
77+ public SentryMeterRegistry (final @ NotNull IScopes scopes , final long pollIntervalMillis ) {
78+ this (scopes , pollIntervalMillis , Clock .SYSTEM , createScheduler (pollIntervalMillis ));
6779 }
6880
6981 SentryMeterRegistry (
82+ final @ NotNull IScopes scopes ,
7083 final long pollIntervalMillis ,
7184 final @ NotNull Clock clock ,
7285 final @ Nullable ScheduledExecutorService scheduler ) {
7386 super (clock );
87+ this .scopes = Objects .requireNonNull (scopes , "Scopes are required" );
7488 validatePollInterval (pollIntervalMillis );
7589 if (pollIntervalMillis > 0 && scheduler == null ) {
7690 throw new IllegalArgumentException (
@@ -79,7 +93,7 @@ public SentryMeterRegistry(final long pollIntervalMillis) {
7993 this .scheduler = pollIntervalMillis == 0 ? null : scheduler ;
8094 config ()
8195 .namingConvention (NamingConvention .identity )
82- .meterFilter (new SentryIgnoredMetricsFilter (this ))
96+ .meterFilter (new SentryIgnoredMetricsFilter (this , scopes ))
8397 .onMeterRemoved (this ::onMeterRemoved );
8498 addIntegrationToSdkVersion (INTEGRATION_NAME );
8599 if (this .scheduler == null ) {
@@ -168,7 +182,7 @@ public SentryMeterRegistry(final long pollIntervalMillis) {
168182 final @ NotNull Meter .Id id ,
169183 final @ NotNull Meter .Type type ,
170184 final @ NotNull Iterable <Measurement > measurements ) {
171- Sentry . getCurrentScopes ()
185+ scopes
172186 .getOptions ()
173187 .getLogger ()
174188 .log (
@@ -192,7 +206,7 @@ void captureCounter(final @NotNull SentryMetricInfo metricInfo, final double val
192206 if (isClosed ()) {
193207 return ;
194208 }
195- Sentry . getCurrentScopes ()
209+ scopes
196210 .metrics ()
197211 .count (metricInfo .getName (), value , metricInfo .getUnit (), metricInfo .createParameters ());
198212 }
@@ -201,7 +215,7 @@ void captureDistribution(final @NotNull SentryMetricInfo metricInfo, final doubl
201215 if (isClosed ()) {
202216 return ;
203217 }
204- Sentry . getCurrentScopes ()
218+ scopes
205219 .metrics ()
206220 .distribution (
207221 metricInfo .getName (), value , metricInfo .getUnit (), metricInfo .createParameters ());
@@ -211,7 +225,7 @@ void captureGauge(final @NotNull SentryMetricInfo metricInfo, final double value
211225 if (isClosed ()) {
212226 return ;
213227 }
214- Sentry . getCurrentScopes ()
228+ scopes
215229 .metrics ()
216230 .gauge (metricInfo .getName (), value , metricInfo .getUnit (), metricInfo .createParameters ());
217231 }
@@ -234,7 +248,7 @@ void pollMeters() {
234248 }
235249
236250 void logPollingFailure (final @ NotNull Throwable throwable , final @ NotNull String meterName ) {
237- Sentry . getCurrentScopes ()
251+ scopes
238252 .getOptions ()
239253 .getLogger ()
240254 .log (
0 commit comments