Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 113 additions & 49 deletions solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java
Copy link
Contributor

Choose a reason for hiding this comment

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

The duplicity of code in here is very unfortunate. No need to slightly vary descriptions; core vs node can be implicit based on the metric name structure. I feel like there should be a kind of inherit-aware factory of instruments (that support it; no gauges) so that all inherit-possible metrics a core might want to register (not just request handlers) could support it automatically. I sympathize I may be suggesting massive scope creep... if I am then we can table it. At least consider the notion and tell me what you think please :-)

FYI some years ago I worked on a competing implementation of the underlying node/core inheritance we're talking about. It was a hack that doesn't matter but the underlying need/feature, I think, is really valuable. I don't love that the ultimate implementation we see now is limited to only RequestHandlers and requires someone to modify their solrconfig.xml to opt-in to this on a handler-by-handler basis. Imagine that such aspects are up for change! Ideally you could simply do this at startup (a singular boolean option) affecting all supported instrument types of all cores. Even the core's own instance need not exist (i.e. node only, core only, or both).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need to slightly vary descriptions; core vs node can be implicit based on the metric name structure.

I wrote this a while ago and my view of this has kind of changed since then. How about to reduce some of the duplicate code here, we merge these 2 metrics names together:

solr_core_requests_total
solr_node_requests_total

into just

solr_requests_total

If there are core attributes then you know it was requests to a core. No core level attributes, then it has to mean node level requests as there is nothing to aggregate on it and we just need 1 metric name.

Example

solr_requests_total{handler="/select",core="foo"} 5
solr_requests_total{handler="/select",core="bar"} 5
solr_requests_total{handler="/select"} 10 <- Node level

I feel like there should be a kind of inherit-aware factory of instruments (that support it; no gauges) so that all inherit-possible metrics a core might want to register (not just request handlers) could support it automatically. I sympathize I may be suggesting massive scope creep...

Definitely scope creep but at the same time I see value in what you are saying here. I'd have to see what implementing this would look like but off the top of my head it'd be a somewhat big refactor. Could we push this back to a 10.x release? You can still calculate your node level requests by summing all the cores in your backend or at aggregation. This is a feature that could very well help your backend aggregation load but doesn't seem like a necessary thing now. Also working on separate branch right now that is removing Dropwizard that is taking time.

I can see about renaming requests to solr_requests_total so that in 10.x its easy to just add this node level recording later if you agree.

Copy link
Contributor

Choose a reason for hiding this comment

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

The refactor for inheritance can be deferred to a minor release.

BTW the JIRA issue for what you are reworking is here, which is a useful examination for providing context / use-case. Imagine thousands of cores at any one time, and furthermore coming and going! It's a metrics cardinality explosion to send any core level metrics to an aggregator. In such a world (that I am very familiar with), you think of the Solr node as a whole that does work. No core metrics. CC @magibney

I don't love the absence of core metrics to mean it's the node level. I worry that filtering is hard. It's imperative that it be possible to easily filter for node metrics at our endpoint. I think our current filtering (fresh custom code) can't handle that. The previous filtering (DropWizard) could definitely support that by filtering to the node registry. Since we register core metrics in a special way (for unloading), maybe our metrics could support a similar filter to not even examine the core ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Once I started migrating DirectUpdateHandler2 this duplicate code became way more prevalent and messy and really began to regret the paradigm I adopted when I originally wrote this. So rewrote it with a factory. Let me know what you think

Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
import org.apache.solr.core.PluginInfo;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrInfoBean;
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.OtelUnit;
import org.apache.solr.metrics.otel.instruments.AttributedLongCounter;
import org.apache.solr.metrics.otel.instruments.AttributedLongTimer;
import org.apache.solr.metrics.otel.instruments.DualRegistryAttributedLongCounter;
import org.apache.solr.metrics.otel.instruments.DualRegistryAttributedLongTimer;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.response.SolrQueryResponse;
Expand Down Expand Up @@ -167,21 +167,13 @@ public SolrMetricsContext getSolrMetricsContext() {
@Override
public void initializeMetrics(
SolrMetricsContext parentContext, Attributes attributes, String scope) {
if (aggregateNodeLevelMetricsEnabled) {
this.solrMetricsContext =
new SolrDelegateRegistryMetricsContext(
parentContext.getMetricManager(),
parentContext.getRegistryName(),
SolrMetricProducer.getUniqueMetricTag(this, parentContext.getTag()),
SolrMetricManager.getRegistryName(SolrInfoBean.Group.node));
} else {
this.solrMetricsContext = parentContext.getChildContext(this);
}
this.solrMetricsContext = parentContext.getChildContext(this);

metrics =
new HandlerMetrics(
solrMetricsContext,
attributes.toBuilder().put(CATEGORY_ATTR, getCategory().toString()).build());
attributes.toBuilder().put(CATEGORY_ATTR, getCategory().toString()).build(),
aggregateNodeLevelMetricsEnabled);

// NOCOMMIT: I don't see value in this metric
Copy link
Contributor

Choose a reason for hiding this comment

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

drop it, albeit maybe not this PR (I don't care)

solrMetricsContext.gauge(
Expand All @@ -193,61 +185,133 @@ public static class HandlerMetrics {
public static final HandlerMetrics NO_OP =
new HandlerMetrics(
new SolrMetricsContext(new SolrMetricManager(null), "NO_OP", "NO_OP"),
Attributes.empty());
Attributes.empty(),
false);

public AttributedLongCounter requests;
public AttributedLongCounter numServerErrors;
public AttributedLongCounter numClientErrors;
public AttributedLongCounter numTimeouts;
public AttributedLongTimer requestTimes;

public HandlerMetrics(SolrMetricsContext solrMetricsContext, Attributes attributes) {
LongCounter requestMetric;
LongCounter errorRequestMetric;
LongCounter timeoutRequestMetric;
LongHistogram requestTimeMetric;

if (solrMetricsContext.getRegistryName().equals("solr.node")) {
requestMetric =
solrMetricsContext.longCounter("solr_node_requests", "Http Solr node requests");
errorRequestMetric =
solrMetricsContext.longCounter(
"solr_node_requests_errors", "HTTP Solr node request errors");
timeoutRequestMetric =
solrMetricsContext.longCounter(
"solr_node_requests_timeout", "HTTP Solr node request timeouts");
requestTimeMetric =
solrMetricsContext.longHistogram(
"solr_node_requests_times", "HTTP Solr node request times", OtelUnit.MILLISECONDS);
public HandlerMetrics(
SolrMetricsContext solrMetricsContext,
Attributes coreAttributes,
boolean aggregateNodeLevelMetricsEnabled) {

if (aggregateNodeLevelMetricsEnabled) {
createDualRegistryMetrics(solrMetricsContext, coreAttributes);
} else {
requestMetric =
solrMetricsContext.longCounter("solr_core_requests", "HTTP Solr core requests");
errorRequestMetric =
solrMetricsContext.longCounter(
"solr_core_requests_errors", "HTTP Solr core request errors");
timeoutRequestMetric =
solrMetricsContext.longCounter(
"solr_core_requests_timeout", "HTTP Solr core request timeouts");
requestTimeMetric =
solrMetricsContext.longHistogram(
"solr_core_requests_times", "HTTP Solr core request times", OtelUnit.MILLISECONDS);
createSingleRegistryMetrics(solrMetricsContext, coreAttributes);
}
}

requests = new AttributedLongCounter(requestMetric, attributes);
private void createDualRegistryMetrics(
SolrMetricsContext solrCoreMetricsContext, Attributes coreAttributes) {
SolrMetricsContext solrNodeMetricsContext =
new SolrMetricsContext(
solrCoreMetricsContext.getMetricManager(),
SolrMetricManager.getRegistryName(SolrInfoBean.Group.node),
null);

Attributes nodeAttributes =
Attributes.builder()
.put(CATEGORY_ATTR, coreAttributes.get(CATEGORY_ATTR))
.put(HANDLER_ATTR, coreAttributes.get(HANDLER_ATTR))
.build();

requests =
new DualRegistryAttributedLongCounter(
requestCounter(solrCoreMetricsContext, false), coreAttributes,
requestCounter(solrNodeMetricsContext, true), nodeAttributes);
numServerErrors =
new DualRegistryAttributedLongCounter(
requestErrorCounter(solrCoreMetricsContext, false),
coreAttributes.toBuilder().put(AttributeKey.stringKey("source"), "server").build(),
requestErrorCounter(solrNodeMetricsContext, true),
nodeAttributes.toBuilder().put(AttributeKey.stringKey("source"), "server").build());

numClientErrors =
new DualRegistryAttributedLongCounter(
requestErrorCounter(solrCoreMetricsContext, false),
coreAttributes.toBuilder().put(AttributeKey.stringKey("source"), "client").build(),
requestErrorCounter(solrNodeMetricsContext, true),
nodeAttributes.toBuilder().put(AttributeKey.stringKey("source"), "client").build());
numTimeouts =
new DualRegistryAttributedLongCounter(
requestTimeoutCounter(solrCoreMetricsContext, false), coreAttributes,
requestTimeoutCounter(solrNodeMetricsContext, true), nodeAttributes);
requestTimes =
new DualRegistryAttributedLongTimer(
requestTimeHistogram(solrCoreMetricsContext, false), coreAttributes,
requestTimeHistogram(solrNodeMetricsContext, true), nodeAttributes);
}

private void createSingleRegistryMetrics(
SolrMetricsContext solrMetricsContext, Attributes coreAttributes) {
boolean isNodeRegistry =
solrMetricsContext
.getRegistryName()
.equals(SolrMetricManager.getRegistryName(SolrInfoBean.Group.node));

Attributes attributes =
isNodeRegistry
? Attributes.builder()
.put(CATEGORY_ATTR, coreAttributes.get(CATEGORY_ATTR))
.put(HANDLER_ATTR, coreAttributes.get(HANDLER_ATTR))
.build()
: coreAttributes;

requests =
new AttributedLongCounter(requestCounter(solrMetricsContext, isNodeRegistry), attributes);
numServerErrors =
new AttributedLongCounter(
errorRequestMetric,
requestErrorCounter(solrMetricsContext, isNodeRegistry),
attributes.toBuilder().put(AttributeKey.stringKey("source"), "server").build());

numClientErrors =
new AttributedLongCounter(
errorRequestMetric,
requestErrorCounter(solrMetricsContext, isNodeRegistry),
attributes.toBuilder().put(AttributeKey.stringKey("source"), "client").build());
numTimeouts =
new AttributedLongCounter(
requestTimeoutCounter(solrMetricsContext, isNodeRegistry), attributes);
requestTimes =
new AttributedLongTimer(
requestTimeHistogram(solrMetricsContext, isNodeRegistry), attributes);
}

private LongCounter requestCounter(
SolrMetricsContext solrMetricsContext, boolean isNodeRegistry) {
return (isNodeRegistry)
? solrMetricsContext.longCounter("solr_node_requests", "HTTP Solr node requests")
: solrMetricsContext.longCounter("solr_core_requests", "HTTP Solr core requests");
}

private LongCounter requestErrorCounter(
SolrMetricsContext solrMetricsContext, boolean isNodeRegistry) {
return (isNodeRegistry)
? solrMetricsContext.longCounter(
"solr_node_requests_errors", "HTTP Solr node request errors")
: solrMetricsContext.longCounter(
"solr_core_requests_errors", "HTTP Solr core request errors");
}

numTimeouts = new AttributedLongCounter(timeoutRequestMetric, attributes);
private LongCounter requestTimeoutCounter(
SolrMetricsContext solrMetricsContext, boolean isNodeRegistry) {
return (isNodeRegistry)
? solrMetricsContext.longCounter(
"solr_node_requests_timeout", "HTTP Solr node request timeouts")
: solrMetricsContext.longCounter(
"solr_core_requests_timeout", "HTTP Solr core request timeouts");
}

requestTimes = new AttributedLongTimer(requestTimeMetric, attributes);
private LongHistogram requestTimeHistogram(
SolrMetricsContext solrMetricsContext, boolean isNodeRegistry) {
return (isNodeRegistry)
? solrMetricsContext.longHistogram(
"solr_node_requests_times", "HTTP Solr node request times", OtelUnit.MILLISECONDS)
: solrMetricsContext.longHistogram(
"solr_core_requests_times", "HTTP Solr core request times", OtelUnit.MILLISECONDS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ public class SearchHandler extends RequestHandlerBase
static final String INIT_FIRST_COMPONENTS = "first-components";
static final String INIT_LAST_COMPONENTS = "last-components";

protected static final String SHARD_HANDLER_SUFFIX = "[shard]";

private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

/**
Expand Down Expand Up @@ -171,7 +169,8 @@ public void initializeMetrics(
.putAll(attributes)
.put(CATEGORY_ATTR, getCategory().toString())
.put(INTERNAL_ATTR, true)
.build());
.build(),
false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AttributedDoubleCounter(DoubleCounter counter, Attributes attributes) {
}

public void inc() {
add(1.0);
counter.add(1.0, attributes);
Copy link
Contributor

Choose a reason for hiding this comment

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

why? I'd rather keep this and furthermore make the method final to prevent something overloading it and maybe increase the likelihood of the JVM inlining it.
Same goes with the other convenience methods on the other instruments.

}

public void add(Double value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public AttributedDoubleUpDownCounter(DoubleUpDownCounter upDownCounter, Attribut
}

public void inc() {
add(1.0);
upDownCounter.add(1.0, attributes);
}

public void dec() {
add(-1.0);
upDownCounter.add(-1.0, attributes);
}

public void add(Double value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AttributedLongCounter(LongCounter baseCounter, Attributes attributes) {
}

public void inc() {
add(1L);
baseCounter.add(1L, attributes);
}

public void add(Long value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public AttributedLongUpDownCounter(LongUpDownCounter upDownCounter, Attributes a
}

public void inc() {
add(1L);
upDownCounter.add(1L, attributes);
}

public void dec() {
add(-1L);
upDownCounter.add(-1L, attributes);
}

public void add(Long value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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;

/**
* An AttributedLongCounter that writes to both core and node registries with corresponding
* attributes.
*/
public class DualRegistryAttributedLongCounter extends AttributedLongCounter {

private final AttributedLongCounter nodeCounter;

public DualRegistryAttributedLongCounter(
LongCounter coreCounter,
Attributes coreAttributes,
LongCounter nodeCounter,
Attributes nodeAttributes) {
super(coreCounter, coreAttributes);
this.nodeCounter = new AttributedLongCounter(nodeCounter, nodeAttributes);
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice to have an assert here that checks that the arguments are not the same instance, and furthermore, check say one attribute we know is only on the node is there. No big deal.

}

@Override
public void inc() {
Copy link
Contributor

Choose a reason for hiding this comment

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

if you follow my recommendation on making the underlying convenience methods final, you can happily drop this and other overloads of convenience methods :-)

super.inc();
nodeCounter.inc();
}

@Override
public void add(Long value) {
super.add(value);
nodeCounter.add(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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;

/**
* An AttributedLongTimer that records to both core and node registries with corresponding
* attributes.
*/
public class DualRegistryAttributedLongTimer extends AttributedLongTimer {

private final AttributedLongTimer nodeTimer;

public DualRegistryAttributedLongTimer(
LongHistogram coreHistogram,
Attributes coreAttributes,
LongHistogram nodeHistogram,
Attributes nodeAttributes) {
super(coreHistogram, coreAttributes);
this.nodeTimer = new AttributedLongTimer(nodeHistogram, nodeAttributes);
}

@Override
public void record(Long value) {
super.record(value);
nodeTimer.record(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ private RequestHandlerBase.HandlerMetrics createHandlerMetrics() {
when(metricsContext.longHistogram(any(), any())).thenReturn(mockLongHistogram);

return new RequestHandlerBase.HandlerMetrics(
metricsContext, Attributes.of(AttributeKey.stringKey("/handler"), "/someBaseMetricPath"));
metricsContext,
Attributes.of(AttributeKey.stringKey("/handler"), "/someBaseMetricPath"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have a key with a leading symbol?

false);
}
}
Loading