Skip to content

Commit

Permalink
NMS-16948: Add essential tags for latency resource
Browse files Browse the repository at this point in the history
Also add resource label for all metrics
  • Loading branch information
cgorantla committed Aug 30, 2024
1 parent 951c6f5 commit 8416ade
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.opennms.netmgt.collection.support.DefaultTimeKeeper;
import org.opennms.netmgt.model.ResourcePath;

import java.util.HashMap;
import java.util.Map;

/**
* <p>CollectionResource interface.</p>
*
Expand Down Expand Up @@ -95,4 +98,9 @@ public interface CollectionResource extends ResourceIdentifier, CollectionVisita
*/
TimeKeeper getTimeKeeper();

// Can be used to add additional tags required by TimeSeries like Prometheus
default Map<String, String> getTags() {
return new HashMap<>();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class LatencyCollectionResource implements CollectionResource {
private final String m_location;
private final Map<AttributeGroupType, AttributeGroup> m_attributeGroups = Maps.newLinkedHashMap();

private final Map<String, String> m_tags = Maps.newHashMap();

/**
* <p>Constructor for LatencyCollectionResource.</p>
*
Expand All @@ -54,6 +56,13 @@ public LatencyCollectionResource(String serviceName, String ipAddress, String lo
m_location = location;
}

public LatencyCollectionResource(String serviceName, String ipAddress, String location, Map<String, String> tags) {
m_serviceName = serviceName;
m_ipAddress = ipAddress;
m_location = location;
m_tags.putAll(tags);
}

/**
* <p>getInstance</p>
*
Expand Down Expand Up @@ -197,4 +206,9 @@ public ResourcePath getParent() {
public TimeKeeper getTimeKeeper() {
return null;
}

@Override
public Map<String, String> getTags() {
return m_tags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import org.opennms.core.cache.Cache;
import org.opennms.integration.api.v1.timeseries.Tag;
import org.opennms.integration.api.v1.timeseries.immutables.ImmutableTag;
import org.opennms.netmgt.collection.api.AbstractPersister;
import org.opennms.netmgt.collection.api.AttributeGroup;
import org.opennms.netmgt.collection.api.AttributeType;
Expand Down Expand Up @@ -104,6 +105,10 @@ public void visitGroup(AttributeGroup group) {
// Set the builder before any calls to persistNumericAttribute are made
CollectionResource resource = group.getResource();
Set<Tag> metaTags = getUserDefinedMetaTags(resource);
resource.getTags().forEach((key, value) -> metaTags.add(new ImmutableTag(key, value)));
if (resource.getInterfaceLabel() != null) {
metaTags.add(new ImmutableTag("resource_label", resource.getInterfaceLabel()));
}
currentBuilder = new TimeseriesPersistOperationBuilder(writer, repository, resource, group.getName(), metaTags,
resourceLevelStringAttributes, this.metricRegistry);
if (resource.getTimeKeeper() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.File;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -152,7 +153,12 @@ private CollectionSet getCollectionSet(MonitoredService service, Map<String, Num
// This happens whether or not storeByGroup is enabled.
// 2) If multiple entries are present, the DSs are created in the same order that they
// appear in the map
LatencyCollectionResource latencyResource = new LatencyCollectionResource(service.getSvcName(), service.getIpAddr(), service.getNodeLocation());
// Add labels to be used in time series that depends on labels
Map<String, String> tags = new HashMap<>();
tags.put("node_id", Integer.toString(service.getNodeId()));
tags.put("node_label", service.getNodeLabel());
tags.put("location", service.getNodeLocation());
LatencyCollectionResource latencyResource = new LatencyCollectionResource(service.getSvcName(), service.getIpAddr(), service.getNodeLocation(), tags);
for (final Entry<String, Number> entry : entries.entrySet()) {
final String ds = entry.getKey();
final Number value = entry.getValue() != null ? entry.getValue() : Double.NaN;
Expand Down

0 comments on commit 8416ade

Please sign in to comment.