Skip to content

Commit 0479364

Browse files
authored
Merge pull request #85 from cisco-open/feature/master/metrics-resource-filters
Added support for filtering reporting resources for metrics
2 parents 94fad06 + dc06079 commit 0479364

File tree

18 files changed

+276
-144
lines changed

18 files changed

+276
-144
lines changed

example-definitions/json/metric-definition.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"aggregationTemporality": "delta",
1919
"valueFunction": "arithmeticSequence(1, 0, \"\")",
2020
"isMonotonic": true,
21-
"reportingResources": [
22-
"pod"
23-
]
21+
"filteredReportingResources": {
22+
"pod": ["k8s.cluster.name=cluster-skyrim"]
23+
}
2424
},
2525
{
2626
"name": "cpu.allocatable",
@@ -47,9 +47,9 @@
4747
0,
4848
1
4949
],
50-
"reportingResources": [
51-
"pod"
52-
]
50+
"filteredReportingResources": {
51+
"pod": ["k8s.pod.status=Running", "k8s.cluster.name=cluster-skyrim"]
52+
}
5353
},
5454
{
5555
"name": "system.filesystem.mountpoint.limit",
@@ -117,9 +117,11 @@
117117
1
118118
],
119119
"reportingResources": [
120-
"managed_replicaset",
121-
"unmanaged_replicaset"
122-
]
120+
"managed_replicaset"
121+
],
122+
"filteredReportingResources": {
123+
"unmanaged_replicaset": ["k8s.workload.kind=unmanaged_replicaset"]
124+
}
123125
},
124126
{
125127
"name": "calls_min",

example-definitions/json/resource-definition.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
"node": "distribution(5, 2, 10)"
121121
},
122122
"attributes": {
123-
"k8s.cluster.name": "\"cluster-\".concat(alphanumericSequence(\"dppteam\"))"
123+
"k8s.cluster.name": "\"cluster-\".concat(alphanumericSequence(\"skyrim\"))"
124124
}
125125
},
126126
{
@@ -296,7 +296,7 @@
296296
"name": "network",
297297
"count": 120,
298298
"attributes": {
299-
"clou.network.id": "alphanumericSequence(\"IDfr3x\").concat(\".network\")"
299+
"cloud.network.id": "alphanumericSequence(\"IDfr3x\").concat(\".network\")"
300300
},
301301
"attributeOperations": [
302302
"copyFromParent(\"aws_ec2\", \"host.id\")"

example-definitions/qa/metric-definition.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ metrics:
2828
aggregationTemporality: delta
2929
valueFunction: 'arithmeticSequence(1, 0, "")'
3030
isMonotonic: true
31-
reportingResources: [ pod ]
31+
filteredReportingResources:
32+
pod: ["k8s.cluster.name=cluster-skyrim"]
3233
- name: cpu.allocatable
3334
unit: "{cores}"
3435
otelType: summary
@@ -41,7 +42,8 @@ metrics:
4142
otelType: summary
4243
valueFunction: 'arithmeticSequenceSummary(1, 2, "", 5)'
4344
quantiles: [ 0, 1 ]
44-
reportingResources: [ pod ]
45+
filteredReportingResources:
46+
pod: ["k8s.pod.status=Running", "k8s.cluster.name=cluster-skyrim"]
4547
- name: system.filesystem.mountpoint.limit
4648
unit: "By"
4749
otelType: summary
@@ -72,7 +74,9 @@ metrics:
7274
valueFunction: 'arithmeticSequenceSummary(1, 2, "", 5)'
7375
isDouble: true
7476
quantiles: [ 0, 0.5, 0.75, 1 ]
75-
reportingResources: [ managed_replicaset, unmanaged_replicaset ]
77+
reportingResources: [ managed_replicaset ]
78+
filteredReportingResources:
79+
unmanaged_replicaset: ["k8s.workload.kind=unmanaged_replicaset"]
7680
- name: calls_min
7781
unit: "{calls}/min"
7882
otelType: summary

example-definitions/qa/resource-definition.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ resources:
2121
namespace: 'distribution(3, 0, 0)'
2222
node: 'distribution(5, 2, 10)'
2323
attributes:
24-
k8s.cluster.name: '"cluster-".concat(alphanumericSequence("dppteam"))'
24+
k8s.cluster.name: '"cluster-".concat(alphanumericSequence("skyrim"))'
2525
- name: namespace
2626
count: 10
2727
childrenDistribution:
@@ -142,7 +142,7 @@ resources:
142142
- name: network
143143
count: 120
144144
attributes:
145-
clou.network.id: 'alphanumericSequence("IDfr3x").concat(".network")'
145+
cloud.network.id: 'alphanumericSequence("IDfr3x").concat(".network")'
146146
attributeOperations:
147147
- 'copyFromParent("aws_ec2", "host.id")'
148148
- name: filesystem

src/main/java/io/opentelemetry/contrib/generator/core/ResourceModelGenerator.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private int getRuntimeResourcesCount(List<RuntimeModification> runtimeModificati
120120
int count = 0;
121121
for (RuntimeModification eachModification: CollectionUtils.emptyIfNull(runtimeModifications).stream()
122122
.filter(modification -> !modification.getResourceModificationType().equals(ResourceModificationType.REMOVE))
123-
.collect(Collectors.toList())) {
123+
.toList()) {
124124
int newCount = ((eachModification.getEndAfterMinutes() - eachModification.getStartAfterMinutes())
125125
/ eachModification.getModificationFrequencyMinutes()) * eachModification.getModificationQuantity();
126126
count = count + newCount;
@@ -135,7 +135,7 @@ private void mapChildResources() {
135135
//We will do this only for resources that have some children defined
136136
for (ResourceDefinition parentType: allResources.values().stream()
137137
.filter(resource -> !MapUtils.emptyIfNull(resource.getChildrenDistribution()).isEmpty())
138-
.collect(Collectors.toList())) {
138+
.toList()) {
139139
setParentToChildrenTypes(parentType.getName(), parentType.getChildrenDistribution().keySet());
140140
//For each childType-distribution expression pair
141141
for (Map.Entry<String, String> eachChildTypeExpr: parentType.getChildrenDistribution().entrySet()) {
@@ -192,6 +192,8 @@ private void executeAttributeOperations() {
192192
jelProcessor.eval(expression);
193193
}
194194
}));
195+
//We also want to set the evaluated attributes at this stage
196+
resourceModel.values().forEach(resourceList -> resourceList.forEach(GeneratorResource::setEvaluatedAttributes));
195197
}
196198

197199
private void setParentToChildrenTypes(String parentType, Set<String> childTypes) {
@@ -265,14 +267,15 @@ private int getResourceLevel(String resurceType) {
265267
*/
266268
@SuppressWarnings("unused")
267269
public static void copyFromParent(String parentType, String attribute) {
268-
for (GeneratorResource eachResource: resourceModel.get(ResourceModelExpressions.expressionsGlobalKey.split(":")[1]).stream()
270+
for (GeneratorResource eachResource: resourceModel
271+
.get(ResourceModelExpressions.expressionsGlobalKey.split(":")[1]).stream()
269272
.filter(resource -> resource.getParentsByType()!=null)
270273
.filter(resource -> resource.getParentsByType().containsKey(parentType))
271-
.collect(Collectors.toList())) {
274+
.toList()) {
272275
Optional<KeyValue> parentAttribute = eachResource.getParentsByType().get(parentType).get(0)
273276
.getOTelResource().getAttributesList().stream()
274277
.filter(attrKV -> attrKV.getKey().equals(attribute)).findAny();
275-
String attributeValue = parentAttribute.isEmpty() ? "" : parentAttribute.get().getValue().getStringValue();
278+
String attributeValue = parentAttribute.map(keyValue -> keyValue.getValue().getStringValue()).orElse("");
276279
eachResource.getOTelResourceBuilder().addAttributes(KeyValue.newBuilder()
277280
.setKey(attribute)
278281
.setValue(CommonUtils.buildAnyValue(attributeValue))
@@ -292,11 +295,12 @@ public static void copyFromParent(String parentType, String attribute) {
292295
*/
293296
@SuppressWarnings("unused")
294297
public static void modifyFromParent(String parentType, String sourceAttribute, String targetAttribute, String suffixExpression) {
295-
for (GeneratorResource eachResource: resourceModel.get(ResourceModelExpressions.expressionsGlobalKey.split(":")[1])) {
298+
for (GeneratorResource eachResource: resourceModel.get(ResourceModelExpressions
299+
.expressionsGlobalKey.split(":")[1])) {
296300
Optional<KeyValue> parentAttribute = eachResource.getParentsByType().get(parentType).get(0)
297301
.getOTelResource().getAttributesList().stream()
298302
.filter(attrKV -> attrKV.getKey().equals(sourceAttribute)).findAny();
299-
String attributeValue = parentAttribute.isEmpty() ? "" : parentAttribute.get().getValue().getStringValue();
303+
String attributeValue = parentAttribute.map(keyValue -> keyValue.getValue().getStringValue()).orElse("");
300304
if (suffixExpression.length() > 0) {
301305
attributeValue = attributeValue + jelProcessor.eval(suffixExpression);
302306
}

src/main/java/io/opentelemetry/contrib/generator/core/RuntimeModificationsThread.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,12 @@ public void run() {
6969
&& minsElapsed > eachModification.getStartAfterMinutes()
7070
&& minsElapsed <= eachModification.getEndAfterMinutes()) {
7171
switch (eachModification.getResourceModificationType()) {
72-
case ADD:
73-
executeAddModification(eachModification.getResourceType(), eachModification.getModificationQuantity());
74-
break;
75-
case REMOVE:
76-
executeRemoveModification(eachModification.getResourceType(), eachModification.getModificationQuantity());
77-
break;
78-
case CHURN:
79-
executeChurnModification(eachModification.getResourceType(), eachModification.getModificationQuantity());
72+
case ADD ->
73+
executeAddModification(eachModification.getResourceType(), eachModification.getModificationQuantity());
74+
case REMOVE ->
75+
executeRemoveModification(eachModification.getResourceType(), eachModification.getModificationQuantity());
76+
case CHURN ->
77+
executeChurnModification(eachModification.getResourceType(), eachModification.getModificationQuantity());
8078
}
8179
modificationsUpdateTimestamps.put(eachModification.getModificationId(), currTime);
8280
}

src/main/java/io/opentelemetry/contrib/generator/core/dto/GeneratorResource.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class GeneratorResource {
4141
private String type; //type of this resource. There can be multiple resources of this type.
4242
private boolean isActive; //if this resource is currently reporting any MELT data
4343
private Resource.Builder otelResource; //OTel representation of the resource
44+
private Map<String, String> evaluatedAttributes;
4445
private Map<String, List<GeneratorResource>> childrenByType; //list of all the child resources of this resource, grouped by type
4546
private Map<String, List<GeneratorResource>> parentsByType; //list of all the parent resources of this resource, grouped by type
4647

@@ -66,6 +67,13 @@ public Map<String, Integer> getParentTypeCounts() {
6667
return parentTypeCounts;
6768
}
6869

70+
public void setEvaluatedAttributes() {
71+
evaluatedAttributes = new HashMap<>();
72+
for (KeyValue eachKv: CollectionUtils.emptyIfNull(otelResource.getAttributesList())) {
73+
evaluatedAttributes.put(eachKv.getKey(), CommonUtils.anyValueToString(eachKv.getValue()));
74+
}
75+
}
76+
6977
@Override
7078
public String toString() {
7179
var separator = ",\n";
@@ -102,10 +110,9 @@ public int hashCode() {
102110

103111
@Override
104112
public boolean equals(Object generatorResource) {
105-
if (!(generatorResource instanceof GeneratorResource)) {
113+
if (!(generatorResource instanceof GeneratorResource compareResource)) {
106114
return false;
107115
}
108-
GeneratorResource compareResource = (GeneratorResource) generatorResource;
109116
if (!Objects.equals(this.type, compareResource.type)) {
110117
return false;
111118
}

src/main/java/io/opentelemetry/contrib/generator/telemetry/logs/LogGeneratorThread.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.opentelemetry.contrib.generator.telemetry.dto.GeneratorState;
2323
import io.opentelemetry.contrib.generator.telemetry.jel.JELProvider;
2424
import io.opentelemetry.contrib.generator.telemetry.logs.dto.LogDefinition;
25+
import io.opentelemetry.contrib.generator.telemetry.misc.Constants;
2526
import io.opentelemetry.contrib.generator.telemetry.transport.PayloadHandler;
2627
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest;
2728
import io.opentelemetry.proto.common.v1.AnyValue;
@@ -82,8 +83,8 @@ public void run() {
8283
.setResource(eachResource)
8384
.addScopeLogs(ScopeLogs.newBuilder()
8485
.setScope(InstrumentationScope.newBuilder()
85-
.setName("@opentelemetry/vodka-exporter")
86-
.setVersion("22.10.0")
86+
.setName(Constants.SELF_NAME)
87+
.setVersion(Constants.SELF_VERSION)
8788
.build())
8889
.addAllLogRecords(otelLogs)
8990
.build())
@@ -120,7 +121,7 @@ private List<Resource> getResourceSubsetByPostCount(String resourceName, int res
120121
//resourceEndIndex is exclusive
121122
int resourceEndIndex;
122123
List<GeneratorResource> resourcesInResourceModel = ResourceModelProvider.getResourceModel(requestID).get(resourceName).stream()
123-
.filter(GeneratorResource::isActive).collect(Collectors.toList());
124+
.filter(GeneratorResource::isActive).toList();
124125
if (resourceCount >= resourcesInResourceModel.size()) {
125126
resourceEndIndex = resourcesInResourceModel.size();
126127
} else {

src/main/java/io/opentelemetry/contrib/generator/telemetry/logs/dto/Logs.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import java.util.List;
2424
import java.util.Set;
25-
import java.util.stream.Collectors;
2625

2726
@Data
2827
public class Logs {

0 commit comments

Comments
 (0)