fix: add agent key auth for ingest endpoints
This commit is contained in:
43
examples/docker/java-instrumented/Dockerfile
Normal file
43
examples/docker/java-instrumented/Dockerfile
Normal file
@@ -0,0 +1,43 @@
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
# 🐍 OPHION - Java Instrumented App Dockerfile
|
||||
# Example showing how to add OpenTelemetry to any Java app
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
|
||||
# Build stage
|
||||
FROM eclipse-temurin:21-jdk-alpine AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy Maven/Gradle files (if using build tool)
|
||||
COPY pom.xml* build.gradle* ./
|
||||
|
||||
# Copy source code
|
||||
COPY src/ ./src/
|
||||
|
||||
# For simple example, compile directly
|
||||
RUN mkdir -p target/classes && \
|
||||
javac -d target/classes src/main/java/*.java
|
||||
|
||||
# Package as JAR
|
||||
RUN cd target/classes && \
|
||||
jar cfe ../app.jar Main .
|
||||
|
||||
# Runtime stage
|
||||
FROM eclipse-temurin:21-jre-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Download OpenTelemetry Java Agent
|
||||
ARG OTEL_AGENT_VERSION=2.1.0
|
||||
RUN wget -q https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v${OTEL_AGENT_VERSION}/opentelemetry-javaagent.jar \
|
||||
-O /opt/opentelemetry-javaagent.jar
|
||||
|
||||
# Copy the built application
|
||||
COPY --from=builder /app/target/app.jar .
|
||||
|
||||
# Expose port
|
||||
EXPOSE 8080
|
||||
|
||||
# Start with OpenTelemetry Java Agent
|
||||
# The JAVA_TOOL_OPTIONS env var in docker-compose.yml enables the agent
|
||||
CMD ["java", "-jar", "app.jar"]
|
||||
62
examples/docker/java-instrumented/docker-compose.yml
Normal file
62
examples/docker/java-instrumented/docker-compose.yml
Normal file
@@ -0,0 +1,62 @@
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
# 🐍 OPHION - Java Auto-Instrumentation Example
|
||||
# Demonstrates automatic tracing for Java applications
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
#
|
||||
# Usage:
|
||||
# docker-compose up -d
|
||||
#
|
||||
# This example shows how to instrument ANY Java app without
|
||||
# code changes using the OpenTelemetry Java Agent
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
java-app:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "8081:8080"
|
||||
environment:
|
||||
# ════════════════════════════════════════════════════
|
||||
# OpenTelemetry Auto-Instrumentation Configuration
|
||||
# ════════════════════════════════════════════════════
|
||||
|
||||
# Service identification
|
||||
- OTEL_SERVICE_NAME=java-example-app
|
||||
- OTEL_SERVICE_VERSION=1.0.0
|
||||
|
||||
# OTLP Exporter configuration (pointing to Ophion collector)
|
||||
- OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
|
||||
- OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
|
||||
- OTEL_TRACES_EXPORTER=otlp
|
||||
- OTEL_METRICS_EXPORTER=otlp
|
||||
- OTEL_LOGS_EXPORTER=otlp
|
||||
|
||||
# Resource attributes
|
||||
- OTEL_RESOURCE_ATTRIBUTES=deployment.environment=development,service.namespace=ophion-examples
|
||||
|
||||
# Sampling (1.0 = 100% of traces)
|
||||
- OTEL_TRACES_SAMPLER=parentbased_traceidratio
|
||||
- OTEL_TRACES_SAMPLER_ARG=1.0
|
||||
|
||||
# Propagation format
|
||||
- OTEL_PROPAGATORS=tracecontext,baggage,b3multi
|
||||
|
||||
# Java Agent path (set in Dockerfile JAVA_TOOL_OPTIONS)
|
||||
- JAVA_TOOL_OPTIONS=-javaagent:/opt/opentelemetry-javaagent.jar
|
||||
|
||||
networks:
|
||||
- ophion
|
||||
restart: unless-stopped
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8080/health"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
|
||||
networks:
|
||||
ophion:
|
||||
external: true
|
||||
115
examples/docker/java-instrumented/src/main/java/Main.java
Normal file
115
examples/docker/java-instrumented/src/main/java/Main.java
Normal file
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* ═══════════════════════════════════════════════════════════
|
||||
* 🐍 OPHION - Example Java Application
|
||||
* This app is automatically instrumented by OpenTelemetry Java Agent
|
||||
* No code changes needed - just the agent JAR!
|
||||
* ═══════════════════════════════════════════════════════════
|
||||
*/
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Main {
|
||||
private static final Logger logger = Logger.getLogger(Main.class.getName());
|
||||
private static final int PORT = 8080;
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0);
|
||||
|
||||
// Routes
|
||||
server.createContext("/", new HomeHandler());
|
||||
server.createContext("/health", new HealthHandler());
|
||||
server.createContext("/api/users", new UsersHandler());
|
||||
server.createContext("/api/slow", new SlowHandler());
|
||||
|
||||
server.setExecutor(Executors.newFixedThreadPool(10));
|
||||
server.start();
|
||||
|
||||
logger.info("🚀 Java server running on port " + PORT);
|
||||
logger.info("📊 OTEL Endpoint: " + System.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"));
|
||||
logger.info("🏷️ Service Name: " + System.getenv("OTEL_SERVICE_NAME"));
|
||||
}
|
||||
|
||||
static class HomeHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
logger.info("Home endpoint called");
|
||||
String response = """
|
||||
{
|
||||
"message": "Hello from Java!",
|
||||
"instrumented": true,
|
||||
"otelEndpoint": "%s"
|
||||
}
|
||||
""".formatted(System.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"));
|
||||
sendResponse(exchange, 200, response);
|
||||
}
|
||||
}
|
||||
|
||||
static class HealthHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
String response = """
|
||||
{
|
||||
"status": "healthy",
|
||||
"service": "java-example"
|
||||
}
|
||||
""";
|
||||
sendResponse(exchange, 200, response);
|
||||
}
|
||||
}
|
||||
|
||||
static class UsersHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
logger.info("Fetching users");
|
||||
// Simulate some work
|
||||
sleep(10 + (int)(Math.random() * 90));
|
||||
String response = """
|
||||
[
|
||||
{"id": 1, "name": "Alice"},
|
||||
{"id": 2, "name": "Bob"},
|
||||
{"id": 3, "name": "Charlie"}
|
||||
]
|
||||
""";
|
||||
sendResponse(exchange, 200, response);
|
||||
}
|
||||
}
|
||||
|
||||
static class SlowHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
logger.info("Starting slow operation");
|
||||
sleep(500);
|
||||
logger.info("Slow operation completed");
|
||||
String response = """
|
||||
{
|
||||
"message": "Slow response",
|
||||
"delay": "500ms"
|
||||
}
|
||||
""";
|
||||
sendResponse(exchange, 200, response);
|
||||
}
|
||||
}
|
||||
|
||||
private static void sendResponse(HttpExchange exchange, int code, String response) throws IOException {
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(code, response.getBytes().length);
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
os.write(response.getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
private static void sleep(int ms) {
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user