|
| 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