Skip to content

Commit

Permalink
Fix MethodKeyRt override & Error enhance (apache#13580)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaosheng <songxiaosheng@apache.org>
  • Loading branch information
AlbumenJ and songxiaosheng committed Dec 27, 2023
1 parent 25b73b1 commit 745ba45
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ private List<Action> calServiceRtActions(Invocation invocation, String registryO
public void calcMethodKeyRt(Invocation invocation, String registryOpType, Long responseTime) {
List<Action> actions;

if (invocation.getServiceModel() != null && invocation.getServiceModel().getServiceMetadata() != null) {
if (getServiceLevel()
&& invocation.getServiceModel() != null
&& invocation.getServiceModel().getServiceMetadata() != null) {
Map<String, Object> attributeMap =
invocation.getServiceModel().getServiceMetadata().getAttributeMap();
Map<String, List<Action>> cache = (Map<String, List<Action>>) attributeMap.get("MethodKeyRt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@
*/
package org.apache.dubbo.metrics.model.sample;

import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.ToDoubleFunction;

import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_METRICS_COLLECTOR_EXCEPTION;

/**
* GaugeMetricSample.
*/
public class GaugeMetricSample<T> extends MetricSample {
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(GaugeMetricSample.class);
private final AtomicBoolean warned = new AtomicBoolean(false);

private final T value;

Expand Down Expand Up @@ -71,7 +78,22 @@ public GaugeMetricSample(
ToDoubleFunction<T> apply) {
super(name, description, tags, Type.GAUGE, category, baseUnit);
this.value = Objects.requireNonNull(value, "The GaugeMetricSample value cannot be null");
this.apply = Objects.requireNonNull(apply, "The GaugeMetricSample apply cannot be null");
Objects.requireNonNull(apply, "The GaugeMetricSample apply cannot be null");
this.apply = (e) -> {
try {
return apply.applyAsDouble(e);
} catch (Throwable t) {
if (warned.compareAndSet(false, true)) {
logger.error(
COMMON_METRICS_COLLECTOR_EXCEPTION,
"",
"",
"Unexpected error occurred when applying the GaugeMetricSample",
t);
}
return 0;
}
};
}

public T getValue() {
Expand Down

0 comments on commit 745ba45

Please sign in to comment.