From b6c9a9609b328c8e4a652d3d608515e5c82cdc04 Mon Sep 17 00:00:00 2001 From: Duncan Harvey <35278470+duncanpharvey@users.noreply.github.com> Date: Fri, 2 Aug 2024 13:31:00 -0400 Subject: [PATCH] Add Datadog Serverless Azure Java Agent (#1) * adds dd-serverless-java-agent * rename to dd-serverless-azure-java-agent --- .github/workflows/release.yaml | 74 +++++++++++++++++ .gitignore | 2 + README.md | 36 +++++++- pom.xml | 83 +++++++++++++++++++ .../com/datadog/ServerlessAzureAgent.java | 35 ++++++++ 5 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/release.yaml create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/datadog/ServerlessAzureAgent.java diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..91cb716 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,74 @@ +name: Build and Release the Serverless Azure Java Agent +on: + push: + tags: + - "dd-serverless-azure-java-agent-**" + +jobs: + build: + name: Build Serverless Azure Java Agent + runs-on: ubuntu-latest + outputs: + mini-agent-version: ${{ steps.get-serverless-mini-agent.outputs.mini_agent_version }} + steps: + - run: echo ${{ github.ref_name }} + - name: Checkout code + uses: actions/checkout@v4 + - name: Get Serverless Mini Agent + id: get-serverless-mini-agent + run: | + LIBDATADOG_RESPONSE=$(curl -s "https://api.github.com/repos/datadog/libdatadog/releases") + MINI_AGENT_VERSION=$(echo "$LIBDATADOG_RESPONSE" | jq -r --arg pattern "sls-v[0-9]*\.[0-9]*\.[0-9]*" '.[] | select(.tag_name | test($pattern)) | .tag_name' | sort -V | tail -n 1) + + echo "Using version ${MINI_AGENT_VERSION} of mini agent" + echo "mini_agent_version=$(echo "$MINI_AGENT_VERSION" | jq -rR 'ltrimstr("sls-")')" >> "$GITHUB_OUTPUT" + + curl --output-dir ./temp/ --create-dirs -O -s -L "https://github.com/DataDog/libdatadog/releases/download/${MINI_AGENT_VERSION}/datadog-serverless-agent.zip" + unzip ./temp/datadog-serverless-agent.zip -d ./temp/datadog-serverless-agent + + mkdir src/main/resources + cp ./temp/datadog-serverless-agent/datadog-serverless-agent-linux-amd64/datadog-serverless-trace-mini-agent src/main/resources/datadog-serverless-trace-mini-agent + - name: Set up JDK 8 + uses: actions/setup-java@v4 + with: + java-version: "8" + distribution: "zulu" + - name: Build dd-serverless-azure-java-agent jar + run: mvn clean install + - name: Upload artifact for release + uses: actions/upload-artifact@v4 + with: + name: release + path: target/${{ github.ref_name }}-jar-with-dependencies.jar + release: + name: Release + needs: [build] + runs-on: ubuntu-latest + env: + MINI_AGENT_VERSION: ${{ needs.build.outputs.mini-agent-version }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Download build artifact + uses: actions/download-artifact@v4 + - name: Rename artifacts + run: | + cp release/${{ github.ref_name }}-jar-with-dependencies.jar release/${{ github.ref_name }}.jar + cp release/${{ github.ref_name }}-jar-with-dependencies.jar release/dd-serverless-azure-java-agent.jar + - name: Release + uses: softprops/action-gh-release@v2 + with: + body: "Uses [${{ env.MINI_AGENT_VERSION }}](https://github.com/DataDog/libdatadog/releases/tag/sls-${{ env.MINI_AGENT_VERSION }}) of the Serverless Mini Agent." + draft: true + generate_release_notes: true + make_latest: true + files: | + release/${{ github.ref_name }}.jar + release/dd-serverless-azure-java-agent.jar + - name: Release + uses: softprops/action-gh-release@v2 + with: + name: Latest + tag_name: download-latest + body: "This release tracks the latest version available, currently **${{ github.ref_name }}**." + files: release/dd-serverless-azure-java-agent.jar diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ef9743a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +datadog-serverless-trace-mini-agent +target diff --git a/README.md b/README.md index 70512fd..5ad4a6f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,34 @@ -# dd-serverless-java-agent -Jave agent used to start the Datadog Serverless Mini Agent. Intended for use with Azure Spring Apps +# Datadog Serverless Azure Java Agent + +Java agent used to start the Datadog Serverless Mini Agent. Intended for use with [Azure Spring Apps](https://azure.microsoft.com/en-us/products/spring-apps). + +# Getting Started + +- From the latest release, download `dd-serverless-azure-java-agent.jar` and upload it to your Azure Spring App in persistent storage. +- Add the Datadog Serverless Azure Java Agent and Datadog Java Tracer as Java agents to your `JVM_OPTIONS`. +``` +-javaagent:/persistent/dd-serverless-azure-java-agent.jar -javaagent:/persistent/dd-java-agent.jar +``` +- Set environment variables +* `DD_API_KEY` = `` +* `DD_SITE` = `datadoghq.com` +* `DD_SERVICE` = `` +* `DD_ENV` = `` +* `DD_TRACE_TRACER_METRICS_ENABLED` = `true` + +# Contributing + +## Setting up development environment + +Follow the instructions in the `dd-trace-java` repo to set up your Java environment: [Setting up development environment](https://github.com/DataDog/dd-trace-java/blob/master/BUILDING.md#setting-up-development-environment). + +## Building the project + +Build the Datadog Serverless Mini Agent from [libdatadog](https://github.com/DataDog/libdatadog) and add the linux binary, `datadog-serverless-trace-mini-agent`, to `src/main/resources`. + +To build the project run: +``` +mvn clean install +``` + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e6b2b07 --- /dev/null +++ b/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + + org.datadoghq.com + dd-serverless-azure-java-agent + 0.1.0 + jar + Datadog Serverless Azure Java Agent + + + + org.slf4j + slf4j-api + 2.0.13 + + + org.slf4j + slf4j-simple + 2.0.13 + + + org.springframework.boot + spring-boot-starter-web + 2.7.18 + + + ch.qos.logback + logback-classic + + + + + + + UTF-8 + + + + + + src/main/resources + + datadog-serverless-trace-mini-agent + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + 1.8 + 1.8 + + + + maven-assembly-plugin + + + package + + single + + + + + + jar-with-dependencies + + + + com.datadog.ServerlessAzureAgent + + + + + + + diff --git a/src/main/java/com/datadog/ServerlessAzureAgent.java b/src/main/java/com/datadog/ServerlessAzureAgent.java new file mode 100644 index 0000000..e745839 --- /dev/null +++ b/src/main/java/com/datadog/ServerlessAzureAgent.java @@ -0,0 +1,35 @@ +package com.datadog; + +import java.io.File; +import java.io.InputStream; +import java.lang.instrument.Instrumentation; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ServerlessAzureAgent { + private static final Logger log = LoggerFactory.getLogger(ServerlessAzureAgent.class); + private static final String fileName = "datadog-serverless-trace-mini-agent"; + + public static void premain(String agentArgs, Instrumentation instrumentation) { + log.info("Attempting to start {}", fileName); + + try (InputStream inputStream = ServerlessAzureAgent.class.getClassLoader() + .getResourceAsStream(fileName)) { + if (inputStream == null) { + log.error("{} not found", fileName); + } + + File executableFile = new File(fileName); + Files.copy(inputStream, executableFile.getAbsoluteFile().toPath(), StandardCopyOption.REPLACE_EXISTING); + executableFile.setExecutable(true); + + ProcessBuilder processBuilder = new ProcessBuilder(executableFile.getAbsolutePath()); + processBuilder.inheritIO(); + processBuilder.start(); + } catch (Exception e) { + log.error("Exception when starting {}", fileName, e); + } + } +}