Skip to content

Commit

Permalink
Minor fix
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
  • Loading branch information
deshsidd committed Aug 12, 2024
1 parent f37a5fa commit 4470bcc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.stream.Collectors;

import io.opentelemetry.sdk.metrics.export.MetricExporter;

Expand Down Expand Up @@ -57,36 +58,50 @@ private static MetricExporter instantiateExporter(Class<MetricExporter> exporter
try {
// Check we ourselves are not being called by unprivileged code.
SpecialPermission.check();
return AccessController.doPrivileged((PrivilegedExceptionAction<MetricExporter>) () -> {
String methodName = "create";
String getDefaultMethod = "getDefault";
for (Method m : exporterProviderClass.getMethods()) {
if (m.getName().equals(getDefaultMethod)) {
methodName = getDefaultMethod;
break;
}
String methodName = "create";
String getDefaultMethod = "getDefault";

Method[] methods = exporterProviderClass.getMethods();
logger.info("Methods available in " + exporterProviderClass.getName() + ": " +
Arrays.stream(methods).map(Method::getName).collect(Collectors.joining(", ")));

for (Method m : exporterProviderClass.getMethods()) {
if (m.getName().equals(getDefaultMethod)) {
methodName = getDefaultMethod;
logger.info("Using 'getDefault' method for instantiation.");
break;
}
try {
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
MethodType methodType = MethodType.methodType(MetricExporter.class);

// Look up the 'create' method with no parameters
MethodHandle handle = lookup.findStatic(exporterProviderClass, methodName, methodType);
return (MetricExporter) handle.invokeExact();
} catch (Throwable e) {
if (e.getCause() instanceof NoSuchMethodException) {
throw new IllegalStateException("No create factory method exist in [" + exporterProviderClass.getName() + "]");
} else {
throw new IllegalStateException(
"MetricExporter instantiation failed for class [" + exporterProviderClass.getName() + "]",
e.getCause()
);
}
}
try {
// Log the method being looked up
logger.info("Looking up method: " + methodName);

MethodHandles.Lookup lookup = MethodHandles.publicLookup();
MethodType methodType = MethodType.methodType(MetricExporter.class);
logger.info("Method type for lookup: " + methodType.toString());

// Look up the 'create' method with no parameters
MethodHandle handle = lookup.findStatic(exporterProviderClass, methodName, methodType);
logger.info("Found method handle for " + methodName);

// Invoke the method handle
MetricExporter exporter = (MetricExporter) handle.invokeExact();
logger.info("Successfully instantiated MetricExporter: " + exporter);

return exporter;
} catch (Throwable e) {
if (e.getCause() instanceof NoSuchMethodException) {
throw new IllegalStateException("No create factory method exist in [" + exporterProviderClass.getName() + "]");
} else {
throw new IllegalStateException(
"MetricExporter instantiation failed for class [" + exporterProviderClass.getName() + "]",
e.getCause()
);
}
});
} catch (PrivilegedActionException ex) {
}
} catch (Exception ex) {
throw new IllegalStateException(
"MetricExporter instantiation failed for class [" + exporterProviderClass.getName() + "]",
"deshsid MetricExporter instantiation failed for class [" + exporterProviderClass.getName() + "]",
ex.getCause()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,4 @@ public void testMetricExporterInvalid() {
Settings settings = Settings.builder().put(OTelTelemetrySettings.OTEL_METRICS_EXPORTER_CLASS_SETTING.getKey(), "abc").build();
assertThrows(IllegalArgumentException.class, () -> OTelMetricsExporterFactory.create(settings));
}

public void testMetricExporterNoCreateFactoryMethod() {
Settings settings = Settings.builder()
.put(
OTelTelemetrySettings.OTEL_METRICS_EXPORTER_CLASS_SETTING.getKey(),
"org.opensearch.telemetry.metrics.exporter.DummyMetricExporter"
)
.build();
IllegalStateException exception = assertThrows(IllegalStateException.class, () -> OTelMetricsExporterFactory.create(settings));
assertEquals(
"MetricExporter instantiation failed for class [org.opensearch.telemetry.metrics.exporter.DummyMetricExporter]",
exception.getMessage()
);
}

public void testMetricExporterNonMetricExporterClass() {
Settings settings = Settings.builder()
.put(OTelTelemetrySettings.OTEL_METRICS_EXPORTER_CLASS_SETTING.getKey(), "java.lang.String")
.build();
IllegalStateException exception = assertThrows(IllegalStateException.class, () -> OTelMetricsExporterFactory.create(settings));
assertEquals("MetricExporter instantiation failed for class [java.lang.String]", exception.getMessage());
assertTrue(exception.getCause() instanceof NoSuchMethodError);

}
}

0 comments on commit 4470bcc

Please sign in to comment.