From a6ea011b226c4ef78d3d38bbe0d2972b5aaf20e2 Mon Sep 17 00:00:00 2001 From: nadav Date: Mon, 24 Jun 2024 16:05:43 +0300 Subject: [PATCH 1/8] Works --- agent/pom.xml | 11 +-- .../src/main/java/io/lumigo/agent/Agent.java | 30 +++++++- layer/generate.sh | 8 ++ pom.xml | 13 ++++ .../core/instrumentation/agent/Loader.java | 51 ++++++++----- .../impl/AmazonHttpClientInstrumentation.java | 2 +- .../impl/ApacheHttpInstrumentation.java | 2 +- .../ApacheKafkaConsumerInstrumentation.java | 2 +- .../ApacheKafkaProducerInstrumentation.java | 2 +- ...wsLambdaRequestHandlerInstrumentation.java | 74 +++++++++++++++++++ 10 files changed, 164 insertions(+), 31 deletions(-) create mode 100755 layer/generate.sh create mode 100644 src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java diff --git a/agent/pom.xml b/agent/pom.xml index aedafed..5f767ab 100644 --- a/agent/pom.xml +++ b/agent/pom.xml @@ -32,12 +32,6 @@ Lumigo https://lumigo.io/ - - Uri Parush - uri@lumigo.io - Lumigo - https://lumigo.io - @@ -104,9 +98,12 @@ io.lumigo.agent.Agent - false + io.lumigo.agent.Agent + io.lumigo.agent.Agent + true true false + Lumigo diff --git a/agent/src/main/java/io/lumigo/agent/Agent.java b/agent/src/main/java/io/lumigo/agent/Agent.java index 8645189..e2458ff 100644 --- a/agent/src/main/java/io/lumigo/agent/Agent.java +++ b/agent/src/main/java/io/lumigo/agent/Agent.java @@ -4,6 +4,7 @@ import java.lang.instrument.Instrumentation; import java.lang.reflect.Method; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Files; @@ -11,19 +12,37 @@ import java.nio.file.Paths; import java.util.LinkedList; import java.util.List; +import java.util.jar.JarFile; import java.util.stream.Collectors; import java.util.stream.Stream; public class Agent { + private static boolean initialized = false; + + public static String LUMIGO_JAVA_TRACER_PATH = "/opt/lumigo/lumigo-tracer.jar"; + + public static void premain(String agentArgs, Instrumentation inst) { + agentmain(agentArgs, inst); + // TODO handle switch off + } + public static void agentmain(String agentArgs, Instrumentation inst) { + if (initialized) { + return; + } + initialized = true; try { URL[] urls; if ("lib".equalsIgnoreCase(agentArgs)) { urls = getUrls(); } else { - urls = new URL[] {new File("/var/task/").toURI().toURL()}; + List jars = new LinkedList<>(); + jars.add(new File("/var/task/").toURI().toURL()); + jars.add(new File(LUMIGO_JAVA_TRACER_PATH).toURI().toURL()); + urls = jars.toArray(new URL[jars.size()]); } + installTracerJar(inst); URLClassLoader newClassLoader = new URLClassLoader(urls, null); Thread.currentThread().setContextClassLoader(newClassLoader); final Class loader = @@ -35,6 +54,15 @@ public static void agentmain(String agentArgs, Instrumentation inst) { } } + private static void installTracerJar(Instrumentation inst) { + try (JarFile jar = new JarFile(new File(new URI(LUMIGO_JAVA_TRACER_PATH)))){ + inst.appendToSystemClassLoaderSearch(jar); + } catch (Exception e) { + System.out.println("Failed to append java tracer jar to system class loader: " + LUMIGO_JAVA_TRACER_PATH); + e.printStackTrace(); + } + } + public static URL[] getUrls() { List jars = new LinkedList<>(); try (Stream paths = Files.walk(Paths.get("/var/task/lib"))) { diff --git a/layer/generate.sh b/layer/generate.sh new file mode 100755 index 0000000..c479540 --- /dev/null +++ b/layer/generate.sh @@ -0,0 +1,8 @@ +mkdir -p lumigo + +cp ../target/java-tracer-1.0.47.jar lumigo/lumigo-tracer.jar +cp ../agent/target/lumigo-agent-1.0.47.jar lumigo/lumigo-agent.jar + +zip -r lumigo-java-layer.zip lumigo + +rm -rf lumigo \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0fc6760..7be8689 100644 --- a/pom.xml +++ b/pom.xml @@ -235,6 +235,19 @@ + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + org.apache.maven.plugins diff --git a/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java b/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java index 77be428..cf69090 100644 --- a/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java +++ b/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java @@ -7,6 +7,9 @@ import net.bytebuddy.agent.builder.AgentBuilder; import org.pmw.tinylog.Logger; +import java.util.Arrays; + +@SuppressWarnings("unused") public class Loader { public static void instrument(java.lang.instrument.Instrumentation inst) { Logger.debug("Start Instrumentation"); @@ -19,28 +22,36 @@ public static void instrument(java.lang.instrument.Instrumentation inst) { new ApacheKafkaProducerInstrumentation(); ApacheKafkaConsumerInstrumentation apacheKafkaConsumerInstrumentation = new ApacheKafkaConsumerInstrumentation(); + AwsLambdaRequestHandlerInstrumentation awsLambdaRequestHandlerInstrumentation = + new AwsLambdaRequestHandlerInstrumentation(); AgentBuilder builder = new AgentBuilder.Default() .disableClassFormatChanges() .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) - .ignore( - not(nameStartsWith("com.amazonaws.http.AmazonHttpClient")) - .and(not(nameStartsWith("org.apache.http.impl.client"))) - .and( - not( - nameStartsWith( - AmazonHttpClientV2Instrumentation - .INSTRUMENTATION_PACKAGE_PREFIX))) - .and( - not( - nameStartsWith( - ApacheKafkaProducerInstrumentation - .INSTRUMENTATION_PACKAGE_PREFIX))) - .and( - not( - nameStartsWith( - ApacheKafkaConsumerInstrumentation - .INSTRUMENTATION_PACKAGE_PREFIX)))) +// .ignore( +// not(nameStartsWith("com.amazonaws.http.AmazonHttpClient")) +// .and(not(nameStartsWith("org.apache.http.impl.client"))) +// .and( +// not( +// nameStartsWith( +// AmazonHttpClientV2Instrumentation +// .INSTRUMENTATION_PACKAGE_PREFIX))) +// .and( +// not( +// nameStartsWith( +// ApacheKafkaProducerInstrumentation +// .INSTRUMENTATION_PACKAGE_PREFIX))) +// .and( +// not( +// nameStartsWith( +// ApacheKafkaConsumerInstrumentation +// .INSTRUMENTATION_PACKAGE_PREFIX))) +// .and( +// not( +// nameStartsWith( +// AwsLambdaRequestHandlerInstrumentation +// .INSTRUMENTATION_PACKAGE_PREFIX))) +// ) .type(apacheHttpInstrumentation.getTypeMatcher()) .transform(apacheHttpInstrumentation.getTransformer()) .type(amazonHttpClientInstrumentation.getTypeMatcher()) @@ -50,7 +61,9 @@ public static void instrument(java.lang.instrument.Instrumentation inst) { .type(apacheKafkaInstrumentation.getTypeMatcher()) .transform(apacheKafkaInstrumentation.getTransformer()) .type(apacheKafkaConsumerInstrumentation.getTypeMatcher()) - .transform(apacheKafkaConsumerInstrumentation.getTransformer()); + .transform(apacheKafkaConsumerInstrumentation.getTransformer()) + .type(awsLambdaRequestHandlerInstrumentation.getTypeMatcher()) + .transform(awsLambdaRequestHandlerInstrumentation.getTransformer()); builder.installOn(inst); Logger.debug("Finish Instrumentation"); diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/AmazonHttpClientInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/AmazonHttpClientInstrumentation.java index a624812..255a59a 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/AmazonHttpClientInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/AmazonHttpClientInstrumentation.java @@ -43,7 +43,7 @@ public static void methodEnter(@Advice.Argument(0) final Request request) { String patchedRoot = spansContainer.getPatchedRoot(); request.getHeaders().put("X-Amzn-Trace-Id", patchedRoot); startTimeMap.put(request.hashCode(), System.currentTimeMillis()); - } catch (Exception e) { + } catch (Throwable e) { Logger.error(e, "Failed to send data on http requests"); } } diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheHttpInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheHttpInstrumentation.java index c96b7ce..e72fbb4 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheHttpInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheHttpInstrumentation.java @@ -59,7 +59,7 @@ public static void methodEnter(@Advice.Argument(0) final HttpUriRequest request) return; } startTimeMap.put(request.hashCode(), System.currentTimeMillis()); - } catch (Exception e) { + } catch (Throwable e) { Logger.error(e); } } diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java index e5345ef..2e74d8b 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java @@ -48,7 +48,7 @@ public static class ApacheKafkaConsumerAdvice { public static void methodEnter(@Advice.FieldValue("clientId") String clientId) { try { startTimeMap.put(clientId, System.currentTimeMillis()); - } catch (Exception e) { + } catch (Throwable e) { Logger.error(e); } } diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaProducerInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaProducerInstrumentation.java index c6b3b37..852fd16 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaProducerInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaProducerInstrumentation.java @@ -78,7 +78,7 @@ public static void methodEnter( .toString() .substring(0, 10) .getBytes(StandardCharsets.UTF_8)); - } catch (Exception e) { + } catch (Throwable e) { Logger.error(e); } } diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java new file mode 100644 index 0000000..67359a6 --- /dev/null +++ b/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java @@ -0,0 +1,74 @@ +package io.lumigo.core.instrumentation.impl; + +import com.amazonaws.services.lambda.runtime.Context; + +import io.lumigo.core.SpansContainer; +import io.lumigo.core.instrumentation.LumigoInstrumentationApi; +import io.lumigo.core.instrumentation.agent.Loader; +import io.lumigo.core.network.Reporter; +import io.lumigo.core.utils.EnvUtil; +import net.bytebuddy.agent.builder.AgentBuilder; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.implementation.bytecode.assign.Assigner.Typing; +import net.bytebuddy.matcher.ElementMatcher; +import org.pmw.tinylog.Logger; + +import static net.bytebuddy.matcher.ElementMatchers.*; + +public class AwsLambdaRequestHandlerInstrumentation implements LumigoInstrumentationApi { + @Override + public ElementMatcher getTypeMatcher() { + return hasSuperType(named("com.amazonaws.services.lambda.runtime.RequestHandler")) + // we don't want to instrument handlers that implement our interfaces because they are already instrumented + .and(not(hasSuperType(named("io.lumigo.handlers.LumigoRequestHandler"))) + .and(not(hasSuperType(named("io.lumigo.handlers.LumigoRequestStreamHandler"))))); + } + + @Override + public AgentBuilder.Transformer.ForAdvice getTransformer() { + return new AgentBuilder.Transformer.ForAdvice() + .include(Loader.class.getClassLoader()) + .advice( + isMethod() + .and(isPublic()) + .and(named("handleRequest")) + .and(takesArgument(1, named("com.amazonaws.services.lambda.runtime.Context"))), + HandleRequestAdvice.class.getName()); + } + + @SuppressWarnings("unused") + public static class HandleRequestAdvice { + @Advice.OnMethodEnter(suppress = Throwable.class) + public static void methodEnter( + @Advice.Argument(value = 0, typing = Typing.DYNAMIC) Object input, + @Advice.Argument(1) Context context, + @Advice.Local("lumigoSpansContainer") SpansContainer spansContainer + ) { + try { + spansContainer = SpansContainer.getInstance(); + spansContainer.init(new EnvUtil().getEnv(), new Reporter(), context, input); + spansContainer.start(); + Logger.debug("Finish sending start message and instrumentation"); + } catch (Throwable e) { + Logger.error(e, "Failed to init span container"); + } + } + + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + public static void methodExit( + @Advice.Return(readOnly = false) Object returnValue, + @Advice.Thrown Throwable throwable, + @Advice.Local("lumigoSpansContainer") SpansContainer spansContainer) { + try { + if (throwable != null) { + spansContainer.endWithException(throwable); + } else { + spansContainer.end(returnValue); + } + } catch (Throwable e) { + Logger.error(e, "Failed to create end span"); + } + } + } +} From 5938e8883b9a08e1c3a66185f6d0406729be1432 Mon Sep 17 00:00:00 2001 From: nadav Date: Mon, 24 Jun 2024 16:54:05 +0300 Subject: [PATCH 2/8] fixes --- .../src/main/java/io/lumigo/agent/Agent.java | 2 +- pom.xml | 1 + .../core/instrumentation/agent/Loader.java | 24 ------------------- .../ApacheKafkaConsumerInstrumentation.java | 3 --- .../ApacheKafkaProducerInstrumentation.java | 4 +--- 5 files changed, 3 insertions(+), 31 deletions(-) diff --git a/agent/src/main/java/io/lumigo/agent/Agent.java b/agent/src/main/java/io/lumigo/agent/Agent.java index e2458ff..7ba1323 100644 --- a/agent/src/main/java/io/lumigo/agent/Agent.java +++ b/agent/src/main/java/io/lumigo/agent/Agent.java @@ -55,7 +55,7 @@ public static void agentmain(String agentArgs, Instrumentation inst) { } private static void installTracerJar(Instrumentation inst) { - try (JarFile jar = new JarFile(new File(new URI(LUMIGO_JAVA_TRACER_PATH)))){ + try (JarFile jar = new JarFile(new File(new File(LUMIGO_JAVA_TRACER_PATH).toURI()))){ inst.appendToSystemClassLoaderSearch(jar); } catch (Exception e) { System.out.println("Failed to append java tracer jar to system class loader: " + LUMIGO_JAVA_TRACER_PATH); diff --git a/pom.xml b/pom.xml index 7be8689..8b5b694 100644 --- a/pom.xml +++ b/pom.xml @@ -182,6 +182,7 @@ json 20210307 + org.junit.jupiter diff --git a/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java b/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java index cf69090..e648b03 100644 --- a/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java +++ b/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java @@ -28,30 +28,6 @@ public static void instrument(java.lang.instrument.Instrumentation inst) { new AgentBuilder.Default() .disableClassFormatChanges() .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) -// .ignore( -// not(nameStartsWith("com.amazonaws.http.AmazonHttpClient")) -// .and(not(nameStartsWith("org.apache.http.impl.client"))) -// .and( -// not( -// nameStartsWith( -// AmazonHttpClientV2Instrumentation -// .INSTRUMENTATION_PACKAGE_PREFIX))) -// .and( -// not( -// nameStartsWith( -// ApacheKafkaProducerInstrumentation -// .INSTRUMENTATION_PACKAGE_PREFIX))) -// .and( -// not( -// nameStartsWith( -// ApacheKafkaConsumerInstrumentation -// .INSTRUMENTATION_PACKAGE_PREFIX))) -// .and( -// not( -// nameStartsWith( -// AwsLambdaRequestHandlerInstrumentation -// .INSTRUMENTATION_PACKAGE_PREFIX))) -// ) .type(apacheHttpInstrumentation.getTypeMatcher()) .transform(apacheHttpInstrumentation.getTransformer()) .type(amazonHttpClientInstrumentation.getTypeMatcher()) diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java index 2e74d8b..bbb05ce 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaConsumerInstrumentation.java @@ -16,9 +16,6 @@ import org.pmw.tinylog.Logger; public class ApacheKafkaConsumerInstrumentation implements LumigoInstrumentationApi { - - public static final String INSTRUMENTATION_PACKAGE_PREFIX = "org.apache.kafka.clients.consumer"; - @Override public ElementMatcher getTypeMatcher() { return named("org.apache.kafka.clients.consumer.KafkaConsumer"); diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaProducerInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaProducerInstrumentation.java index 852fd16..829baa6 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaProducerInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/ApacheKafkaProducerInstrumentation.java @@ -21,9 +21,6 @@ import org.pmw.tinylog.Logger; public class ApacheKafkaProducerInstrumentation implements LumigoInstrumentationApi { - - public static final String INSTRUMENTATION_PACKAGE_PREFIX = "org.apache.kafka.clients.producer"; - @Override public ElementMatcher getTypeMatcher() { return named("org.apache.kafka.clients.producer.KafkaProducer"); @@ -50,6 +47,7 @@ public AgentBuilder.Transformer.ForAdvice getTransformer() { ApacheKafkaProducerAdvice.class.getName()); } + @SuppressWarnings("unused") public static class ApacheKafkaProducerAdvice { public static final SpansContainer spansContainer = SpansContainer.getInstance(); From 90f45ff7fcb7d79f54b381ab83c2bc54acfbe28c Mon Sep 17 00:00:00 2001 From: nadav Date: Mon, 24 Jun 2024 17:07:49 +0300 Subject: [PATCH 3/8] support kill switch in agent --- agent/src/main/java/io/lumigo/agent/Agent.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/agent/src/main/java/io/lumigo/agent/Agent.java b/agent/src/main/java/io/lumigo/agent/Agent.java index 7ba1323..f1027da 100644 --- a/agent/src/main/java/io/lumigo/agent/Agent.java +++ b/agent/src/main/java/io/lumigo/agent/Agent.java @@ -4,7 +4,6 @@ import java.lang.instrument.Instrumentation; import java.lang.reflect.Method; import java.net.MalformedURLException; -import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Files; @@ -23,8 +22,9 @@ public class Agent { public static String LUMIGO_JAVA_TRACER_PATH = "/opt/lumigo/lumigo-tracer.jar"; public static void premain(String agentArgs, Instrumentation inst) { - agentmain(agentArgs, inst); - // TODO handle switch off + if (!isKillSwitchOn()) { + agentmain(agentArgs, inst); + } } public static void agentmain(String agentArgs, Instrumentation inst) { @@ -37,10 +37,7 @@ public static void agentmain(String agentArgs, Instrumentation inst) { if ("lib".equalsIgnoreCase(agentArgs)) { urls = getUrls(); } else { - List jars = new LinkedList<>(); - jars.add(new File("/var/task/").toURI().toURL()); - jars.add(new File(LUMIGO_JAVA_TRACER_PATH).toURI().toURL()); - urls = jars.toArray(new URL[jars.size()]); + urls = new URL[] {new File("/var/task/").toURI().toURL(), new File(LUMIGO_JAVA_TRACER_PATH).toURI().toURL()}; } installTracerJar(inst); URLClassLoader newClassLoader = new URLClassLoader(urls, null); @@ -58,7 +55,6 @@ private static void installTracerJar(Instrumentation inst) { try (JarFile jar = new JarFile(new File(new File(LUMIGO_JAVA_TRACER_PATH).toURI()))){ inst.appendToSystemClassLoaderSearch(jar); } catch (Exception e) { - System.out.println("Failed to append java tracer jar to system class loader: " + LUMIGO_JAVA_TRACER_PATH); e.printStackTrace(); } } @@ -84,4 +80,9 @@ public static URL[] getUrls() { } return jars.toArray(new URL[jars.size()]); } + + public static boolean isKillSwitchOn() { + String value = System.getenv("LUMIGO_SWITCH_OFF"); + return "true".equalsIgnoreCase(value); + } } From 002be9d5e2b3cb33ac4e5faa0cea05b8c063cef7 Mon Sep 17 00:00:00 2001 From: nadav Date: Mon, 24 Jun 2024 17:19:12 +0300 Subject: [PATCH 4/8] README --- .bumpversion.cfg | 2 ++ README.md | 9 ++++++++- layer/generate.sh | 4 +--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 14454ff..2debdb2 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -11,4 +11,6 @@ tag = True [bumpversion:file:src/main/java/io/lumigo/core/configuration/Configuration.java] +[bumpversion:file:layer/generate.sh] + [bumpversion:file:src/main/resources/lumigo-version.txt] diff --git a/README.md b/README.md index 48459db..12bca8e 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,13 @@ Find the latest version here (the format of the version will be n.n.n): } } ``` + +## Lambda Auto tracing with lambda layer + +* Add to your lambda a new layer with the arn from here +* Add environment variable `JAVA_TOOL_OPTIONS` and set it to `-javaagent:/opt/lumigo/lumigo-agent.jar` (This is instead of the flag for more than java11 support) +* Add the `LUMIGO_TRACER_TOKEN` env var. + ### Configuration @@ -120,7 +127,7 @@ class MyFunction implements RequestHandler { ### Support Java 11 and Above Add the environment variable `JAVA_TOOL_OPTIONS` to your Lambda functions and set it to -`-Djdk.attach.allowAttachSelf=true` in addition to the manual code mentioned above. +`-Djdk.attach.allowAttachSelf=true` in addition to the manual code mentioned above (This is not needed for the auto trace with lambda layer). ### Supported Instrumentation Libraries diff --git a/layer/generate.sh b/layer/generate.sh index c479540..5f52c5b 100755 --- a/layer/generate.sh +++ b/layer/generate.sh @@ -3,6 +3,4 @@ mkdir -p lumigo cp ../target/java-tracer-1.0.47.jar lumigo/lumigo-tracer.jar cp ../agent/target/lumigo-agent-1.0.47.jar lumigo/lumigo-agent.jar -zip -r lumigo-java-layer.zip lumigo - -rm -rf lumigo \ No newline at end of file +zip -r lumigo-java-layer.zip lumigo \ No newline at end of file From e4fd39f66a0d85ec1861e596ed2c531bd15b69bc Mon Sep 17 00:00:00 2001 From: CircleCI Date: Mon, 24 Jun 2024 17:53:35 +0300 Subject: [PATCH 5/8] Support release --- layer/generate.sh | 6 ----- layers/LAYERS.md | 4 ++++ pom.xml | 41 +++++++++++++++++++++++----------- scripts/bd_to_prod.sh | 20 +++++++++++++++++ scripts/prepare_layer_files.sh | 15 +++++++++++++ 5 files changed, 67 insertions(+), 19 deletions(-) delete mode 100755 layer/generate.sh create mode 100644 layers/LAYERS.md create mode 100755 scripts/prepare_layer_files.sh diff --git a/layer/generate.sh b/layer/generate.sh deleted file mode 100755 index 5f52c5b..0000000 --- a/layer/generate.sh +++ /dev/null @@ -1,6 +0,0 @@ -mkdir -p lumigo - -cp ../target/java-tracer-1.0.47.jar lumigo/lumigo-tracer.jar -cp ../agent/target/lumigo-agent-1.0.47.jar lumigo/lumigo-agent.jar - -zip -r lumigo-java-layer.zip lumigo \ No newline at end of file diff --git a/layers/LAYERS.md b/layers/LAYERS.md new file mode 100644 index 0000000..34d9289 --- /dev/null +++ b/layers/LAYERS.md @@ -0,0 +1,4 @@ +Java Layers +---- +| Region | ARN | +| --- | --- | diff --git a/pom.xml b/pom.xml index 8b5b694..1265ebc 100644 --- a/pom.xml +++ b/pom.xml @@ -236,19 +236,6 @@ - - org.apache.maven.plugins - maven-shade-plugin - 3.2.4 - - - package - - shade - - - - org.apache.maven.plugins @@ -386,4 +373,32 @@ + + + shade + + + includeShade + true + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + + + \ No newline at end of file diff --git a/scripts/bd_to_prod.sh b/scripts/bd_to_prod.sh index de09605..bfc232a 100755 --- a/scripts/bd_to_prod.sh +++ b/scripts/bd_to_prod.sh @@ -40,6 +40,26 @@ mvn -f agent/pom.xml nexus-staging:release mvn -Dmaven.test.skip=true -Dfindbugs.skip=true clean deploy mvn nexus-staging:release +echo "Creating lumigo-java-tracer layer" +./scripts/prepare_layer_files.sh + +echo "Creating layer latest version arn table md file (LAYERS.md)" +commit_version="$(git describe --abbrev=0 --tags)" +../utils/common_bash/create_layer.sh \ + --layer-name lumigo-java-tracer \ + --region ALL \ + --package-folder python \ + --version "$commit_version" \ + --runtimes "java11 java17 java21" + +cd ../larn && npm i -g +larn -r java11 -n layers/LAYERS.md --filter lumigo-java-tracer -p ~/java-tracer +cd ../java-tracer + +git add layers/LAYERS.md +git commit -m "docs: update layers md [skip ci]" +git push origin master + echo "Create release tag" push_tags diff --git a/scripts/prepare_layer_files.sh b/scripts/prepare_layer_files.sh new file mode 100755 index 0000000..1ccef4b --- /dev/null +++ b/scripts/prepare_layer_files.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +mkdir -p lumigo + +mvn -Dfindbugs.skip=true -DskipTests=true -DincludeShade=true clean package +pushd agent + mvn -Dfindbugs.skip=true -DskipTests=true clean package +popd + +cp target/java-tracer-1.0.47.jar lumigo/lumigo-tracer.jar +cp agent/target/lumigo-agent-1.0.47.jar lumigo/lumigo-agent.jar + +zip -r lumigo-java-layer.zip lumigo + +rm -rf lumigo \ No newline at end of file From 49a0f20fcf4cece70ca1e7ad94a178982de4b02f Mon Sep 17 00:00:00 2001 From: CircleCI Date: Mon, 24 Jun 2024 17:55:42 +0300 Subject: [PATCH 6/8] checks --- .../src/main/java/io/lumigo/agent/Agent.java | 8 +++-- .../core/instrumentation/agent/Loader.java | 5 ---- ...wsLambdaRequestHandlerInstrumentation.java | 30 ++++++++++++------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/agent/src/main/java/io/lumigo/agent/Agent.java b/agent/src/main/java/io/lumigo/agent/Agent.java index f1027da..0d6401e 100644 --- a/agent/src/main/java/io/lumigo/agent/Agent.java +++ b/agent/src/main/java/io/lumigo/agent/Agent.java @@ -37,7 +37,11 @@ public static void agentmain(String agentArgs, Instrumentation inst) { if ("lib".equalsIgnoreCase(agentArgs)) { urls = getUrls(); } else { - urls = new URL[] {new File("/var/task/").toURI().toURL(), new File(LUMIGO_JAVA_TRACER_PATH).toURI().toURL()}; + urls = + new URL[] { + new File("/var/task/").toURI().toURL(), + new File(LUMIGO_JAVA_TRACER_PATH).toURI().toURL() + }; } installTracerJar(inst); URLClassLoader newClassLoader = new URLClassLoader(urls, null); @@ -52,7 +56,7 @@ public static void agentmain(String agentArgs, Instrumentation inst) { } private static void installTracerJar(Instrumentation inst) { - try (JarFile jar = new JarFile(new File(new File(LUMIGO_JAVA_TRACER_PATH).toURI()))){ + try (JarFile jar = new JarFile(new File(new File(LUMIGO_JAVA_TRACER_PATH).toURI()))) { inst.appendToSystemClassLoaderSearch(jar); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java b/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java index e648b03..3d82db0 100644 --- a/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java +++ b/src/main/java/io/lumigo/core/instrumentation/agent/Loader.java @@ -1,14 +1,9 @@ package io.lumigo.core.instrumentation.agent; -import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; -import static net.bytebuddy.matcher.ElementMatchers.not; - import io.lumigo.core.instrumentation.impl.*; import net.bytebuddy.agent.builder.AgentBuilder; import org.pmw.tinylog.Logger; -import java.util.Arrays; - @SuppressWarnings("unused") public class Loader { public static void instrument(java.lang.instrument.Instrumentation inst) { diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java index 67359a6..cde099e 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java @@ -1,7 +1,8 @@ package io.lumigo.core.instrumentation.impl; -import com.amazonaws.services.lambda.runtime.Context; +import static net.bytebuddy.matcher.ElementMatchers.*; +import com.amazonaws.services.lambda.runtime.Context; import io.lumigo.core.SpansContainer; import io.lumigo.core.instrumentation.LumigoInstrumentationApi; import io.lumigo.core.instrumentation.agent.Loader; @@ -14,15 +15,19 @@ import net.bytebuddy.matcher.ElementMatcher; import org.pmw.tinylog.Logger; -import static net.bytebuddy.matcher.ElementMatchers.*; - public class AwsLambdaRequestHandlerInstrumentation implements LumigoInstrumentationApi { @Override public ElementMatcher getTypeMatcher() { return hasSuperType(named("com.amazonaws.services.lambda.runtime.RequestHandler")) - // we don't want to instrument handlers that implement our interfaces because they are already instrumented - .and(not(hasSuperType(named("io.lumigo.handlers.LumigoRequestHandler"))) - .and(not(hasSuperType(named("io.lumigo.handlers.LumigoRequestStreamHandler"))))); + // we don't want to instrument handlers that implement our interfaces because they + // are already instrumented + .and( + not(hasSuperType(named("io.lumigo.handlers.LumigoRequestHandler"))) + .and( + not( + hasSuperType( + named( + "io.lumigo.handlers.LumigoRequestStreamHandler"))))); } @Override @@ -31,9 +36,13 @@ public AgentBuilder.Transformer.ForAdvice getTransformer() { .include(Loader.class.getClassLoader()) .advice( isMethod() - .and(isPublic()) - .and(named("handleRequest")) - .and(takesArgument(1, named("com.amazonaws.services.lambda.runtime.Context"))), + .and(isPublic()) + .and(named("handleRequest")) + .and( + takesArgument( + 1, + named( + "com.amazonaws.services.lambda.runtime.Context"))), HandleRequestAdvice.class.getName()); } @@ -43,8 +52,7 @@ public static class HandleRequestAdvice { public static void methodEnter( @Advice.Argument(value = 0, typing = Typing.DYNAMIC) Object input, @Advice.Argument(1) Context context, - @Advice.Local("lumigoSpansContainer") SpansContainer spansContainer - ) { + @Advice.Local("lumigoSpansContainer") SpansContainer spansContainer) { try { spansContainer = SpansContainer.getInstance(); spansContainer.init(new EnvUtil().getEnv(), new Reporter(), context, input); From 34d3d6c6fa84e1c2b034c145c21c4e1c96b71dec Mon Sep 17 00:00:00 2001 From: CircleCI Date: Wed, 26 Jun 2024 12:07:57 +0300 Subject: [PATCH 7/8] fixes --- README.md | 2 +- agent/src/main/java/io/lumigo/agent/Agent.java | 2 +- pom.xml | 5 +++++ scripts/bd_to_prod.sh | 2 +- scripts/prepare_layer_files.sh | 16 ++++++---------- .../impl/AmazonHttpClientV2Instrumentation.java | 5 ++++- .../AwsLambdaRequestHandlerInstrumentation.java | 1 + 7 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 12bca8e..5726dc1 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ Find the latest version here (the format of the version will be n.n.n): ## Lambda Auto tracing with lambda layer * Add to your lambda a new layer with the arn from here -* Add environment variable `JAVA_TOOL_OPTIONS` and set it to `-javaagent:/opt/lumigo/lumigo-agent.jar` (This is instead of the flag for more than java11 support) +* Add environment variable `JAVA_TOOL_OPTIONS` and set it to `-javaagent:/opt/lumigo-java/lumigo-agent.jar` (This is instead of the flag for more than java11 support) * Add the `LUMIGO_TRACER_TOKEN` env var. diff --git a/agent/src/main/java/io/lumigo/agent/Agent.java b/agent/src/main/java/io/lumigo/agent/Agent.java index 0d6401e..48b1f22 100644 --- a/agent/src/main/java/io/lumigo/agent/Agent.java +++ b/agent/src/main/java/io/lumigo/agent/Agent.java @@ -19,7 +19,7 @@ public class Agent { private static boolean initialized = false; - public static String LUMIGO_JAVA_TRACER_PATH = "/opt/lumigo/lumigo-tracer.jar"; + public static String LUMIGO_JAVA_TRACER_PATH = "/opt/lumigo-java/lumigo-tracer.jar"; public static void premain(String agentArgs, Instrumentation inst) { if (!isKillSwitchOn()) { diff --git a/pom.xml b/pom.xml index 1265ebc..a08a40d 100644 --- a/pom.xml +++ b/pom.xml @@ -116,21 +116,25 @@ software.amazon.awssdk dynamodb 2.25.45 + provided software.amazon.awssdk sqs 2.25.45 + provided software.amazon.awssdk sns 2.25.45 + provided software.amazon.awssdk kinesis 2.25.45 + provided @@ -138,6 +142,7 @@ org.apache.kafka kafka-clients 3.1.0 + provided diff --git a/scripts/bd_to_prod.sh b/scripts/bd_to_prod.sh index bfc232a..ac67d64 100755 --- a/scripts/bd_to_prod.sh +++ b/scripts/bd_to_prod.sh @@ -48,7 +48,7 @@ commit_version="$(git describe --abbrev=0 --tags)" ../utils/common_bash/create_layer.sh \ --layer-name lumigo-java-tracer \ --region ALL \ - --package-folder python \ + --package-folder lumigo-java \ --version "$commit_version" \ --runtimes "java11 java17 java21" diff --git a/scripts/prepare_layer_files.sh b/scripts/prepare_layer_files.sh index 1ccef4b..56b4528 100755 --- a/scripts/prepare_layer_files.sh +++ b/scripts/prepare_layer_files.sh @@ -1,15 +1,11 @@ #!/usr/bin/env bash -mkdir -p lumigo +mkdir -p lumigo-java -mvn -Dfindbugs.skip=true -DskipTests=true -DincludeShade=true clean package -pushd agent - mvn -Dfindbugs.skip=true -DskipTests=true clean package -popd +MVN_DEFAULT_FLAGS="-Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -Dmaven.wagon.http.retryHandler.requestSentEnabled=true -Dmaven.wagon.http.retryHandler.count=10" -cp target/java-tracer-1.0.47.jar lumigo/lumigo-tracer.jar -cp agent/target/lumigo-agent-1.0.47.jar lumigo/lumigo-agent.jar +mvn "$MVN_DEFAULT_FLAGS" -Dmaven.test.skip=true -Dfindbugs.skip=true -DincludeShade=true clean package --quiet +mvn "$MVN_DEFAULT_FLAGS" -f agent/pom.xml clean package --quiet -zip -r lumigo-java-layer.zip lumigo - -rm -rf lumigo \ No newline at end of file +cp target/java-tracer-1.0.47.jar lumigo-java/lumigo-tracer.jar +cp agent/target/lumigo-agent-1.0.47.jar lumigo-java/lumigo-agent.jar diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/AmazonHttpClientV2Instrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/AmazonHttpClientV2Instrumentation.java index 03e2e14..434eb4b 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/AmazonHttpClientV2Instrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/AmazonHttpClientV2Instrumentation.java @@ -36,17 +36,20 @@ public AgentBuilder.Transformer.ForAdvice getTransformer() { AmazonHttpClientV2Advice.class.getName()); } + @SuppressWarnings("unused") public static class AmazonHttpClientV2Advice { @Advice.OnMethodExit(suppress = Throwable.class) public static void methodExit( @Advice.Return final List interceptors) { - Logger.debug("Added Lumigo TracingExecutionInterceptor"); + Logger.debug("At AmazonHttpClientV2Instrumentation$AmazonHttpClientV2Advice"); for (ExecutionInterceptor interceptor : interceptors) { if (interceptor instanceof TracingExecutionInterceptor) { + Logger.debug("Lumigo TracingExecutionInterceptor already exists, skipping..."); return; // list already has our interceptor, return to builder } } interceptors.add(new TracingExecutionInterceptor()); + Logger.debug("Added Lumigo TracingExecutionInterceptor"); } public static class TracingExecutionInterceptor implements ExecutionInterceptor { diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java index cde099e..49fb8d3 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java @@ -54,6 +54,7 @@ public static void methodEnter( @Advice.Argument(1) Context context, @Advice.Local("lumigoSpansContainer") SpansContainer spansContainer) { try { + Logger.debug("Start AwsLambdaRequestHandlerInstrumentation$HandleRequestAdvice"); spansContainer = SpansContainer.getInstance(); spansContainer.init(new EnvUtil().getEnv(), new Reporter(), context, input); spansContainer.start(); From ebff81f3c18b7112ce05a5fddeee039d5b6938cc Mon Sep 17 00:00:00 2001 From: CircleCI Date: Wed, 26 Jun 2024 12:37:42 +0300 Subject: [PATCH 8/8] Fix cehcks --- .../impl/AwsLambdaRequestHandlerInstrumentation.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java b/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java index 49fb8d3..65f2015 100644 --- a/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java +++ b/src/main/java/io/lumigo/core/instrumentation/impl/AwsLambdaRequestHandlerInstrumentation.java @@ -48,14 +48,14 @@ public AgentBuilder.Transformer.ForAdvice getTransformer() { @SuppressWarnings("unused") public static class HandleRequestAdvice { + public static final SpansContainer spansContainer = SpansContainer.getInstance(); + @Advice.OnMethodEnter(suppress = Throwable.class) public static void methodEnter( @Advice.Argument(value = 0, typing = Typing.DYNAMIC) Object input, - @Advice.Argument(1) Context context, - @Advice.Local("lumigoSpansContainer") SpansContainer spansContainer) { + @Advice.Argument(1) Context context) { try { Logger.debug("Start AwsLambdaRequestHandlerInstrumentation$HandleRequestAdvice"); - spansContainer = SpansContainer.getInstance(); spansContainer.init(new EnvUtil().getEnv(), new Reporter(), context, input); spansContainer.start(); Logger.debug("Finish sending start message and instrumentation"); @@ -67,8 +67,7 @@ public static void methodEnter( @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void methodExit( @Advice.Return(readOnly = false) Object returnValue, - @Advice.Thrown Throwable throwable, - @Advice.Local("lumigoSpansContainer") SpansContainer spansContainer) { + @Advice.Thrown Throwable throwable) { try { if (throwable != null) { spansContainer.endWithException(throwable);