sigram commented on code in PR #4917: URL: https://github.com/apache/solr/pull/4917#discussion_r4037610218
########## solr/core/src/test/org/apache/solr/metrics/SolrMetricsContextTest.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.metrics; + +import io.opentelemetry.exporter.prometheus.PrometheusMetricReader; +import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.metrics.otel.OtelUnit; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class SolrMetricsContextTest extends SolrTestCaseJ4 { + private static final String REGISTRY = "test_context_registry"; + private SolrMetricManager metricManager; + private PrometheusMetricReader reader; + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + metricManager = new SolrMetricManager(InMemoryMetricExporter.create()); + metricManager.meterProvider(REGISTRY); + reader = metricManager.getPrometheusMetricReader(REGISTRY); + } + + @After + @Override + public void tearDown() throws Exception { + metricManager.closeAllRegistries(); + super.tearDown(); + } + + /** Callbacks registered through a context must stop firing once the context is closed. */ + @Test + public void testCloseUnregistersObservableCallbacks() { + SolrMetricsContext ctx = new SolrMetricsContext(metricManager, REGISTRY); + AtomicInteger invocations = new AtomicInteger(); + + ctx.observableLongGauge("long_gauge", "d", m -> invocations.incrementAndGet()); + ctx.observableLongGauge( + "long_gauge_unit", "d", m -> invocations.incrementAndGet(), OtelUnit.BYTES); + ctx.observableDoubleGauge("double_gauge", "d", m -> invocations.incrementAndGet()); + ctx.observableDoubleGauge( + "double_gauge_unit", "d", m -> invocations.incrementAndGet(), OtelUnit.BYTES); + ctx.observableLongCounter("long_counter", "d", m -> invocations.incrementAndGet()); + ctx.observableLongCounter( + "long_counter_unit", "d", m -> invocations.incrementAndGet(), OtelUnit.BYTES); + ctx.observableDoubleCounter("double_counter", "d", m -> invocations.incrementAndGet()); + ctx.observableDoubleCounter( + "double_counter_unit", "d", m -> invocations.incrementAndGet(), OtelUnit.BYTES); + + reader.collect(); + assertEquals("all 8 callbacks should fire while the context is open", 8, invocations.get()); + + ctx.close(); + invocations.set(0); + reader.collect(); + assertEquals("no callback should fire after the context is closed", 0, invocations.get()); + } Review Comment: I wonder if we could somehow add a test to actually detect leaks... I imagine something like using the `DiagnosticCommand` MXBean with `GC.class_histogram` command to do a full GC and list the class/instance histogram, then do several known-to-be-tricky operations like add/commit/commitWithin, core reload, schema change etc, and then do the histogram again (which again performs a full GC) to see if differences are "reasonable". -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
