Skip to content

Commit

Permalink
Merge pull request #92 from intergral/spi_error
Browse files Browse the repository at this point in the history
fix(spi): handle errors during SPI loading to ensure we do not break …
  • Loading branch information
Umaaz authored Jan 8, 2024
2 parents a81461d + d8bbc4f commit 58350c3
Show file tree
Hide file tree
Showing 14 changed files with 433 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .idea/runConfigurations/Agent_ONLY_Load_with_JavaAgent.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# main - Unreleased
- **[FEATURE]**: plugin: Add plugin for Otel [#87](https://github.com/intergral/deep/pull/87) [@Umaaz](https://github.com/Umaaz)
- **[FEATURE]**: traces: Add apis for creating traces from tracepoints [#87](https://github.com/intergral/deep/pull/87) [@Umaaz](https://github.com/Umaaz)
- **[BUGFIX]**: fix issue with SPI loading failing [#92](https://github.com/intergral/deep/pull/92) [@Umaaz](https://github.com/Umaaz)
- **[BUGFIX]**: fix issue with method entry tracepoings [#91](https://github.com/intergral/deep/pull/91) [@Umaaz](https://github.com/Umaaz)

# 1.1.4 (15/12/2023)
- **[CHANGE]**: plugin: Add new API for registering plugins [#84](https://github.com/intergral/deep/pull/84) [@Umaaz](https://github.com/Umaaz)
Expand Down
16 changes: 14 additions & 2 deletions agent/src/main/java/com/intergral/deep/agent/resource/SpiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
import com.intergral.deep.agent.api.spi.Ordered;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Utilities to load SPI services.
*/
public final class SpiUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(SpiUtil.class);

private SpiUtil() {
}

Expand All @@ -28,8 +33,15 @@ public static <T extends Ordered> List<T> loadOrdered(Class<T> spiClass,
static <T extends Ordered> List<T> loadOrdered(
Class<T> spiClass, ClassLoader serviceClassLoader, ServiceLoaderFinder serviceLoaderFinder) {
List<T> result = new ArrayList<>();
for (T service : serviceLoaderFinder.load(spiClass, serviceClassLoader)) {
result.add(service);
final Iterator<T> iterator = serviceLoaderFinder.load(spiClass, serviceClassLoader).iterator();
while (iterator.hasNext()) {
try {
result.add(iterator.next());
} catch (Throwable t) {
// WARNING - exception from this will have the wrong stack trace
// the error will come from 'hasNext' not 'next'
LOGGER.error("Cannot load provider.", t);
}
}
result.sort(Comparator.comparing(Ordered::order));
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2023 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.intergral.deep.agent.resource;

import com.intergral.deep.agent.api.spi.Ordered;

/**
* This type is used to test SPI loading.
*/
public interface ITestProvider extends Ordered {

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ void loadsOrderedSpi() {

assertEquals(3, loadedSpi.size());
}

@Test
void canHandleConfigErrors() {
final List<ITestProvider> ts = SpiUtil.loadOrdered(ITestProvider.class, getClass().getClassLoader());
assertEquals(1, ts.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2023 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.intergral.deep.agent.resource;


/**
* This type is used to test SPI loading.
*/
public class TestProvider implements ITestProvider {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright (C) 2024 Intergral GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

com.intergral.deep.agent.resource.TestProvider
com.intergral.deep.agent.test.NotExists
38 changes: 38 additions & 0 deletions examples/agent-only-load/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
44 changes: 44 additions & 0 deletions examples/agent-only-load/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (C) 2023 Intergral GmbH
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU Affero General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU Affero General Public License for more details.
~
~ You should have received a copy of the GNU Affero General Public License
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.intergral.deep.examples</groupId>
<artifactId>examples</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>agent-only-load</artifactId>
<name>Agent Load</name>
<description>This example uses the agent load (-javaagent) to load the deep agent. It therefore
has no dependencies of code related to deep.
</description>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2023 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.intergral.deep.examples;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

public class BaseTest {

protected final Properties systemProps = System.getProperties();


public String newId() {
return UUID.randomUUID().toString();
}


public Map<Character, Integer> makeCharCountMap(final String str) {
final HashMap<Character, Integer> res = new HashMap<Character, Integer>();

for (int i = 0; i < str.length(); i++) {
final char c = str.charAt(i);
final Integer cnt = res.get(c);
if (cnt == null) {
res.put(c, 0);
} else {
res.put(c, cnt + 1);
}
}

return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2023 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.intergral.deep.examples;



/**
* This example expects the deep agent to be loaded via the javaagent vm option.
* <p>
* See RunConfigurations for IDEA:
* <ul>
* <li>Agent ONLY Load with JavaAgent</li>
* </ul>
*/
public class Main {

/**
* Main entry for example.
*
* @param args the startup arguments
* @throws Throwable if we error
*/
public static void main(String[] args) throws Throwable {

final SimpleTest ts = new SimpleTest("This is a test", 2);
//noinspection InfiniteLoopStatement
for (; ; ) {
try {
ts.message(ts.newId());
} catch (Exception e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}

//noinspection BusyWait
Thread.sleep(1000);
}
}
}
Loading

0 comments on commit 58350c3

Please sign in to comment.