Skip to content

Commit 954d461

Browse files
Merge pull request #2 from logdash-io/feat/package-auto-check-script
feat: package auto check script
2 parents c271318 + f74f0d5 commit 954d461

File tree

7 files changed

+372
-0
lines changed

7 files changed

+372
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Check deployed package
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
branches:
10+
- main
11+
12+
jobs:
13+
check-deployed-package:
14+
name: Check deployed package
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
packages: read
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Make run script executable
25+
run: chmod +x check-deployed-package/run.sh
26+
27+
- name: Verify environment variables
28+
run: |
29+
echo "GITHUB_ACTOR: ${{ github.actor }}"
30+
echo "Has GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN != '' }}"
31+
echo "Has LOGDASH_API_KEY: ${{ secrets.LOGDASH_API_KEY != '' }}"
32+
33+
- name: Run LogDash demo
34+
env:
35+
LOGDASH_API_KEY: ${{ secrets.LOGDASH_API_KEY }}
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
GITHUB_ACTOR: ${{ github.actor }}
38+
run: ./check-deployed-package/run.sh

check-deployed-package/Check.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.logdash.check;
2+
3+
import io.logdash.sdk.Logdash;
4+
5+
import java.util.Map;
6+
7+
public class Check {
8+
public static void main(String[] args) {
9+
System.out.println("=== LogDash Java SDK Demo ===");
10+
11+
// Get package version (would need to be handled differently in a real scenario)
12+
System.out.println("Using logdash package version: " + getLogdashVersion());
13+
System.out.println();
14+
15+
String apiKey = System.getenv("LOGDASH_API_KEY");
16+
String logsSeed = System.getenv().getOrDefault("LOGS_SEED", "default");
17+
String metricsSeed = System.getenv().getOrDefault("METRICS_SEED", "1");
18+
19+
System.out.println("Using API Key: " + apiKey);
20+
System.out.println("Using Logs Seed: " + logsSeed);
21+
System.out.println("Using Metrics Seed: " + metricsSeed);
22+
23+
try (Logdash logdash = Logdash.create(apiKey)) {
24+
var logger = logdash.logger();
25+
var metrics = logdash.metrics();
26+
27+
// Log some messages with seed appended
28+
logger.info("This is an info log " + logsSeed);
29+
logger.error("This is an error log " + logsSeed);
30+
logger.warn("This is a warning log " + logsSeed);
31+
logger.debug("This is a debug log " + logsSeed);
32+
logger.http("This is a http log " + logsSeed);
33+
logger.silly("This is a silly log " + logsSeed);
34+
logger.info("This is an info log " + logsSeed);
35+
logger.verbose("This is a verbose log " + logsSeed);
36+
37+
// Set and mutate metrics with seed
38+
int seedValue = Integer.parseInt(metricsSeed);
39+
metrics.set("users", seedValue);
40+
metrics.mutate("users", 1);
41+
42+
// Wait to ensure data is sent
43+
try {
44+
Thread.sleep(2000);
45+
} catch (InterruptedException e) {
46+
Thread.currentThread().interrupt();
47+
}
48+
}
49+
}
50+
51+
private static String getLogdashVersion() {
52+
try {
53+
return Check.class.getPackage().getImplementationVersion();
54+
} catch (Exception e) {
55+
return "unknown";
56+
}
57+
}
58+
}

check-deployed-package/Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
FROM eclipse-temurin:17-jdk
2+
3+
ARG GITHUB_TOKEN
4+
ARG GITHUB_ACTOR
5+
6+
WORKDIR /app
7+
8+
# Install Maven
9+
RUN apt-get update && apt-get install -y maven && rm -rf /var/lib/apt/lists/*
10+
11+
# Set environment variables for Maven authentication
12+
ENV GITHUB_TOKEN=${GITHUB_TOKEN}
13+
ENV GITHUB_ACTOR=${GITHUB_ACTOR}
14+
15+
# Copy Maven settings with GitHub authentication
16+
COPY check-deployed-package/settings.xml /root/.m2/settings.xml
17+
18+
# Copy project files
19+
COPY check-deployed-package/pom.xml .
20+
COPY check-deployed-package/Check.java src/main/java/com/logdash/check/Check.java
21+
22+
# Download dependencies and compile
23+
RUN mvn dependency:resolve compile -B --no-transfer-progress \
24+
-Dgithub.username=${GITHUB_ACTOR} \
25+
-Dgithub.password=${GITHUB_TOKEN}
26+
27+
# Add environment variable validation
28+
ENV LOGDASH_API_KEY=""
29+
ENV LOGS_SEED=""
30+
ENV METRICS_SEED=""
31+
32+
# Run the application with environment validation
33+
CMD if [ -z "$LOGDASH_API_KEY" ]; then \
34+
echo "Error: LOGDASH_API_KEY environment variable is required"; \
35+
exit 1; \
36+
fi; \
37+
mvn exec:java -B --no-transfer-progress

check-deployed-package/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# LogDash Java SDK Demo
2+
3+
Use command below to test if the published SDK works:
4+
5+
```
6+
LOGDASH_API_KEY=<your-api-key> ./check-deployed-package/run.sh
7+
```
8+

check-deployed-package/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.logdash</groupId>
6+
<artifactId>check</artifactId>
7+
<version>1.0.0</version>
8+
9+
<properties>
10+
<maven.compiler.source>17</maven.compiler.source>
11+
<maven.compiler.target>17</maven.compiler.target>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>io.logdash</groupId>
18+
<artifactId>logdash</artifactId>
19+
<version>0.1.0</version>
20+
</dependency>
21+
</dependencies>
22+
23+
<repositories>
24+
<repository>
25+
<releases>
26+
<enabled>true</enabled>
27+
</releases>
28+
<snapshots>
29+
<enabled>true</enabled>
30+
</snapshots>
31+
<id>github</id>
32+
<name>GitHub Packages</name>
33+
<url>https://maven.pkg.github.com/logdash-io/java-sdk</url>
34+
</repository>
35+
</repositories>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-compiler-plugin</artifactId>
42+
<version>3.11.0</version>
43+
<configuration>
44+
<source>17</source>
45+
<target>17</target>
46+
</configuration>
47+
</plugin>
48+
<plugin>
49+
<groupId>org.codehaus.mojo</groupId>
50+
<artifactId>exec-maven-plugin</artifactId>
51+
<version>3.1.0</version>
52+
<configuration>
53+
<mainClass>com.logdash.check.Check</mainClass>
54+
<options>
55+
<option>-Dfile.encoding=UTF-8</option>
56+
</options>
57+
</configuration>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
</project>

check-deployed-package/run.sh

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Generate random 5-character seed
5+
LOGS_SEED=$(openssl rand -hex 2 | cut -c1-5)
6+
echo "Generated seed: $LOGS_SEED"
7+
8+
# Generate random metrics seed (1-1,000,000)
9+
METRICS_SEED=$(awk 'BEGIN{srand(); print int(rand()*1000000)+1}')
10+
echo "Generated metrics seed: $METRICS_SEED"
11+
12+
echo "Building LogDash Java demo Docker image (using published package)..."
13+
docker build --no-cache \
14+
--build-arg GITHUB_TOKEN="${GITHUB_TOKEN}" \
15+
--build-arg GITHUB_ACTOR="${GITHUB_ACTOR}" \
16+
-t logdash-java-demo \
17+
-f check-deployed-package/Dockerfile .
18+
19+
echo
20+
echo "Running LogDash Java demo..."
21+
echo
22+
23+
# Run in non-interactive mode which works everywhere
24+
docker run --rm \
25+
-e LOGDASH_API_KEY="${LOGDASH_API_KEY}" \
26+
-e LOGS_SEED="${LOGS_SEED}" \
27+
-e METRICS_SEED="${METRICS_SEED}" \
28+
logdash-java-demo
29+
30+
echo
31+
echo "Demo completed!"
32+
33+
echo
34+
echo "Authenticating with LogDash API..."
35+
36+
# Authenticate with API using the API key
37+
AUTH_RESPONSE=$(curl -s -X 'POST' \
38+
'https://api.logdash.io/auth/api-key' \
39+
-H 'accept: application/json' \
40+
-H 'Content-Type: application/json' \
41+
-d "{
42+
\"apiKey\": \"${LOGDASH_API_KEY}\"
43+
}")
44+
45+
# Extract token and projectId from response
46+
TOKEN=$(echo "$AUTH_RESPONSE" | grep -o '"token":"[^"]*"' | sed 's/"token":"\(.*\)"/\1/')
47+
PROJECT_ID=$(echo "$AUTH_RESPONSE" | grep -o '"projectId":"[^"]*"' | sed 's/"projectId":"\(.*\)"/\1/')
48+
49+
if [ -z "$TOKEN" ] || [ -z "$PROJECT_ID" ]; then
50+
echo "Error: Failed to authenticate with LogDash API"
51+
echo "Response: $AUTH_RESPONSE"
52+
exit 1
53+
fi
54+
55+
echo "Authentication successful. Project ID: $PROJECT_ID"
56+
57+
echo
58+
echo "Fetching logs from LogDash API..."
59+
60+
# Fetch logs from the API
61+
LOGS_RESPONSE=$(curl -s -X 'GET' \
62+
"https://api.logdash.io/projects/${PROJECT_ID}/logs?limit=10" \
63+
-H 'accept: application/json' \
64+
-H "Authorization: Bearer ${TOKEN}")
65+
66+
echo "Logs fetched successfully"
67+
68+
echo
69+
echo "Validating log messages..."
70+
71+
# Expected log messages with seed
72+
EXPECTED_MESSAGES="This is an info log ${LOGS_SEED}
73+
This is an error log ${LOGS_SEED}
74+
This is a warning log ${LOGS_SEED}
75+
This is a debug log ${LOGS_SEED}
76+
This is a http log ${LOGS_SEED}
77+
This is a silly log ${LOGS_SEED}
78+
This is an info log ${LOGS_SEED}
79+
This is a verbose log ${LOGS_SEED}"
80+
81+
# Check if all expected messages are present in the logs
82+
echo "$EXPECTED_MESSAGES" | while IFS= read -r expected_msg; do
83+
if ! echo "$LOGS_RESPONSE" | grep -q "$expected_msg"; then
84+
echo "Error: Expected log message not found: '$expected_msg'"
85+
echo "Logs response: $LOGS_RESPONSE"
86+
exit 1
87+
fi
88+
echo "✓ Found: '$expected_msg'"
89+
done
90+
91+
echo
92+
echo "Fetching metrics from LogDash API..."
93+
94+
# Fetch metrics from the API
95+
METRICS_RESPONSE=$(curl -s -X 'GET' \
96+
"https://api.logdash.io/projects/${PROJECT_ID}/metrics" \
97+
-H 'accept: application/json' \
98+
-H "Authorization: Bearer ${TOKEN}")
99+
100+
echo "Metrics fetched successfully"
101+
102+
echo
103+
echo "Validating metrics..."
104+
105+
# Expected users metric value (metrics_seed + 1)
106+
EXPECTED_USERS_VALUE=$((METRICS_SEED + 1))
107+
108+
# Check if users metric exists with correct value
109+
if ! echo "$METRICS_RESPONSE" | grep -q '"name":"users"'; then
110+
echo "Error: Users metric not found"
111+
echo "Metrics response: $METRICS_RESPONSE"
112+
exit 1
113+
fi
114+
115+
# Extract the value of the users metric and check if it matches expected value
116+
USERS_VALUE=$(echo "$METRICS_RESPONSE" | sed 's/},{/}\n{/g' | grep '"name":"users"' | grep -o '"value":[0-9]*' | sed 's/"value"://')
117+
118+
if [ "$USERS_VALUE" != "$EXPECTED_USERS_VALUE" ]; then
119+
echo "Error: Users metric value mismatch. Expected: $EXPECTED_USERS_VALUE, Found: $USERS_VALUE"
120+
echo "Metrics response: $METRICS_RESPONSE"
121+
exit 1
122+
fi
123+
124+
echo "✓ Found users metric with correct value: $USERS_VALUE"
125+
126+
echo
127+
echo "All expected log messages and metrics found successfully!"
128+
echo "Validation completed!"

check-deployed-package/settings.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
5+
https://maven.apache.org/xsd/settings-1.0.0.xsd">
6+
7+
<servers>
8+
<server>
9+
<id>github</id>
10+
<username>${env.GITHUB_ACTOR}</username>
11+
<password>${env.GITHUB_TOKEN}</password>
12+
</server>
13+
</servers>
14+
15+
<profiles>
16+
<profile>
17+
<id>github-auth</id>
18+
<activation>
19+
<activeByDefault>true</activeByDefault>
20+
</activation>
21+
<repositories>
22+
<repository>
23+
<id>github</id>
24+
<name>GitHub Packages</name>
25+
<url>https://maven.pkg.github.com/logdash-io/java-sdk</url>
26+
<releases>
27+
<enabled>true</enabled>
28+
<updatePolicy>daily</updatePolicy>
29+
</releases>
30+
<snapshots>
31+
<enabled>true</enabled>
32+
<updatePolicy>always</updatePolicy>
33+
</snapshots>
34+
</repository>
35+
</repositories>
36+
</profile>
37+
</profiles>
38+
39+
<activeProfiles>
40+
<activeProfile>github-auth</activeProfile>
41+
</activeProfiles>
42+
</settings>

0 commit comments

Comments
 (0)