diff --git a/.github/workflows/check-deployed-package.yml b/.github/workflows/check-deployed-package.yml new file mode 100644 index 0000000..0d49d33 --- /dev/null +++ b/.github/workflows/check-deployed-package.yml @@ -0,0 +1,38 @@ +name: Check deployed package + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + check-deployed-package: + name: Check deployed package + runs-on: ubuntu-latest + permissions: + contents: read + packages: read + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Make run script executable + run: chmod +x check-deployed-package/run.sh + + - name: Verify environment variables + run: | + echo "GITHUB_ACTOR: ${{ github.actor }}" + echo "Has GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN != '' }}" + echo "Has LOGDASH_API_KEY: ${{ secrets.LOGDASH_API_KEY != '' }}" + + - name: Run LogDash demo + env: + LOGDASH_API_KEY: ${{ secrets.LOGDASH_API_KEY }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_ACTOR: ${{ github.actor }} + run: ./check-deployed-package/run.sh \ No newline at end of file diff --git a/check-deployed-package/Check.java b/check-deployed-package/Check.java new file mode 100644 index 0000000..3913667 --- /dev/null +++ b/check-deployed-package/Check.java @@ -0,0 +1,58 @@ +package com.logdash.check; + +import io.logdash.sdk.Logdash; + +import java.util.Map; + +public class Check { + public static void main(String[] args) { + System.out.println("=== LogDash Java SDK Demo ==="); + + // Get package version (would need to be handled differently in a real scenario) + System.out.println("Using logdash package version: " + getLogdashVersion()); + System.out.println(); + + String apiKey = System.getenv("LOGDASH_API_KEY"); + String logsSeed = System.getenv().getOrDefault("LOGS_SEED", "default"); + String metricsSeed = System.getenv().getOrDefault("METRICS_SEED", "1"); + + System.out.println("Using API Key: " + apiKey); + System.out.println("Using Logs Seed: " + logsSeed); + System.out.println("Using Metrics Seed: " + metricsSeed); + + try (Logdash logdash = Logdash.create(apiKey)) { + var logger = logdash.logger(); + var metrics = logdash.metrics(); + + // Log some messages with seed appended + logger.info("This is an info log " + logsSeed); + logger.error("This is an error log " + logsSeed); + logger.warn("This is a warning log " + logsSeed); + logger.debug("This is a debug log " + logsSeed); + logger.http("This is a http log " + logsSeed); + logger.silly("This is a silly log " + logsSeed); + logger.info("This is an info log " + logsSeed); + logger.verbose("This is a verbose log " + logsSeed); + + // Set and mutate metrics with seed + int seedValue = Integer.parseInt(metricsSeed); + metrics.set("users", seedValue); + metrics.mutate("users", 1); + + // Wait to ensure data is sent + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + + private static String getLogdashVersion() { + try { + return Check.class.getPackage().getImplementationVersion(); + } catch (Exception e) { + return "unknown"; + } + } +} \ No newline at end of file diff --git a/check-deployed-package/Dockerfile b/check-deployed-package/Dockerfile new file mode 100644 index 0000000..86c99aa --- /dev/null +++ b/check-deployed-package/Dockerfile @@ -0,0 +1,37 @@ +FROM eclipse-temurin:17-jdk + +ARG GITHUB_TOKEN +ARG GITHUB_ACTOR + +WORKDIR /app + +# Install Maven +RUN apt-get update && apt-get install -y maven && rm -rf /var/lib/apt/lists/* + +# Set environment variables for Maven authentication +ENV GITHUB_TOKEN=${GITHUB_TOKEN} +ENV GITHUB_ACTOR=${GITHUB_ACTOR} + +# Copy Maven settings with GitHub authentication +COPY check-deployed-package/settings.xml /root/.m2/settings.xml + +# Copy project files +COPY check-deployed-package/pom.xml . +COPY check-deployed-package/Check.java src/main/java/com/logdash/check/Check.java + +# Download dependencies and compile +RUN mvn dependency:resolve compile -B --no-transfer-progress \ + -Dgithub.username=${GITHUB_ACTOR} \ + -Dgithub.password=${GITHUB_TOKEN} + +# Add environment variable validation +ENV LOGDASH_API_KEY="" +ENV LOGS_SEED="" +ENV METRICS_SEED="" + +# Run the application with environment validation +CMD if [ -z "$LOGDASH_API_KEY" ]; then \ + echo "Error: LOGDASH_API_KEY environment variable is required"; \ + exit 1; \ + fi; \ + mvn exec:java -B --no-transfer-progress diff --git a/check-deployed-package/README.md b/check-deployed-package/README.md new file mode 100644 index 0000000..4941f66 --- /dev/null +++ b/check-deployed-package/README.md @@ -0,0 +1,8 @@ +# LogDash Java SDK Demo + +Use command below to test if the published SDK works: + +``` +LOGDASH_API_KEY= ./check-deployed-package/run.sh +``` + diff --git a/check-deployed-package/pom.xml b/check-deployed-package/pom.xml new file mode 100644 index 0000000..d595667 --- /dev/null +++ b/check-deployed-package/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + com.logdash + check + 1.0.0 + + + 17 + 17 + UTF-8 + + + + + io.logdash + logdash + 0.1.0 + + + + + + + true + + + true + + github + GitHub Packages + https://maven.pkg.github.com/logdash-io/java-sdk + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + 17 + 17 + + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + com.logdash.check.Check + + + + + + + + diff --git a/check-deployed-package/run.sh b/check-deployed-package/run.sh new file mode 100755 index 0000000..7c71d0c --- /dev/null +++ b/check-deployed-package/run.sh @@ -0,0 +1,128 @@ +#!/bin/sh +set -e + +# Generate random 5-character seed +LOGS_SEED=$(openssl rand -hex 2 | cut -c1-5) +echo "Generated seed: $LOGS_SEED" + +# Generate random metrics seed (1-1,000,000) +METRICS_SEED=$(awk 'BEGIN{srand(); print int(rand()*1000000)+1}') +echo "Generated metrics seed: $METRICS_SEED" + +echo "Building LogDash Java demo Docker image (using published package)..." +docker build --no-cache \ + --build-arg GITHUB_TOKEN="${GITHUB_TOKEN}" \ + --build-arg GITHUB_ACTOR="${GITHUB_ACTOR}" \ + -t logdash-java-demo \ + -f check-deployed-package/Dockerfile . + +echo +echo "Running LogDash Java demo..." +echo + +# Run in non-interactive mode which works everywhere +docker run --rm \ + -e LOGDASH_API_KEY="${LOGDASH_API_KEY}" \ + -e LOGS_SEED="${LOGS_SEED}" \ + -e METRICS_SEED="${METRICS_SEED}" \ + logdash-java-demo + +echo +echo "Demo completed!" + +echo +echo "Authenticating with LogDash API..." + +# Authenticate with API using the API key +AUTH_RESPONSE=$(curl -s -X 'POST' \ + 'https://api.logdash.io/auth/api-key' \ + -H 'accept: application/json' \ + -H 'Content-Type: application/json' \ + -d "{ + \"apiKey\": \"${LOGDASH_API_KEY}\" +}") + +# Extract token and projectId from response +TOKEN=$(echo "$AUTH_RESPONSE" | grep -o '"token":"[^"]*"' | sed 's/"token":"\(.*\)"/\1/') +PROJECT_ID=$(echo "$AUTH_RESPONSE" | grep -o '"projectId":"[^"]*"' | sed 's/"projectId":"\(.*\)"/\1/') + +if [ -z "$TOKEN" ] || [ -z "$PROJECT_ID" ]; then + echo "Error: Failed to authenticate with LogDash API" + echo "Response: $AUTH_RESPONSE" + exit 1 +fi + +echo "Authentication successful. Project ID: $PROJECT_ID" + +echo +echo "Fetching logs from LogDash API..." + +# Fetch logs from the API +LOGS_RESPONSE=$(curl -s -X 'GET' \ + "https://api.logdash.io/projects/${PROJECT_ID}/logs?limit=10" \ + -H 'accept: application/json' \ + -H "Authorization: Bearer ${TOKEN}") + +echo "Logs fetched successfully" + +echo +echo "Validating log messages..." + +# Expected log messages with seed +EXPECTED_MESSAGES="This is an info log ${LOGS_SEED} +This is an error log ${LOGS_SEED} +This is a warning log ${LOGS_SEED} +This is a debug log ${LOGS_SEED} +This is a http log ${LOGS_SEED} +This is a silly log ${LOGS_SEED} +This is an info log ${LOGS_SEED} +This is a verbose log ${LOGS_SEED}" + +# Check if all expected messages are present in the logs +echo "$EXPECTED_MESSAGES" | while IFS= read -r expected_msg; do + if ! echo "$LOGS_RESPONSE" | grep -q "$expected_msg"; then + echo "Error: Expected log message not found: '$expected_msg'" + echo "Logs response: $LOGS_RESPONSE" + exit 1 + fi + echo "✓ Found: '$expected_msg'" +done + +echo +echo "Fetching metrics from LogDash API..." + +# Fetch metrics from the API +METRICS_RESPONSE=$(curl -s -X 'GET' \ + "https://api.logdash.io/projects/${PROJECT_ID}/metrics" \ + -H 'accept: application/json' \ + -H "Authorization: Bearer ${TOKEN}") + +echo "Metrics fetched successfully" + +echo +echo "Validating metrics..." + +# Expected users metric value (metrics_seed + 1) +EXPECTED_USERS_VALUE=$((METRICS_SEED + 1)) + +# Check if users metric exists with correct value +if ! echo "$METRICS_RESPONSE" | grep -q '"name":"users"'; then + echo "Error: Users metric not found" + echo "Metrics response: $METRICS_RESPONSE" + exit 1 +fi + +# Extract the value of the users metric and check if it matches expected value +USERS_VALUE=$(echo "$METRICS_RESPONSE" | sed 's/},{/}\n{/g' | grep '"name":"users"' | grep -o '"value":[0-9]*' | sed 's/"value"://') + +if [ "$USERS_VALUE" != "$EXPECTED_USERS_VALUE" ]; then + echo "Error: Users metric value mismatch. Expected: $EXPECTED_USERS_VALUE, Found: $USERS_VALUE" + echo "Metrics response: $METRICS_RESPONSE" + exit 1 +fi + +echo "✓ Found users metric with correct value: $USERS_VALUE" + +echo +echo "All expected log messages and metrics found successfully!" +echo "Validation completed!" \ No newline at end of file diff --git a/check-deployed-package/settings.xml b/check-deployed-package/settings.xml new file mode 100644 index 0000000..7b3ca4b --- /dev/null +++ b/check-deployed-package/settings.xml @@ -0,0 +1,42 @@ + + + + + + github + ${env.GITHUB_ACTOR} + ${env.GITHUB_TOKEN} + + + + + + github-auth + + true + + + + github + GitHub Packages + https://maven.pkg.github.com/logdash-io/java-sdk + + true + daily + + + true + always + + + + + + + + github-auth + +