|
| 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 | +} |
0 commit comments