-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #571 from Azquelt/proxy-metric-registry
Support running against both MP Metrics 2.3 and 3.0
- Loading branch information
Showing
7 changed files
with
212 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ava/org/eclipse/microprofile/fault/tolerance/tck/metrics/util/MetricRegistryProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.microprofile.fault.tolerance.tck.metrics.util; | ||
|
||
import java.lang.reflect.Proxy; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.inject.Produces; | ||
import javax.enterprise.inject.spi.CDI; | ||
|
||
import org.eclipse.microprofile.metrics.MetricRegistry.Type; | ||
import org.eclipse.microprofile.metrics.annotation.RegistryType; | ||
|
||
/** | ||
* Registers a MetricRegistryProxy bean for the BASE scope | ||
*/ | ||
@ApplicationScoped | ||
public class MetricRegistryProvider { | ||
|
||
@Produces | ||
@RegistryType(type = Type.BASE) | ||
public MetricRegistryProxy getBaseRegistry() { | ||
Object metricRegistry = CDI.current().select(MetricRegistryProxyHandler.METRIC_REGISTRY_CLAZZ, RegistryTypeLiteral.BASE).get(); | ||
return getProxy(metricRegistry); | ||
} | ||
|
||
private MetricRegistryProxy getProxy(Object metricRegistry) { | ||
MetricRegistryProxyHandler handler = new MetricRegistryProxyHandler(metricRegistry); | ||
ClassLoader cl = MetricRegistryProvider.class.getClassLoader(); | ||
return (MetricRegistryProxy) Proxy.newProxyInstance(cl, new Class<?>[]{MetricRegistryProxy.class}, handler); | ||
|
||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...n/java/org/eclipse/microprofile/fault/tolerance/tck/metrics/util/MetricRegistryProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.microprofile.fault.tolerance.tck.metrics.util; | ||
|
||
import java.util.Map; | ||
import java.util.SortedMap; | ||
|
||
import org.eclipse.microprofile.metrics.Counter; | ||
import org.eclipse.microprofile.metrics.Gauge; | ||
import org.eclipse.microprofile.metrics.Metadata; | ||
import org.eclipse.microprofile.metrics.Metric; | ||
import org.eclipse.microprofile.metrics.MetricID; | ||
|
||
/** | ||
* Interface which includes the methods we need to call on MetricRegistry | ||
* <p> | ||
* This allows us to proxy these calls so that this code can run against MP Metrics 2.3 and MP Metrics 3.0 | ||
* where MetricRegistry was changed from an abstract class to an interface | ||
*/ | ||
public interface MetricRegistryProxy { | ||
|
||
Map<MetricID, Metric> getMetrics(); | ||
|
||
SortedMap<MetricID, Counter> getCounters(); | ||
|
||
@SuppressWarnings("rawtypes") // Must match MetricRegistry signature | ||
SortedMap<MetricID, Gauge> getGauges(); | ||
|
||
Map<String, Metadata> getMetadata(); | ||
} |
63 changes: 63 additions & 0 deletions
63
...org/eclipse/microprofile/fault/tolerance/tck/metrics/util/MetricRegistryProxyHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
******************************************************************************* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.microprofile.fault.tolerance.tck.metrics.util; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Invocation handler which dynamically loads the MetricRegistry class and invokes its methods by reflection | ||
*/ | ||
public class MetricRegistryProxyHandler implements InvocationHandler { | ||
|
||
private static final String METRIC_REGISTRY_CLASS_NAME = "org.eclipse.microprofile.metrics.MetricRegistry"; | ||
|
||
public static final Class<?> METRIC_REGISTRY_CLAZZ; | ||
|
||
private final Object metricRegistryInstance; | ||
|
||
static { | ||
try { | ||
METRIC_REGISTRY_CLAZZ = Class.forName(METRIC_REGISTRY_CLASS_NAME); | ||
} | ||
catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Failed to find MetricRegistry class", e); | ||
} | ||
} | ||
|
||
public MetricRegistryProxyHandler(Object metricRegistryInstance) { | ||
METRIC_REGISTRY_CLAZZ.cast(metricRegistryInstance); // Check instance is correct class | ||
this.metricRegistryInstance = metricRegistryInstance; | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
Method realMethod = METRIC_REGISTRY_CLAZZ.getMethod(method.getName(), method.getParameterTypes()); | ||
try { | ||
return realMethod.invoke(metricRegistryInstance, args); | ||
} | ||
catch (InvocationTargetException e) { | ||
throw e.getCause(); | ||
} | ||
} | ||
|
||
} |