Skip to content

Commit a68211b

Browse files
authored
Merge pull request #98 from cisco-open/feature/master/user-defined-expressions
Refactored JELProviders to enable users to add custom expressions
2 parents 14e6102 + 4eccd22 commit a68211b

22 files changed

+837
-181
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import io.opentelemetry.contrib.generator.core.dto.*;
2020
import io.opentelemetry.contrib.generator.core.jel.ExpressionProcessor;
21-
import io.opentelemetry.contrib.generator.core.jel.JELProvider;
21+
import io.opentelemetry.contrib.generator.core.jel.ResourceExpressionsJELProvider;
2222
import io.opentelemetry.contrib.generator.core.jel.methods.ResourceModelExpressions;
2323
import io.opentelemetry.contrib.generator.core.utils.CommonUtils;
2424
import io.opentelemetry.proto.common.v1.KeyValue;
@@ -47,7 +47,7 @@ public class ResourceModelGenerator {
4747

4848
private final Map<String, ResourceDefinition> allResources; //input resource definitions
4949
private final String requestID;
50-
private static final ExpressionProcessor jelProcessor = JELProvider.getJelProcessor();
50+
private static final ExpressionProcessor jelProcessor = ResourceExpressionsJELProvider.getJelProcessor();
5151
private static Map<String, List<GeneratorResource>> resourceModel; //output resource model
5252
private Map<String, ResourceType> typeMappings; //stores parent & child types for each resource type
5353

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

src/main/java/io/opentelemetry/contrib/generator/core/jel/JELProvider.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2022 AppDynamics Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.contrib.generator.core.jel;
18+
19+
import io.opentelemetry.contrib.generator.core.exception.GeneratorException;
20+
21+
import java.lang.reflect.Method;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
/**
26+
* Provides ELProcessor initialized with all the expression methods supported by the resource model definition YAML.
27+
*/
28+
public class ResourceExpressionsJELProvider {
29+
30+
private static ExpressionProcessor jelProcessor;
31+
32+
private ResourceExpressionsJELProvider() {}
33+
34+
public static ExpressionProcessor getJelProcessor() {
35+
if (jelProcessor == null) {
36+
jelProcessor = new ExpressionProcessor();
37+
}
38+
var expressionsClass = "io.opentelemetry.contrib.generator.core.jel.methods.ResourceModelExpressions";
39+
List<String> methods = Arrays.asList("counter", "UUIDFromStringCounter", "roundRobin", "alphanumericSequenceFromEnv",
40+
"alphanumericSequence", "IPv4Sequence", "distribution", "count", "getLong", "getDouble", "getBoolean");
41+
addMethods(expressionsClass, methods);
42+
var operationsClass = "io.opentelemetry.contrib.generator.core.ResourceModelGenerator";
43+
methods = Arrays.asList("copyFromParent", "modifyFromParent");
44+
addMethods(operationsClass, methods);
45+
return jelProcessor;
46+
}
47+
48+
private static void addMethods(String expressionsClass, List<String> methods) {
49+
methods.forEach(method -> addExpression("", "", expressionsClass, method));
50+
}
51+
52+
/**
53+
* See <a href="https://jakarta.ee/specifications/platform/9/apidocs/jakarta/el/elprocessor#defineFunction-java.lang.String-java.lang.String-java.lang.String-java.lang.String-">...</a>
54+
* Must be called in your code before telemetry generation is started.
55+
*/
56+
public static void addExpression(String prefix, String function, String className, String methodName) {
57+
if (jelProcessor == null) {
58+
jelProcessor = new ExpressionProcessor();
59+
}
60+
try {
61+
jelProcessor.defineFunction(prefix, function, className, methodName);
62+
} catch (ClassNotFoundException e) {
63+
throw new GeneratorException("Unable to find " + className + " having expression methods");
64+
} catch (NoSuchMethodException e) {
65+
throw new GeneratorException("Unknown expression method " + className + "." + methodName +
66+
" provided for class", e);
67+
}
68+
}
69+
70+
/**
71+
* See <a href="https://jakarta.ee/specifications/platform/9/apidocs/jakarta/el/elprocessor#defineFunction-java.lang.String-java.lang.String-java.lang.reflect.Method-">...</a>
72+
* Must be called in your code before telemetry generation is started.
73+
*/
74+
public static void addExpression(String prefix, String function, Method method) {
75+
if (jelProcessor == null) {
76+
jelProcessor = new ExpressionProcessor();
77+
}
78+
try {
79+
jelProcessor.defineFunction(prefix, function, method);
80+
} catch (NoSuchMethodException e) {
81+
throw new GeneratorException("Unknown expression method " + method + " provided", e);
82+
}
83+
}
84+
}

src/main/java/io/opentelemetry/contrib/generator/core/jel/methods/ResourceModelExpressions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package io.opentelemetry.contrib.generator.core.jel.methods;
1818

1919
import io.opentelemetry.contrib.generator.core.jel.ExpressionProcessor;
20-
import io.opentelemetry.contrib.generator.core.jel.JELProvider;
20+
import io.opentelemetry.contrib.generator.core.jel.ResourceExpressionsJELProvider;
2121
import io.opentelemetry.contrib.generator.core.jel.helpers.AlphanumericHelper;
2222
import io.opentelemetry.contrib.generator.core.jel.helpers.IPHelper;
2323
import jakarta.el.ELProcessor;
@@ -41,7 +41,7 @@ public class ResourceModelExpressions {
4141
private static final ConcurrentHashMap<String, Integer> counters = new ConcurrentHashMap<>();
4242
private static final ConcurrentHashMap<String, Double> doubleCounters = new ConcurrentHashMap<>();
4343
private static final ConcurrentHashMap<String, String> stringCounters = new ConcurrentHashMap<>();
44-
private static final ExpressionProcessor jelProcessor = JELProvider.getJelProcessor();
44+
private static final ExpressionProcessor jelProcessor = ResourceExpressionsJELProvider.getJelProcessor();
4545
public static String expressionsGlobalKey = ""; //Modified by the resource model generator every time a new resource/attribute is being processed
4646

4747
private ResourceModelExpressions() {}

src/main/java/io/opentelemetry/contrib/generator/telemetry/jel/JELProvider.java

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)