Skip to content

Commit

Permalink
NMS-16966: Moved scope creation out of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
christianpape committed Jan 16, 2025
1 parent 9843579 commit edaaea2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ public List<Event> evaluateAndCreateEvents(CollectionResourceWrapper resource, M
// This reference contains the function that will be used by each evaluator to retrieve the status
AtomicReference<EvaluateFunction> evaluateFunctionRef = new AtomicReference<>(null);

// compute scope here, see NMS-16966
final Scope scope = getScopeForResource(resource);

// Depending on the type of threshold, we want to evaluate it differently
// Threshold Values like value, rearm, trigger and expression are interpolated and cached in state so that
// subsequent evaluations don't need to do any interpolation.
Expand All @@ -250,7 +253,6 @@ public void visit(ThresholdConfigWrapper thresholdConfigWrapper) {
ThresholdValuesSupplier thresholdValuesSupplier = new ThresholdValuesSupplier() {
@Override
public ThresholdEvaluatorState.ThresholdValues get() {
Scope scope = getScopeForResource(resource);
ThresholdEvaluatorState.ThresholdValues thresholdValues = thresholdConfigWrapper.interpolateThresholdValues(scope);
thresholdValues.setDsValue(computedValue);
return thresholdValues;
Expand All @@ -272,8 +274,6 @@ public void visit(ExpressionConfigWrapper expressionConfigWrapper) {
// also retrieve the interpolated expression so we can persist it in our state going forward
@Override
public ExpressionConfigWrapper.ExpressionThresholdValues get() throws ThresholdExpressionException {

Scope scope = getScopeForResource(resource);
return expressionConfigWrapper
.interpolateAndEvaluate(values, scope);
}
Expand Down Expand Up @@ -311,24 +311,28 @@ public double get(String evaluatedExpression) throws ThresholdExpressionExceptio
return events;
}

public Scope getScopeForResource(CollectionResourceWrapper resource) {
public static Scope getScopeForResource(EntityScopeProvider entityScopeProvider, CollectionResourceWrapper resource) {
// Default to empty scopes and then attempt to populate each of node, interface, and service
// scopes below
Scope[] scopes = new Scope[]{EmptyScope.EMPTY, EmptyScope.EMPTY, EmptyScope.EMPTY};

if (resource != null) {
scopes[0] = m_entityScopeProvider.getScopeForNode(resource.getNodeId());
scopes[0] = entityScopeProvider.getScopeForNode(resource.getNodeId());
String interfaceIp = resource.getHostAddress();
if (interfaceIp != null) {
scopes[1] = m_entityScopeProvider.getScopeForInterface(resource.getNodeId(),
scopes[1] = entityScopeProvider.getScopeForInterface(resource.getNodeId(),
interfaceIp);
scopes[2] = m_entityScopeProvider.getScopeForService(resource.getNodeId(),
scopes[2] = entityScopeProvider.getScopeForService(resource.getNodeId(),
InetAddresses.forString(interfaceIp), resource.getServiceName());
}
}
return new FallbackScope(scopes);
}

public Scope getScopeForResource(CollectionResourceWrapper resource) {
return getScopeForResource(m_entityScopeProvider, resource);
}

/**
* <p>addThreshold</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ protected final List<Event> applyThresholds(CollectionResourceWrapper resourceWr
LOG.debug("applyThresholds: Ignoring resource {} because required attributes map is empty.", resourceWrapper);
return eventsList;
}
// compute scope here, see NMS-16966
final var scope = ThresholdEntity.getScopeForResource(m_entityScopeProvider, resourceWrapper);

LOG.debug("applyThresholds: Applying thresholds on {} using {} attributes.", resourceWrapper, attributesMap.size());
Date date = new Date();
synchronized(m_thresholdGroups) {
Expand Down Expand Up @@ -285,8 +288,6 @@ protected final List<Event> applyThresholds(CollectionResourceWrapper resourceWr
if(!valueMissing || relaxed) {
LOG.info("applyThresholds: All attributes found for {}, evaluating", resourceWrapper);

final var scope = thresholdEntity.getScopeForResource(resourceWrapper);

resourceWrapper.setDsLabel(Interpolator.interpolate(thresholdEntity.getDatasourceLabel(), scope).output);
try {
List<Event> thresholdEvents = thresholdEntity.evaluateAndCreateEvents(resourceWrapper, values, date);
Expand Down Expand Up @@ -314,6 +315,9 @@ protected boolean passedThresholdFilters(CollectionResourceWrapper resource, Thr
return false;
}

// compute scope here, see NMS-16966
final var scope = ThresholdEntity.getScopeForResource(m_entityScopeProvider, resource);

// Find the filters for threshold definition for selected group/dataSource
final List<ResourceFilter> filters = thresholdEntity.getThresholdConfig().getBasethresholddef().getResourceFilters();
if (filters.size() == 0) return true;
Expand All @@ -326,8 +330,6 @@ protected boolean passedThresholdFilters(CollectionResourceWrapper resource, Thr
LOG.debug("passedThresholdFilters: filter #{}: field={}, regex='{}'", count, f.getField(), f.getContent().orElse(null));
count++;

final var scope = thresholdEntity.getScopeForResource(resource);

// Read Resource Attribute and apply filter rules if attribute is not null
String attr = resource.getFieldValue(Interpolator.interpolate(f.getField(), scope).output);
if (attr != null) {
Expand Down

0 comments on commit edaaea2

Please sign in to comment.