Skip to content

Commit 5d9a230

Browse files
committed
Added e2e test for user defined expressions and a minor fix in parent-child resource mapping
1 parent 5529c82 commit 5d9a230

File tree

8 files changed

+469
-8
lines changed

8 files changed

+469
-8
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ private void mapChildResources() {
145145
var nextChildIndex = 0;
146146
//For each resource of the parent type
147147
for (var parentCounter = 0; parentCounter < parentType.getCountWithRuntimeModifications(); parentCounter++) {
148+
if (nextChildIndex >= childrenSize) {
149+
nextChildIndex = 0;
150+
}
148151
int count = jelProcessor.eval(eachChildTypeExpr.getValue());
149152
int childEndIndex = nextChildIndex + count;
150153
if (childEndIndex > childrenSize) {
@@ -155,14 +158,12 @@ private void mapChildResources() {
155158
setParentToChildren(resourceModel.get(parentType.getName()).get(parentCounter), childType,
156159
nextChildIndex, childEndIndex);
157160
nextChildIndex = nextChildIndex + count;
158-
if (nextChildIndex >= childrenSize) {
159-
nextChildIndex = 0;
160-
}
161161
}
162162
//Map any remaining child resource to the last parent resource
163163
if (nextChildIndex < childrenSize) {
164-
log.debug("Remaining children of type '" + childType + "' mapped to the last parent of type '" + parentType +
165-
"' at index " + (parentType.getCountWithRuntimeModifications()-1));
164+
log.debug("Remaining children of type '" + childType + "' from index " + nextChildIndex +
165+
" mapped to the last parent of type '" + parentType + "' at index " +
166+
(parentType.getCountWithRuntimeModifications()-1));
166167
setParentToChildren(resourceModel.get(parentType.getName()).get(parentType.getCountWithRuntimeModifications()-1), childType,
167168
nextChildIndex, childrenSize);
168169
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.opentelemetry.contrib.generator.telemetry.jel;
2+
3+
import org.apache.commons.lang3.RandomUtils;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.UUID;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.ConcurrentMap;
10+
11+
public class CustomExpressions {
12+
13+
private static final ConcurrentMap<String, Integer> counterMaps = new ConcurrentHashMap<>();
14+
private static final ConcurrentMap<String, Boolean> boolMaps = new ConcurrentHashMap<>();
15+
private static final List<Character> hexChars = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
16+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
17+
'v', 'w', 'x', 'y', 'z');
18+
19+
public static int geometricWith2(String stateKey) {
20+
if (!counterMaps.containsKey(stateKey)) {
21+
counterMaps.put(stateKey, 1);
22+
return 1;
23+
}
24+
counterMaps.put(stateKey, counterMaps.get(stateKey) * 2);
25+
return counterMaps.get(stateKey);
26+
}
27+
28+
public static synchronized int increaseByXDecreaseByY(String stateKey, int X, int Y, int startVal) {
29+
if (!counterMaps.containsKey(stateKey)) {
30+
counterMaps.put(stateKey, startVal);
31+
boolMaps.put(stateKey, true);
32+
return startVal;
33+
}
34+
if (boolMaps.get(stateKey)) {
35+
counterMaps.put(stateKey, counterMaps.get(stateKey) + X);
36+
boolMaps.put(stateKey, false);
37+
} else {
38+
counterMaps.put(stateKey, counterMaps.get(stateKey) - Y);
39+
boolMaps.put(stateKey, true);
40+
}
41+
return counterMaps.get(stateKey);
42+
}
43+
44+
public static String increasingErrorSeverity(String stateKey) {
45+
if (!counterMaps.containsKey(stateKey)) {
46+
counterMaps.put(stateKey, 1);
47+
return "INFO";
48+
}
49+
counterMaps.put(stateKey, counterMaps.get(stateKey) + 1);
50+
int currCount = counterMaps.get(stateKey);
51+
boolean isError = currCount > 4 && currCount % 5 == 0;
52+
isError = isError || currCount > 8 && currCount % 4 == 0;
53+
isError = isError || currCount > 12 && currCount % 3 == 0;
54+
isError = isError || currCount > 16;
55+
return isError ? "ERROR" : "INFO";
56+
}
57+
58+
public static String randomIPv6() {
59+
int groupLen = 4;
60+
StringBuilder ipv6Builer = new StringBuilder(randomHexadecimal(groupLen));
61+
for (int i=0; i<7; i++) {
62+
ipv6Builer.append(":").append(randomHexadecimal(groupLen));
63+
}
64+
return ipv6Builer.toString();
65+
}
66+
67+
public static String randomUUID() {
68+
return UUID.randomUUID().toString();
69+
}
70+
71+
public static String randomHexadecimal(int length) {
72+
StringBuilder hexBuilder = new StringBuilder();
73+
for (int i=0; i<length; i++) {
74+
hexBuilder.append(hexChars.get(RandomUtils.nextInt(0, 36)));
75+
}
76+
return hexBuilder.toString();
77+
}
78+
}

0 commit comments

Comments
 (0)