Skip to content

Commit

Permalink
Merge pull request #84 from intergral/plugin_api
Browse files Browse the repository at this point in the history
feat(plugin): add new api for plugin registration in api
  • Loading branch information
Umaaz authored Dec 15, 2023
2 parents 4dc0272 + 573d322 commit b5f8478
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
uses: robinraju/release-downloader@v1.8
with:
latest: true
fileName: Maven Site
fileName: Maven.Site

- name: Unpack
run: |
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# main - Unreleased

- **[CHANGE]**: plugin: Add new API for registering plugins [#84](https://github.com/intergral/deep/pull/84) [@Umaaz](https://github.com/Umaaz)

# 1.1.3 (15/12/2023)
- **[CHANGE]**: update: Update deep proto to 1.0.3 [#74](https://github.com/intergral/deep/pull/74) [@Umaaz](https://github.com/Umaaz)
- **[CHANGE]**: update: Update deep proto to 1.0.3 [#74](https://github.com/intergral/deep/pull/74) [@Umaaz](https://github.com/Umaaz)
- **[CHANGE]**: plugin: Change the plugins to use SPI to load [#77](https://github.com/intergral/deep/pull/77) [@Umaaz](https://github.com/Umaaz)
- **[FEATURE]**: metrics: Add support for metrics from tracepoints [#74](https://github.com/intergral/deep/pull/74) [@Umaaz](https://github.com/Umaaz)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.intergral.deep.agent.api;

import com.intergral.deep.agent.api.plugin.MetricDefinition;
import com.intergral.deep.agent.api.spi.IDeepPlugin;
import com.intergral.deep.agent.api.tracepoint.ITracepoint.ITracepointRegistration;
import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -55,6 +56,14 @@ public interface IDeep {
*/
String getVersion();

/**
* This allows the registration of custom plugins.
*
* @param plugin the plugin that can be used to decorate snapshots
* @return a {@link IRegistration} that can be used to unregister the plugin
*/
IRegistration<IDeepPlugin> registerPlugin(final IDeepPlugin plugin);

/**
* Create a tracepoint that will only exist on this instance.
*
Expand Down
7 changes: 7 additions & 0 deletions agent/src/main/java/com/intergral/deep/agent/DeepAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.intergral.deep.agent.api.DeepVersion;
import com.intergral.deep.agent.api.IDeep;
import com.intergral.deep.agent.api.IRegistration;
import com.intergral.deep.agent.api.plugin.MetricDefinition;
import com.intergral.deep.agent.api.resource.Resource;
import com.intergral.deep.agent.api.spi.IDeepPlugin;
Expand Down Expand Up @@ -84,6 +85,12 @@ public String getVersion() {
return DeepVersion.VERSION;
}

@Override
public IRegistration<IDeepPlugin> registerPlugin(final IDeepPlugin plugin) {
plugin.configure(this.settings, Reflection.getInstance());
return this.settings.addPlugin(plugin);
}

@Override
public ITracepointRegistration registerTracepoint(final String path, final int line) {
return registerTracepoint(path, line, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.intergral.deep.agent.settings;

import com.intergral.deep.agent.api.IRegistration;
import com.intergral.deep.agent.api.logger.ITracepointLogger;
import com.intergral.deep.agent.api.logger.TracepointLogger;
import com.intergral.deep.agent.api.resource.Resource;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class Settings implements ISettings {
private static final AtomicBoolean IS_ACTIVE = new AtomicBoolean(true);
private final Properties properties;
private Resource resource;
private Collection<IDeepPlugin> plugins = Collections.emptyList();
private final Collection<IDeepPlugin> plugins = new ArrayList<>();
private ITracepointLogger tracepointLogger = new TracepointLogger();

private Settings(Properties properties) {
Expand Down Expand Up @@ -321,7 +322,7 @@ public List<String> getAsList(final String key) {
* @return the discovered plugin with the given name, or {@code null} if a plugin with the provided name and type cannot be found.
*/
public <T> T getPluginByName(final Class<T> clazz, final String name) {
final Collection<T> plugins = getPlugins(clazz, t -> t.getClass().getName().endsWith(name));
final Collection<T> plugins = getPlugins(clazz, t -> t.getClass().getName().equals(name));
if (plugins.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -364,7 +365,8 @@ private <T> Collection<T> getPlugins(final Class<T> target, final Predicate<T> p
* @param plugins the plugins to use
*/
public void setPlugins(Collection<IDeepPlugin> plugins) {
this.plugins = plugins;
this.plugins.clear();
this.plugins.addAll(plugins);
}

/**
Expand Down Expand Up @@ -419,6 +421,30 @@ public void setTracepointLogger(final ITracepointLogger tracepointLogger) {
this.tracepointLogger = tracepointLogger;
}

/**
* Add a plugin to the current config.
*
* @param plugin the new plugin
* @return the plugin registration
*/
public IRegistration<IDeepPlugin> addPlugin(final IDeepPlugin plugin) {
this.plugins.add(plugin);
return new IRegistration<IDeepPlugin>() {
@Override
public void unregister() {
final boolean removeIf = Settings.this.plugins.removeIf(existing -> existing == plugin);
if (!removeIf) {
throw new IllegalStateException(String.format("cannot remove plugin: %s", plugin));
}
}

@Override
public IDeepPlugin get() {
return plugin;
}
};
}

/**
* Used to indicate an invalid config value.
*/
Expand Down
14 changes: 14 additions & 0 deletions agent/src/test/java/com/intergral/deep/agent/DeepAgentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static org.mockito.Mockito.times;

import com.intergral.deep.agent.api.DeepVersion;
import com.intergral.deep.agent.api.IRegistration;
import com.intergral.deep.agent.api.spi.IDeepPlugin;
import com.intergral.deep.agent.api.tracepoint.ITracepoint;
import com.intergral.deep.agent.api.tracepoint.ITracepoint.ITracepointRegistration;
import com.intergral.deep.agent.settings.Settings;
Expand Down Expand Up @@ -79,7 +81,19 @@ void start_shouldSetPluginsAndResource() throws IOException {

Mockito.verify(settings).setPlugins(Mockito.anyCollection());
Mockito.verify(settings).setResource(Mockito.any());
}

@Test
void registerPlugin() {
//noinspection unchecked
final IRegistration<IDeepPlugin> iRegistration = Mockito.mock(IRegistration.class);
Mockito.when(settings.addPlugin(Mockito.any())).thenReturn(iRegistration);

final IDeepPlugin deepPlugin = Mockito.mock(IDeepPlugin.class);
deepAgent.registerPlugin(deepPlugin);

Mockito.verify(settings, times(1)).addPlugin(Mockito.any());
Mockito.verify(deepPlugin, times(1)).configure(settings, Reflection.getInstance());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.intergral.deep.agent.api.IRegistration;
import com.intergral.deep.agent.api.logger.ITracepointLogger;
import com.intergral.deep.agent.api.plugin.IMetricProcessor;
import com.intergral.deep.agent.api.spi.IDeepPlugin;
Expand Down Expand Up @@ -177,6 +178,32 @@ void plugins() {
assertNull(notFound);
}

@Test
void addPlugin() {
final Settings settings = Settings.build(new HashMap<>());

final TestPlugin plugin = new TestPlugin();
final IRegistration<IDeepPlugin> iDeepPluginIRegistration = settings.addPlugin(plugin);

assertNotNull(iDeepPluginIRegistration.get());

assertSame(plugin, iDeepPluginIRegistration.get());

assertEquals(1, settings.getPlugins().size());

assertNotNull(settings.getPluginByName(TestPlugin.class, TestPlugin.class.getName()));

iDeepPluginIRegistration.unregister();

assertEquals(0, settings.getPlugins().size());

assertNull(settings.getPluginByName(TestPlugin.class, TestPlugin.class.getName()));

final IllegalStateException illegalStateException = assertThrows(IllegalStateException.class, iDeepPluginIRegistration::unregister);
assertEquals("cannot remove plugin: TestPlugin{}", illegalStateException.getMessage());

}

private static class TestPlugin implements IDeepPlugin, IMetricProcessor, ITracepointLogger {

@Override
Expand Down Expand Up @@ -207,5 +234,10 @@ public void summary(final String name, final Map<String, Object> labels, final S
public void logTracepoint(final String logMsg, final String tracepointId, final String snapshotId) {

}

@Override
public String toString() {
return "TestPlugin{}";
}
}
}

0 comments on commit b5f8478

Please sign in to comment.