Monitoring your Spring Cloud Function with IOpipe

Stephanie Gawroriski (Xer)
IOpipe Blog
Published in
5 min readSep 13, 2018

--

If you use the Spring Cloud Function framework, you can monitor executions of it using IOpipe. Starting with version 1.8.0 of the IOpipe Java agent you are able to wrap Spring Cloud Functions using the Generic Entry Point handler without making any modifications to your code!

In this tutorial, we will be setting up an example project with Spring Cloud Function and then wrapping that with IOpipe.

Getting Started

The first example we will be wrapping is in the Spring Cloud Function repository, specifically located here https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-aws. Since the sample we want is in this repository we will then git clone https://github.com/spring-cloud/spring-cloud-function and naturally navigate to the subdirectory containing the example (spring-cloud-function-samples/function-sample-aws).

The first thing to do is to test that this example actually works, so we run mvn package. Then we wait until everything is downloaded, compiled, then placed into a package. When it is complete we should get a JAR output at target/function-sample-aws-2.0.0.BUILD-SNAPSHOT-aws.jar-- this is the one we want to upload to AWS. Login to AWS and enter the Lambda manager. Create a new function: we will call it spring-cloud-function and set it to use Java 8. For the role, create a new role from a template and call it spring-cloud-function, we will not choose any role templates and leave it blank. Then we hit Create Function and we will now see the settings for the new Lambda function.

Since for our first test we will not be wrapping with IOpipe, we can just upload target/function-sample-aws-2.0.0.BUILD-SNAPSHOT-aws.jar and set the entry point to example.Handler. Then we hit save and wait for the upload to be complete. Now we have to configure a new test event, if you hit Test at the top it should give you a dialog to create a new test since none exist. The event template should be "Hello World", we can just name this test "hello". Since the input is always JSON data, we will make our input: {"value":"Hello! Squirrels are cute!"} (this will be reformatted accordingly). Once we finish writing the test, we will hit the Test button to run our test. It should be a success!

Wrapping with IOpipe

Now that our spring cloud function is working, we want to wrap the lambda with IOpipe to provide us with metrics and other functionality, like labels and tracing. This will assume that you already have an IOpipe project setup and you know your user token, if not visit our documentation and follow the instructions there.

The first thing to do is to add IOpipe to the dependencies, we place the following into the dependency group in pom.xml:

<dependency>
<groupId>com.iopipe</groupId>
<artifactId>iopipe</artifactId>
<version>1.8.0</version>
</dependency>

Another step is to explicitly include com.amazonaws:aws-lambda-java-events, since it seems that it is no longer included in the AWS classpath by default. We can just find that dependency and remove the <scope>provided</scope>. Additionally, the dependency is out of date, and we need to specify a new version which can be found above (the following in the POM: <aws-lambda-events.version>2.0.2</aws-lambda-events.version>), this just needs to be changed to 2.2.2so it reads <aws-lambda-events.version>2.2.2</aws-lambda-events.version>.

Then we re-run mvn package which will generate the JAR file again. This tests that it works without actually configuring IOpipe-- this should succeed with the following output:

Configuring IOpipe

Now that that works, we want to configure the IOpipe Java Agent to report to IOpipe. Any Java Lambda using version 1.8.0+ of the IOpipe Java agent will not need to perform any modification of code to wrap with IOpipe. The process is simply adjusting the entry point and adding environment variables:

  • Set IOPIPE_GENERIC_HANDLER to example.Handler, the handler we want to call.
  • Change the entry point of the Lambda to com.iopipe.generic.GenericAWSRequestHandler.
  • Set IOPIPE_TOKEN to your token, this will be on the project details page and the installation page.

Hit Save. Then hit Test again and it will produce the following:

Looking at the Dashboard

Now that IOpipe is wrapping your lambda, you will be able to see it in the IOpipe Dashboard like so:

Then clicking through that function we will get a list of one invocation since this test only has a single invocation run with the IOpipe agent configured. Then we get to the single invocation page:

That is essentially everything you need to get started with using IOpipe to wrap your functions.

So now you know how to wrap your Spring Cloud Function with IOpipe so that you can gain more insight into how your methods are operating. With this, you may continue on to use custom metrics, labels, profiling, and other IOpipe functionality to gather information as needed.

Excited to see more? Try our 21-day free trial, or if you have any questions, join our community Slack!

--

--

I develop SquirrelJME which is an implementation of Java ME 8 (CLDC 8/MEEP 8/MIDP 3). I love squirrels, they are adorable and cute 🐿️!