Apache Solr open-source search software
SOLR-17785: Integrate foundational OTEL meter instruments (#3384)
Matthew Biscocho437767d52081
diff --git a/solr/core/src/java/org/apache/solr/blockcache/Metrics.java b/solr/core/src/java/org/apache/solr/blockcache/Metrics.javaindex d7e13b9132f..dcdb4550d8c 100644--- a/solr/core/src/java/org/apache/solr/blockcache/Metrics.java+++ b/solr/core/src/java/org/apache/solr/blockcache/Metrics.java@@ -16,6 +16,7 @@*/package org.apache.solr.blockcache;+import io.opentelemetry.api.common.Attributes;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicLong;@@ -54,8 +55,10 @@ public class Metrics extends SolrCacheBase implements SolrInfoBean {private SolrMetricsContext solrMetricsContext;private long previous = System.nanoTime();+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {solrMetricsContext = parentContext.getChildContext(this);metricsMap =new MetricsMap(diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.javaindex 0c45e84dd6e..75e1d43d60b 100644--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java@@ -31,6 +31,7 @@ import static org.apache.solr.security.AuthenticationPlugin.AUTHENTICATION_PLUGIimport com.github.benmanes.caffeine.cache.Interner;import com.google.common.annotations.VisibleForTesting;+import io.opentelemetry.api.common.Attributes;import io.opentelemetry.api.trace.Tracer;import io.opentelemetry.exporter.prometheus.PrometheusMetricReader;import jakarta.inject.Singleton;@@ -516,7 +517,9 @@ public class CoreContainer {newVersion, getResourceLoader().newInstance(klas, AuditLoggerPlugin.class));newAuditloggerPlugin.plugin.init(auditConf);- newAuditloggerPlugin.plugin.initializeMetrics(solrMetricsContext, "/auditlogging");+ // TODO SOLR-17458: Add Otel+ newAuditloggerPlugin.plugin.initializeMetrics(+ solrMetricsContext, Attributes.empty(), "/auditlogging");} else {log.debug("Security conf doesn't exist. Skipping setup for audit logging module.");}@@ -581,7 +584,9 @@ public class CoreContainer {if (authenticationPlugin != null) {authenticationPlugin.plugin.init(authenticationConfig);setupHttpClientForAuthPlugin(authenticationPlugin.plugin);- authenticationPlugin.plugin.initializeMetrics(solrMetricsContext, "/authentication");+ // TODO SOLR-17458: Add Otel+ authenticationPlugin.plugin.initializeMetrics(+ solrMetricsContext, Attributes.empty(), "/authentication");}this.authenticationPlugin = authenticationPlugin;try {@@ -776,13 +781,16 @@ public class CoreContainer {shardHandlerFactory =ShardHandlerFactory.newInstance(cfg.getShardHandlerFactoryPluginInfo(), loader);if (shardHandlerFactory instanceof SolrMetricProducer metricProducer) {- metricProducer.initializeMetrics(solrMetricsContext, "httpShardHandler");+ // TODO SOLR-17458: Add Otel+ metricProducer.initializeMetrics(solrMetricsContext, Attributes.empty(), "httpShardHandler");}updateShardHandler = new UpdateShardHandler(cfg.getUpdateShardHandlerConfig());solrClientProvider =new HttpSolrClientProvider(cfg.getUpdateShardHandlerConfig(), solrMetricsContext);- updateShardHandler.initializeMetrics(solrMetricsContext, "updateShardHandler");+ // TODO SOLR-17458: Add Otel+ updateShardHandler.initializeMetrics(+ solrMetricsContext, Attributes.empty(), "updateShardHandler");solrClientCache = new SolrClientCache(solrClientProvider.getSolrClient());Map<String, CacheConfig> cachesConfig = cfg.getCachesConfig();@@ -793,7 +801,8 @@ public class CoreContainer {for (Map.Entry<String, CacheConfig> e : cachesConfig.entrySet()) {SolrCache<?, ?> c = e.getValue().newInstance();String cacheName = e.getKey();- c.initializeMetrics(solrMetricsContext, "nodeLevelCache/" + cacheName);+ // TODO SOLR-17458: Add Otel+ c.initializeMetrics(solrMetricsContext, Attributes.empty(), "nodeLevelCache/" + cacheName);m.put(cacheName, c);}this.caches = Collections.unmodifiableMap(m);@@ -807,13 +816,18 @@ public class CoreContainer {if (isZooKeeperAware()) {solrClientCache.setDefaultZKHost(getZkController().getZkServerAddress());// initialize ZkClient metrics- zkSys.getZkMetricsProducer().initializeMetrics(solrMetricsContext, "zkClient");+ // TODO SOLR-17458: Add Otel+ zkSys+ .getZkMetricsProducer()+ .initializeMetrics(solrMetricsContext, Attributes.empty(), "zkClient");pkiAuthenticationSecurityBuilder =new PKIAuthenticationPlugin(this,zkSys.getZkController().getNodeName(),(PublicKeyHandler) containerHandlers.get(PublicKeyHandler.PATH));- pkiAuthenticationSecurityBuilder.initializeMetrics(solrMetricsContext, "/authentication/pki");+ // TODO SOLR-17458: Add Otel+ pkiAuthenticationSecurityBuilder.initializeMetrics(+ solrMetricsContext, Attributes.empty(), "/authentication/pki");fileStore = new DistribFileStore(this);registerV2ApiIfEnabled(ClusterFileStore.class);@@ -877,10 +891,12 @@ public class CoreContainer {metricsHandler = new MetricsHandler(this);containerHandlers.put(METRICS_PATH, metricsHandler);- metricsHandler.initializeMetrics(solrMetricsContext, METRICS_PATH);+ // TODO SOLR-17458: Add Otel+ metricsHandler.initializeMetrics(solrMetricsContext, Attributes.empty(), METRICS_PATH);containerHandlers.put(AUTHZ_PATH, securityConfHandler);- securityConfHandler.initializeMetrics(solrMetricsContext, AUTHZ_PATH);+ // TODO SOLR-17458: Add Otel+ securityConfHandler.initializeMetrics(solrMetricsContext, Attributes.empty(), AUTHZ_PATH);containerHandlers.put(AUTHC_PATH, securityConfHandler);PluginInfo[] metricReporters = cfg.getMetricsConfig().getMetricReporters();@@ -1002,7 +1018,8 @@ public class CoreContainer {"version");SolrFieldCacheBean fieldCacheBean = new SolrFieldCacheBean();- fieldCacheBean.initializeMetrics(solrMetricsContext, null);+ // TODO SOLR-17458: Otel migration+ fieldCacheBean.initializeMetrics(solrMetricsContext, Attributes.empty(), "");if (isZooKeeperAware()) {metricManager.loadClusterReporters(metricReporters, this);@@ -2328,7 +2345,9 @@ public class CoreContainer {containerHandlers.put(path, (SolrRequestHandler) handler);}if (handler instanceof SolrMetricProducer) {- ((SolrMetricProducer) handler).initializeMetrics(solrMetricsContext, path);+ ((SolrMetricProducer) handler)+ // TODO SOLR-17458: Add Otel+ .initializeMetrics(solrMetricsContext, Attributes.empty(), path);}return handler;}diff --git a/solr/core/src/java/org/apache/solr/core/HttpSolrClientProvider.java b/solr/core/src/java/org/apache/solr/core/HttpSolrClientProvider.javaindex 2bf25a896f6..68207965e48 100644--- a/solr/core/src/java/org/apache/solr/core/HttpSolrClientProvider.java+++ b/solr/core/src/java/org/apache/solr/core/HttpSolrClientProvider.java@@ -16,6 +16,7 @@*/package org.apache.solr.core;+import io.opentelemetry.api.common.Attributes;import java.util.List;import java.util.concurrent.TimeUnit;import org.apache.solr.client.solrj.impl.Http2SolrClient;@@ -68,7 +69,8 @@ final class HttpSolrClientProvider implements AutoCloseable {var solrMetricsContext = parentContext.getChildContext(this);String expandedScope =SolrMetricManager.mkName(METRIC_SCOPE_NAME, SolrInfoBean.Category.HTTP.name());- trackHttpSolrMetrics.initializeMetrics(solrMetricsContext, expandedScope);+ // TODO SOLR-17458: Add Otel+ trackHttpSolrMetrics.initializeMetrics(solrMetricsContext, Attributes.empty(), expandedScope);}Http2SolrClient getSolrClient() {diff --git a/solr/core/src/java/org/apache/solr/core/OpenTelemetryConfigurator.java b/solr/core/src/java/org/apache/solr/core/OpenTelemetryConfigurator.javaindex 90170866dfe..47547b4950f 100644--- a/solr/core/src/java/org/apache/solr/core/OpenTelemetryConfigurator.java+++ b/solr/core/src/java/org/apache/solr/core/OpenTelemetryConfigurator.java@@ -17,15 +17,16 @@package org.apache.solr.core;+import com.google.common.annotations.VisibleForTesting;import io.opentelemetry.api.GlobalOpenTelemetry;import io.opentelemetry.api.trace.Tracer;import io.opentelemetry.context.Context;import io.opentelemetry.context.Scope;import io.opentelemetry.context.propagation.ContextPropagators;-import io.opentelemetry.exporter.prometheus.PrometheusMetricReader;import io.opentelemetry.sdk.OpenTelemetrySdk;import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;import io.opentelemetry.sdk.metrics.SdkMeterProvider;+import io.opentelemetry.sdk.metrics.export.MetricReader;import io.opentelemetry.sdk.trace.SdkTracerProvider;import io.opentelemetry.sdk.trace.samplers.Sampler;import java.lang.invoke.MethodHandles;@@ -61,7 +62,7 @@ public abstract class OpenTelemetryConfigurator implements NamedListInitializedP* SDK.*/public static synchronized void initializeOpenTelemetrySdk(- NodeConfig cfg, SolrResourceLoader loader, PrometheusMetricReader prometheusMetricReader) {+ NodeConfig cfg, SolrResourceLoader loader, MetricReader metricReader) {PluginInfo info = (cfg != null) ? cfg.getTracerConfiguratorPluginInfo() : null;if (info != null && info.isEnabled()) {@@ -72,7 +73,7 @@ public abstract class OpenTelemetryConfigurator implements NamedListInitializedP} else {// Initializing sampler as always off to replicate no-op Tracer providerOpenTelemetryConfigurator.configureOpenTelemetrySdk(- SdkMeterProvider.builder().registerMetricReader(prometheusMetricReader).build(),+ SdkMeterProvider.builder().registerMetricReader(metricReader).build(),SdkTracerProvider.builder().setSampler(Sampler.alwaysOff()).build());}}@@ -185,4 +186,10 @@ public abstract class OpenTelemetryConfigurator implements NamedListInitializedPprotected static String envNameToSyspropName(String envName) {return envName.toLowerCase(Locale.ROOT).replace("_", ".");}++ @VisibleForTesting+ public static void resetForTest() {+ loaded = false;+ GlobalOpenTelemetry.resetForTest();+ }}diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.javaindex dbfe8f7ee0a..34d0fbdc4a3 100644--- a/solr/core/src/java/org/apache/solr/core/SolrCore.java+++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java@@ -23,6 +23,7 @@ import com.codahale.metrics.Counter;import com.codahale.metrics.Timer;import com.github.benmanes.caffeine.cache.Cache;import com.github.benmanes.caffeine.cache.Caffeine;+import io.opentelemetry.api.common.Attributes;import java.io.Closeable;import java.io.FileNotFoundException;import java.io.IOException;@@ -1104,7 +1105,8 @@ public class SolrCore implements SolrInfoBean, Closeable {setLatestSchema(schema);// initialize core metrics- initializeMetrics(solrMetricsContext, null);+ // TODO SOLR-17458: Add Otel+ initializeMetrics(solrMetricsContext, Attributes.empty(), "");// init pluggable circuit breakers, after metrics because some circuit breakers use metricsinitPlugins(null, CircuitBreaker.class);@@ -1112,7 +1114,8 @@ public class SolrCore implements SolrInfoBean, Closeable {SolrFieldCacheBean solrFieldCacheBean = new SolrFieldCacheBean();// this is registered at the CONTAINER level because it's not core-specific - for now we// also register it here for back-compat- solrFieldCacheBean.initializeMetrics(solrMetricsContext, "core");+ // TODO SOLR-17458: Add Otel+ solrFieldCacheBean.initializeMetrics(solrMetricsContext, Attributes.empty(), "core");infoRegistry.put("fieldCache", solrFieldCacheBean);this.maxWarmingSearchers = solrConfig.maxWarmingSearchers;@@ -1189,7 +1192,8 @@ public class SolrCore implements SolrInfoBean, Closeable {// Allow the directory factory to report metricsif (directoryFactory instanceof SolrMetricProducer) {((SolrMetricProducer) directoryFactory)- .initializeMetrics(solrMetricsContext, "directoryFactory");+ // TODO SOLR-17458: Add Otel+ .initializeMetrics(solrMetricsContext, Attributes.empty(), "directoryFactory");}bufferUpdatesIfConstructing(coreDescriptor);@@ -1301,8 +1305,10 @@ public class SolrCore implements SolrInfoBean, Closeable {return coreMetricManager;}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {newSearcherCounter = parentContext.counter("new", Category.SEARCHER.toString());newSearcherTimer = parentContext.timer("time", Category.SEARCHER.toString(), "new");newSearcherWarmupTimer = parentContext.timer("warmup", Category.SEARCHER.toString(), "new");diff --git a/solr/core/src/java/org/apache/solr/core/ZkContainer.java b/solr/core/src/java/org/apache/solr/core/ZkContainer.javaindex b5fdc3c5718..c6a1b79d283 100644--- a/solr/core/src/java/org/apache/solr/core/ZkContainer.java+++ b/solr/core/src/java/org/apache/solr/core/ZkContainer.java@@ -19,6 +19,7 @@ package org.apache.solr.core;import static org.apache.solr.common.cloud.ZkStateReader.HTTPS;import static org.apache.solr.common.cloud.ZkStateReader.HTTPS_PORT_PROP;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.nio.file.Path;@@ -145,8 +146,10 @@ public class ZkContainer {new SolrMetricProducer() {SolrMetricsContext ctx;+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {ctx = parentContext.getChildContext(this);ctx.gauge(metricsMap, true, scope, null, SolrInfoBean.Category.CONTAINER.toString());diff --git a/solr/core/src/java/org/apache/solr/handler/ExportHandler.java b/solr/core/src/java/org/apache/solr/handler/ExportHandler.javaindex 8fa1ba64e38..6a83eb78783 100644--- a/solr/core/src/java/org/apache/solr/handler/ExportHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/ExportHandler.java@@ -19,6 +19,7 @@ package org.apache.solr.handler;import static org.apache.solr.common.params.CommonParams.JSON;+import io.opentelemetry.api.common.Attributes;import java.lang.invoke.MethodHandles;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;@@ -84,9 +85,11 @@ public class ExportHandler extends SearchHandler {}}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {- super.initializeMetrics(parentContext, scope);+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ super.initializeMetrics(parentContext, attributes, scope);this.writerMetricsPath = SolrMetricManager.mkName("writer", getCategory().toString(), scope);}diff --git a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.javaindex 8367e9166f9..d3aa17b7b12 100644--- a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java@@ -30,6 +30,7 @@ import static org.apache.solr.handler.admin.api.ReplicationAPIBase.OFFSET;import static org.apache.solr.handler.admin.api.ReplicationAPIBase.STATUS;import static org.apache.solr.handler.admin.api.ReplicationAPIBase.TLOG_FILE;+import io.opentelemetry.api.common.Attributes;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;@@ -836,10 +837,11 @@ public class ReplicationHandler extends RequestHandlerBase}}- // TODO: Handle compatibility in 8.x+ // NOCOMMIT SOLR-17458: Update with OTEL@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {- super.initializeMetrics(parentContext, scope);+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ super.initializeMetrics(parentContext, attributes, scope);solrMetricsContext.gauge(() ->(core != null && !core.isClosed()diff --git a/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java b/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.javaindex bc5331ef556..e633a91113d 100644--- a/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java+++ b/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java@@ -23,6 +23,8 @@ import com.codahale.metrics.Counter;import com.codahale.metrics.Meter;import com.codahale.metrics.Timer;import java.io.IOException;+import io.opentelemetry.api.common.AttributeKey;+import io.opentelemetry.api.common.Attributes;import java.lang.invoke.MethodHandles;import java.util.Collection;import java.util.Collections;@@ -45,6 +47,8 @@ import org.apache.solr.metrics.SolrDelegateRegistryMetricsContext;import org.apache.solr.metrics.SolrMetricManager;import org.apache.solr.metrics.SolrMetricProducer;import org.apache.solr.metrics.SolrMetricsContext;+import org.apache.solr.metrics.otel.instruments.AttributedLongCounter;+import org.apache.solr.metrics.otel.instruments.AttributedLongTimer;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.response.SolrQueryResponse;@@ -162,7 +166,8 @@ public abstract class RequestHandlerBase}@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {if (aggregateNodeLevelMetricsEnabled) {this.solrMetricsContext =new SolrDelegateRegistryMetricsContext(@@ -173,7 +178,10 @@ public abstract class RequestHandlerBase} else {this.solrMetricsContext = parentContext.getChildContext(this);}- metrics = new HandlerMetrics(solrMetricsContext, getCategory().toString(), scope);++ metrics = new HandlerMetrics(solrMetricsContext, attributes, getCategory().toString(), scope);++ // NOCOMMIT: I don't see value in this metricsolrMetricsContext.gauge(() -> handlerStart, true, "handlerStart", getCategory().toString(), scope);}@@ -186,7 +194,8 @@ public abstract class RequestHandlerBasenew SolrMetricManager(null, new MetricsConfig.MetricsConfigBuilder().setEnabled(false).build(), null),"NO_OP",- "NO_OP"));+ "NO_OP"),+ Attributes.empty());public final Meter numErrors;public final Meter numServerErrors;@@ -196,7 +205,16 @@ public abstract class RequestHandlerBasepublic final Timer requestTimes;public final Counter totalTime;- public HandlerMetrics(SolrMetricsContext solrMetricsContext, String... metricPath) {+ public AttributedLongCounter otelRequests;+ public AttributedLongCounter otelNumServerErrors;+ public AttributedLongCounter otelNumClientErrors;+ public AttributedLongCounter otelNumTimeouts;+ public AttributedLongTimer otelRequestTimes;++ public HandlerMetrics(+ SolrMetricsContext solrMetricsContext, Attributes attributes, String... metricPath) {++ // NOCOMMIT SOLR-17458: To be removednumErrors = solrMetricsContext.meter("errors", metricPath);numServerErrors = solrMetricsContext.meter("serverErrors", metricPath);numClientErrors = solrMetricsContext.meter("clientErrors", metricPath);@@ -204,6 +222,43 @@ public abstract class RequestHandlerBaserequests = solrMetricsContext.counter("requests", metricPath);requestTimes = solrMetricsContext.timer("requestTimes", metricPath);totalTime = solrMetricsContext.counter("totalTime", metricPath);++ var baseRequestMetric =+ solrMetricsContext.longCounter("solr_metrics_core_requests", "HTTP Solr request counts");++ var baseRequestTimeMetric =+ solrMetricsContext.longHistogram(+ "solr_metrics_core_requests_times", "HTTP Solr request times", "ms");++ otelRequests =+ new AttributedLongCounter(+ baseRequestMetric,+ Attributes.builder().putAll(attributes).put(TYPE_ATTR, "requests").build());++ otelNumServerErrors =+ new AttributedLongCounter(+ baseRequestMetric,+ Attributes.builder()+ .putAll(attributes)+ .put(AttributeKey.stringKey("source"), "server")+ .put(TYPE_ATTR, "errors")+ .build());++ otelNumClientErrors =+ new AttributedLongCounter(+ baseRequestMetric,+ Attributes.builder()+ .putAll(attributes)+ .put(AttributeKey.stringKey("source"), "client")+ .put(TYPE_ATTR, "errors")+ .build());++ otelNumTimeouts =+ new AttributedLongCounter(+ baseRequestMetric,+ Attributes.builder().putAll(attributes).put(TYPE_ATTR, "timeouts").build());++ otelRequestTimes = new AttributedLongTimer(baseRequestTimeMetric, attributes);}}@@ -227,10 +282,13 @@ public abstract class RequestHandlerBaseif (publishCpuTime) {ThreadCpuTimer.beginContext(REQUEST_CPU_TIMER_CONTEXT);}+HandlerMetrics metrics = getMetricsForThisRequest(req);metrics.requests.inc();+ metrics.otelRequests.inc();Timer.Context timer = metrics.requestTimes.time();+ AttributedLongTimer.MetricTimer otelTimer = metrics.otelRequestTimes.start();try {TestInjection.injectLeaderTragedy(req.getCore());if (pluginInfo != null && pluginInfo.attributes.containsKey(USEPARAM))@@ -243,6 +301,7 @@ public abstract class RequestHandlerBaseif (!haveCompleteResults(rsp.getResponseHeader())) {metrics.numTimeouts.mark();+ metrics.otelNumTimeouts.inc();rsp.setHttpCaching(false);}} catch (QueryLimitsExceededException e) {@@ -255,6 +314,7 @@ public abstract class RequestHandlerBasetry {long elapsed = timer.stop();metrics.totalTime.inc(elapsed);+ otelTimer.stop();if (publishCpuTime) {Optional<Long> cpuTime = ThreadCpuTimer.readMSandReset(REQUEST_CPU_TIMER_CONTEXT);@@ -300,9 +360,11 @@ public abstract class RequestHandlerBaseif (isClientError) {log.error("Client exception", e);metrics.numClientErrors.mark();+ metrics.otelNumClientErrors.inc();} else {log.error("Server exception", e);metrics.numServerErrors.mark();+ metrics.otelNumServerErrors.inc();}}diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.javaindex 86c68759774..b0237978593 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java@@ -25,6 +25,7 @@ import com.github.benmanes.caffeine.cache.Cache;import com.github.benmanes.caffeine.cache.Caffeine;import com.github.benmanes.caffeine.cache.Expiry;import com.github.benmanes.caffeine.cache.Ticker;+import io.opentelemetry.api.common.Attributes;import java.lang.invoke.MethodHandles;import java.util.ArrayList;import java.util.Collection;@@ -127,9 +128,11 @@ public class CoreAdminHandler extends RequestHandlerBase implements PermissionNa+ "it is a special Handler configured directly by the RequestDispatcher");}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {- super.initializeMetrics(parentContext, scope);+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ super.initializeMetrics(parentContext, attributes, scope);coreAdminAsyncTracker.standardExecutor =MetricUtils.instrumentedExecutorService(coreAdminAsyncTracker.standardExecutor,diff --git a/solr/core/src/java/org/apache/solr/handler/admin/MetricsHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/MetricsHandler.javaindex 4bba3414be9..f2b501b8a41 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/MetricsHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/MetricsHandler.java@@ -51,7 +51,6 @@ import org.apache.solr.handler.RequestHandlerBase;import org.apache.solr.metrics.SolrMetricManager;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrRequestInfo;-import org.apache.solr.response.PrometheusResponseWriter;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.security.AuthorizationContext;import org.apache.solr.security.PermissionNameProvider;@@ -127,11 +126,12 @@ public class MetricsHandler extends RequestHandlerBase implements PermissionNamereturn;}+ // TODO SOLR-17458: Make this the default option after dropwizard removalif (PROMETHEUS_METRICS_WT.equals(params.get(CommonParams.WT))) {- response = handlePrometheusExport(params);- consumer.accept("metrics", response);+ consumer.accept("metrics", metricManager.getMetricReader().collect());return;}+String[] keys = params.getParams(KEY_PARAM);if (keys != null && keys.length > 0) {handleKeyRequest(keys, consumer);@@ -179,59 +179,6 @@ public class MetricsHandler extends RequestHandlerBase implements PermissionNamereturn response;}- private NamedList<Object> handlePrometheusExport(SolrParams params) {- NamedList<Object> response = new SimpleOrderedMap<>();- MetricFilter mustMatchFilter = parseMustMatchFilter(params);- Predicate<CharSequence> propertyFilter = parsePropertyFilter(params);- List<MetricType> metricTypes = parseMetricTypes(params);- List<MetricFilter> metricFilters =- metricTypes.stream().map(MetricType::asMetricFilter).collect(Collectors.toList());-- Set<String> requestedRegistries = parseRegistries(params);- MetricRegistry mergedCoreRegistries = new MetricRegistry();-- for (String registryName : requestedRegistries) {- MetricRegistry dropwizardRegistry = metricManager.registry(registryName);-- // Merge all core registries into a single registry and- // append the core name to the metric to avoid duplicate metrics name- if (registryName.startsWith("solr.core")) {- mergedCoreRegistries.registerAll(getCoreNameFromRegistry(registryName), dropwizardRegistry);- continue;- }-- PrometheusResponseWriter.toPrometheus(- dropwizardRegistry,- registryName,- metricFilters,- mustMatchFilter,- propertyFilter,- false,- false,- true,- (registry) -> {- response.add(registryName, registry);- });- }-- if (!mergedCoreRegistries.getMetrics().isEmpty()) {- PrometheusResponseWriter.toPrometheus(- mergedCoreRegistries,- "solr.core",- metricFilters,- mustMatchFilter,- propertyFilter,- false,- false,- true,- (registry) -> {- response.add("solr.core", registry);- });- }-- return response;- }-private static class MetricsExpr {Pattern registryRegex;MetricFilter metricFilter;diff --git a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandlerFactory.java b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandlerFactory.javaindex abb5f31b398..dd3630ea203 100644--- a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandlerFactory.java+++ b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandlerFactory.java@@ -18,6 +18,7 @@ package org.apache.solr.handler.component;import static org.apache.solr.util.stats.InstrumentedHttpListenerFactory.KNOWN_METRIC_NAME_STRATEGIES;+import io.opentelemetry.api.common.Attributes;import java.lang.invoke.MethodHandles;import java.util.Iterator;import java.util.List;@@ -439,10 +440,12 @@ public class HttpShardHandlerFactory extends ShardHandlerFactory}@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {solrMetricsContext = parentContext.getChildContext(this);String expandedScope = SolrMetricManager.mkName(scope, SolrInfoBean.Category.QUERY.name());- httpListenerFactory.initializeMetrics(solrMetricsContext, expandedScope);+ // TODO SOLR-17458: Add Otel+ httpListenerFactory.initializeMetrics(solrMetricsContext, Attributes.empty(), expandedScope);commExecutor =MetricUtils.instrumentedExecutorService(commExecutor,diff --git a/solr/core/src/java/org/apache/solr/handler/component/SearchComponent.java b/solr/core/src/java/org/apache/solr/handler/component/SearchComponent.javaindex 913078c27ee..de041a7220e 100644--- a/solr/core/src/java/org/apache/solr/handler/component/SearchComponent.java+++ b/solr/core/src/java/org/apache/solr/handler/component/SearchComponent.java@@ -16,6 +16,7 @@*/package org.apache.solr.handler.component;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.util.Map;import org.apache.solr.core.SolrInfoBean;@@ -103,8 +104,10 @@ public abstract class SearchComponent implements SolrInfoBean, NamedListInitialireturn solrMetricsContext;}+ // TODO SOLR-17458: Update with Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {// By default don't register any metrics - but prepare a child contextthis.solrMetricsContext = parentContext.getChildContext(this);}diff --git a/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java b/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.javaindex ed2e71e632a..fdbf1a37f3b 100644--- a/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java@@ -28,6 +28,8 @@ import static org.apache.solr.request.SolrRequestInfo.getQueryLimits;import static org.apache.solr.response.SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY;import com.codahale.metrics.Counter;+import io.opentelemetry.api.common.AttributeKey;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;@@ -64,7 +66,6 @@ import org.apache.solr.core.PluginInfo;import org.apache.solr.core.SolrCore;import org.apache.solr.handler.RequestHandlerBase;import org.apache.solr.logging.MDCLoggingContext;-import org.apache.solr.metrics.MetricsMap;import org.apache.solr.metrics.SolrMetricsContext;import org.apache.solr.pkg.PackageAPI;import org.apache.solr.pkg.PackageListeners;@@ -154,18 +155,28 @@ public class SearchHandler extends RequestHandlerBase}}+ // TODO SOLR-17458: Fix metric Attributes@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {- super.initializeMetrics(parentContext, scope);+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ super.initializeMetrics(+ parentContext,+ Attributes.builder()+ .putAll(attributes)+ .put(AttributeKey.stringKey("category"), getCategory().toString())+ .put(AttributeKey.stringKey("internal"), "false")+ .build(),+ scope);metricsShard =new HandlerMetrics( // will register various metrics in the context- solrMetricsContext, getCategory().toString(), scope + SHARD_HANDLER_SUFFIX);- solrMetricsContext.gauge(- new MetricsMap(map -> shardPurposes.forEach((k, v) -> map.putNoEx(k, v.getCount()))),- true,- "purposes",- getCategory().toString(),- scope + SHARD_HANDLER_SUFFIX);+ solrMetricsContext,+ Attributes.builder()+ .putAll(attributes)+ .put(AttributeKey.stringKey("category"), getCategory().toString())+ .put(AttributeKey.stringKey("internal"), "true")+ .build(),+ getCategory().toString(),+ scope + SHARD_HANDLER_SUFFIX);}@Overridediff --git a/solr/core/src/java/org/apache/solr/handler/component/SuggestComponent.java b/solr/core/src/java/org/apache/solr/handler/component/SuggestComponent.javaindex 60b4871c060..f32934451ac 100644--- a/solr/core/src/java/org/apache/solr/handler/component/SuggestComponent.java+++ b/solr/core/src/java/org/apache/solr/handler/component/SuggestComponent.java@@ -16,6 +16,7 @@*/package org.apache.solr.handler.component;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.nio.file.Files;@@ -381,9 +382,11 @@ public class SuggestComponent extends SearchComponentreturn "Suggester component";}+ // TODO SOLR-17458: Migrate to OTEL@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {- super.initializeMetrics(parentContext, scope);+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ super.initializeMetrics(parentContext, attributes, scope);this.solrMetricsContext.gauge(() -> ramBytesUsed(), true, "totalSizeInBytes", getCategory().toString());diff --git a/solr/core/src/java/org/apache/solr/highlight/HighlightingPluginBase.java b/solr/core/src/java/org/apache/solr/highlight/HighlightingPluginBase.javaindex 7028c8c8e8e..c0e45ef582f 100644--- a/solr/core/src/java/org/apache/solr/highlight/HighlightingPluginBase.java+++ b/solr/core/src/java/org/apache/solr/highlight/HighlightingPluginBase.java@@ -17,6 +17,7 @@package org.apache.solr.highlight;import com.codahale.metrics.Counter;+import io.opentelemetry.api.common.Attributes;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import org.apache.solr.common.params.SolrParams;@@ -62,8 +63,10 @@ public abstract class HighlightingPluginBase implements SolrInfoBean {return solrMetricsContext;}+ // TODO SOLR-17458: Migrate to OTEL@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {solrMetricsContext = parentContext.getChildContext(this);numRequests = solrMetricsContext.counter("requests", getCategory().toString(), scope);}diff --git a/solr/core/src/java/org/apache/solr/jersey/RequestMetricHandling.java b/solr/core/src/java/org/apache/solr/jersey/RequestMetricHandling.javaindex 830c78d38b0..6cea50d03d2 100644--- a/solr/core/src/java/org/apache/solr/jersey/RequestMetricHandling.java+++ b/solr/core/src/java/org/apache/solr/jersey/RequestMetricHandling.java@@ -33,6 +33,7 @@ import java.lang.invoke.MethodHandles;import org.apache.solr.client.api.model.SolrJerseyResponse;import org.apache.solr.core.PluginBag;import org.apache.solr.handler.RequestHandlerBase;+import org.apache.solr.metrics.otel.instruments.AttributedLongTimer;import org.apache.solr.request.SolrQueryRequest;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -89,9 +90,12 @@ public class RequestMetricHandling {final RequestHandlerBase.HandlerMetrics metrics =handlerBase.getMetricsForThisRequest(solrQueryRequest);- requestContext.setProperty(HANDLER_METRICS, metrics);- requestContext.setProperty(TIMER, metrics.requestTimes.time());+ // TODO SOLR-17458: Recording both Otel and dropwizard for now. Will remove Dropwizard after+ // removalmetrics.requests.inc();+ requestContext.setProperty(TIMER, metrics.otelRequestTimes.start());+ requestContext.setProperty("dropwizardTimer", metrics.requestTimes.time());+ requestContext.setProperty(HANDLER_METRICS, metrics);}}@@ -117,13 +121,16 @@ public class RequestMetricHandling {final SolrJerseyResponse response = (SolrJerseyResponse) responseContext.getEntity();if (Boolean.TRUE.equals(response.responseHeader.partialResults)) {metrics.numTimeouts.mark();+ metrics.otelNumTimeouts.inc();}} else {log.debug("Skipping partialResults check because entity was not SolrJerseyResponse");}+ final var dropwizardTimer = (Timer.Context) requestContext.getProperty("dropwizardTimer");+ metrics.totalTime.inc(dropwizardTimer.stop());- final Timer.Context timer = (Timer.Context) requestContext.getProperty(TIMER);- metrics.totalTime.inc(timer.stop());+ final var timer = (AttributedLongTimer.MetricTimer) requestContext.getProperty(TIMER);+ timer.stop();}}}diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrCoreMetricManager.java b/solr/core/src/java/org/apache/solr/metrics/SolrCoreMetricManager.javaindex a1c5f92fd5f..62a342180f2 100644--- a/solr/core/src/java/org/apache/solr/metrics/SolrCoreMetricManager.java+++ b/solr/core/src/java/org/apache/solr/metrics/SolrCoreMetricManager.java@@ -17,6 +17,8 @@package org.apache.solr.metrics;import com.codahale.metrics.MetricRegistry;+import io.opentelemetry.api.common.AttributeKey;+import io.opentelemetry.api.common.Attributes;import java.io.Closeable;import java.io.IOException;import org.apache.solr.cloud.CloudDescriptor;@@ -33,6 +35,13 @@ import org.apache.solr.core.SolrInfoBean;*/public class SolrCoreMetricManager implements Closeable {+ public static final AttributeKey<String> COLLECTION_ATTR = AttributeKey.stringKey("collection");+ public static final AttributeKey<String> CORE_ATTR = AttributeKey.stringKey("core");+ public static final AttributeKey<String> SHARD_ATTR = AttributeKey.stringKey("shard");+ public static final AttributeKey<String> REPLICA_ATTR = AttributeKey.stringKey("replica");+ public static final AttributeKey<String> HANDLER_ATTR = AttributeKey.stringKey("handler");+ public static final AttributeKey<String> SCOPE_ATTR = AttributeKey.stringKey("scope");+private final SolrCore core;private SolrMetricsContext solrMetricsContext;private SolrMetricManager metricManager;@@ -135,8 +144,19 @@ public class SolrCoreMetricManager implements Closeable {+ ", producer = "+ producer);}++ // NOCOMMIT SOLR-17458: These attributes may not work for standalone mode// use deprecated method for back-compat, remove in 9.0- producer.initializeMetrics(solrMetricsContext, scope);+ producer.initializeMetrics(+ solrMetricsContext,+ Attributes.builder()+ .put(CORE_ATTR, core.getCoreDescriptor().getName())+ .put(COLLECTION_ATTR, collectionName)+ .put(SHARD_ATTR, shardName)+ .put(REPLICA_ATTR, replicaName)+ .put((scope.startsWith("/")) ? HANDLER_ATTR : SCOPE_ATTR, scope)+ .build(),+ scope);}/** Return the registry used by this SolrCore. */diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java b/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.javaindex b8700d3a412..bcefc0b3ddc 100644--- a/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java+++ b/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java@@ -26,7 +26,30 @@ import com.codahale.metrics.MetricRegistry;import com.codahale.metrics.MetricSet;import com.codahale.metrics.SharedMetricRegistries;import com.codahale.metrics.Timer;+import io.opentelemetry.api.metrics.DoubleCounter;+import io.opentelemetry.api.metrics.DoubleCounterBuilder;+import io.opentelemetry.api.metrics.DoubleGauge;+import io.opentelemetry.api.metrics.DoubleGaugeBuilder;+import io.opentelemetry.api.metrics.DoubleHistogram;+import io.opentelemetry.api.metrics.DoubleHistogramBuilder;+import io.opentelemetry.api.metrics.DoubleUpDownCounter;+import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder;+import io.opentelemetry.api.metrics.LongCounter;+import io.opentelemetry.api.metrics.LongCounterBuilder;+import io.opentelemetry.api.metrics.LongGauge;+import io.opentelemetry.api.metrics.LongGaugeBuilder;+import io.opentelemetry.api.metrics.LongHistogram;+import io.opentelemetry.api.metrics.LongHistogramBuilder;+import io.opentelemetry.api.metrics.LongUpDownCounter;+import io.opentelemetry.api.metrics.LongUpDownCounterBuilder;import io.opentelemetry.api.metrics.MeterProvider;+import io.opentelemetry.api.metrics.ObservableDoubleCounter;+import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;+import io.opentelemetry.api.metrics.ObservableDoubleUpDownCounter;+import io.opentelemetry.api.metrics.ObservableLongCounter;+import io.opentelemetry.api.metrics.ObservableLongGauge;+import io.opentelemetry.api.metrics.ObservableLongMeasurement;+import io.opentelemetry.api.metrics.ObservableLongUpDownCounter;import io.opentelemetry.exporter.prometheus.PrometheusMetricReader;import java.io.IOException;import java.lang.invoke.MethodHandles;@@ -46,6 +69,7 @@ import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;+import java.util.function.Consumer;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;import java.util.stream.Collectors;@@ -124,7 +148,8 @@ public class SolrMetricManager {private final MetricRegistry.MetricSupplier<Histogram> histogramSupplier;private final MeterProvider meterProvider;- private final PrometheusMetricReader prometheusMetricReader;+ private final Map<String, io.opentelemetry.api.metrics.Meter> meters = new ConcurrentHashMap<>();+ private final PrometheusMetricReader metricReader;public SolrMetricManager() {metricsConfig = new MetricsConfig.MetricsConfigBuilder().build();@@ -133,15 +158,13 @@ public class SolrMetricManager {timerSupplier = MetricSuppliers.timerSupplier(null, null);histogramSupplier = MetricSuppliers.histogramSupplier(null, null);meterProvider = MetricUtils.getMeterProvider();- prometheusMetricReader = null;+ metricReader = null;}public SolrMetricManager(- SolrResourceLoader loader,- MetricsConfig metricsConfig,- PrometheusMetricReader prometheusMetricReader) {+ SolrResourceLoader loader, MetricsConfig metricsConfig, PrometheusMetricReader metricReader) {this.metricsConfig = metricsConfig;- this.prometheusMetricReader = prometheusMetricReader;+ this.metricReader = metricReader;this.meterProvider = MetricUtils.getMeterProvider();counterSupplier = MetricSuppliers.counterSupplier(loader, metricsConfig.getCounterSupplier());meterSupplier = MetricSuppliers.meterSupplier(loader, metricsConfig.getMeterSupplier());@@ -150,6 +173,167 @@ public class SolrMetricManager {MetricSuppliers.histogramSupplier(loader, metricsConfig.getHistogramSupplier());}+ public LongCounter longCounter(+ String registry, String counterName, String description, String unit) {+ LongCounterBuilder builder =+ meterProvider.get(registry).counterBuilder(counterName).setDescription(description);+ if (unit != null) builder.setUnit(unit);++ return builder.build();+ }++ public LongUpDownCounter longUpDownCounter(+ String registry, String counterName, String description, String unit) {+ LongUpDownCounterBuilder builder =+ meterProvider.get(registry).upDownCounterBuilder(counterName).setDescription(description);+ if (unit != null) builder.setUnit(unit);++ return builder.build();+ }++ public DoubleUpDownCounter doubleUpDownCounter(+ String registry, String counterName, String description, String unit) {+ DoubleUpDownCounterBuilder builder =+ meterProvider+ .get(registry)+ .upDownCounterBuilder(counterName)+ .setDescription(description)+ .ofDoubles();+ if (unit != null) builder.setUnit(unit);++ return builder.build();+ }++ public DoubleCounter doubleCounter(+ String registry, String counterName, String description, String unit) {+ DoubleCounterBuilder builder =+ meterProvider+ .get(registry)+ .counterBuilder(counterName)+ .setDescription(description)+ .ofDoubles();+ if (unit != null) builder.setUnit(unit);++ return builder.build();+ }++ public DoubleHistogram doubleHistogram(+ String registry, String histogramName, String description, String unit) {+ DoubleHistogramBuilder builder =+ meterProvider.get(registry).histogramBuilder(histogramName).setDescription(description);+ if (unit != null) builder.setUnit(unit);++ return builder.build();+ }++ public LongHistogram longHistogram(+ String registry, String histogramName, String description, String unit) {+ LongHistogramBuilder builder =+ meterProvider+ .get(registry)+ .histogramBuilder(histogramName)+ .setDescription(description)+ .ofLongs();++ if (unit != null) builder.setUnit(unit);++ return builder.build();+ }++ public DoubleGauge doubleGauge(+ String registry, String gaugeName, String description, String unit) {+ DoubleGaugeBuilder builder =+ meterProvider.get(registry).gaugeBuilder(gaugeName).setDescription(description);+ if (unit != null) builder.setUnit(unit);++ return builder.build();+ }++ public LongGauge longGauge(String registry, String gaugeName, String description, String unit) {+ LongGaugeBuilder builder =+ meterProvider.get(registry).gaugeBuilder(gaugeName).setDescription(description).ofLongs();+ if (unit != null) builder.setUnit(unit);++ return builder.build();+ }++ public ObservableLongCounter observableLongCounter(+ String registry,+ String counterName,+ String description,+ Consumer<ObservableLongMeasurement> callback,+ String unit) {+ LongCounterBuilder builder =+ meterProvider.get(registry).counterBuilder(counterName).setDescription(description);+ if (unit != null) builder.setUnit(unit);++ return builder.buildWithCallback(callback);+ }++ public ObservableDoubleCounter observableDoubleCounter(+ String registry,+ String counterName,+ String description,+ Consumer<ObservableDoubleMeasurement> callback,+ String unit) {+ DoubleCounterBuilder builder =+ meterProvider+ .get(registry)+ .counterBuilder(counterName)+ .setDescription(description)+ .ofDoubles();+ if (unit != null) builder.setUnit(unit);++ return builder.buildWithCallback(callback);+ }++ public ObservableLongGauge observableLongGauge(+ String registry,+ String gaugeName,+ String description,+ Consumer<ObservableLongMeasurement> callback,+ String unit) {+ LongGaugeBuilder builder =+ meterProvider.get(registry).gaugeBuilder(gaugeName).setDescription(description).ofLongs();+ if (unit != null) builder.setUnit(unit);++ return builder.buildWithCallback(callback);+ }++ public ObservableLongUpDownCounter observableLongUpDownCounter(+ String registry,+ String counterName,+ String description,+ Consumer<ObservableLongMeasurement> callback,+ String unit) {+ LongUpDownCounterBuilder builder =+ meterProvider.get(registry).upDownCounterBuilder(counterName).setDescription(description);+ if (unit != null) builder.setUnit(unit);++ return builder.buildWithCallback(callback);+ }++ public ObservableDoubleUpDownCounter observableDoubleUpDownCounter(+ String registry,+ String counterName,+ String description,+ Consumer<ObservableDoubleMeasurement> callback,+ String unit) {+ DoubleUpDownCounterBuilder builder =+ meterProvider+ .get(registry)+ .upDownCounterBuilder(counterName)+ .setDescription(description)+ .ofDoubles();+ if (unit != null) builder.setUnit(unit);++ return builder.buildWithCallback(callback);+ }++ public io.opentelemetry.api.metrics.Meter getOtelRegistry(String registryName) {+ return meters.get(registryName);+ }+// for unit testspublic MetricRegistry.MetricSupplier<Counter> getCounterSupplier() {return counterSupplier;@@ -407,6 +591,7 @@ public class SolrMetricManager {* @param name registry name* @return true if this name points to a registry that already exists, false otherwise*/+ // TODO SOLR-17458: We may not needpublic boolean hasRegistry(String name) {Set<String> names = registryNames();name = enforcePrefix(name);@@ -420,6 +605,7 @@ public class SolrMetricManager {* with a wildcard use the full registry name starting with {@link #REGISTRY_NAME_PREFIX}* @return set of existing registry names where at least one pattern matched.*/+ // TODO SOLR-17458: We may not need? Maybe make a custom OTEL metric reader insteadpublic Set<String> registryNames(String... patterns) throws PatternSyntaxException {if (patterns == null || patterns.length == 0) {return registryNames();@@ -456,6 +642,7 @@ public class SolrMetricManager {* @param registry already normalized name* @return true if the name matches one of shared registries*/+ // TODO SOLR-17458: We may not needprivate static boolean isSharedRegistry(String registry) {return JETTY_REGISTRY.equals(registry) || JVM_REGISTRY.equals(registry);}@@ -467,6 +654,7 @@ public class SolrMetricManager {* @param registry name of the registry* @return existing or newly created registry*/+ // TODO SOLR-17458: We may not needpublic MetricRegistry registry(String registry) {return registry(registry, true);}@@ -496,6 +684,7 @@ public class SolrMetricManager {}}+ // TODO SOLR-17458: We may not needprivate static MetricRegistry getOrCreateRegistry(ConcurrentMap<String, MetricRegistry> map, String registry) {final MetricRegistry existing = map.get(registry);@@ -517,6 +706,7 @@ public class SolrMetricManager {** @param registry name of the registry to remove*/+ // TODO SOLR-17458: You can't delete OTEL meterspublic void removeRegistry(String registry) {// close any reporters for this registry firstcloseReporters(registry, null);@@ -543,6 +733,7 @@ public class SolrMetricManager {* exist, so the swap operation will only rename the existing registry without creating an* empty one under the previous name.*/+ // NOCOMMIT SOLR-17458: Don't think we needpublic void swapRegistries(String registry1, String registry2) {registry1 = enforcePrefix(registry1);registry2 = enforcePrefix(registry2);@@ -574,6 +765,7 @@ public class SolrMetricManager {* Potential conflict resolution strategies when attempting to register a new metric that already* exists*/+ // TODO SOLR-17458: Don't think we needpublic enum ResolutionStrategy {/*** The existing metric will be kept and the new metric will be ignored. If no metric exists,@@ -596,6 +788,7 @@ public class SolrMetricManager {* @param metricPath (optional) additional top-most metric name path elements* @throws Exception if a metric with this name already exists.*/+ // I don't think we do something like this for OTEL?public void registerAll(String registry, MetricSet metrics, ResolutionStrategy strategy, String... metricPath)throws Exception {@@ -619,6 +812,7 @@ public class SolrMetricManager {** @param registry registry name*/+ // TODO SOLR-17458: Don't think we needpublic void clearRegistry(String registry) {registry(registry).removeMatching(MetricFilter.ALL);}@@ -633,6 +827,8 @@ public class SolrMetricManager {* start with the prefix will be removed.* @return set of metrics names that have been removed.*/+ // TODO SOLR-17458: This is not supported in otel. Metrics are immutable. We can at best filter+ // thempublic Set<String> clearMetrics(String registry, String... metricPath) {PrefixFilter filter;if (metricPath == null || metricPath.length == 0) {@@ -652,6 +848,8 @@ public class SolrMetricManager {* @param metricFilter filter (null is equivalent to {@link MetricFilter#ALL}).* @return map of matching names and metrics*/+ // TODO SOLR-17458: We will create an OTEL metric reader for this instead. For tests, we can+ // create an in-memory metric readerpublic Map<String, Metric> getMetrics(String registry, MetricFilter metricFilter) {if (metricFilter == null || metricFilter == MetricFilter.ALL) {return registry(registry).getMetrics();@@ -670,6 +868,7 @@ public class SolrMetricManager {* @param metricPath (optional) additional top-most metric name path elements* @return existing or a newly created {@link Meter}*/+ // TODO SOLR-17458: Don't need thispublic Meter meter(SolrMetricsContext context, String registry, String metricName, String... metricPath) {final String name = mkName(metricName, metricPath);@@ -688,6 +887,7 @@ public class SolrMetricManager {* @param metricPath (optional) additional top-most metric name path elements* @return existing or a newly created {@link Timer}*/+ // TODO SOLR-17458: Don't need thispublic Timer timer(SolrMetricsContext context, String registry, String metricName, String... metricPath) {final String name = mkName(metricName, metricPath);@@ -706,6 +906,7 @@ public class SolrMetricManager {* @param metricPath (optional) additional top-most metric name path elements* @return existing or a newly created {@link Counter}*/+ // TODO SOLR-17458: Don't need thispublic Counter counter(SolrMetricsContext context, String registry, String metricName, String... metricPath) {final String name = mkName(metricName, metricPath);@@ -724,6 +925,7 @@ public class SolrMetricManager {* @param metricPath (optional) additional top-most metric name path elements* @return existing or a newly created {@link Histogram}*/+ // TODO SOLR-17458: Don't need thispublic Histogram histogram(SolrMetricsContext context, String registry, String metricName, String... metricPath) {final String name = mkName(metricName, metricPath);@@ -737,6 +939,7 @@ public class SolrMetricManager {* @deprecated use {@link #registerMetric(SolrMetricsContext, String, Metric, ResolutionStrategy,* String, String...)}*/+ // TODO SOLR-17458: Don't need this@Deprecatedpublic void registerMetric(SolrMetricsContext context,@@ -764,6 +967,7 @@ public class SolrMetricManager {* notation* @param metricPath (optional) additional top-most metric name path elements*/+ // TODO SOLR-17458: Don't need thispublic void registerMetric(SolrMetricsContext context,String registry,@@ -1398,4 +1602,8 @@ public class SolrMetricManager {public MetricsConfig getMetricsConfig() {return metricsConfig;}++ public PrometheusMetricReader getMetricReader() {+ return this.metricReader;+ }}diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrMetricProducer.java b/solr/core/src/java/org/apache/solr/metrics/SolrMetricProducer.javaindex 335a76cb7b8..6e06f59c338 100644--- a/solr/core/src/java/org/apache/solr/metrics/SolrMetricProducer.java+++ b/solr/core/src/java/org/apache/solr/metrics/SolrMetricProducer.java@@ -16,11 +16,15 @@*/package org.apache.solr.metrics;+import io.opentelemetry.api.common.AttributeKey;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;/** Used by objects that expose metrics through {@link SolrMetricManager}. */public interface SolrMetricProducer extends AutoCloseable {+ public static final AttributeKey<String> TYPE_ATTR = AttributeKey.stringKey("type");+/*** Unique metric tag identifies components with the same life-cycle, which should be registered /* unregistered together. It is in the format of A:B:C, where A is the parent of B is the parent@@ -40,20 +44,18 @@ public interface SolrMetricProducer extends AutoCloseable {}/**- * Initialize metrics specific to this producer.+ * NOCOMMIT SOLR-17458: The Scope parameter will be removed with Dropwizard*- * @param parentContext parent metrics context. If this component has the same life-cycle as the- * parent it can simply use the parent context, otherwise it should obtain a child context- * using {@link SolrMetricsContext#getChildContext(Object)} passing <code>this</code> as the- * child object.- * @param scope component scope+ * <p>{@link Attributes} passed is the base or common set of attributes that should be attached to+ * every metric that will be initialized*/- void initializeMetrics(SolrMetricsContext parentContext, String scope);+ void initializeMetrics(SolrMetricsContext parentContext, Attributes attributes, String scope);/*** Implementations should return the context used in {@link #initializeMetrics(SolrMetricsContext,- * String)} to ensure proper cleanup of metrics at the end of the life-cycle of this component.- * This should be the child context if one was created, or null if the parent context was used.+ * Attributes, String)} to ensure proper cleanup of metrics at the end of the life-cycle of this+ * component. This should be the child context if one was created, or null if the parent context+ * was used.*/SolrMetricsContext getSolrMetricsContext();diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrMetricsContext.java b/solr/core/src/java/org/apache/solr/metrics/SolrMetricsContext.javaindex f98d5c0ba80..269575798ec 100644--- a/solr/core/src/java/org/apache/solr/metrics/SolrMetricsContext.java+++ b/solr/core/src/java/org/apache/solr/metrics/SolrMetricsContext.java@@ -23,9 +23,19 @@ import com.codahale.metrics.Histogram;import com.codahale.metrics.Meter;import com.codahale.metrics.MetricRegistry;import com.codahale.metrics.Timer;+import io.opentelemetry.api.metrics.DoubleCounter;+import io.opentelemetry.api.metrics.DoubleGauge;+import io.opentelemetry.api.metrics.DoubleHistogram;+import io.opentelemetry.api.metrics.LongCounter;+import io.opentelemetry.api.metrics.LongGauge;+import io.opentelemetry.api.metrics.LongHistogram;+import io.opentelemetry.api.metrics.LongUpDownCounter;+import io.opentelemetry.api.metrics.ObservableLongGauge;+import io.opentelemetry.api.metrics.ObservableLongMeasurement;import java.util.Map;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;+import java.util.function.Consumer;import org.apache.solr.util.stats.MetricUtils;/**@@ -48,31 +58,37 @@ public class SolrMetricsContext {}/** See {@link SolrMetricManager#nullNumber()}. */+ // TODO Removepublic Object nullNumber() {return metricManager.nullNumber();}/** See {@link SolrMetricManager#notANumber()}. */+ // TODO Removepublic Object notANumber() {return metricManager.notANumber();}/** See {@link SolrMetricManager#nullString()}. */+ // TODO Removepublic Object nullString() {return metricManager.nullString();}/** See {@link SolrMetricManager#nullObject()}. */+ // TODO Removepublic Object nullObject() {return metricManager.nullObject();}/** Metrics tag that represents objects with the same life-cycle. */+ // TODO Removepublic String getTag() {return tag;}/** Return metric registry name used in this context. */+ // TODO Change this to OTEL Scopepublic String getRegistryName() {return registryName;}@@ -94,6 +110,7 @@ public class SolrMetricsContext {* </code>) of components that register gauge metrics with references to the current object's* instance. Failure to do so may result in hard-to-debug memory leaks.</b>*/+ // TODO don't need thispublic void unregister() {metricManager.unregisterGauges(registryName, tag);}@@ -105,6 +122,7 @@ public class SolrMetricsContext {** @param child child object that produces metrics with a different life-cycle than the parent.*/+ // TODO We shouldn't need child context anymorepublic SolrMetricsContext getChildContext(Object child) {SolrMetricsContext childContext =new SolrMetricsContext(@@ -116,21 +134,94 @@ public class SolrMetricsContext {* Register a metric name that this component reports. This method is called by various metric* registration methods in {@link org.apache.solr.metrics.SolrMetricManager} in order to capture* what metric names are reported from this component (which in turn is called from {@link- * org.apache.solr.metrics.SolrMetricProducer#initializeMetrics(SolrMetricsContext, String)}).+ * SolrMetricProducer#initializeMetrics(SolrMetricsContext,+ * io.opentelemetry.api.common.Attributes, String)}).*/+ // TODO We can continue to register metric namespublic void registerMetricName(String name) {metricNames.add(name);}/** Return a snapshot of metric values that this component reports. */+ // TODO Don't think this is needed anymorepublic Map<String, Object> getMetricsSnapshot() {return MetricUtils.convertMetrics(getMetricRegistry(), metricNames);}+ public LongCounter longCounter(String metricName, String description) {+ return longCounter(metricName, description, null);+ }++ public LongCounter longCounter(String metricName, String description, String unit) {+ return metricManager.longCounter(registryName, metricName, description, unit);+ }++ public LongUpDownCounter longUpDownCounter(String metricName, String description) {+ return longUpDownCounter(metricName, description, null);+ }++ public LongUpDownCounter longUpDownCounter(String metricName, String description, String unit) {+ return metricManager.longUpDownCounter(registryName, metricName, description, unit);+ }++ public DoubleCounter doubleCounter(String metricName, String description) {+ return doubleCounter(metricName, description, null);+ }++ public DoubleCounter doubleCounter(String metricName, String description, String unit) {+ return metricManager.doubleCounter(registryName, metricName, description, unit);+ }++ public DoubleHistogram doubleHistogram(String metricName, String description) {+ return metricManager.doubleHistogram(registryName, metricName, description, null);+ }++ public DoubleHistogram doubleHistogram(String metricName, String description, String unit) {+ return metricManager.doubleHistogram(registryName, metricName, description, unit);+ }++ public LongHistogram longHistogram(String metricName, String description) {+ return metricManager.longHistogram(registryName, metricName, description, null);+ }++ public LongHistogram longHistogram(String metricName, String description, String unit) {+ return metricManager.longHistogram(registryName, metricName, description, unit);+ }++ public LongGauge longGauge(String metricName, String description) {+ return metricManager.longGauge(registryName, metricName, description, null);+ }++ public LongGauge longGauge(String metricName, String description, String unit) {+ return metricManager.longGauge(registryName, metricName, description, unit);+ }++ public DoubleGauge doubleGauge(String metricName, String description) {+ return metricManager.doubleGauge(registryName, metricName, description, null);+ }++ public DoubleGauge doubleGauge(String metricName, String description, String unit) {+ return metricManager.doubleGauge(registryName, metricName, description, unit);+ }++ public ObservableLongGauge observableLongGauge(+ String metricName, String description, Consumer<ObservableLongMeasurement> callback) {+ return metricManager.observableLongGauge(registryName, metricName, description, callback, null);+ }++ public ObservableLongGauge observableLongGauge(+ String metricName,+ String description,+ String unit,+ Consumer<ObservableLongMeasurement> callback) {+ return metricManager.observableLongGauge(registryName, metricName, description, callback, unit);+ }+/*** Convenience method for {@link SolrMetricManager#meter(SolrMetricsContext, String, String,* String...)}.*/+ // TODO Removepublic Meter meter(String metricName, String... metricPath) {return metricManager.meter(this, registryName, metricName, metricPath);}@@ -139,6 +230,7 @@ public class SolrMetricsContext {* Convenience method for {@link SolrMetricManager#counter(SolrMetricsContext, String, String,* String...)}.*/+ // TODO Removepublic Counter counter(String metricName, String... metricPath) {return metricManager.counter(this, registryName, metricName, metricPath);}@@ -147,6 +239,7 @@ public class SolrMetricsContext {* Convenience method for {@link SolrMetricManager#registerGauge(SolrMetricsContext, String,* Gauge, String, SolrMetricManager.ResolutionStrategy, String, String...)}.*/+ // TODO Removepublic void gauge(Gauge<?> gauge, boolean force, String metricName, String... metricPath) {metricManager.registerGauge(this,@@ -164,6 +257,7 @@ public class SolrMetricsContext {* Convenience method for {@link SolrMetricManager#meter(SolrMetricsContext, String, String,* String...)}.*/+ // TODO Removepublic Timer timer(String metricName, String... metricPath) {return metricManager.timer(this, registryName, metricName, metricPath);}@@ -172,6 +266,7 @@ public class SolrMetricsContext {* Convenience method for {@link SolrMetricManager#histogram(SolrMetricsContext, String, String,* String...)}.*/+ // TODO Removepublic Histogram histogram(String metricName, String... metricPath) {return metricManager.histogram(this, registryName, metricName, metricPath);}@@ -179,6 +274,7 @@ public class SolrMetricsContext {/*** Get the {@link MetricRegistry} instance that is used for registering metrics in this context.*/+ // TODO Change this to OTEL Scope?public MetricRegistry getMetricRegistry() {return metricManager.registry(registryName);}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmMetric.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleCounter.javasimilarity index 60%rename from solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmMetric.javarename to solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleCounter.javaindex 75541640488..43adf8ad087 100644--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmMetric.java+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleCounter.java@@ -14,14 +14,26 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.metrics.prometheus.jvm;+package org.apache.solr.metrics.otel.instruments;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrMetric;+import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.DoubleCounter;-/** Base class is a wrapper to export a solr.jvm {@link com.codahale.metrics.Metric} */-public abstract class SolrJvmMetric extends SolrMetric {- public SolrJvmMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);+public class AttributedDoubleCounter {++ private final DoubleCounter counter;+ private final Attributes attributes;++ public AttributedDoubleCounter(DoubleCounter counter, Attributes attributes) {+ this.counter = counter;+ this.attributes = attributes;+ }++ public void inc() {+ add(1.0);+ }++ public void add(Double value) {+ counter.add(value, attributes);}}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyMetric.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleGauge.javasimilarity index 63%rename from solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyMetric.javarename to solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleGauge.javaindex 95211f6d9eb..ba809a0fd80 100644--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyMetric.java+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleGauge.java@@ -14,14 +14,21 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.metrics.prometheus.jetty;+package org.apache.solr.metrics.otel.instruments;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrMetric;+import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.DoubleGauge;-/** Base class is a wrapper to export a solr.jetty {@link com.codahale.metrics.Metric} */-public abstract class SolrJettyMetric extends SolrMetric {- public SolrJettyMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);+public class AttributedDoubleGauge {+ private final DoubleGauge gauge;+ private final Attributes attributes;++ public AttributedDoubleGauge(DoubleGauge gauge, Attributes attributes) {+ this.gauge = gauge;+ this.attributes = attributes;+ }++ public void set(Double value) {+ gauge.set(value, attributes);}}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrNodeMetric.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleHistogram.javasimilarity index 61%rename from solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrNodeMetric.javarename to solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleHistogram.javaindex 34795e240de..7f573a3022a 100644--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrNodeMetric.java+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleHistogram.java@@ -14,16 +14,22 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.metrics.prometheus.node;+package org.apache.solr.metrics.otel.instruments;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrMetric;+import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.DoubleHistogram;-/** Base class is a wrapper to export a solr.node {@link com.codahale.metrics.Metric} */-public abstract class SolrNodeMetric extends SolrMetric {- public static final String NODE_THREAD_POOL = "solr_metrics_node_thread_pool";+public class AttributedDoubleHistogram {- public SolrNodeMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);+ private final DoubleHistogram histogram;+ private final Attributes attributes;++ public AttributedDoubleHistogram(DoubleHistogram histogram, Attributes attributes) {+ this.histogram = histogram;+ this.attributes = attributes;+ }++ public void record(Double value) {+ histogram.record(value, attributes);}}diff --git a/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleUpDownCounter.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleUpDownCounter.javanew file mode 100644index 00000000000..23c0d3b9a53--- /dev/null+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedDoubleUpDownCounter.java@@ -0,0 +1,43 @@+/*+ * 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.otel.instruments;++import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.DoubleUpDownCounter;++public class AttributedDoubleUpDownCounter {++ private final DoubleUpDownCounter upDownCounter;+ private final Attributes attributes;++ public AttributedDoubleUpDownCounter(DoubleUpDownCounter upDownCounter, Attributes attributes) {+ this.upDownCounter = upDownCounter;+ this.attributes = attributes;+ }++ public void inc() {+ add(1.0);+ }++ public void dec() {+ add(-1.0);+ }++ public void add(Double value) {+ upDownCounter.add(value, attributes);+ }+}diff --git a/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongCounter.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongCounter.javanew file mode 100644index 00000000000..f33b4784c53--- /dev/null+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongCounter.java@@ -0,0 +1,39 @@+/*+ * 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.otel.instruments;++import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.LongCounter;++public class AttributedLongCounter {++ private final LongCounter baseCounter;+ private final io.opentelemetry.api.common.Attributes attributes;++ public AttributedLongCounter(LongCounter baseCounter, Attributes attributes) {+ this.baseCounter = baseCounter;+ this.attributes = attributes;+ }++ public void inc() {+ add(1L);+ }++ public void add(Long value) {+ baseCounter.add(value, attributes);+ }+}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/SolrNoOpMetric.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongGauge.javasimilarity index 64%rename from solr/core/src/java/org/apache/solr/metrics/prometheus/SolrNoOpMetric.javarename to solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongGauge.javaindex 4d3dcbe87ab..5b34f0dc31a 100644--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/SolrNoOpMetric.java+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongGauge.java@@ -14,16 +14,22 @@* See the License for the specific language governing permissions and* limitations under the License.*/-package org.apache.solr.metrics.prometheus;+package org.apache.solr.metrics.otel.instruments;-public class SolrNoOpMetric extends SolrMetric {- public SolrNoOpMetric() {}+import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.LongGauge;- @Override- public SolrMetric parseLabels() {- return this;+public class AttributedLongGauge {++ private final LongGauge gauge;+ private final Attributes attributes;++ public AttributedLongGauge(LongGauge gauge, Attributes attributes) {+ this.gauge = gauge;+ this.attributes = attributes;}- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {}+ public void set(Long value) {+ gauge.set(value, attributes);+ }}diff --git a/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongHistogram.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongHistogram.javanew file mode 100644index 00000000000..6984fadbfa5--- /dev/null+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongHistogram.java@@ -0,0 +1,36 @@+/*+ * 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.otel.instruments;++import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.LongHistogram;++public class AttributedLongHistogram {++ protected final LongHistogram histogram;+ protected final Attributes attributes;++ public AttributedLongHistogram(+ LongHistogram histogram, io.opentelemetry.api.common.Attributes attributes) {+ this.histogram = histogram;+ this.attributes = attributes;+ }++ public void record(Long value) {+ histogram.record(value, attributes);+ }+}diff --git a/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongTimer.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongTimer.javanew file mode 100644index 00000000000..588a81507db--- /dev/null+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongTimer.java@@ -0,0 +1,55 @@+/*+ * 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.otel.instruments;++import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.LongHistogram;+import org.apache.solr.util.RTimer;++/**+ * This class records the elapsed time (in milliseconds) to a {@link LongHistogram} with the use of+ * a {@link MetricTimer}+ */+public class AttributedLongTimer extends AttributedLongHistogram {++ public AttributedLongTimer(LongHistogram histogram, Attributes attributes) {+ super(histogram, attributes);+ }++ /**+ * Return a {@link MetricTimer} and starts the timer. When the timer calls {@link+ * MetricTimer#stop()}, the elapsed time is recorded+ */+ public MetricTimer start() {+ return new MetricTimer(this);+ }++ public static class MetricTimer extends RTimer {+ private final AttributedLongTimer bound;++ private MetricTimer(AttributedLongTimer bound) {+ this.bound = bound;+ }++ @Override+ public double stop() {+ double elapsedTime = super.stop();+ bound.record(Double.valueOf(elapsedTime).longValue());+ return elapsedTime;+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongUpDownCounter.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongUpDownCounter.javanew file mode 100644index 00000000000..91487c9e677--- /dev/null+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/AttributedLongUpDownCounter.java@@ -0,0 +1,43 @@+/*+ * 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.otel.instruments;++import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.LongUpDownCounter;++public class AttributedLongUpDownCounter {++ private final LongUpDownCounter upDownCounter;+ private final Attributes attributes;++ public AttributedLongUpDownCounter(LongUpDownCounter upDownCounter, Attributes attributes) {+ this.upDownCounter = upDownCounter;+ this.attributes = attributes;+ }++ public void inc() {+ add(1L);+ }++ public void dec() {+ add(-1L);+ }++ public void add(Long value) {+ upDownCounter.add(value, attributes);+ }+}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/package-info.java b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/package-info.javasimilarity index 74%rename from solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/package-info.javarename to solr/core/src/java/org/apache/solr/metrics/otel/instruments/package-info.javaindex c3c039c879e..fcbb9581d92 100644--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/package-info.java+++ b/solr/core/src/java/org/apache/solr/metrics/otel/instruments/package-info.java@@ -16,7 +16,8 @@*//**- * The {@link org.apache.solr.metrics.prometheus.jetty.SolrPrometheusJettyFormatter} is responsible- * for exporting solr.jetty registry metrics to Prometheus.+ * A collection of bound OTEL instruments which are reusable wrappers around OpenTelemetry metric+ * instruments that are pre-bound to a fixed set of Attributes. This avoids repeated attribute+ * allocation on every metric recording.*/-package org.apache.solr.metrics.prometheus.jetty;+package org.apache.solr.metrics.otel.instruments;diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/package-info.java b/solr/core/src/java/org/apache/solr/metrics/otel/package-info.javasimilarity index 79%rename from solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/package-info.javarename to solr/core/src/java/org/apache/solr/metrics/otel/package-info.javaindex 5150b3e2a19..91ec79ab3ff 100644--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/package-info.java+++ b/solr/core/src/java/org/apache/solr/metrics/otel/package-info.java@@ -16,7 +16,7 @@*//**- * The {@link org.apache.solr.metrics.prometheus.jvm.SolrPrometheusJvmFormatter} is responsible for- * exporting solr.jvm registry metrics to Prometheus.+ * Interfaces for creating wrappers around Open Telemetry instruments created from {@link+ * io.opentelemetry.api.metrics.MeterProvider} to record and measure metrics*/-package org.apache.solr.metrics.prometheus.jvm;+package org.apache.solr.metrics.otel;diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/SolrMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/SolrMetric.javadeleted file mode 100644index e95316c0565..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/SolrMetric.java+++ /dev/null@@ -1,57 +0,0 @@-/*- * 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.prometheus;--import com.codahale.metrics.Metric;-import io.prometheus.metrics.model.snapshots.Labels;-import java.util.ArrayList;-import java.util.HashMap;-import java.util.Map;--/**- * Base class is a wrapper to categorize and export {@link com.codahale.metrics.Metric} to {@link- * io.prometheus.metrics.model.snapshots.DataPointSnapshot} and register to a {@link- * SolrPrometheusFormatter}. {@link com.codahale.metrics.MetricRegistry} does not support tags- * unlike prometheus. Metrics registered to the registry need to be parsed out from the metric name- * to be exported to {@link io.prometheus.metrics.model.snapshots.DataPointSnapshot}- */-public abstract class SolrMetric {- public Metric dropwizardMetric;- public String metricName;- public Map<String, String> labels = new HashMap<>();-- public SolrMetric() {}-- public SolrMetric(Metric dropwizardMetric, String metricName) {- this.dropwizardMetric = dropwizardMetric;- this.metricName = metricName;- }-- /*- * Parse labels from the Dropwizard Metric name to be exported- */- public abstract SolrMetric parseLabels();-- /*- * Export metric to Prometheus with labels- */- public abstract void toPrometheus(SolrPrometheusFormatter formatter);-- public Labels getLabels() {- return Labels.of(new ArrayList<>(labels.keySet()), new ArrayList<>(labels.values()));- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/PrometheusCoreFormatterInfo.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/core/PrometheusCoreFormatterInfo.javadeleted file mode 100644index b80b406d356..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/PrometheusCoreFormatterInfo.java+++ /dev/null@@ -1,33 +0,0 @@-/*- * 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.prometheus.core;--public interface PrometheusCoreFormatterInfo {- /** Category of prefix Solr Core dropwizard handler metric names */- enum CoreCategory {- ADMIN,- QUERY,- UPDATE,- REPLICATION,- TLOG,- CACHE,- SEARCHER,- HIGHLIGHTER,- INDEX,- CORE- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreCacheMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreCacheMetric.javadeleted file mode 100644index d4d9627c222..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreCacheMetric.java+++ /dev/null@@ -1,51 +0,0 @@-/*- * 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.prometheus.core;--import com.codahale.metrics.Gauge;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/** Dropwizard metrics of name CACHE.* */-public class SolrCoreCacheMetric extends SolrCoreMetric {- public static final String CORE_CACHE_SEARCHER_METRICS = "solr_metrics_core_cache";-- public SolrCoreCacheMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * CACHE.searcher.documentCache- */-- @Override- public SolrCoreMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- if (dropwizardMetric instanceof Gauge) {- labels.put("cacheType", parsedMetric[2]);- }- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- if (dropwizardMetric instanceof Gauge) {- formatter.exportGauge(CORE_CACHE_SEARCHER_METRICS, (Gauge<?>) dropwizardMetric, getLabels());- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreHighlighterMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreHighlighterMetric.javadeleted file mode 100644index d3da3d3426f..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreHighlighterMetric.java+++ /dev/null@@ -1,49 +0,0 @@-/*- * 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.prometheus.core;--import com.codahale.metrics.Counter;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/** Dropwizard metrics of name HIGHLIGHTER.* */-public class SolrCoreHighlighterMetric extends SolrCoreMetric {- public static final String CORE_HIGHLIGHER_METRICS = "solr_metrics_core_highlighter_requests";-- public SolrCoreHighlighterMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * HIGHLIGHTER.SolrBoundaryScanner.default.requests- * HIGHLIGHTER.SolrFragmentsBuilder.colored.requests- */-- @Override- public SolrCoreMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- labels.put("type", parsedMetric[1]);- labels.put("item", parsedMetric[2]);- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- formatter.exportCounter(CORE_HIGHLIGHER_METRICS, (Counter) dropwizardMetric, getLabels());- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreIndexMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreIndexMetric.javadeleted file mode 100644index e45e1e955bd..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreIndexMetric.java+++ /dev/null@@ -1,47 +0,0 @@-/*- * 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.prometheus.core;--import com.codahale.metrics.Gauge;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/** Dropwizard metrics of name INDEX.* */-public class SolrCoreIndexMetric extends SolrCoreMetric {- public static final String CORE_INDEX_METRICS = "solr_metrics_core_index_size_bytes";-- public SolrCoreIndexMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * INDEX.sizeInBytes- */-- @Override- public SolrCoreMetric parseLabels() {- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- if (metricName.endsWith("sizeInBytes")) {- formatter.exportGauge(CORE_INDEX_METRICS, (Gauge<?>) dropwizardMetric, getLabels());- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreMetric.javadeleted file mode 100644index 59236b18629..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreMetric.java+++ /dev/null@@ -1,56 +0,0 @@-/*- * 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.prometheus.core;--import com.codahale.metrics.Metric;-import java.util.regex.Matcher;-import java.util.regex.Pattern;-import org.apache.solr.common.SolrException;-import org.apache.solr.metrics.prometheus.SolrMetric;--/** Base class is a wrapper to export a solr.core {@link com.codahale.metrics.Metric} */-public abstract class SolrCoreMetric extends SolrMetric {-- public static Pattern CLOUD_CORE_PATTERN =- Pattern.compile(- "(?<core>^core_(?<collection>.*)_(?<shard>shard[0-9]+)_(?<replica>replica_.[0-9]+)).(.*)$");- public static Pattern STANDALONE_CORE_PATTERN = Pattern.compile("^core_(?<core>.*?)\\.(.*)$");-- public String coreName;-- public SolrCoreMetric(Metric dropwizardMetric, String metricName) {- // Remove Core Name prefix from metric as it is no longer needed for tag parsing from this point- super(dropwizardMetric, metricName.substring(metricName.indexOf(".") + 1));- Matcher cloudPattern = CLOUD_CORE_PATTERN.matcher(metricName);- Matcher standalonePattern = STANDALONE_CORE_PATTERN.matcher(metricName);- if (cloudPattern.find()) {- coreName = cloudPattern.group("core");- cloudPattern- .namedGroups()- .forEach((key, value) -> labels.put(key, cloudPattern.group(value)));- } else if (standalonePattern.find()) {- coreName = standalonePattern.group("core");- standalonePattern- .namedGroups()- .forEach((key, value) -> labels.put(key, standalonePattern.group(value)));- } else {- throw new SolrException(- SolrException.ErrorCode.SERVER_ERROR,- "Core name does not match pattern for parsing in metric " + metricName);- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreTlogMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreTlogMetric.javadeleted file mode 100644index 7286c99d986..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreTlogMetric.java+++ /dev/null@@ -1,51 +0,0 @@-/*- * 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.prometheus.core;--import com.codahale.metrics.Meter;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/** Dropwizard metrics of name TLOG.* */-public class SolrCoreTlogMetric extends SolrCoreMetric {- public static final String CORE_TLOG_METRICS = "solr_metrics_core_tlog";-- public SolrCoreTlogMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * TLOG.buffered.ops- */-- @Override- public SolrCoreMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- if (dropwizardMetric instanceof Meter) {- labels.put("item", parsedMetric[1]);- }- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- if (dropwizardMetric instanceof Meter) {- formatter.exportMeter(CORE_TLOG_METRICS, (Meter) dropwizardMetric, getLabels());- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrPrometheusCoreFormatter.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrPrometheusCoreFormatter.javadeleted file mode 100644index dd3ed025ddf..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrPrometheusCoreFormatter.java+++ /dev/null@@ -1,70 +0,0 @@-/*- * 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.prometheus.core;--import com.codahale.metrics.Metric;-import com.google.common.base.Enums;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrNoOpMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/**- * This class maintains a {@link io.prometheus.metrics.model.snapshots.MetricSnapshot}s exported- * from solr.core {@link com.codahale.metrics.MetricRegistry}- */-public class SolrPrometheusCoreFormatter extends SolrPrometheusFormatter- implements PrometheusCoreFormatterInfo {-- public SolrPrometheusCoreFormatter() {- super();- }-- @Override- public void exportDropwizardMetric(Metric dropwizardMetric, String metricName) {- SolrMetric solrCoreMetric = categorizeMetric(dropwizardMetric, metricName);- solrCoreMetric.parseLabels().toPrometheus(this);- }-- @Override- public SolrMetric categorizeMetric(Metric dropwizardMetric, String metricName) {- String metricCategory = metricName.split("\\.", 3)[1];- if (!Enums.getIfPresent(PrometheusCoreFormatterInfo.CoreCategory.class, metricCategory)- .isPresent()) {- return new SolrNoOpMetric();- }- switch (CoreCategory.valueOf(metricCategory)) {- case ADMIN:- case QUERY:- case UPDATE:- case REPLICATION:- return new SolrCoreHandlerMetric(dropwizardMetric, metricName);- case TLOG:- return new SolrCoreTlogMetric(dropwizardMetric, metricName);- case CACHE:- return new SolrCoreCacheMetric(dropwizardMetric, metricName);- case SEARCHER:- return new SolrCoreSearcherMetric(dropwizardMetric, metricName);- case HIGHLIGHTER:- return new SolrCoreHighlighterMetric(dropwizardMetric, metricName);- case INDEX:- return new SolrCoreIndexMetric(dropwizardMetric, metricName);- case CORE:- default:- return new SolrNoOpMetric();- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/package-info.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/core/package-info.javadeleted file mode 100644index acabe654867..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/core/package-info.java+++ /dev/null@@ -1,22 +0,0 @@-/*- * 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.- */--/**- * The {@link org.apache.solr.metrics.prometheus.core.SolrPrometheusCoreFormatter} is responsible- * for exporting solr.core registry metrics to Prometheus.- */-package org.apache.solr.metrics.prometheus.core;diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyDispatchesMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyDispatchesMetric.javadeleted file mode 100644index cd057418845..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyDispatchesMetric.java+++ /dev/null@@ -1,46 +0,0 @@-/*- * 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.prometheus.jetty;--import com.codahale.metrics.Metric;-import com.codahale.metrics.Timer;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/* Dropwizard metrics of name *.dispatches */-public class SolrJettyDispatchesMetric extends SolrJettyMetric {- public static final String JETTY_DISPATCHES_TOTAL = "solr_metrics_jetty_dispatches";-- public SolrJettyDispatchesMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * org.eclipse.jetty.server.handler.DefaultHandler.dispatches- */-- @Override- public SolrMetric parseLabels() {- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- formatter.exportTimerCount(JETTY_DISPATCHES_TOTAL, (Timer) dropwizardMetric, getLabels());- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyReqRespMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyReqRespMetric.javadeleted file mode 100644index 374f0bdfd59..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrJettyReqRespMetric.java+++ /dev/null@@ -1,65 +0,0 @@-/*- * 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.prometheus.jetty;--import com.codahale.metrics.Counter;-import com.codahale.metrics.Meter;-import com.codahale.metrics.Metric;-import com.codahale.metrics.Timer;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/* Dropwizard metrics of name *.xx-responses and *-requests */-public class SolrJettyReqRespMetric extends SolrJettyMetric {- public static final String JETTY_RESPONSES_TOTAL = "solr_metrics_jetty_response";- public static final String JETTY_REQUESTS_TOTAL = "solr_metrics_jetty_requests";-- public SolrJettyReqRespMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * org.eclipse.jetty.server.handler.DefaultHandler.2xx-responses- * org.eclipse.jetty.server.handler.DefaultHandler.get-requests- */-- @Override- public SolrMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- String label = parsedMetric[parsedMetric.length - 1].split("-")[0];- if (metricName.endsWith("xx-responses")) {- labels.put("status", label);- } else {- labels.put("method", label);- }- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- if (metricName.endsWith("xx-responses")) {- formatter.exportMeter(JETTY_RESPONSES_TOTAL, (Meter) dropwizardMetric, getLabels());- } else if (metricName.endsWith("-requests")) {- if (dropwizardMetric instanceof Counter) {- formatter.exportCounter(JETTY_REQUESTS_TOTAL, (Counter) dropwizardMetric, getLabels());- } else if (dropwizardMetric instanceof Timer) {- formatter.exportTimerCount(JETTY_REQUESTS_TOTAL, (Timer) dropwizardMetric, getLabels());- }- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrPrometheusJettyFormatter.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrPrometheusJettyFormatter.javadeleted file mode 100644index 88eae10d39f..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jetty/SolrPrometheusJettyFormatter.java+++ /dev/null@@ -1,49 +0,0 @@-/*- * 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.prometheus.jetty;--import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrNoOpMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/**- * This class maintains a {@link io.prometheus.metrics.model.snapshots.MetricSnapshot}s exported- * from solr.jetty {@link com.codahale.metrics.MetricRegistry}- */-public class SolrPrometheusJettyFormatter extends SolrPrometheusFormatter {- public SolrPrometheusJettyFormatter() {- super();- }-- @Override- public void exportDropwizardMetric(Metric dropwizardMetric, String metricName) {- SolrMetric solrJettyMetric = categorizeMetric(dropwizardMetric, metricName);- solrJettyMetric.parseLabels().toPrometheus(this);- }-- @Override- public SolrMetric categorizeMetric(Metric dropwizardMetric, String metricName) {- if (metricName.endsWith("xx-responses") || metricName.endsWith("-requests")) {- return new SolrJettyReqRespMetric(dropwizardMetric, metricName);- } else if (metricName.endsWith(".dispatches")) {- return new SolrJettyDispatchesMetric(dropwizardMetric, metricName);- } else {- return new SolrNoOpMetric();- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/PrometheusJvmFormatterInfo.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/PrometheusJvmFormatterInfo.javadeleted file mode 100644index 660154ece4a..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/PrometheusJvmFormatterInfo.java+++ /dev/null@@ -1,30 +0,0 @@-/*- * 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.prometheus.jvm;--public interface PrometheusJvmFormatterInfo {- /** Category of prefix Solr JVM dropwizard handler metric names */- enum JvmCategory {- buffers,- gc,- memory,- os,- threads,- classes,- system- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmBuffersMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmBuffersMetric.javadeleted file mode 100644index 58f7ae697a5..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmBuffersMetric.java+++ /dev/null@@ -1,56 +0,0 @@-/*- * 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.prometheus.jvm;--import com.codahale.metrics.Gauge;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/* Dropwizard metrics of name buffers.* */-public class SolrJvmBuffersMetric extends SolrJvmMetric {- public static final String JVM_BUFFERS = "solr_metrics_jvm_buffers";- public static final String JVM_BUFFERS_BYTES = "solr_metrics_jvm_buffers_bytes";-- public SolrJvmBuffersMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * buffers.direct.MemoryUsed- * buffers.mapped.Count- */-- @Override- public SolrJvmMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- labels.put("pool", parsedMetric[1]);- labels.put("item", parsedMetric[2]);- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- String[] parsedMetric = metricName.split("\\.");- String metricType = parsedMetric[parsedMetric.length - 1];- if (metricType.equals("Count")) {- formatter.exportGauge(JVM_BUFFERS, (Gauge<?>) dropwizardMetric, getLabels());- } else if (metricType.equals(("MemoryUsed")) || metricType.equals(("TotalCapacity"))) {- formatter.exportGauge(JVM_BUFFERS_BYTES, (Gauge<?>) dropwizardMetric, getLabels());- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmGcMetrics.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmGcMetrics.javadeleted file mode 100644index 7dbb8baa817..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmGcMetrics.java+++ /dev/null@@ -1,54 +0,0 @@-/*- * 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.prometheus.jvm;--import com.codahale.metrics.Gauge;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/* Dropwizard metrics of name gc.* */-public class SolrJvmGcMetrics extends SolrJvmMetric {- public static final String JVM_GC = "solr_metrics_jvm_gc";- public static final String JVM_GC_SECONDS = "solr_metrics_jvm_gc_seconds";-- public SolrJvmGcMetrics(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * gc.G1-Old-Generation.time- * gc.G1-Young-Generation.count- */-- @Override- public SolrMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- labels.put("item", parsedMetric[1]);- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- if (metricName.endsWith(".count")) {- formatter.exportGauge(JVM_GC, (Gauge<?>) dropwizardMetric, getLabels());- } else if (metricName.endsWith(".time")) {- formatter.exportGauge(JVM_GC_SECONDS, (Gauge<?>) dropwizardMetric, getLabels());- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmMemoryMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmMemoryMetric.javadeleted file mode 100644index 5ba1189c283..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmMemoryMetric.java+++ /dev/null@@ -1,65 +0,0 @@-/*- * 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.prometheus.jvm;--import com.codahale.metrics.Gauge;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/* Dropwizard metrics of name memory.* */-public class SolrJvmMemoryMetric extends SolrJvmMetric {- public static final String JVM_MEMORY_POOL_BYTES = "solr_metrics_jvm_memory_pools_bytes";- public static final String JVM_MEMORY = "solr_metrics_jvm_heap";-- public SolrJvmMemoryMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * memory.heap.usage- * memory.non-heap.committed- * memory.total.committed- * memory.pools.CodeHeap-'non-nmethods'.committed- */-- @Override- public SolrMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- labels.put("item", parsedMetric[parsedMetric.length - 1]);- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- String[] parsedMetric = metricName.split("\\.");- String metricType = parsedMetric[1];- switch (metricType) {- case "heap":- case "non-heap":- case "total":- labels.put("memory", parsedMetric[1]);- formatter.exportGauge(JVM_MEMORY, (Gauge<?>) dropwizardMetric, getLabels());- break;- case "pools":- labels.put("space", parsedMetric[2]);- formatter.exportGauge(JVM_MEMORY_POOL_BYTES, (Gauge<?>) dropwizardMetric, getLabels());- break;- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmOsMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmOsMetric.javadeleted file mode 100644index 9b5d4ce1d69..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrJvmOsMetric.java+++ /dev/null@@ -1,58 +0,0 @@-/*- * 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.prometheus.jvm;--import com.codahale.metrics.Gauge;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/* Dropwizard metrics of name os.* and threads.* */-public class SolrJvmOsMetric extends SolrJvmMetric {- public static final String JVM_OS_THREADS = "solr_metrics_jvm_threads";- public static final String JVM_OS = "solr_metrics_os";-- public SolrJvmOsMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * os.availableProcessors- * threads.peak.count- */-- @Override- public SolrMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- if (parsedMetric[0].equals("threads")) {- labels.put("item", parsedMetric[1]);- } else {- labels.put("item", parsedMetric[parsedMetric.length - 1]);- }- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- if (metricName.startsWith("threads.")) {- formatter.exportGauge(JVM_OS_THREADS, (Gauge<?>) dropwizardMetric, getLabels());- } else {- formatter.exportGauge(JVM_OS, (Gauge<?>) dropwizardMetric, getLabels());- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrPrometheusJvmFormatter.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrPrometheusJvmFormatter.javadeleted file mode 100644index 888a0cb9927..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/jvm/SolrPrometheusJvmFormatter.java+++ /dev/null@@ -1,61 +0,0 @@-/*- * 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.prometheus.jvm;--import com.codahale.metrics.Metric;-import com.google.common.base.Enums;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrNoOpMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/**- * This class maintains a {@link io.prometheus.metrics.model.snapshots.MetricSnapshot}s exported- * from solr.jvm {@link com.codahale.metrics.MetricRegistry}- */-public class SolrPrometheusJvmFormatter extends SolrPrometheusFormatter- implements PrometheusJvmFormatterInfo {- public SolrPrometheusJvmFormatter() {- super();- }-- @Override- public void exportDropwizardMetric(Metric dropwizardMetric, String metricName) {- SolrMetric solrJvmMetric = categorizeMetric(dropwizardMetric, metricName);- solrJvmMetric.parseLabels().toPrometheus(this);- }-- @Override- public SolrMetric categorizeMetric(Metric dropwizardMetric, String metricName) {- String metricCategory = metricName.split("\\.", 2)[0];- if (!Enums.getIfPresent(JvmCategory.class, metricCategory).isPresent()) {- return new SolrNoOpMetric();- }- switch (JvmCategory.valueOf(metricCategory)) {- case gc:- return new SolrJvmGcMetrics(dropwizardMetric, metricName);- case memory:- return new SolrJvmMemoryMetric(dropwizardMetric, metricName);- case os:- case threads:- return new SolrJvmOsMetric(dropwizardMetric, metricName);- case buffers:- return new SolrJvmBuffersMetric(dropwizardMetric, metricName);- default:- return new SolrNoOpMetric();- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/PrometheusNodeFormatterInfo.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/node/PrometheusNodeFormatterInfo.javadeleted file mode 100644index ab462751d60..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/PrometheusNodeFormatterInfo.java+++ /dev/null@@ -1,27 +0,0 @@-/*- * 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.prometheus.node;--public interface PrometheusNodeFormatterInfo {- /** Category of prefix Solr Node dropwizard handler metric names */- enum NodeCategory {- ADMIN,- UPDATE,- CONTAINER,- CACHE- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrNodeContainerMetric.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrNodeContainerMetric.javadeleted file mode 100644index 2e3e16645c2..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrNodeContainerMetric.java+++ /dev/null@@ -1,57 +0,0 @@-/*- * 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.prometheus.node;--import com.codahale.metrics.Gauge;-import com.codahale.metrics.Metric;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/* Dropwizard metrics of name CONTAINER.* */-public class SolrNodeContainerMetric extends SolrNodeMetric {- public static final String NODE_CORES = "solr_metrics_node_cores";- public static final String NODE_CORE_FS_BYTES = "solr_metrics_node_core_root_fs_bytes";-- public SolrNodeContainerMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- /*- * Metric examples being exported- * CONTAINER.fs.coreRoot.totalSpace- * CONTAINER.cores.loaded- */-- @Override- public SolrMetric parseLabels() {- String[] parsedMetric = metricName.split("\\.");- labels.put("category", parsedMetric[0]);- return this;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {- String[] parsedMetric = metricName.split("\\.");- if (metricName.startsWith("CONTAINER.cores.")) {- labels.put("item", parsedMetric[2]);- formatter.exportGauge(NODE_CORES, (Gauge<?>) dropwizardMetric, getLabels());- } else if (metricName.startsWith("CONTAINER.fs.coreRoot.")) {- labels.put("item", parsedMetric[3]);- formatter.exportGauge(NODE_CORE_FS_BYTES, (Gauge<?>) dropwizardMetric, getLabels());- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrPrometheusNodeFormatter.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrPrometheusNodeFormatter.javadeleted file mode 100644index eb542e05b77..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/SolrPrometheusNodeFormatter.java+++ /dev/null@@ -1,104 +0,0 @@-/*- * 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.prometheus.node;--import static org.apache.solr.metrics.prometheus.node.SolrNodeMetric.NODE_THREAD_POOL;--import com.codahale.metrics.Counter;-import com.codahale.metrics.Meter;-import com.codahale.metrics.Metric;-import com.google.common.base.Enums;-import io.prometheus.metrics.model.snapshots.Labels;-import org.apache.solr.metrics.prometheus.SolrMetric;-import org.apache.solr.metrics.prometheus.SolrNoOpMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;--/**- * This class maintains a {@link io.prometheus.metrics.model.snapshots.MetricSnapshot}s exported- * from solr.node {@link com.codahale.metrics.MetricRegistry}- */-public class SolrPrometheusNodeFormatter extends SolrPrometheusFormatter- implements PrometheusNodeFormatterInfo {- public SolrPrometheusNodeFormatter() {- super();- }-- @Override- public void exportDropwizardMetric(Metric dropwizardMetric, String metricName) {- if (metricName.contains(".threadPool.")) {- exportThreadPoolMetric(dropwizardMetric, metricName);- return;- }-- SolrMetric solrNodeMetric = categorizeMetric(dropwizardMetric, metricName);- solrNodeMetric.parseLabels().toPrometheus(this);- }-- @Override- public SolrMetric categorizeMetric(Metric dropwizardMetric, String metricName) {- String metricCategory = metricName.split("\\.", 2)[0];- if (!Enums.getIfPresent(PrometheusNodeFormatterInfo.NodeCategory.class, metricCategory)- .isPresent()) {- return new SolrNoOpMetric();- }- switch (NodeCategory.valueOf(metricCategory)) {- case ADMIN:- case UPDATE:- return new SolrNodeHandlerMetric(dropwizardMetric, metricName);- case CONTAINER:- return new SolrNodeContainerMetric(dropwizardMetric, metricName);- default:- return new SolrNoOpMetric();- }- }-- /*- * Metric examples being exported- * ADMIN./admin/cores.threadPool.parallelCoreAdminExecutor.completed- * CONTAINER.threadPool.coreContainerWorkExecutor.completed- */- private void exportThreadPoolMetric(Metric dropwizardMetric, String metricName) {- Labels labels;- String[] parsedMetric = metricName.split("\\.");- if (parsedMetric.length >= 5) {- labels =- Labels.of(- "category",- parsedMetric[0],- "handler",- parsedMetric[1],- "executer",- parsedMetric[3],- "task",- parsedMetric[parsedMetric.length - 1]);- } else {- labels =- Labels.of(- "category",- parsedMetric[0],- "executer",- parsedMetric[2],- "task",- parsedMetric[parsedMetric.length - 1]);- }- if (dropwizardMetric instanceof Counter) {- exportCounter(NODE_THREAD_POOL, (Counter) dropwizardMetric, labels);- } else if (dropwizardMetric instanceof Meter) {- exportMeter(NODE_THREAD_POOL, (Meter) dropwizardMetric, labels);- }- }-}diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/package-info.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/node/package-info.javadeleted file mode 100644index 64de64aa73c..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/node/package-info.java+++ /dev/null@@ -1,22 +0,0 @@-/*- * 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.- */--/**- * The {@link org.apache.solr.metrics.prometheus.node.SolrPrometheusNodeFormatter} is responsible- * for exporting solr.node registry metrics to Prometheus.- */-package org.apache.solr.metrics.prometheus.node;diff --git a/solr/core/src/java/org/apache/solr/metrics/prometheus/package-info.java b/solr/core/src/java/org/apache/solr/metrics/prometheus/package-info.javadeleted file mode 100644index 68f0c2a983e..00000000000--- a/solr/core/src/java/org/apache/solr/metrics/prometheus/package-info.java+++ /dev/null@@ -1,25 +0,0 @@-/*- * 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.- */--/**- * The {@link org.apache.solr.metrics.prometheus.SolrPrometheusFormatter} is responsible for- * collecting Prometheus metrics from exporting {@link com.codahale.metrics.Metric}'s from {@link- * com.codahale.metrics.MetricRegistry} {@link org.apache.solr.metrics.prometheus.SolrMetric} is a- * wrapper to export {@link com.codahale.metrics.Metric} to {@link- * io.prometheus.metrics.model.snapshots.DataPointSnapshot}- */-package org.apache.solr.metrics.prometheus;diff --git a/solr/core/src/java/org/apache/solr/response/PrometheusResponseWriter.java b/solr/core/src/java/org/apache/solr/response/PrometheusResponseWriter.javaindex 5919a0ec858..5a375bdb408 100644--- a/solr/core/src/java/org/apache/solr/response/PrometheusResponseWriter.java+++ b/solr/core/src/java/org/apache/solr/response/PrometheusResponseWriter.java@@ -16,29 +16,12 @@*/package org.apache.solr.response;-import com.codahale.metrics.Histogram;-import com.codahale.metrics.Metric;-import com.codahale.metrics.MetricFilter;-import com.codahale.metrics.MetricRegistry;-import com.codahale.metrics.Timer;import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter;+import io.prometheus.metrics.model.snapshots.MetricSnapshots;import java.io.IOException;import java.io.OutputStream;import java.lang.invoke.MethodHandles;-import java.util.List;-import java.util.Map;-import java.util.function.Consumer;-import java.util.function.Predicate;-import org.apache.solr.common.SolrException;-import org.apache.solr.common.util.NamedList;-import org.apache.solr.metrics.AggregateMetric;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;-import org.apache.solr.metrics.prometheus.core.SolrPrometheusCoreFormatter;-import org.apache.solr.metrics.prometheus.jetty.SolrPrometheusJettyFormatter;-import org.apache.solr.metrics.prometheus.jvm.SolrPrometheusJvmFormatter;-import org.apache.solr.metrics.prometheus.node.SolrPrometheusNodeFormatter;import org.apache.solr.request.SolrQueryRequest;-import org.apache.solr.util.stats.MetricUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -57,90 +40,12 @@ public class PrometheusResponseWriter implements QueryResponseWriter {public void write(OutputStream out, SolrQueryRequest request, SolrQueryResponse response, String contentType)throws IOException {- NamedList<Object> prometheusRegistries =- (NamedList<Object>) response.getValues().get("metrics");var prometheusTextFormatWriter = new PrometheusTextFormatWriter(false);- for (Map.Entry<String, Object> prometheusRegistry : prometheusRegistries) {- var prometheusFormatter = (SolrPrometheusFormatter) prometheusRegistry.getValue();- prometheusTextFormatWriter.write(out, prometheusFormatter.collect());- }+ prometheusTextFormatWriter.write(out, (MetricSnapshots) response.getValues().get("metrics"));}@Overridepublic String getContentType(SolrQueryRequest request, SolrQueryResponse response) {return CONTENT_TYPE_PROMETHEUS;}-- /**- * Provides a representation of the given Dropwizard metric registry as {@link- * SolrPrometheusCoreFormatter}-s. Only those metrics are converted which match at least one of- * the given MetricFilter instances.- *- * @param registry the {@link MetricRegistry} to be converted- * @param shouldMatchFilters a list of {@link MetricFilter} instances. A metric must match <em>any- * one</em> of the filters from this list to be included in the output- * @param mustMatchFilter a {@link MetricFilter}. A metric <em>must</em> match this filter to be- * included in the output.- * @param propertyFilter limit what properties of a metric are returned- * @param skipHistograms discard any {@link Histogram}-s and histogram parts of {@link Timer}-s.- * @param skipAggregateValues discard internal values of {@link AggregateMetric}-s.- * @param compact use compact representation for counters and gauges.- * @param consumer consumer that accepts produced {@link SolrPrometheusCoreFormatter}-s- */- public static void toPrometheus(- MetricRegistry registry,- String registryName,- List<MetricFilter> shouldMatchFilters,- MetricFilter mustMatchFilter,- Predicate<CharSequence> propertyFilter,- boolean skipHistograms,- boolean skipAggregateValues,- boolean compact,- Consumer<SolrPrometheusFormatter> consumer) {- Map<String, Metric> dropwizardMetrics = registry.getMetrics();- var formatter = getFormatterType(registryName);- if (formatter == null) {- return;- }-- MetricUtils.toMaps(- registry,- shouldMatchFilters,- mustMatchFilter,- propertyFilter,- skipHistograms,- skipAggregateValues,- compact,- false,- (metricName, metric) -> {- try {- Metric dropwizardMetric = dropwizardMetrics.get(metricName);- formatter.exportDropwizardMetric(dropwizardMetric, metricName);- } catch (Exception e) {- throw new SolrException(- SolrException.ErrorCode.SERVER_ERROR,- "Error occurred exporting Dropwizard Metric to Prometheus",- e);- }- });-- consumer.accept(formatter);- }-- public static SolrPrometheusFormatter getFormatterType(String registryName) {- String[] parsedRegistry = registryName.split("\\.");-- switch (parsedRegistry[1]) {- case "core":- return new SolrPrometheusCoreFormatter();- case "jvm":- return new SolrPrometheusJvmFormatter();- case "jetty":- return new SolrPrometheusJettyFormatter();- case "node":- return new SolrPrometheusNodeFormatter();- default:- return null;- }- }}diff --git a/solr/core/src/java/org/apache/solr/search/CaffeineCache.java b/solr/core/src/java/org/apache/solr/search/CaffeineCache.javaindex 091dd984fa5..8251cd0be96 100644--- a/solr/core/src/java/org/apache/solr/search/CaffeineCache.java+++ b/solr/core/src/java/org/apache/solr/search/CaffeineCache.java@@ -24,6 +24,7 @@ import com.github.benmanes.caffeine.cache.RemovalCause;import com.github.benmanes.caffeine.cache.RemovalListener;import com.github.benmanes.caffeine.cache.stats.CacheStats;import com.google.common.annotations.VisibleForTesting;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.io.UncheckedIOException;import java.lang.invoke.MethodHandles;@@ -473,8 +474,10 @@ public class CaffeineCache<K, V> extends SolrCacheBasereturn name() + (cacheMap != null ? cacheMap.getValue().toString() : "");}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {solrMetricsContext = parentContext.getChildContext(this);cacheMap =new MetricsMap(diff --git a/solr/core/src/java/org/apache/solr/search/QParserPlugin.java b/solr/core/src/java/org/apache/solr/search/QParserPlugin.javaindex 4917a083d60..9dcca155e8d 100644--- a/solr/core/src/java/org/apache/solr/search/QParserPlugin.java+++ b/solr/core/src/java/org/apache/solr/search/QParserPlugin.java@@ -16,6 +16,7 @@*/package org.apache.solr.search;+import io.opentelemetry.api.common.Attributes;import java.util.Collections;import java.util.HashMap;import java.util.Map;@@ -122,7 +123,8 @@ public abstract class QParserPlugin implements NamedListInitializedPlugin, SolrI}@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {// by default do nothing}diff --git a/solr/core/src/java/org/apache/solr/search/SolrFieldCacheBean.java b/solr/core/src/java/org/apache/solr/search/SolrFieldCacheBean.javaindex 8d0755f159d..265360bd7b9 100644--- a/solr/core/src/java/org/apache/solr/search/SolrFieldCacheBean.java+++ b/solr/core/src/java/org/apache/solr/search/SolrFieldCacheBean.java@@ -17,6 +17,7 @@package org.apache.solr.search;import org.apache.solr.common.util.EnvUtils;+import io.opentelemetry.api.common.Attributes;import org.apache.solr.core.SolrInfoBean;import org.apache.solr.metrics.MetricsMap;import org.apache.solr.metrics.SolrMetricsContext;@@ -52,8 +53,10 @@ public class SolrFieldCacheBean implements SolrInfoBean {return solrMetricsContext;}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {this.solrMetricsContext = parentContext;MetricsMap metricsMap =new MetricsMap(diff --git a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.javaindex 5498d9202a8..e92b1001b79 100644--- a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java+++ b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java@@ -19,6 +19,7 @@ package org.apache.solr.search;import static org.apache.solr.search.CpuAllowedLimit.TIMING_CONTEXT;import com.codahale.metrics.Gauge;+import io.opentelemetry.api.common.Attributes;import java.io.Closeable;import java.io.IOException;import java.lang.invoke.MethodHandles;@@ -602,9 +603,13 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIthis.solrMetricsContext = core.getSolrMetricsContext().getChildContext(this);for (SolrCache<?, ?> cache : cacheList) {cache.initializeMetrics(- solrMetricsContext, SolrMetricManager.mkName(cache.name(), STATISTICS_KEY));+ // TODO SOLR-17458: Add Otel+ solrMetricsContext,+ Attributes.empty(),+ SolrMetricManager.mkName(cache.name(), STATISTICS_KEY));}- initializeMetrics(solrMetricsContext, STATISTICS_KEY);+ // TODO SOLR-17458: Add Otel+ initializeMetrics(solrMetricsContext, Attributes.empty(), STATISTICS_KEY);registerTime = new Date();}@@ -2601,8 +2606,10 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrIreturn solrMetricsContext;}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {parentContext.gauge(() -> name, true, "searcherName", Category.SEARCHER.toString(), scope);parentContext.gauge(() -> cachingEnabled, true, "caching", Category.SEARCHER.toString(), scope);parentContext.gauge(() -> openTime, true, "openedAt", Category.SEARCHER.toString(), scope);diff --git a/solr/core/src/java/org/apache/solr/security/AuditLoggerPlugin.java b/solr/core/src/java/org/apache/solr/security/AuditLoggerPlugin.javaindex a3a8a7c15ca..78dfdd4d572 100644--- a/solr/core/src/java/org/apache/solr/security/AuditLoggerPlugin.java+++ b/solr/core/src/java/org/apache/solr/security/AuditLoggerPlugin.java@@ -22,6 +22,7 @@ import com.codahale.metrics.Timer;import com.fasterxml.jackson.annotation.JsonInclude.Include;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;+import io.opentelemetry.api.common.Attributes;import java.io.Closeable;import java.io.IOException;import java.io.StringWriter;@@ -260,8 +261,10 @@ public abstract class AuditLoggerPlugin implements Closeable, Runnable, SolrInfothis.formatter = formatter;}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, final String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, final String scope) {solrMetricsContext = parentContext.getChildContext(this);String className = this.getClass().getSimpleName();log.debug("Initializing metrics for {}", className);diff --git a/solr/core/src/java/org/apache/solr/security/AuthenticationPlugin.java b/solr/core/src/java/org/apache/solr/security/AuthenticationPlugin.javaindex 5a847cef0c4..8d588aada46 100644--- a/solr/core/src/java/org/apache/solr/security/AuthenticationPlugin.java+++ b/solr/core/src/java/org/apache/solr/security/AuthenticationPlugin.java@@ -19,6 +19,7 @@ package org.apache.solr.security;import com.codahale.metrics.Counter;import com.codahale.metrics.Meter;import com.codahale.metrics.Timer;+import io.opentelemetry.api.common.Attributes;import jakarta.servlet.FilterChain;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletRequestWrapper;@@ -141,8 +142,10 @@ public abstract class AuthenticationPlugin implements SolrInfoBean {return solrMetricsContext;}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {this.solrMetricsContext = parentContext.getChildContext(this);// MetricsnumErrors = this.solrMetricsContext.meter("errors", getCategory().toString(), scope);diff --git a/solr/core/src/java/org/apache/solr/security/MultiAuthPlugin.java b/solr/core/src/java/org/apache/solr/security/MultiAuthPlugin.javaindex f8a557c9e8d..903218eed56 100644--- a/solr/core/src/java/org/apache/solr/security/MultiAuthPlugin.java+++ b/solr/core/src/java/org/apache/solr/security/MultiAuthPlugin.java@@ -16,6 +16,7 @@*/package org.apache.solr.security;+import io.opentelemetry.api.common.Attributes;import jakarta.servlet.FilterChain;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;@@ -193,10 +194,13 @@ public class MultiAuthPlugin extends AuthenticationPlugin}}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {for (AuthenticationPlugin plugin : pluginMap.values()) {- plugin.initializeMetrics(parentContext, scope);+ // TODO SOLR-17458: Add Otel+ plugin.initializeMetrics(parentContext, Attributes.empty(), scope);}}diff --git a/solr/core/src/java/org/apache/solr/security/MultiDestinationAuditLogger.java b/solr/core/src/java/org/apache/solr/security/MultiDestinationAuditLogger.javaindex 5dc04422f78..d834d3b3c3b 100644--- a/solr/core/src/java/org/apache/solr/security/MultiDestinationAuditLogger.java+++ b/solr/core/src/java/org/apache/solr/security/MultiDestinationAuditLogger.java@@ -18,6 +18,7 @@ package org.apache.solr.security;import static org.apache.solr.common.SolrException.ErrorCode.SERVER_ERROR;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.util.ArrayList;@@ -129,9 +130,11 @@ public class MultiDestinationAuditLogger extends AuditLoggerPlugin implements Re}@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {- super.initializeMetrics(parentContext, scope);- plugins.forEach(p -> p.initializeMetrics(solrMetricsContext, scope));+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ super.initializeMetrics(parentContext, attributes, scope);+ // TODO SOLR-17458: Add Otel+ plugins.forEach(p -> p.initializeMetrics(solrMetricsContext, Attributes.empty(), scope));}@Overridediff --git a/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java b/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.javaindex f990f3cf58f..a9e74fbcdb5 100644--- a/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java+++ b/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java@@ -17,6 +17,7 @@package org.apache.solr.update;import com.codahale.metrics.Meter;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.lang.reflect.Array;@@ -205,8 +206,10 @@ public class DirectUpdateHandler2 extends UpdateHandler}}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {if (core.getSolrConfig().getUpdateHandlerInfo().aggregateNodeLevelMetricsEnabled) {this.solrMetricsContext =new SolrDelegateRegistryMetricsContext(diff --git a/solr/core/src/java/org/apache/solr/update/PeerSync.java b/solr/core/src/java/org/apache/solr/update/PeerSync.javaindex a37edab2db3..0036c856a6b 100644--- a/solr/core/src/java/org/apache/solr/update/PeerSync.java+++ b/solr/core/src/java/org/apache/solr/update/PeerSync.java@@ -24,6 +24,7 @@ import static org.apache.solr.update.processor.DistributingUpdateProcessorFactorimport com.codahale.metrics.Counter;import com.codahale.metrics.Timer;import com.google.common.annotations.VisibleForTesting;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.net.ConnectException;@@ -143,8 +144,10 @@ public class PeerSync implements SolrMetricProducer {return solrMetricsContext;}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {this.solrMetricsContext = parentContext.getChildContext(this);syncTime = solrMetricsContext.timer("time", scope, METRIC_SCOPE);syncErrors = solrMetricsContext.counter("errors", scope, METRIC_SCOPE);diff --git a/solr/core/src/java/org/apache/solr/update/PeerSyncWithLeader.java b/solr/core/src/java/org/apache/solr/update/PeerSyncWithLeader.javaindex 8dff6d15432..d6630dc46b5 100644--- a/solr/core/src/java/org/apache/solr/update/PeerSyncWithLeader.java+++ b/solr/core/src/java/org/apache/solr/update/PeerSyncWithLeader.java@@ -24,6 +24,7 @@ import static org.apache.solr.update.PeerSync.percentile;import com.codahale.metrics.Counter;import com.codahale.metrics.Timer;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.util.List;@@ -98,8 +99,10 @@ public class PeerSyncWithLeader implements SolrMetricProducer {return solrMetricsContext;}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {this.solrMetricsContext = parentContext.getChildContext(this);syncTime = solrMetricsContext.timer("time", scope, METRIC_SCOPE);syncErrors = solrMetricsContext.counter("errors", scope, METRIC_SCOPE);diff --git a/solr/core/src/java/org/apache/solr/update/UpdateLog.java b/solr/core/src/java/org/apache/solr/update/UpdateLog.javaindex cd7e1bbae3f..3d379c5d808 100644--- a/solr/core/src/java/org/apache/solr/update/UpdateLog.java+++ b/solr/core/src/java/org/apache/solr/update/UpdateLog.java@@ -23,6 +23,7 @@ import com.carrotsearch.hppc.LongHashSet;import com.carrotsearch.hppc.LongSet;import com.codahale.metrics.Gauge;import com.codahale.metrics.Meter;+import io.opentelemetry.api.common.Attributes;import java.io.Closeable;import java.io.IOException;import java.io.UncheckedIOException;@@ -626,8 +627,10 @@ public class UpdateLog implements PluginInfoInitialized, SolrMetricProducer {}}+ // TODO SOLR-17458: Migrate to Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {solrMetricsContext = parentContext.getChildContext(this);bufferedOpsGauge =() -> {diff --git a/solr/core/src/java/org/apache/solr/update/UpdateShardHandler.java b/solr/core/src/java/org/apache/solr/update/UpdateShardHandler.javaindex e214c0cc3a8..157fc1513b0 100644--- a/solr/core/src/java/org/apache/solr/update/UpdateShardHandler.java+++ b/solr/core/src/java/org/apache/solr/update/UpdateShardHandler.java@@ -17,6 +17,7 @@package org.apache.solr.update;import com.google.common.annotations.VisibleForTesting;+import io.opentelemetry.api.common.Attributes;import java.lang.invoke.MethodHandles;import java.util.Set;import java.util.concurrent.ExecutorService;@@ -162,11 +163,14 @@ public class UpdateShardHandler implements SolrInfoBean {return this.getClass().getName();}+ // TODO SOLR-17458: Add Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {solrMetricsContext = parentContext.getChildContext(this);String expandedScope = SolrMetricManager.mkName(scope, getCategory().name());- trackHttpSolrMetrics.initializeMetrics(solrMetricsContext, expandedScope);+ // TODO SOLR-17458: Add Otel+ trackHttpSolrMetrics.initializeMetrics(solrMetricsContext, Attributes.empty(), expandedScope);updateExecutor =MetricUtils.instrumentedExecutorService(updateExecutor,diff --git a/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpListenerFactory.java b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpListenerFactory.javaindex 6af4bccaad3..68436cfe76d 100644--- a/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpListenerFactory.java+++ b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpListenerFactory.java@@ -20,6 +20,7 @@ package org.apache.solr.util.stats;import static org.apache.solr.metrics.SolrMetricManager.mkName;import com.codahale.metrics.Timer;+import io.opentelemetry.api.common.Attributes;import io.opentelemetry.api.trace.Span;import java.util.Locale;import java.util.Map;@@ -124,8 +125,10 @@ public class InstrumentedHttpListenerFactory implements SolrMetricProducer, Httpreturn solrMetricsContext.timer(nameStrategy.getNameFor(scope, request));}+ // TODO SOLR-17458: Add Otel@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {this.solrMetricsContext = parentContext;this.scope = scope;}diff --git a/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpRequestExecutor.java b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpRequestExecutor.javanew file mode 100644index 00000000000..0473d9e0cee--- /dev/null+++ b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpRequestExecutor.java@@ -0,0 +1,161 @@+/*+ * 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.util.stats;++import static org.apache.solr.metrics.SolrMetricManager.mkName;++import com.codahale.metrics.Timer;+import io.opentelemetry.api.common.Attributes;+import java.io.IOException;+import java.net.URISyntaxException;+import java.util.Locale;+import java.util.Map;+import org.apache.http.HttpClientConnection;+import org.apache.http.HttpException;+import org.apache.http.HttpRequest;+import org.apache.http.HttpResponse;+import org.apache.http.RequestLine;+import org.apache.http.client.methods.HttpRequestWrapper;+import org.apache.http.client.utils.URIBuilder;+import org.apache.http.protocol.HttpContext;+import org.apache.http.protocol.HttpRequestExecutor;+import org.apache.solr.common.util.CollectionUtil;+import org.apache.solr.metrics.SolrMetricProducer;+import org.apache.solr.metrics.SolrMetricsContext;+import org.apache.solr.util.tracing.TraceUtils;++/**+ * Sub-class of HttpRequestExecutor which tracks metrics interesting to solr Inspired and partially+ * copied from dropwizard httpclient library+ */+public class InstrumentedHttpRequestExecutor extends HttpRequestExecutor+ implements SolrMetricProducer {+ public static final HttpClientMetricNameStrategy QUERYLESS_URL_AND_METHOD =+ (scope, request) -> {+ try {+ final RequestLine requestLine = request.getRequestLine();+ String schemeHostPort = null;+ if (request instanceof HttpRequestWrapper wrapper) {+ if (wrapper.getTarget() != null) {+ schemeHostPort =+ wrapper.getTarget().getSchemeName()+ + "://"+ + wrapper.getTarget().getHostName()+ + ":"+ + wrapper.getTarget().getPort();+ }+ }+ final URIBuilder url = new URIBuilder(requestLine.getUri());+ return mkName(+ (schemeHostPort != null ? schemeHostPort : "")+ + url.removeQuery().build().toString()+ + "."+ + methodNameString(request),+ scope);+ } catch (URISyntaxException e) {+ throw new IllegalArgumentException(e);+ }+ };++ public static final HttpClientMetricNameStrategy METHOD_ONLY =+ (scope, request) -> mkName(methodNameString(request), scope);++ public static final HttpClientMetricNameStrategy HOST_AND_METHOD =+ (scope, request) -> {+ try {+ final RequestLine requestLine = request.getRequestLine();+ String schemeHostPort = null;+ if (request instanceof HttpRequestWrapper wrapper) {+ if (wrapper.getTarget() != null) {+ schemeHostPort =+ wrapper.getTarget().getSchemeName()+ + "://"+ + wrapper.getTarget().getHostName()+ + ":"+ + wrapper.getTarget().getPort();+ }+ }+ final URIBuilder url = new URIBuilder(requestLine.getUri());+ return mkName(+ (schemeHostPort != null ? schemeHostPort : "") + "." + methodNameString(request),+ scope);+ } catch (URISyntaxException e) {+ throw new IllegalArgumentException(e);+ }+ };++ public static final Map<String, HttpClientMetricNameStrategy> KNOWN_METRIC_NAME_STRATEGIES =+ CollectionUtil.newHashMap(3);++ static {+ KNOWN_METRIC_NAME_STRATEGIES.put("queryLessURLAndMethod", QUERYLESS_URL_AND_METHOD);+ KNOWN_METRIC_NAME_STRATEGIES.put("hostAndMethod", HOST_AND_METHOD);+ KNOWN_METRIC_NAME_STRATEGIES.put("methodOnly", METHOD_ONLY);+ }++ protected String scope;+ protected HttpClientMetricNameStrategy nameStrategy;+ protected SolrMetricsContext solrMetricsContext;++ public InstrumentedHttpRequestExecutor(+ int waitForContinue, HttpClientMetricNameStrategy nameStrategy) {+ super(waitForContinue);+ this.nameStrategy = nameStrategy;+ }++ public InstrumentedHttpRequestExecutor(HttpClientMetricNameStrategy nameStrategy) {+ this.nameStrategy = nameStrategy;+ }++ private static String methodNameString(HttpRequest request) {+ return request.getRequestLine().getMethod().toLowerCase(Locale.ROOT) + ".requests";+ }++ @Override+ public SolrMetricsContext getSolrMetricsContext() {+ return solrMetricsContext;+ }++ @Override+ public HttpResponse execute(HttpRequest request, HttpClientConnection conn, HttpContext context)+ throws IOException, HttpException {+ Timer.Context timerContext = null;+ if (solrMetricsContext != null) {+ timerContext = timer(request).time();+ }+ TraceUtils.injectTraceContext(request);+ try {+ return super.execute(request, conn, context);+ } finally {+ if (timerContext != null) {+ timerContext.stop();+ }+ }+ }++ private Timer timer(HttpRequest request) {+ return solrMetricsContext.timer(null, nameStrategy.getNameFor(scope, request));+ }++ @Override+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ this.solrMetricsContext = parentContext.getChildContext(this);+ this.scope = scope;+ }+}diff --git a/solr/core/src/java/org/apache/solr/util/stats/InstrumentedPoolingHttpClientConnectionManager.java b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedPoolingHttpClientConnectionManager.javanew file mode 100644index 00000000000..8174613c72a--- /dev/null+++ b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedPoolingHttpClientConnectionManager.java@@ -0,0 +1,78 @@+/*+ * 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.util.stats;++import io.opentelemetry.api.common.Attributes;+import org.apache.http.config.Registry;+import org.apache.http.conn.socket.ConnectionSocketFactory;+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;+import org.apache.solr.metrics.SolrMetricManager;+import org.apache.solr.metrics.SolrMetricProducer;+import org.apache.solr.metrics.SolrMetricsContext;++/**+ * Sub-class of PoolingHttpClientConnectionManager which tracks metrics interesting to Solr.+ * Inspired by dropwizard metrics-httpclient library implementation.+ */+public class InstrumentedPoolingHttpClientConnectionManager+ extends PoolingHttpClientConnectionManager implements SolrMetricProducer {++ private SolrMetricsContext solrMetricsContext;++ public InstrumentedPoolingHttpClientConnectionManager(+ Registry<ConnectionSocketFactory> socketFactoryRegistry) {+ super(socketFactoryRegistry);+ }++ @Override+ public SolrMetricsContext getSolrMetricsContext() {+ return solrMetricsContext;+ }++ // TODO SOLR-17458: Migrate to Otel+ @Override+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ this.solrMetricsContext = parentContext.getChildContext(this);+ solrMetricsContext.gauge(+ () -> getTotalStats().getAvailable(),+ true,+ SolrMetricManager.mkName("availableConnections", scope));+ // this acquires a lock on the connection pool; remove if contention sucks+ solrMetricsContext.gauge(+ () -> getTotalStats().getLeased(),+ true,+ SolrMetricManager.mkName("leasedConnections", scope));+ solrMetricsContext.gauge(+ () -> getTotalStats().getMax(), true, SolrMetricManager.mkName("maxConnections", scope));+ solrMetricsContext.gauge(+ () -> getTotalStats().getPending(),+ true,+ SolrMetricManager.mkName("pendingConnections", scope));+ }++ @Override+ public void close() {+ super.close();+ try {+ SolrMetricProducer.super.close();+ } catch (Exception e) {+ throw new RuntimeException("Exception closing.", e);+ }+ }+}diff --git a/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java b/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.javaindex d0f37de403f..8b79fc26b35 100644--- a/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java+++ b/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java@@ -27,6 +27,8 @@ import com.codahale.metrics.MetricRegistry;import com.codahale.metrics.Snapshot;import com.codahale.metrics.Timer;import io.opentelemetry.api.GlobalOpenTelemetry;+import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.common.AttributesBuilder;import io.opentelemetry.api.metrics.MeterProvider;import java.beans.BeanInfo;import java.beans.IntrospectionException;@@ -53,6 +55,7 @@ import java.util.function.Predicate;import org.apache.solr.common.ConditionalKeyMapWriter;import org.apache.solr.common.IteratorWriter;import org.apache.solr.common.MapWriter;+import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrInfoBean;@@ -858,4 +861,20 @@ public class MetricUtils {public static MeterProvider getMeterProvider() {return GlobalOpenTelemetry.getMeterProvider();}++ public static Attributes createAttributes(String... attributes) {+ if (attributes.length % 2 == 1) {+ throw new SolrException(+ SolrException.ErrorCode.SERVER_ERROR, "Odd number of field/value strings passed");+ }++ AttributesBuilder builder = Attributes.builder();+ for (int i = 0; i < attributes.length; i += 2) {+ String key = attributes[i];+ String value = attributes[i + 1];+ builder.put(key, value);+ }++ return builder.build();+ }}diff --git a/solr/core/src/test/org/apache/solr/SolrInfoBeanTest.java b/solr/core/src/test/org/apache/solr/SolrInfoBeanTest.javaindex c09be4fccfd..e8e61bc5fe5 100644--- a/solr/core/src/test/org/apache/solr/SolrInfoBeanTest.java+++ b/solr/core/src/test/org/apache/solr/SolrInfoBeanTest.java@@ -16,6 +16,7 @@*/package org.apache.solr;+import io.opentelemetry.api.common.Attributes;import java.net.URI;import java.net.URL;import java.nio.file.Files;@@ -64,7 +65,8 @@ public class SolrInfoBeanTest extends SolrTestCaseJ4 {if (SolrInfoBean.class.isAssignableFrom(clazz)) {try {SolrInfoBean info = clazz.asSubclass(SolrInfoBean.class).getConstructor().newInstance();- info.initializeMetrics(solrMetricsContext, scope);+ // TODO SOLR-17458: Fix test later+ info.initializeMetrics(solrMetricsContext, Attributes.empty(), scope);// System.out.println( info.getClass() );assertNotNull(info.getClass().getCanonicalName(), info.getName());diff --git a/solr/core/src/test/org/apache/solr/blockcache/BufferStoreTest.java b/solr/core/src/test/org/apache/solr/blockcache/BufferStoreTest.javaindex 77d92378f1a..46bc29685ca 100644--- a/solr/core/src/test/org/apache/solr/blockcache/BufferStoreTest.java+++ b/solr/core/src/test/org/apache/solr/blockcache/BufferStoreTest.java@@ -16,6 +16,7 @@*/package org.apache.solr.blockcache;+import io.opentelemetry.api.common.Attributes;import java.math.BigDecimal;import java.util.Map;import org.apache.lucene.tests.util.TestUtil;@@ -42,7 +43,8 @@ public class BufferStoreTest extends SolrTestCase {String registry = TestUtil.randomSimpleString(random(), 2, 10);String scope = TestUtil.randomSimpleString(random(), 2, 10);SolrMetricsContext solrMetricsContext = new SolrMetricsContext(metricManager, registry, "foo");- metrics.initializeMetrics(solrMetricsContext, scope);+ // TODO SOLR-17458: Fix test later+ metrics.initializeMetrics(solrMetricsContext, Attributes.empty(), scope);metricsMap =(MetricsMap)((SolrMetricManager.GaugeWrapper)diff --git a/solr/core/src/test/org/apache/solr/handler/RequestHandlerBaseTest.java b/solr/core/src/test/org/apache/solr/handler/RequestHandlerBaseTest.javaindex ba8fff85468..6b23c268556 100644--- a/solr/core/src/test/org/apache/solr/handler/RequestHandlerBaseTest.java+++ b/solr/core/src/test/org/apache/solr/handler/RequestHandlerBaseTest.java@@ -17,15 +17,23 @@package org.apache.solr.handler;+import static org.apache.solr.metrics.SolrMetricProducer.TYPE_ATTR;import static org.mockito.ArgumentMatchers.any;+import static org.mockito.ArgumentMatchers.argThat;+import static org.mockito.ArgumentMatchers.eq;import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;-import static org.mockito.Mockito.verifyNoInteractions;import static org.mockito.Mockito.when;import com.codahale.metrics.Counter;import com.codahale.metrics.Meter;import com.codahale.metrics.Timer;+import io.opentelemetry.api.common.AttributeKey;+import io.opentelemetry.api.common.Attributes;+import io.opentelemetry.api.metrics.LongCounter;+import io.opentelemetry.api.metrics.LongHistogram;import java.util.Map;import org.apache.solr.SolrTestCaseJ4;import org.apache.solr.common.SolrException;@@ -44,8 +52,11 @@ import org.junit.Test;/** Unit tests for the metric and exception handling in {@link RequestHandlerBase} */public class RequestHandlerBaseTest extends SolrTestCaseJ4 {+ public static final AttributeKey<String> SOURCE_ATTR = AttributeKey.stringKey("source");private SolrCore solrCore;private CoreContainer coreContainer;+ private LongCounter mockLongCounter;+ private LongHistogram mockLongHistogram;@BeforeClasspublic static void ensureWorkingMockito() {@@ -56,6 +67,8 @@ public class RequestHandlerBaseTest extends SolrTestCaseJ4 {public void initMocks() {solrCore = mock(SolrCore.class);coreContainer = mock(CoreContainer.class);+ mockLongCounter = mock(LongCounter.class);+ mockLongHistogram = mock(LongHistogram.class);}@Test@@ -65,9 +78,13 @@ public class RequestHandlerBaseTest extends SolrTestCaseJ4 {RequestHandlerBase.processErrorMetricsOnException(e, metrics);- verify(metrics.numErrors).mark();- verify(metrics.numServerErrors).mark();- verifyNoInteractions(metrics.numClientErrors);+ verify(mockLongCounter, never())+ .add(+ eq(1L),+ argThat(+ attrs ->+ "errors".equals(attrs.get(TYPE_ATTR))+ && "source".equals(attrs.get(AttributeKey.stringKey("client")))));}@Test@@ -77,9 +94,20 @@ public class RequestHandlerBaseTest extends SolrTestCaseJ4 {RequestHandlerBase.processErrorMetricsOnException(e, metrics);- verifyNoInteractions(metrics.numErrors);- verifyNoInteractions(metrics.numServerErrors);- verifyNoInteractions(metrics.numClientErrors);+ verify(mockLongCounter, never())+ .add(+ eq(1L),+ argThat(+ attrs ->+ "errors".equals(attrs.get(TYPE_ATTR))+ && "client".equals(attrs.get(SOURCE_ATTR))));+ verify(mockLongCounter, never())+ .add(+ eq(1L),+ argThat(+ attrs ->+ "errors".equals(attrs.get(TYPE_ATTR))+ && "server".equals(attrs.get(SOURCE_ATTR))));}@Test@@ -89,9 +117,21 @@ public class RequestHandlerBaseTest extends SolrTestCaseJ4 {RequestHandlerBase.processErrorMetricsOnException(e, metrics);- verify(metrics.numErrors).mark();- verify(metrics.numClientErrors).mark();- verifyNoInteractions(metrics.numServerErrors);+ verify(mockLongCounter, times(1))+ .add(+ eq(1L),+ argThat(+ attrs ->+ "errors".equals(attrs.get(TYPE_ATTR))+ && "client".equals(attrs.get(SOURCE_ATTR))));++ verify(mockLongCounter, never())+ .add(+ eq(1L),+ argThat(+ attrs ->+ "errors".equals(attrs.get(TYPE_ATTR))+ && "server".equals(attrs.get(SOURCE_ATTR))));}@Test@@ -171,10 +211,17 @@ public class RequestHandlerBaseTest extends SolrTestCaseJ4 {// requires a MetricsManager, which requires ...private RequestHandlerBase.HandlerMetrics createHandlerMetrics() {final SolrMetricsContext metricsContext = mock(SolrMetricsContext.class);+when(metricsContext.timer(any(), any())).thenReturn(mock(Timer.class));when(metricsContext.meter(any(), any())).then(invocation -> mock(Meter.class));when(metricsContext.counter(any(), any())).thenReturn(mock(Counter.class));- return new RequestHandlerBase.HandlerMetrics(metricsContext, "someBaseMetricPath");+ when(metricsContext.longCounter(any(), any())).thenReturn(mockLongCounter);+ when(metricsContext.longHistogram(any(), any())).thenReturn(mockLongHistogram);++ return new RequestHandlerBase.HandlerMetrics(+ metricsContext,+ Attributes.of(AttributeKey.stringKey("scope"), "someBaseMetricPath"),+ "someBaseMetricPath");}}diff --git a/solr/core/src/test/org/apache/solr/handler/admin/MBeansHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/MBeansHandlerTest.javaindex 05232b7f05e..e7395188bb0 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/MBeansHandlerTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/MBeansHandlerTest.java@@ -16,6 +16,7 @@*/package org.apache.solr.handler.admin;+import io.opentelemetry.api.common.Attributes;import java.lang.invoke.MethodHandles;import java.util.ArrayList;import java.util.Collections;@@ -171,7 +172,8 @@ public class MBeansHandlerTest extends SolrTestCaseJ4 {}@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {this.solrMetricsContext = parentContext.getChildContext(this);}@@ -183,6 +185,7 @@ public class MBeansHandlerTest extends SolrTestCaseJ4 {bean.initializeMetrics(new SolrMetricsContext(h.getCoreContainer().getMetricManager(), "testMetricsSnapshot", "foobar"),+ Attributes.empty(),"foo");runSnapshots = true;Thread modifier =diff --git a/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.javaindex 5e17ca9cf8e..96001e15b63 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java@@ -18,8 +18,7 @@package org.apache.solr.handler.admin;import com.codahale.metrics.Counter;-import com.codahale.metrics.Gauge;-import com.codahale.metrics.SettableGauge;+import io.opentelemetry.api.common.Attributes;import io.prometheus.metrics.model.snapshots.CounterSnapshot;import io.prometheus.metrics.model.snapshots.GaugeSnapshot;import io.prometheus.metrics.model.snapshots.Labels;@@ -37,12 +36,9 @@ import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.core.PluginBag;import org.apache.solr.core.PluginInfo;-import org.apache.solr.core.SolrCore;import org.apache.solr.handler.RequestHandlerBase;import org.apache.solr.metrics.MetricsMap;-import org.apache.solr.metrics.SolrMetricManager;import org.apache.solr.metrics.SolrMetricsContext;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrRequestHandler;import org.apache.solr.response.SolrQueryResponse;@@ -69,15 +65,6 @@ public class MetricsHandlerTest extends SolrTestCaseJ4 {c = h.getCoreContainer().getMetricManager().counter(null, "solr.jetty", "solrtest_foo:bar");c.inc(3);- // Manually register for Prometheus Formatter tests- registerGauge("solr.jvm", "gc.G1-Old-Generation.count");- registerGauge("solr.jvm", "gc.G1-Old-Generation.time");- registerGauge("solr.jvm", "memory.heap.committed");- registerGauge("solr.jvm", "memory.pools.CodeHeap-'non-nmethods'.committed");- registerGauge("solr.jvm", "threads.count");- registerGauge("solr.jvm", "os.availableProcessors");- registerGauge("solr.jvm", "buffers.direct.Count");- registerGauge("solr.jvm", "buffers.direct.MemoryUsed");h.getCoreContainer().getMetricManager().meter(null, "solr.jetty", "org.eclipse.jetty.server.handler.DefaultHandler.2xx-responses");@@ -706,439 +693,6 @@ public class MetricsHandlerTest extends SolrTestCaseJ4 {assertTrue(map.toString(), map.containsKey("count"));}- @Test- @SuppressWarnings("unchecked")- public void testPrometheusMetricsCore() throws Exception {- MetricsHandler handler = new MetricsHandler(h.getCoreContainer());-- SolrQueryResponse resp = new SolrQueryResponse();- handler.handleRequestBody(- req(- CommonParams.QT,- "/admin/metrics",- MetricsHandler.COMPACT_PARAM,- "false",- CommonParams.WT,- "prometheus"),- resp);-- NamedList<?> values = resp.getValues();- assertNotNull(values.get("metrics"));- values = (NamedList<?>) values.get("metrics");- SolrPrometheusFormatter formatter = (SolrPrometheusFormatter) values.get("solr.core");- assertNotNull(formatter);- MetricSnapshots actualSnapshots = formatter.collect();- assertNotNull(actualSnapshots);-- MetricSnapshot actualSnapshot =- getMetricSnapshot(actualSnapshots, "solr_metrics_core_request_time_ms");- SummarySnapshot.SummaryDataPointSnapshot actualSummaryDataPoint =- getSummaryDataPointSnapshot(- actualSnapshot,- Labels.of("category", "QUERY", "core", "collection1", "handler", "/select[shard]"));- assertEquals(0, (float) actualSummaryDataPoint.getCount(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_core_requests");- CounterSnapshot.CounterDataPointSnapshot actualCounterDataPoint =- getCounterDatapointSnapshot(- actualSnapshot,- Labels.of(- "category",- "QUERY",- "core",- "collection1",- "handler",- "/select[shard]",- "type",- "requests"));- assertEquals(0, actualCounterDataPoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_core_cache");- GaugeSnapshot.GaugeDataPointSnapshot actualGaugeDataPoint =- getGaugeDatapointSnapshot(- actualSnapshot,- Labels.of("cacheType", "fieldValueCache", "core", "collection1", "item", "hits"));- assertEquals(0, actualGaugeDataPoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_core_highlighter_requests");- actualCounterDataPoint =- getCounterDatapointSnapshot(- actualSnapshot,- Labels.of("item", "default", "core", "collection1", "type", "SolrFragmenter"));- assertEquals(0, actualCounterDataPoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_core_searcher_documents");- actualGaugeDataPoint =- getGaugeDatapointSnapshot(- actualSnapshot, Labels.of("core", "collection1", "type", "numDocs"));- assertEquals(0, actualGaugeDataPoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_core_update_handler");- actualGaugeDataPoint =- getGaugeDatapointSnapshot(- actualSnapshot,- Labels.of(- "category",- "UPDATE",- "core",- "collection1",- "type",- "adds",- "handler",- "updateHandler"));- assertEquals(0, actualGaugeDataPoint.getValue(), 0);-- actualSnapshot =- getMetricSnapshot(actualSnapshots, "solr_metrics_core_searcher_warmup_time_ms");- actualSummaryDataPoint =- getSummaryDataPointSnapshot(- actualSnapshot, Labels.of("core", "collection1", "type", "warmup"));- assertEquals(0, (float) actualSummaryDataPoint.getCount(), 0);-- handler.close();- }-- @Test- @SuppressWarnings("unchecked")- public void testPrometheusMetricsNode() throws Exception {- MetricsHandler handler = new MetricsHandler(h.getCoreContainer());-- SolrQueryResponse resp = new SolrQueryResponse();- handler.handleRequestBody(- req(- CommonParams.QT,- "/admin/metrics",- MetricsHandler.COMPACT_PARAM,- "false",- CommonParams.WT,- "prometheus"),- resp);-- NamedList<?> values = resp.getValues();- assertNotNull(values.get("metrics"));- values = (NamedList<?>) values.get("metrics");- SolrPrometheusFormatter formatter = (SolrPrometheusFormatter) values.get("solr.node");- assertNotNull(formatter);- MetricSnapshots actualSnapshots = formatter.collect();- assertNotNull(actualSnapshots);-- MetricSnapshot actualSnapshot =- getMetricSnapshot(actualSnapshots, "solr_metrics_node_requests");-- CounterSnapshot.CounterDataPointSnapshot actualCounterDataPoint =- getCounterDatapointSnapshot(- actualSnapshot,- Labels.of("category", "ADMIN", "handler", "/admin/info", "type", "requests"));- assertEquals(0, actualCounterDataPoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_node_requests_time");- actualCounterDataPoint =- getCounterDatapointSnapshot(- actualSnapshot, Labels.of("category", "ADMIN", "handler", "/admin/info"));- assertEquals(0, actualCounterDataPoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_node_thread_pool");- actualCounterDataPoint =- getCounterDatapointSnapshot(- actualSnapshot,- Labels.of(- "category",- "ADMIN",- "executer",- "parallelCoreAdminExecutor",- "handler",- "/admin/cores",- "task",- "completed"));- assertEquals(0, actualCounterDataPoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_node_core_root_fs_bytes");- var actualGaugeDataPoint =- getGaugeDatapointSnapshot(- actualSnapshot, Labels.of("category", "CONTAINER", "item", "totalSpace"));- assertNotNull(actualGaugeDataPoint);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_node_cores");- actualGaugeDataPoint =- getGaugeDatapointSnapshot(- actualSnapshot, Labels.of("category", "CONTAINER", "item", "lazy"));- assertEquals(0, actualGaugeDataPoint.getValue(), 0);-- handler.close();- }-- @Test- @SuppressWarnings("unchecked")- public void testPrometheusMetricsJvm() throws Exception {- // Some JVM metrics are non-deterministic due to testing environment such as- // availableProcessors. We confirm snapshot exists and is nonNull instead.- MetricsHandler handler = new MetricsHandler(h.getCoreContainer());-- SolrQueryResponse resp = new SolrQueryResponse();- handler.handleRequestBody(- req(- CommonParams.QT,- "/admin/metrics",- MetricsHandler.COMPACT_PARAM,- "false",- CommonParams.WT,- "prometheus"),- resp);-- NamedList<?> values = resp.getValues();- assertNotNull(values.get("metrics"));- values = (NamedList<?>) values.get("metrics");- SolrPrometheusFormatter formatter = (SolrPrometheusFormatter) values.get("solr.jvm");- assertNotNull(formatter);- MetricSnapshots actualSnapshots = formatter.collect();- assertNotNull(actualSnapshots);-- MetricSnapshot actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_jvm_threads");- GaugeSnapshot.GaugeDataPointSnapshot actualGaugeDataPoint =- getGaugeDatapointSnapshot(actualSnapshot, Labels.of("item", "count"));- assertNotNull(actualGaugeDataPoint);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_jvm_memory_pools_bytes");- actualGaugeDataPoint =- getGaugeDatapointSnapshot(- actualSnapshot, Labels.of("item", "committed", "space", "CodeHeap-'non-nmethods'"));- assertNotNull(actualGaugeDataPoint);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_jvm_buffers");- actualGaugeDataPoint =- getGaugeDatapointSnapshot(actualSnapshot, Labels.of("item", "Count", "pool", "direct"));- assertNotNull(actualGaugeDataPoint);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_jvm_heap");- actualGaugeDataPoint =- getGaugeDatapointSnapshot(actualSnapshot, Labels.of("item", "committed", "memory", "heap"));- assertNotNull(actualGaugeDataPoint);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_jvm_buffers_bytes");- actualGaugeDataPoint =- getGaugeDatapointSnapshot(- actualSnapshot, Labels.of("item", "MemoryUsed", "pool", "direct"));- assertNotNull(actualGaugeDataPoint);-- handler.close();- }-- @Test- @SuppressWarnings("unchecked")- public void testPrometheusMetricsJetty() throws Exception {- MetricsHandler handler = new MetricsHandler(h.getCoreContainer());-- SolrQueryResponse resp = new SolrQueryResponse();- handler.handleRequestBody(- req(- CommonParams.QT,- "/admin/metrics",- MetricsHandler.COMPACT_PARAM,- "false",- CommonParams.WT,- "prometheus"),- resp);-- NamedList<?> values = resp.getValues();- assertNotNull(values.get("metrics"));- values = (NamedList<?>) values.get("metrics");- SolrPrometheusFormatter formatter = (SolrPrometheusFormatter) values.get("solr.jetty");- assertNotNull(formatter);- MetricSnapshots actualSnapshots = formatter.collect();- assertNotNull(actualSnapshots);-- MetricSnapshot actualSnapshot =- getMetricSnapshot(actualSnapshots, "solr_metrics_jetty_response");- CounterSnapshot.CounterDataPointSnapshot actualCounterDatapoint =- getCounterDatapointSnapshot(actualSnapshot, Labels.of("status", "2xx"));- assertEquals(0, actualCounterDatapoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_jetty_requests");- actualCounterDatapoint =- getCounterDatapointSnapshot(actualSnapshot, Labels.of("method", "active"));- assertEquals(0, actualCounterDatapoint.getValue(), 0);-- actualSnapshot = getMetricSnapshot(actualSnapshots, "solr_metrics_jetty_dispatches");- actualCounterDatapoint = getCounterDatapointSnapshot(actualSnapshot, Labels.of());- assertEquals(0, actualCounterDatapoint.getValue(), 0);-- handler.close();- }-- @Test- @SuppressWarnings("unchecked")- public void testPrometheusMetricsFilter() throws Exception {- MetricsHandler handler = new MetricsHandler(h.getCoreContainer());-- SolrQueryResponse resp = new SolrQueryResponse();- handler.handleRequestBody(- req(- CommonParams.QT,- "/admin/metrics",- CommonParams.WT,- "prometheus",- "group",- "core",- "type",- "counter",- "prefix",- "QUERY"),- resp);-- NamedList<?> values = resp.getValues();- assertNotNull(values.get("metrics"));- values = (NamedList<?>) values.get("metrics");- SolrPrometheusFormatter formatter = (SolrPrometheusFormatter) values.get("solr.core");- assertNotNull(formatter);- MetricSnapshots actualSnapshots = formatter.collect();- assertNotNull(actualSnapshots);-- actualSnapshots.forEach(- (k) -> {- k.getDataPoints()- .forEach(- (datapoint) -> {- assertTrue(datapoint instanceof CounterSnapshot.CounterDataPointSnapshot);- assertEquals("QUERY", datapoint.getLabels().get("category"));- });- });-- handler.close();- }-- @Test- public void testMetricsUnload() throws Exception {-- SolrCore core = h.getCoreContainer().getCore("collection1");- // .getRequestHandlers().put("/dumphandler", new DumpRequestHandler());- RefreshablePluginHolder pluginHolder = null;- try {- PluginInfo info =- new PluginInfo(- SolrRequestHandler.TYPE,- Map.of("name", "/dumphandler", "class", DumpRequestHandler.class.getName()));- DumpRequestHandler requestHandler = new DumpRequestHandler();- requestHandler.gaugevals = Map.of("d_k1", "v1", "d_k2", "v2");- pluginHolder = new RefreshablePluginHolder(info, requestHandler);- core.getRequestHandlers().put("/dumphandler", pluginHolder);-- } finally {- core.close();- }-- MetricsHandler handler = new MetricsHandler(h.getCoreContainer());-- SolrQueryResponse resp = new SolrQueryResponse();- handler.handleRequestBody(- req(- CommonParams.QT,- "/admin/metrics",- CommonParams.WT,- "json",- MetricsHandler.COMPACT_PARAM,- "true",- "key",- "solr.core.collection1:QUERY./dumphandler.dumphandlergauge"),- resp);-- assertEquals(- "v1",- resp.getValues()- ._getStr(- Arrays.asList(- "metrics", "solr.core.collection1:QUERY./dumphandler.dumphandlergauge", "d_k1"),- null));- assertEquals(- "v2",- resp.getValues()- ._getStr(- Arrays.asList(- "metrics", "solr.core.collection1:QUERY./dumphandler.dumphandlergauge", "d_k2"),- null));- pluginHolder.closeHandler();- resp = new SolrQueryResponse();- handler.handleRequestBody(- req(- CommonParams.QT,- "/admin/metrics",- CommonParams.WT,- "json",- MetricsHandler.COMPACT_PARAM,- "true",- "key",- "solr.core.collection1:QUERY./dumphandler.dumphandlergauge"),- resp);-- assertNull(- resp.getValues()- ._getStr(- Arrays.asList(- "metrics", "solr.core.collection1:QUERY./dumphandler.dumphandlergauge", "d_k1"),- null));- assertNull(- resp.getValues()- ._getStr(- Arrays.asList(- "metrics", "solr.core.collection1:QUERY./dumphandler.dumphandlergauge", "d_k2"),- null));-- DumpRequestHandler requestHandler = new DumpRequestHandler();- requestHandler.gaugevals = Map.of("d_k1", "v1.1", "d_k2", "v2.1");- pluginHolder.reset(requestHandler);- resp = new SolrQueryResponse();- handler.handleRequestBody(- req(- CommonParams.QT,- "/admin/metrics",- CommonParams.WT,- "json",- MetricsHandler.COMPACT_PARAM,- "true",- "key",- "solr.core.collection1:QUERY./dumphandler.dumphandlergauge"),- resp);-- assertEquals(- "v1.1",- resp.getValues()- ._getStr(- Arrays.asList(- "metrics", "solr.core.collection1:QUERY./dumphandler.dumphandlergauge", "d_k1"),- null));- assertEquals(- "v2.1",- resp.getValues()- ._getStr(- Arrays.asList(- "metrics", "solr.core.collection1:QUERY./dumphandler.dumphandlergauge", "d_k2"),- null));-- handler.close();- }-- public static void registerGauge(String registry, String metricName) {- Gauge<Number> metric =- new SettableGauge<>() {- @Override- public void setValue(Number value) {}-- @Override- public Number getValue() {- return 0;- }- };- h.getCoreContainer()- .getMetricManager()- .registerGauge(- null,- registry,- metric,- "",- SolrMetricManager.ResolutionStrategy.IGNORE,- metricName,- "");- }-private MetricSnapshot getMetricSnapshot(MetricSnapshots snapshots, String metricName) {return snapshots.stream().filter(ss -> ss.getMetadata().getPrometheusName().equals(metricName))@@ -1199,7 +753,8 @@ public class MetricsHandlerTest extends SolrTestCaseJ4 {void reset(DumpRequestHandler rh) {this.rh = rh;- if (metricsInfo != null) this.rh.initializeMetrics(metricsInfo, "/dumphandler");+ if (metricsInfo != null)+ this.rh.initializeMetrics(metricsInfo, Attributes.empty(), "/dumphandler");}@Override@@ -1224,8 +779,9 @@ public class MetricsHandlerTest extends SolrTestCaseJ4 {}@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {- super.initializeMetrics(parentContext, scope);+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ super.initializeMetrics(parentContext, attributes, scope);MetricsMap metrics = new MetricsMap(map -> gaugevals.forEach((k, v) -> map.putNoEx(k, v)));solrMetricsContext.gauge(metrics, true, "dumphandlergauge", getCategory().toString(), scope);}diff --git a/solr/core/src/test/org/apache/solr/metrics/SolrCoreMetricTest.java b/solr/core/src/test/org/apache/solr/metrics/SolrCoreMetricTest.javadeleted file mode 100644index 9ee34056e5e..00000000000--- a/solr/core/src/test/org/apache/solr/metrics/SolrCoreMetricTest.java+++ /dev/null@@ -1,82 +0,0 @@-/*- * 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 com.codahale.metrics.Metric;-import io.prometheus.metrics.model.snapshots.Labels;-import org.apache.solr.SolrTestCaseJ4;-import org.apache.solr.metrics.prometheus.SolrPrometheusFormatter;-import org.apache.solr.metrics.prometheus.core.SolrCoreMetric;-import org.junit.Test;--public class SolrCoreMetricTest extends SolrTestCaseJ4 {-- @Test- public void testStandaloneDefaultLabels() {- String dropwizardMetricName = "core_Core_Name.test_core_metric";-- String expectedCoreName = "Core_Name";- String expectedMetricName = "test_core_metric";- Labels expectedLabels = Labels.of("core", expectedCoreName);- TestSolrCoreMetric testSolrCoreMetric = new TestSolrCoreMetric(null, dropwizardMetricName);-- assertEquals(expectedCoreName, testSolrCoreMetric.coreName);- assertEquals(expectedMetricName, testSolrCoreMetric.metricName);- assertEquals(expectedLabels, testSolrCoreMetric.getLabels());- }-- @Test- public void testCloudDefaultLabels() {- String dropwizardMetricName = "core_Collection_Name_shard1_replica_n1.test_core_metric";-- String expectedCoreName = "core_Collection_Name_shard1_replica_n1";- String expectedMetricName = "test_core_metric";- String expectedCollectionName = "Collection_Name";- String expectedShardName = "shard1";- String expectedReplicaName = "replica_n1";-- Labels expectedLabels =- Labels.of(- "core",- expectedCoreName,- "collection",- expectedCollectionName,- "shard",- expectedShardName,- "replica",- expectedReplicaName);- TestSolrCoreMetric testSolrCoreMetric = new TestSolrCoreMetric(null, dropwizardMetricName);-- assertEquals(expectedCoreName, testSolrCoreMetric.coreName);- assertEquals(expectedMetricName, testSolrCoreMetric.metricName);- assertEquals(expectedLabels, testSolrCoreMetric.getLabels());- }-- static class TestSolrCoreMetric extends SolrCoreMetric {- public TestSolrCoreMetric(Metric dropwizardMetric, String metricName) {- super(dropwizardMetric, metricName);- }-- @Override- public SolrCoreMetric parseLabels() {- return null;- }-- @Override- public void toPrometheus(SolrPrometheusFormatter formatter) {}- }-}diff --git a/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java b/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.javaindex e1cd960eb15..d22e520d207 100644--- a/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java+++ b/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java@@ -17,6 +17,7 @@package org.apache.solr.metrics;import com.codahale.metrics.Counter;+import io.opentelemetry.api.common.Attributes;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;@@ -88,7 +89,8 @@ public final class SolrMetricTestUtils {SolrMetricsContext solrMetricsContext;@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {this.solrMetricsContext = parentContext.getChildContext(this);if (category == null) {throw new IllegalArgumentException("null category");diff --git a/solr/core/src/test/org/apache/solr/response/TestPrometheusResponseWriter.java b/solr/core/src/test/org/apache/solr/response/TestPrometheusResponseWriter.javaindex f52b76d5706..4792be1021c 100644--- a/solr/core/src/test/org/apache/solr/response/TestPrometheusResponseWriter.java+++ b/solr/core/src/test/org/apache/solr/response/TestPrometheusResponseWriter.java@@ -114,6 +114,10 @@ public class TestPrometheusResponseWriter extends SolrTestCaseJ4 {} else {actualValue = actualMetric.split(" ")[1];}+ if (actualMetric.startsWith("target_info")) {+ // Skip standard OTEL metric+ return;+ }assertTrue("All metrics should start with 'solr_metrics_'",actualMetric.startsWith("solr_metrics_"));@@ -127,54 +131,6 @@ public class TestPrometheusResponseWriter extends SolrTestCaseJ4 {}}- public void testPrometheusDummyOutput() throws Exception {- String expectedCore =- "solr_metrics_core_requests_total{category=\"QUERY\",core=\"collection1\",handler=\"/dummy/metrics\",type=\"requests\"} 10.0";- String expectedNode =- "solr_metrics_node_requests_total{category=\"ADMIN\",handler=\"/dummy/metrics\",type=\"requests\"} 20.0";- String expectedJetty = "solr_metrics_jetty_response_total{status=\"2xx\"} 30.0";- String expectedJvm = "solr_metrics_jvm_gc{item=\"dummyMetrics\"} 0.0";-- ModifiableSolrParams params = new ModifiableSolrParams();- params.set("wt", "prometheus");- var req = new GenericSolrRequest(METHOD.GET, "/admin/metrics", SolrRequestType.ADMIN, params);- req.setResponseParser(new NoOpResponseParser("prometheus"));-- try (SolrClient adminClient = getHttpSolrClient(solrClientTestRule.getBaseUrl())) {- NamedList<Object> res = adminClient.request(req);- assertNotNull("null response from server", res);- String output = (String) res.get("response");- assertEquals(- expectedCore,- output- .lines()- .filter(line -> line.contains(expectedCore))- .collect(Collectors.toList())- .get(0));- assertEquals(- expectedNode,- output- .lines()- .filter(line -> line.contains(expectedNode))- .collect(Collectors.toList())- .get(0));- assertEquals(- expectedJetty,- output- .lines()- .filter(line -> line.contains(expectedJetty))- .collect(Collectors.toList())- .get(0));- assertEquals(- expectedJvm,- output- .lines()- .filter(line -> line.contains(expectedJvm))- .collect(Collectors.toList())- .get(0));- }- }-private static void registerGauge(SolrMetricManager metricManager, String registry, String metricName) {Gauge<Number> metric =diff --git a/solr/core/src/test/org/apache/solr/search/TestCaffeineCache.java b/solr/core/src/test/org/apache/solr/search/TestCaffeineCache.javaindex b4c69600fbd..d70ea43a468 100644--- a/solr/core/src/test/org/apache/solr/search/TestCaffeineCache.java+++ b/solr/core/src/test/org/apache/solr/search/TestCaffeineCache.java@@ -19,6 +19,7 @@ package org.apache.solr.search;import com.github.benmanes.caffeine.cache.Cache;import com.github.benmanes.caffeine.cache.Caffeine;import com.github.benmanes.caffeine.cache.RemovalCause;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;@@ -47,10 +48,12 @@ public class TestCaffeineCache extends SolrTestCase {public void testSimple() {CaffeineCache<Integer, String> lfuCache = new CaffeineCache<>();SolrMetricsContext solrMetricsContext = new SolrMetricsContext(metricManager, registry, "foo");- lfuCache.initializeMetrics(solrMetricsContext, scope + "-1");+ // TODO SOLR-17458: Fix tests for OTEL+ lfuCache.initializeMetrics(solrMetricsContext, Attributes.empty(), scope + "-1");CaffeineCache<Integer, String> newLFUCache = new CaffeineCache<>();- newLFUCache.initializeMetrics(solrMetricsContext, scope + "-2");+ // TODO SOLR-17458: Fix tests for OTEL+ newLFUCache.initializeMetrics(solrMetricsContext, Attributes.empty(), scope + "-2");Map<String, String> params = new HashMap<>();params.put("size", "100");diff --git a/solr/core/src/test/org/apache/solr/search/TestSolrCachePerf.java b/solr/core/src/test/org/apache/solr/search/TestSolrCachePerf.javaindex 84d49d22f3b..989140b28f0 100644--- a/solr/core/src/test/org/apache/solr/search/TestSolrCachePerf.java+++ b/solr/core/src/test/org/apache/solr/search/TestSolrCachePerf.java@@ -16,6 +16,7 @@*/package org.apache.solr.search;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;@@ -105,7 +106,9 @@ public class TestSolrCachePerf extends SolrTestCaseJ4 {CacheRegenerator cr = new NoOpRegenerator();Object o = cache.init(params, null, cr);cache.setState(SolrCache.State.LIVE);- cache.initializeMetrics(new SolrMetricsContext(metricManager, "foo", "bar"), "foo");+ // TODO SOLR-17458: Fix test later+ cache.initializeMetrics(+ new SolrMetricsContext(metricManager, "foo", "bar"), Attributes.empty(), "foo");AtomicBoolean stop = new AtomicBoolean();SummaryStatistics perImplRatio =ratioStats.computeIfAbsent(clazz.getSimpleName(), c -> new SummaryStatistics());diff --git a/solr/core/src/test/org/apache/solr/search/TestSolrFieldCacheBean.java b/solr/core/src/test/org/apache/solr/search/TestSolrFieldCacheBean.javaindex 4aeb4da7c36..a69730afc4b 100644--- a/solr/core/src/test/org/apache/solr/search/TestSolrFieldCacheBean.java+++ b/solr/core/src/test/org/apache/solr/search/TestSolrFieldCacheBean.java@@ -16,6 +16,7 @@*/package org.apache.solr.search;+import io.opentelemetry.api.common.Attributes;import java.util.Map;import java.util.Random;import org.apache.lucene.tests.util.TestUtil;@@ -70,7 +71,7 @@ public class TestSolrFieldCacheBean extends SolrTestCaseJ4 {SolrMetricManager metricManager = h.getCoreContainer().getMetricManager();SolrMetricsContext solrMetricsContext =new SolrMetricsContext(metricManager, registryName, "foo");- mbean.initializeMetrics(solrMetricsContext, null);+ mbean.initializeMetrics(solrMetricsContext, Attributes.empty(), null);MetricsMap metricsMap =(MetricsMap)((SolrMetricManager.GaugeWrapper)@@ -89,7 +90,7 @@ public class TestSolrFieldCacheBean extends SolrTestCaseJ4 {SolrMetricManager metricManager = h.getCoreContainer().getMetricManager();SolrMetricsContext solrMetricsContext =new SolrMetricsContext(metricManager, registryName, "foo");- mbean.initializeMetrics(solrMetricsContext, null);+ mbean.initializeMetrics(solrMetricsContext, Attributes.empty(), null);MetricsMap metricsMap =(MetricsMap)((SolrMetricManager.GaugeWrapper)diff --git a/solr/core/src/test/org/apache/solr/search/TestThinCache.java b/solr/core/src/test/org/apache/solr/search/TestThinCache.javaindex 6a23b82568f..de14da997ea 100644--- a/solr/core/src/test/org/apache/solr/search/TestThinCache.java+++ b/solr/core/src/test/org/apache/solr/search/TestThinCache.java@@ -16,6 +16,7 @@*/package org.apache.solr.search;+import io.opentelemetry.api.common.Attributes;import java.nio.file.Files;import java.nio.file.Path;import java.util.Collections;@@ -94,12 +95,14 @@ public class TestThinCache extends SolrTestCaseJ4 {ThinCache<Object, Integer, String> lfuCache = new ThinCache<>();lfuCache.setBacking(cacheScope, backing);SolrMetricsContext solrMetricsContext = new SolrMetricsContext(metricManager, registry, "foo");- lfuCache.initializeMetrics(solrMetricsContext, scope + "-1");+ // TODO SOLR-17458: Fix test later+ lfuCache.initializeMetrics(solrMetricsContext, Attributes.empty(), scope + "-1");Object cacheScope2 = new Object();ThinCache<Object, Integer, String> newLFUCache = new ThinCache<>();newLFUCache.setBacking(cacheScope2, backing);- newLFUCache.initializeMetrics(solrMetricsContext, scope + "-2");+ // TODO SOLR-17458: Fix test later+ newLFUCache.initializeMetrics(solrMetricsContext, Attributes.empty(), scope + "-2");Map<String, String> params = new HashMap<>();params.put("size", "100");diff --git a/solr/core/src/test/org/apache/solr/search/ThinCache.java b/solr/core/src/test/org/apache/solr/search/ThinCache.javaindex ea7a155922e..c39ae8b30cc 100644--- a/solr/core/src/test/org/apache/solr/search/ThinCache.java+++ b/solr/core/src/test/org/apache/solr/search/ThinCache.java@@ -19,6 +19,7 @@ package org.apache.solr.search;import com.github.benmanes.caffeine.cache.RemovalCause;import com.github.benmanes.caffeine.cache.RemovalListener;import com.google.common.annotations.VisibleForTesting;+import io.opentelemetry.api.common.Attributes;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.lang.ref.WeakReference;@@ -291,7 +292,8 @@ public class ThinCache<S, K, V> extends SolrCacheBaseprivate long priorEvictions;@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {solrMetricsContext = parentContext.getChildContext(this);cacheMap =new MetricsMap(diff --git a/solr/core/src/test/org/apache/solr/util/tracing/TestSimplePropagatorDistributedTracing.java b/solr/core/src/test/org/apache/solr/util/tracing/TestSimplePropagatorDistributedTracing.javaindex 270c53c0471..80940b31da6 100644--- a/solr/core/src/test/org/apache/solr/util/tracing/TestSimplePropagatorDistributedTracing.java+++ b/solr/core/src/test/org/apache/solr/util/tracing/TestSimplePropagatorDistributedTracing.java@@ -35,6 +35,7 @@ import org.apache.solr.client.solrj.request.UpdateRequest;import org.apache.solr.client.solrj.response.CollectionAdminResponse;import org.apache.solr.cloud.SolrCloudTestCase;import org.apache.solr.common.util.SuppressForbidden;+import org.apache.solr.core.OpenTelemetryConfigurator;import org.apache.solr.core.SolrCore;import org.apache.solr.handler.admin.api.CreateCore;import org.apache.solr.logging.MDCLoggingContext;@@ -50,6 +51,8 @@ public class TestSimplePropagatorDistributedTracing extends SolrCloudTestCase {@BeforeClasspublic static void setupCluster() throws Exception {+ OpenTelemetryConfigurator.resetForTest();+configureCluster(4).addConfig("conf", configset("cloud-minimal")).configure();Tracer tracer = GlobalOpenTelemetry.get().getTracer("solr");diff --git a/solr/modules/cross-dc/src/test/org/apache/solr/crossdc/handler/MirroringCollectionsHandlerTest.java b/solr/modules/cross-dc/src/test/org/apache/solr/crossdc/handler/MirroringCollectionsHandlerTest.javaindex a4cb9a97cc3..ed914dfaeb1 100644--- a/solr/modules/cross-dc/src/test/org/apache/solr/crossdc/handler/MirroringCollectionsHandlerTest.java+++ b/solr/modules/cross-dc/src/test/org/apache/solr/crossdc/handler/MirroringCollectionsHandlerTest.java@@ -31,6 +31,7 @@ import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.cloud.SolrZkClient;import org.apache.solr.common.params.SolrParams;import org.apache.solr.core.CoreContainer;+import org.apache.solr.core.OpenTelemetryConfigurator;import org.apache.solr.core.SolrXmlConfig;import org.apache.solr.crossdc.common.KafkaCrossDcConf;import org.apache.solr.crossdc.common.KafkaMirroringSink;@@ -160,6 +161,8 @@ public class MirroringCollectionsHandlerTest extends SolrTestCaseJ4 {@Testpublic void testCoreContainerInit() throws Exception {+ OpenTelemetryConfigurator.resetForTest();+Path home = createTempDir();String solrXml = IOUtils.resourceToString("/mirroring-solr.xml", StandardCharsets.UTF_8);CoreContainer cores = new CoreContainer(SolrXmlConfig.fromString(home, solrXml));diff --git a/solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/CustomTestOtelTracerConfigurator.java b/solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/CustomTestOtelTracerConfigurator.javaindex 16c2d20a41e..e208d6d0034 100644--- a/solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/CustomTestOtelTracerConfigurator.java+++ b/solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/CustomTestOtelTracerConfigurator.java@@ -16,7 +16,6 @@*/package org.apache.solr.opentelemetry;-import io.opentelemetry.api.GlobalOpenTelemetry;import io.opentelemetry.api.trace.Tracer;import io.opentelemetry.sdk.OpenTelemetrySdk;import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;@@ -99,7 +98,6 @@ public class CustomTestOtelTracerConfigurator extends OtelTracerConfigurator {exporter = null;}System.clearProperty("otel.traces.exporter");- GlobalOpenTelemetry.resetForTest();}}}diff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.javaindex c49c6843923..c475f2b23d3 100644--- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java+++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java@@ -25,6 +25,7 @@ import static org.hamcrest.core.StringContains.containsString;import com.carrotsearch.randomizedtesting.RandomizedContext;import com.carrotsearch.randomizedtesting.RandomizedTest;import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;+import io.opentelemetry.api.GlobalOpenTelemetry;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.Reader;@@ -249,6 +250,7 @@ public abstract class SolrTestCaseJ4 extends SolrTestCase {@BeforeClasspublic static void setupTestCases() {+ GlobalOpenTelemetry.resetForTest();resetExceptionIgnores();testExecutor =diff --git a/solr/test-framework/src/java/org/apache/solr/core/MockQuerySenderListenerReqHandler.java b/solr/test-framework/src/java/org/apache/solr/core/MockQuerySenderListenerReqHandler.javaindex f70cdc59cbd..622a0c25a14 100644--- a/solr/test-framework/src/java/org/apache/solr/core/MockQuerySenderListenerReqHandler.java+++ b/solr/test-framework/src/java/org/apache/solr/core/MockQuerySenderListenerReqHandler.java@@ -16,6 +16,7 @@*/package org.apache.solr.core;+import io.opentelemetry.api.common.Attributes;import java.util.concurrent.atomic.AtomicInteger;import org.apache.solr.common.util.NamedList;import org.apache.solr.handler.RequestHandlerBase;@@ -38,8 +39,9 @@ public class MockQuerySenderListenerReqHandler extends RequestHandlerBase {}@Override- public void initializeMetrics(SolrMetricsContext parentContext, String scope) {- super.initializeMetrics(parentContext, scope);+ public void initializeMetrics(+ SolrMetricsContext parentContext, Attributes attributes, String scope) {+ super.initializeMetrics(parentContext, attributes, scope);solrMetricsContext.gauge(() -> initCounter.intValue(), true, "initCount", getCategory().toString(), scope);}