Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@
"localArguments": ["--invoke-image", "public.ecr.aws/lambda/python:3.8.2023.11.18.02"],
"skipNewImageCheck": true
}
},
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "SAM:Run Java Canary",
"invokeTarget": {
"target": "template",
"templatePath": "${workspaceFolder}/java-canary/template.yml",
"logicalId": "JavaCanary"
},
"lambda": {
"payload": {
"json": {
"canaryName": "LocalSyntheticsCanary",
"artifactS3Location": {
"s3Bucket": "cw-syn-results-123456789012-us-west-2",
"s3Key": "local-run-artifacts",
},
"customerCanaryHandlerName": "org.example.monitoring.MonitoringAppGeneric::handler",
"canaryRunId": "random-uuid"
}
},
"environmentVariables": {}
},
"sam": {
"localArguments": ["--invoke-image", "public.ecr.aws/lambda/java:21"],
"skipNewImageCheck": true
}
}
]
}
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ The repository contains code samples for both NodeJS canary and Python canary.
```
2. Run `pip3 install -r requirements.txt -t .` to install canary dependencies.

### Java canary
1. The code for the Java canary is located in the java-canary directory.
```
cd synthetics-canary-local-debugging-sample/java-canary
```
2. Use the build.gradle if you would like to add additional dependencies.

## Visual Studio Code launch configuration
The launch configuration file is located at `.vscode/launch.json`. It contains configuration to allow the template file to be discovered by VS Code. It defines Lambda payload with required parameters to invoke the canary successfully. Here’s the launch configuration for NodeJS canary.

Expand Down Expand Up @@ -110,12 +117,20 @@ If you use [JetBrains IDE](https://www.jetbrains.com/idea/) then you will requir


## Run canary locally with SAM CLI
1. Make sure to specify your own S3 bucket name for `s3Bucket` in `event.json`.
1. Make sure to specify your own S3 bucket name for `s3Bucket` in `event.json` and change the handler name appropriately.
2. To run NodeJS canary, go to `nodejs-canary` directory and run following commands.
- `sam build`
- `sam local invoke -e NodeJSPuppeteerCanary ../event.json` (To run Puppeteer canary)
- `sam local invoke -e NodeJSPlaywrightCanary ../event.json` (To run Playwright canary)
3. Similary to run Python canary, go to `python-canary` directory and run same commands as above.
- `sam local invoke NodeJSPuppeteerCanary -e event.json` (To run Puppeteer canary)
- `sam local invoke NodeJSPlaywrightCanary -e event.json` (To run Playwright canary)
3. To run Python canary, go to `python-canary` directory and run the following commands.
- `sam build`
- `sam local invoke PythonSeleniumCanary -e event.json`
4. To run Java canary, go to `java-canary` directory and run the following commands.
- `gradle wrapper && ./gradlew clean build `
- `sam local invoke JavaCanary -e event.json`
Any generic Java code can be run as a canary and the code can be modularized using Synthetics provided library functions.
Please refer `MonitoringAppGeneric.java` for an example of creating Java canaries without steps.
Please refere `MonitoringAppWithSteps.java` for an example of creating Java canaries with steps.

## Caveat

Expand Down
9 changes: 9 additions & 0 deletions java-canary/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions java-canary/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
59 changes: 59 additions & 0 deletions java-canary/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
// Use the interfaces provided by the Synthetics library for creating Java canaries with steps
// https://mvnrepository.com/artifact/software.amazon.synthetics/aws-cloudwatch-synthetics-sdk-java
// This is used for imports for the form software.amazon.synthetics.*
// Please refer to MonitoringAppWithSteps.java for example usage.
compileOnly 'software.amazon.synthetics:aws-cloudwatch-synthetics-sdk-java:1.0.0'
}

test {
useJUnitPlatform()
}

// Build the zip to be used as Canary code.
task buildZip(type: Zip) {

// Name of the zip file that contains the user code along with the required dependencies.
// This will be written to the build directory in the root directory.
archiveFileName.set("example-canary.zip")
destinationDirectory.set(file("$buildDir"))

// Include processed resources (like application.properties) in the zip root
from processResources

// Create a 'lib' directory in the zip containing all dependencies
into('lib') {
// Include all runtime dependencies (external JARs)
from configurations.runtimeClasspath
// Include the compiled project JAR file
from(tasks.named("jar"))
}

// Include the synthetics configuration file in the zip root
from "src/main/java/resources/synthetics.json"

doLast {
println "Artifact written to: ${archiveFile.get().asFile.absolutePath}"
}
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

tasks.named("build") {
dependsOn "buildZip"
}

9 changes: 9 additions & 0 deletions java-canary/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"canaryName": "LocalSyntheticsCanary",
"artifactS3Location": {
"s3Bucket": "cw-syn-results-123456789012-us-west-2",
"s3Key": "local-run-artifacts"
},
"customerCanaryHandlerName": "org.example.monitoring.MonitoringAppWithSteps::handler",
"canaryRunId": "random-uuid"
}
10 changes: 10 additions & 0 deletions java-canary/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format

[versions]
guava = "32.1.3-jre"
junit-jupiter = "5.10.1"

[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
Binary file added java-canary/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions java-canary/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading